summaryrefslogtreecommitdiff
path: root/src/Noddybox.Emulation.Xna.Keyboard/KeyboardDriver.cs
diff options
context:
space:
mode:
authorIan C <ianc@noddybox.co.uk>2012-05-01 22:30:05 +0000
committerIan C <ianc@noddybox.co.uk>2012-05-01 22:30:05 +0000
commit78adb7aa6ccd4bfe8924d08ac655b325f32d7562 (patch)
tree09e0fc9e31e94085f963a2b48476805879f330e3 /src/Noddybox.Emulation.Xna.Keyboard/KeyboardDriver.cs
parente0df170a198f6b875fbbe37a7fa8a3c5fdf3aa5c (diff)
Working version of the XNA keyboard.
Diffstat (limited to 'src/Noddybox.Emulation.Xna.Keyboard/KeyboardDriver.cs')
-rw-r--r--src/Noddybox.Emulation.Xna.Keyboard/KeyboardDriver.cs200
1 files changed, 200 insertions, 0 deletions
diff --git a/src/Noddybox.Emulation.Xna.Keyboard/KeyboardDriver.cs b/src/Noddybox.Emulation.Xna.Keyboard/KeyboardDriver.cs
new file mode 100644
index 0000000..b0671b8
--- /dev/null
+++ b/src/Noddybox.Emulation.Xna.Keyboard/KeyboardDriver.cs
@@ -0,0 +1,200 @@
+// 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.Keyboard
+{
+ /// <summary>
+ /// Displays and drives the keyboard.
+ /// </summary>
+ /// <typeparam name="T">The enumerated type matching the symbols in the used keyboard definition.</typeparam>
+ public class KeyboardDriver<T> where T: struct,IConvertible
+ {
+ #region Event arguments
+
+ /// <summary>
+ /// Event data when a key is pressed or released.
+ /// </summary>
+ public class KeyPress : EventArgs
+ {
+ /// <summary>
+ /// The key that was pressed.
+ /// </summary>
+ public T Key {get;set;}
+
+ /// <summary>
+ /// True if the key was pressed. False if released.
+ /// </summary>
+ public bool Pressed {get; set;}
+ }
+
+ #endregion
+
+ #region Private data
+
+ private class KeyState
+ {
+ public T Symbol {get;set;}
+ public bool IsPressed {get; set;}
+ public bool IsSticky {get; set;}
+ }
+
+ private Rectangle[] keymapPos;
+ private KeyState[] keymapState;
+ private int keymapSize;
+ private KeyboardDefinition keyboard;
+ private Texture2D image;
+ private Vector2 position;
+ private Texture2D overlay;
+
+ #endregion
+
+ #region Private members
+
+ private void RaiseKeyChange(KeyState state)
+ {
+ if (KeyEvent != null)
+ {
+ KeyEvent(this, new KeyPress() {Key = state.Symbol, Pressed = state.IsPressed});
+ }
+ }
+
+ #endregion
+
+ #region Public members
+
+ /// <summary>
+ /// Updates the keyboard. Note that this will consume all the key press events.
+ /// </summary>
+ public void Update()
+ {
+ foreach (TouchLocation t in TouchPanel.GetState())
+ {
+ int x = (int)t.Position.X;
+ int y = (int)t.Position.Y;
+ int f = 0;
+ KeyState key = null;
+
+ for(f = 0; f < keymapSize && key == null; f++)
+ {
+ if (keymapPos[f].Contains(x, y))
+ {
+ key = keymapState[f];
+ }
+ }
+
+ if (key != null)
+ {
+ if (t.State == TouchLocationState.Pressed)
+ {
+ if (key.IsSticky)
+ {
+ key.IsPressed = !key.IsPressed;
+ }
+ else
+ {
+ key.IsPressed = true;
+ }
+
+ RaiseKeyChange(key);
+ }
+ else if (t.State == TouchLocationState.Released)
+ {
+ if (!key.IsSticky)
+ {
+ key.IsPressed = false;
+ RaiseKeyChange(key);
+ }
+ }
+ }
+ }
+ }
+
+ /// <summary>
+ /// Draw the keyboard
+ /// </summary>
+ /// <param name="spriteBatch"></param>
+ public void Draw(SpriteBatch spriteBatch)
+ {
+ spriteBatch.Draw(image, position, Color.White);
+
+ for(int f = 0; f < keymapSize; f++)
+ {
+ if (keymapState[f].IsPressed)
+ {
+ spriteBatch.Draw(overlay, keymapPos[f], new Color(200, 200, 0, 25));
+ }
+ }
+ }
+
+ #endregion
+
+ #region Events
+
+ public EventHandler<KeyPress> KeyEvent;
+
+ #endregion
+
+ #region Constructors
+
+ /// <summary>
+ /// Constructor.
+ /// </summary>
+ /// <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)
+ {
+ this.keyboard = keyboard;
+ this.image = image;
+ this.position = position;
+
+ keymapSize = keyboard.Definitions.Count;
+ keymapPos = new Rectangle[keymapSize];
+ keymapState = new KeyState[keymapSize];
+
+ for(int f = 0; f < keymapSize; f++)
+ {
+ KeyboardKey k = keyboard.Definitions[f];
+
+ keymapPos[f] = new Rectangle(k.X, k.Y, k.Width, k.Height);
+
+ keymapState[f] = new KeyState()
+ {
+ Symbol = (T)Enum.Parse(typeof(T), k.KeySymbol, false),
+ IsPressed = false,
+ IsSticky = k.Sticky
+ };
+ }
+
+ Color c = Color.White;
+ overlay = new Texture2D(graphics, 2, 2, false, SurfaceFormat.Color);
+ overlay.SetData<Color>(new Color[] {c, c, c, c});
+ }
+
+ #endregion
+ }
+}