summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan C <ianc@noddybox.co.uk>2012-05-27 22:18:17 +0000
committerIan C <ianc@noddybox.co.uk>2012-05-27 22:18:17 +0000
commit60b52b009c2f74cea7d5ffb0ad62cbbc55c41729 (patch)
treedcc5a467f604fb01f8568f18e5b3d1e26d094566
parent299769ecba0a96ca8dc9551a639b9e5e6d23b3d2 (diff)
Updated keyboard to chain itself to InputManager.
-rw-r--r--WindowsPhone/KeyboardTest.suobin50176 -> 51712 bytes
-rw-r--r--WindowsPhone/KeyboardTest/KeyboardTest/Game1.cs13
-rw-r--r--WindowsPhone/Noddybox.Emulation.Xna.Input/Noddybox.Emulation.Xna.Input.csproj3
-rw-r--r--src/Noddybox.Emulation.Xna.Input/InputManager.cs102
-rw-r--r--src/Noddybox.Emulation.Xna.Input/Keyboard/KeyboardDriver.cs151
5 files changed, 196 insertions, 73 deletions
diff --git a/WindowsPhone/KeyboardTest.suo b/WindowsPhone/KeyboardTest.suo
index 2887aa6..bfb7f24 100644
--- a/WindowsPhone/KeyboardTest.suo
+++ b/WindowsPhone/KeyboardTest.suo
Binary files differ
diff --git a/WindowsPhone/KeyboardTest/KeyboardTest/Game1.cs b/WindowsPhone/KeyboardTest/KeyboardTest/Game1.cs
index ccd991c..55d936e 100644
--- a/WindowsPhone/KeyboardTest/KeyboardTest/Game1.cs
+++ b/WindowsPhone/KeyboardTest/KeyboardTest/Game1.cs
@@ -12,6 +12,7 @@ using Microsoft.Xna.Framework.Media;
using Noddybox.Emulation.Xna.Input.Keyboard;
using Noddybox.Emulation.Keyboard.Schema;
using System.IO;
+using Noddybox.Emulation.Xna.Input;
namespace KeyboardTest
{
@@ -40,6 +41,8 @@ namespace KeyboardTest
Key0
}
+ InputManager manager;
+
KeyboardDriver<KeySymbol> keyboard;
public Game1()
@@ -91,10 +94,12 @@ namespace KeyboardTest
def = KeyboardDefinition.Load(stream);
}
- keyboard = new KeyboardDriver<KeySymbol>(graphics.GraphicsDevice, keyboardImage, Vector2.Zero, def);
+ manager = new InputManager();
+
+ keyboard = new KeyboardDriver<KeySymbol>(manager, graphics.GraphicsDevice, keyboardImage, Vector2.Zero, def);
keyboard.KeyEvent += KeyPress;
- keyboard.TouchEvent += Touch;
+ manager.TouchEvent += Touch;
}
/// <summary>
@@ -114,7 +119,7 @@ namespace KeyboardTest
protected override void Update(GameTime gameTime)
{
// TODO: Add your update logic here
- keyboard.Update();
+ manager.Update();
base.Update(gameTime);
}
@@ -149,7 +154,7 @@ namespace KeyboardTest
output.Add(String.Format("{0} - {1}", e.Key.ToString(), e.Pressed.ToString()));
}
- private void Touch(object sender, KeyboardDriver<KeySymbol>.TouchLocationEventArgs e)
+ private void Touch(object sender, InputManager.TouchLocationEventArgs e)
{
if (output.Count > 35)
{
diff --git a/WindowsPhone/Noddybox.Emulation.Xna.Input/Noddybox.Emulation.Xna.Input.csproj b/WindowsPhone/Noddybox.Emulation.Xna.Input/Noddybox.Emulation.Xna.Input.csproj
index 42babf6..8088063 100644
--- a/WindowsPhone/Noddybox.Emulation.Xna.Input/Noddybox.Emulation.Xna.Input.csproj
+++ b/WindowsPhone/Noddybox.Emulation.Xna.Input/Noddybox.Emulation.Xna.Input.csproj
@@ -59,6 +59,9 @@
<Reference Include="System.Net" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="..\..\src\Noddybox.Emulation.Xna.Input\InputManager.cs">
+ <Link>InputManager.cs</Link>
+ </Compile>
<Compile Include="..\..\src\Noddybox.Emulation.Xna.Input\Joystick\JoystickDriver.cs">
<Link>Joystick\JoystickDriver.cs</Link>
</Compile>
diff --git a/src/Noddybox.Emulation.Xna.Input/InputManager.cs b/src/Noddybox.Emulation.Xna.Input/InputManager.cs
new file mode 100644
index 0000000..8b0adde
--- /dev/null
+++ b/src/Noddybox.Emulation.Xna.Input/InputManager.cs
@@ -0,0 +1,102 @@
+// This file is part of the Noddybox.Emulation C# suite.
+//
+// Noddybox.Emulation is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Noddybox.Emulation is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Noddybox.Emulation. If not, see <http://www.gnu.org/licenses/>.
+//
+// Copyright (c) 2012 Ian Cowburn
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Noddybox.Emulation.Keyboard.Schema;
+using Microsoft.Xna.Framework.Graphics;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Input.Touch;
+
+namespace Noddybox.Emulation.Xna.Input
+{
+ /// <summary>
+ /// Provides a common source for getting to the inputs.
+ /// </summary>
+ public class InputManager
+ {
+ #region Event arguments
+
+ /// <summary>
+ /// Event data when the screen is touched.
+ /// </summary>
+ public class TouchLocationEventArgs : EventArgs
+ {
+ /// <summary>
+ /// Whether the event has been consumed by one of the subscribers.
+ /// </summary>
+ public bool Handled {get; set;}
+ /// <summary>
+ /// The touch.
+ /// </summary>
+ public TouchLocation Location {get; set;}
+ }
+
+ #endregion
+
+ #region Public members
+
+ /// <summary>
+ /// Reads the toch screen and passes the events on.
+ /// </summary>
+ public void Update()
+ {
+ MulticastDelegate d = TouchEvent as MulticastDelegate;
+
+ if (d != null)
+ {
+ foreach (TouchLocation t in TouchPanel.GetState())
+ {
+ TouchLocationEventArgs args = new TouchLocationEventArgs()
+ {
+ Location = t,
+ Handled = false
+ };
+
+ foreach (var f in d.GetInvocationList())
+ {
+ if (!args.Handled)
+ {
+ f.DynamicInvoke(this, args);
+ }
+ }
+ }
+ }
+ }
+
+ #endregion
+
+ #region Events
+
+ public EventHandler<TouchLocationEventArgs> TouchEvent;
+
+ #endregion
+
+ #region Constructors
+
+ /// <summary>
+ /// Constructor.
+ /// </summary>
+ public InputManager()
+ {
+ }
+
+ #endregion
+ }
+}
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