diff --git a/RhythmBullet/RhythmBulletGame.cs b/RhythmBullet/RhythmBulletGame.cs index 1c95fd3..ad2f0d7 100644 --- a/RhythmBullet/RhythmBulletGame.cs +++ b/RhythmBullet/RhythmBulletGame.cs @@ -7,6 +7,7 @@ using RhythmBullet.Zer01HD.Game.Screens; using RhythmBullet.Zer01HD.Utilities; using RhythmBullet.Zer01HD.Utilities.ContentSystem; using RhythmBullet.Zer01HD.Utilities.DataTypes; +using RhythmBullet.Zer01HD.Utilities.Input; using RhythmBullet.Zer01HD.Utilities.UI.ScreenSystem; using System; using System.Diagnostics; @@ -131,6 +132,7 @@ namespace RhythmBullet break; } + InputUtilities.Update(); base.Update(gameTime); } diff --git a/RhythmBullet/Zer01HD/Utilities/Input/IInputListener.cs b/RhythmBullet/Zer01HD/Utilities/Input/IInputListener.cs new file mode 100644 index 0000000..bd8423e --- /dev/null +++ b/RhythmBullet/Zer01HD/Utilities/Input/IInputListener.cs @@ -0,0 +1,16 @@ +using Microsoft.Xna.Framework.Input; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RhythmBullet.Zer01HD.Utilities.Input +{ + public interface IInputListener + { + bool KeyboardStateChanged(KeyboardState state); + + bool MouseStateChanged(MouseState state); + } +} diff --git a/RhythmBullet/Zer01HD/Utilities/Input/InputListener.cs b/RhythmBullet/Zer01HD/Utilities/Input/InputListener.cs new file mode 100644 index 0000000..4da5d1c --- /dev/null +++ b/RhythmBullet/Zer01HD/Utilities/Input/InputListener.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Xna.Framework.Input; + +namespace RhythmBullet.Zer01HD.Utilities.Input +{ + class InputListener : IInputListener + { + public bool KeyboardStateChanged(KeyboardState state) + { + return true; + } + + public bool MouseStateChanged(MouseState state) + { + return true; + } + } +} diff --git a/RhythmBullet/Zer01HD/Utilities/Input/InputUtilities.cs b/RhythmBullet/Zer01HD/Utilities/Input/InputUtilities.cs new file mode 100644 index 0000000..3425c60 --- /dev/null +++ b/RhythmBullet/Zer01HD/Utilities/Input/InputUtilities.cs @@ -0,0 +1,73 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Input; +using RhythmBullet.Zer01HD.Utilities.Input; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RhythmBullet.Zer01HD.Utilities.Input +{ + public static class InputUtilities + { + public static List InputListeners; + static KeyboardState keyboardState; + static MouseState mouseState; + + static InputUtilities() + { + InputListeners = new List(); + } + + public static void Update() + { + KeyboardState updatedKeyboardState = Keyboard.GetState(); + MouseState updatedMouseState = Mouse.GetState(); + bool disableKeyboard = false; + bool disableMouse = false; + foreach (IInputListener inputListener in InputListeners) + { + if (!disableKeyboard && keyboardState != updatedKeyboardState) + { + disableKeyboard = inputListener.KeyboardStateChanged(updatedKeyboardState); + } + if (!disableMouse && mouseState != updatedMouseState) + { + disableMouse = inputListener.MouseStateChanged(updatedMouseState); + } + if (disableKeyboard && disableMouse) + { + break; + } + } + + UpdateStates(); + } + + public static bool MouseClicked() + { + if (mouseState.LeftButton == ButtonState.Pressed) + { + return Mouse.GetState().LeftButton != ButtonState.Pressed; + } + return false; + } + + private static void UpdateStates() + { + keyboardState = Keyboard.GetState(); + mouseState = Mouse.GetState(); + } + + public static bool MouseWithinBoundries(Rectangle bounds) + { + MouseState mouseState = Mouse.GetState(); + if (mouseState.X >= bounds.X && mouseState.X <= (bounds.X + bounds.Width) && mouseState.Y >= bounds.Y && mouseState.Y <= (bounds.Y + bounds.Height)) + { + return true; + } + return false; + } + } +} diff --git a/RhythmBullet/Zer01HD/Utilities/UI/Modular/Module.cs b/RhythmBullet/Zer01HD/Utilities/UI/Modular/Module.cs deleted file mode 100644 index b6f2bea..0000000 --- a/RhythmBullet/Zer01HD/Utilities/UI/Modular/Module.cs +++ /dev/null @@ -1,38 +0,0 @@ -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Graphics; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace RhythmBullet.Zer01HD.UI.Modular -{ - public class Module - { - public Vector2 Position = new Vector2(); - public float Width, Height; - public ModuleGroup Parent; - public string Name; - public Color Color; - - public virtual void Update(GameTime gameTime) - { - - } - - public virtual void Draw(SpriteBatch batch) - { - - } - - public void RemoveFromParent() - { - if (Parent == null) - { - throw new InvalidOperationException("Parent is null."); - } - Parent.RemoveModule(this); - } - } -} diff --git a/RhythmBullet/Zer01HD/Utilities/UI/Modular/Text.cs b/RhythmBullet/Zer01HD/Utilities/UI/Modular/Text.cs index 527253d..c778ded 100644 --- a/RhythmBullet/Zer01HD/Utilities/UI/Modular/Text.cs +++ b/RhythmBullet/Zer01HD/Utilities/UI/Modular/Text.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; namespace RhythmBullet.Zer01HD.UI.Modular { - public class Text : Module + public class Text : UIModule { private SpriteFont font; private float scale; diff --git a/RhythmBullet/Zer01HD/Utilities/UI/Modular/UIModule.cs b/RhythmBullet/Zer01HD/Utilities/UI/Modular/UIModule.cs new file mode 100644 index 0000000..5a0a01a --- /dev/null +++ b/RhythmBullet/Zer01HD/Utilities/UI/Modular/UIModule.cs @@ -0,0 +1,62 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; +using RhythmBullet.Zer01HD.Utilities.Input; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RhythmBullet.Zer01HD.UI.Modular +{ + public class UIModule : IInputListener + { + public Rectangle Bounds; + public UIModuleGroup Parent; + public string Name; + public Color Color; + + public virtual void Update(GameTime gameTime) + { + } + + public virtual void Draw(SpriteBatch batch) + { + + } + + public Rectangle ConvertToParentCoordinates(Rectangle bounds) + { + if (Parent != null) + { + Rectangle parentHitbox = Parent.ConvertToParentCoordinates(bounds); + int tX = bounds.X + parentHitbox.X; + int tY = bounds.Y + parentHitbox.Y; + return new Rectangle(tX, tY, bounds.Width, bounds.Height); + } else + { + return bounds; + } + } + + public void RemoveFromParent() + { + if (Parent == null) + { + throw new InvalidOperationException("Parent is null."); + } + Parent.RemoveModule(this); + } + + public virtual bool KeyboardStateChanged(KeyboardState state) + { + return true; + } + + public virtual bool MouseStateChanged(MouseState state) + { + return true; + } + } +} diff --git a/RhythmBullet/Zer01HD/Utilities/UI/Modular/ModuleGroup.cs b/RhythmBullet/Zer01HD/Utilities/UI/Modular/UIModuleGroup.cs similarity index 55% rename from RhythmBullet/Zer01HD/Utilities/UI/Modular/ModuleGroup.cs rename to RhythmBullet/Zer01HD/Utilities/UI/Modular/UIModuleGroup.cs index f144f1d..b1f8768 100644 --- a/RhythmBullet/Zer01HD/Utilities/UI/Modular/ModuleGroup.cs +++ b/RhythmBullet/Zer01HD/Utilities/UI/Modular/UIModuleGroup.cs @@ -5,15 +5,17 @@ using System.Text; using System.Threading.Tasks; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; +using RhythmBullet.Zer01HD.Utilities.Input; namespace RhythmBullet.Zer01HD.UI.Modular { - public class ModuleGroup : Module + public class UIModuleGroup : UIModule { - List modules = new List(); + List modules = new List(); Rectangle scissorBounds; RasterizerState scissorRasterizer = new RasterizerState(); - public ModuleGroup(bool crop = false) + public UIModuleGroup(bool crop = false) { if (crop) { @@ -28,23 +30,23 @@ namespace RhythmBullet.Zer01HD.UI.Modular { batch.End(); batch.Begin(SpriteSortMode.Deferred, null, null, null, scissorRasterizer); - scissorBounds.Width = (int) Width; - scissorBounds.Height = (int) Height; - scissorBounds.X = (int) Position.X; - scissorBounds.Y = (int) Position.Y; + scissorBounds.Width = Bounds.Width; + scissorBounds.Height = Bounds.Height; + scissorBounds.X = Bounds.X; + scissorBounds.Y = Bounds.Y; Rectangle scissor = scissorBounds; scissorBounds = batch.GraphicsDevice.ScissorRectangle; batch.GraphicsDevice.ScissorRectangle = scissor; } - foreach (Module module in modules) + foreach (UIModule module in modules) { - float offsetX = module.Position.X; - float offsetY = module.Position.Y; - module.Position.X = Position.X + offsetX; - module.Position.Y = Position.Y + offsetY; + int offsetX = module.Bounds.X; + int offsetY = module.Bounds.Y; + module.Bounds.X = Bounds.X + offsetX; + module.Bounds.Y = Bounds.Y + offsetY; module.Draw(batch); - module.Position.X = offsetX; - module.Position.Y = offsetY; + module.Bounds.X = offsetX; + module.Bounds.Y = offsetY; } if (scissorBounds != null) @@ -57,15 +59,15 @@ namespace RhythmBullet.Zer01HD.UI.Modular public override void Update(GameTime gameTime) { - foreach (Module module in modules) + foreach (UIModule module in modules) { module.Update(gameTime); } } - public void AddModule(params Module[] addModules) + public void AddModule(params UIModule[] addModules) { - foreach (Module module in addModules) + foreach (UIModule module in addModules) { if (modules.Contains(module)) { @@ -76,10 +78,28 @@ namespace RhythmBullet.Zer01HD.UI.Modular } } - public void RemoveModule(Module module) + public void RemoveModule(UIModule module) { module.Parent = null; modules.Remove(module); } + + public override bool KeyboardStateChanged(KeyboardState state) + { + foreach (UIModule module in modules) + { + module.KeyboardStateChanged(state); + } + return false; + } + + public override bool MouseStateChanged(MouseState state) + { + foreach (UIModule module in modules) + { + module.MouseStateChanged(state); + } + return false; + } } }