summaryrefslogtreecommitdiff
path: root/src/Noddybox.Emulation.Xna.Input/Keyboard
diff options
context:
space:
mode:
Diffstat (limited to 'src/Noddybox.Emulation.Xna.Input/Keyboard')
-rw-r--r--src/Noddybox.Emulation.Xna.Input/Keyboard/KeyboardDriver.cs151
1 files changed, 82 insertions, 69 deletions
diff --git a/src/Noddybox.Emulation.Xna.Input/Keyboard/KeyboardDriver.cs b/src/Noddybox.Emulation.Xna.Input/Keyboard/KeyboardDriver.cs
index cfab190..4fa265a 100644
--- a/src/Noddybox.Emulation.Xna.Input/Keyboard/KeyboardDriver.cs
+++ b/src/Noddybox.Emulation.Xna.Input/Keyboard/KeyboardDriver.cs
@@ -50,17 +50,6 @@ namespace Noddybox.Emulation.Xna.Input.Keyboard
public bool Pressed {get; set;}
}
- /// <summary>
- /// Event data when the screen is touched away from a key.
- /// </summary>
- public class TouchLocationEventArgs : EventArgs
- {
- /// <summary>
- /// The touch.
- /// </summary>
- public TouchLocation Location {get; set;}
- }
-
#endregion
#region Private data
@@ -73,6 +62,7 @@ namespace Noddybox.Emulation.Xna.Input.Keyboard
public int TouchId {get; set;}
}
+ private InputManager manager;
private Rectangle[] keymapPos;
private KeyState[] keymapState;
private int keymapSize;
@@ -80,90 +70,109 @@ namespace Noddybox.Emulation.Xna.Input.Keyboard
private Texture2D image;
private Vector2 position;
private Texture2D overlay;
+ private bool subscribed;
#endregion
#region Private members
- #endregion
-
- #region Public members
-
/// <summary>
- /// Updates the keyboard. Note that this will consume all the key press events.
+ /// Updates the keyboard.
/// </summary>
- public void Update()
+ public void TouchScreenHandler(object sender, InputManager.TouchLocationEventArgs e)
{
- foreach (TouchLocation t in TouchPanel.GetState())
- {
- int x = (int)t.Position.X;
- int y = (int)t.Position.Y;
- int f = 0;
- KeyState key = null;
+ int x = (int)e.Location.Position.X;
+ int y = (int)e.Location.Position.Y;
+ int f = 0;
+ KeyState key = null;
- if (t.State == TouchLocationState.Pressed)
+ if (e.Location.State == TouchLocationState.Pressed)
+ {
+ for(f = 0; f < keymapSize && key == null; f++)
{
- for(f = 0; f < keymapSize && key == null; f++)
+ if (keymapPos[f].Contains(x, y))
{
- if (keymapPos[f].Contains(x, y))
- {
- key = keymapState[f];
- }
+ key = keymapState[f];
}
+ }
- if (key != null)
- {
- key.TouchId = t.Id;
+ if (key != null)
+ {
+ key.TouchId = e.Location.Id;
- if (key.IsSticky)
- {
- key.IsPressed = !key.IsPressed;
- }
- else
- {
- key.IsPressed = true;
- }
+ if (key.IsSticky)
+ {
+ key.IsPressed = !key.IsPressed;
+ }
+ else
+ {
+ key.IsPressed = true;
+ }
- if (KeyEvent != null)
- {
- KeyEvent(this, new KeyPressEventArgs() {Key = key.Symbol, Pressed = key.IsPressed});
- }
+ if (KeyEvent != null)
+ {
+ KeyEvent(this, new KeyPressEventArgs() {Key = key.Symbol, Pressed = key.IsPressed});
}
}
- else if (t.State == TouchLocationState.Released)
+ }
+ else if (e.Location.State == TouchLocationState.Released)
+ {
+ for(f = 0; f < keymapSize && key == null; f++)
{
- for(f = 0; f < keymapSize && key == null; f++)
+ if (keymapState[f].TouchId == e.Location.Id)
{
- if (keymapState[f].TouchId == t.Id)
- {
- key = keymapState[f];
- }
+ key = keymapState[f];
}
+ }
- if (key != null)
+ if (key != null)
+ {
+ if (!key.IsSticky)
{
- if (!key.IsSticky)
- {
- key.IsPressed = false;
+ key.IsPressed = false;
- if (KeyEvent != null)
- {
- KeyEvent(this, new KeyPressEventArgs() {Key = key.Symbol, Pressed = key.IsPressed});
- }
+ if (KeyEvent != null)
+ {
+ KeyEvent(this, new KeyPressEventArgs() {Key = key.Symbol, Pressed = key.IsPressed});
}
}
}
+ }
- // If no key handled, pass on the event
- //
- if (key == null && keymapState.Where(r => r.TouchId == t.Id).Count() == 0)
- {
- if (TouchEvent != null)
- {
- TouchEvent(this, new TouchLocationEventArgs() {Location = t});
- }
- }
+ // Check if input was used.
+ //
+ if (key != null || keymapState.Where(r => r.TouchId == e.Location.Id).Count() > 0)
+ {
+ e.Handled = true;
+ }
+ }
+
+ #endregion
+
+ #region Public members
+
+ /// <summary>
+ /// Stops consuming inputs.
+ /// </summary>
+ public void StopKeyboardUpdates()
+ {
+ if (subscribed)
+ {
+ manager.TouchEvent -= TouchScreenHandler;
+ subscribed = false;
+ }
+ }
+
+ /// <summary>
+ /// Starts consuming inputs.
+ /// </summary>
+ public void StartKeyboardUpdates()
+ {
+ if (!subscribed)
+ {
+ manager.TouchEvent += TouchScreenHandler;
+ subscribed = true;
}
}
@@ -189,7 +198,6 @@ namespace Noddybox.Emulation.Xna.Input.Keyboard
#region Events
public EventHandler<KeyPressEventArgs> KeyEvent;
- public EventHandler<TouchLocationEventArgs> TouchEvent;
#endregion
@@ -198,15 +206,18 @@ namespace Noddybox.Emulation.Xna.Input.Keyboard
/// <summary>
/// Constructor.
/// </summary>
+ /// <param name="manager">The input manager to attached to.</param>
/// <param name="graphics">The graphics device to associate textures with.</param>
/// <param name="image">The image for the keyboard.</param>
/// <param name="position">Where to draw and offset the keyboard to.</param>
/// <param name="keyboard">The keyboard to drive this from.</param>
- public KeyboardDriver(GraphicsDevice graphics, Texture2D image, Vector2 position, KeyboardDefinition keyboard)
+ public KeyboardDriver(InputManager manager, GraphicsDevice graphics, Texture2D image, Vector2 position, KeyboardDefinition keyboard)
{
+ this.manager = manager;
this.keyboard = keyboard;
this.image = image;
this.position = position;
+ this.subscribed = false;
keymapSize = keyboard.Definitions.Count;
keymapPos = new Rectangle[keymapSize];
@@ -229,6 +240,8 @@ namespace Noddybox.Emulation.Xna.Input.Keyboard
Color c = Color.White;
overlay = new Texture2D(graphics, 2, 2, false, SurfaceFormat.Color);
overlay.SetData<Color>(new Color[] {c, c, c, c});
+
+ StartKeyboardUpdates();
}
#endregion