added proper input system (untested)

This commit is contained in:
Harrison Deng 2018-11-08 23:48:24 -06:00
parent 99a8531b47
commit fcd2278796
8 changed files with 214 additions and 57 deletions

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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;
}
}
}

View File

@ -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<IInputListener> InputListeners;
static KeyboardState keyboardState;
static MouseState mouseState;
static InputUtilities()
{
InputListeners = new List<IInputListener>();
}
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;
}
}
}

View File

@ -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);
}
}
}

View File

@ -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;

View File

@ -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;
}
}
}

View File

@ -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<Module> modules = new List<Module>();
List<UIModule> modules = new List<UIModule>();
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;
}
}
}