diff options
author | Ian C <ianc@noddybox.co.uk> | 2012-05-01 22:30:05 +0000 |
---|---|---|
committer | Ian C <ianc@noddybox.co.uk> | 2012-05-01 22:30:05 +0000 |
commit | 78adb7aa6ccd4bfe8924d08ac655b325f32d7562 (patch) | |
tree | 09e0fc9e31e94085f963a2b48476805879f330e3 | |
parent | e0df170a198f6b875fbbe37a7fa8a3c5fdf3aa5c (diff) |
Working version of the XNA keyboard.
-rw-r--r-- | Native/EmuKeyboardDesigner/EmuKeyboardDesigner.suo | bin | 50176 -> 50176 bytes | |||
-rw-r--r-- | WindowsPhone/KeyboardTest.suo | bin | 46592 -> 44544 bytes | |||
-rw-r--r-- | WindowsPhone/KeyboardTest/KeyboardTest/Game1.cs | 35 | ||||
-rw-r--r-- | WindowsPhone/KeyboardTest/KeyboardTest/Spectrum.keyboard | bin | 235 -> 235 bytes | |||
-rw-r--r-- | WindowsPhone/KeyboardTest/KeyboardTestContent/Courier New.spritefont | 52 | ||||
-rw-r--r-- | WindowsPhone/KeyboardTest/KeyboardTestContent/KeyboardTestContent.contentproj | 7 | ||||
-rw-r--r-- | src/Noddybox.Emulation.Xna.Keyboard/KeyboardDriver.cs | 200 |
7 files changed, 290 insertions, 4 deletions
diff --git a/Native/EmuKeyboardDesigner/EmuKeyboardDesigner.suo b/Native/EmuKeyboardDesigner/EmuKeyboardDesigner.suo Binary files differindex db01de5..6a93ec2 100644 --- a/Native/EmuKeyboardDesigner/EmuKeyboardDesigner.suo +++ b/Native/EmuKeyboardDesigner/EmuKeyboardDesigner.suo diff --git a/WindowsPhone/KeyboardTest.suo b/WindowsPhone/KeyboardTest.suo Binary files differindex 9205510..585204c 100644 --- a/WindowsPhone/KeyboardTest.suo +++ b/WindowsPhone/KeyboardTest.suo diff --git a/WindowsPhone/KeyboardTest/KeyboardTest/Game1.cs b/WindowsPhone/KeyboardTest/KeyboardTest/Game1.cs index e5af385..95e4a68 100644 --- a/WindowsPhone/KeyboardTest/KeyboardTest/Game1.cs +++ b/WindowsPhone/KeyboardTest/KeyboardTest/Game1.cs @@ -23,6 +23,8 @@ namespace KeyboardTest GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D keyboardImage;
+ List<string> output = new List<string>() {"Hello World"};
+ SpriteFont font;
public enum KeySymbol
{
@@ -43,7 +45,10 @@ namespace KeyboardTest public Game1()
{
graphics = new GraphicsDeviceManager(this);
- graphics.SupportedOrientations = DisplayOrientation.Portrait;
+ graphics.PreferredBackBufferWidth = 480;
+ graphics.PreferredBackBufferHeight = 800;
+
+ //graphics.SupportedOrientations = DisplayOrientation.Portrait;
Content.RootDirectory = "Content";
@@ -77,6 +82,7 @@ namespace KeyboardTest spriteBatch = new SpriteBatch(GraphicsDevice);
keyboardImage = Content.Load<Texture2D>("keyboard");
+ font = Content.Load<SpriteFont>("Courier New");
KeyboardDefinition def;
@@ -85,7 +91,9 @@ namespace KeyboardTest def = KeyboardDefinition.Load(stream);
}
- keyboard = new KeyboardDriver<KeySymbol>(keyboardImage, Vector2.Zero, def);
+ keyboard = new KeyboardDriver<KeySymbol>(graphics.GraphicsDevice, keyboardImage, Vector2.Zero, def);
+
+ keyboard.KeyEvent += KeyPress;
}
/// <summary>
@@ -105,7 +113,7 @@ namespace KeyboardTest protected override void Update(GameTime gameTime)
{
// TODO: Add your update logic here
-
+ keyboard.Update();
base.Update(gameTime);
}
@@ -115,10 +123,29 @@ namespace KeyboardTest /// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
- graphics.GraphicsDevice.Clear(Color.PaleVioletRed);
+ graphics.GraphicsDevice.Clear(Color.MidnightBlue);
spriteBatch.Begin();
keyboard.Draw(spriteBatch);
+
+ Vector2 pos = new Vector2(0, 200);
+
+ foreach(string s in output)
+ {
+ spriteBatch.DrawString(font, s, pos, Color.White);
+ pos.Y += 30;
+ }
+
spriteBatch.End();
}
+
+ private void KeyPress(object sender, KeyboardDriver<KeySymbol>.KeyPress e)
+ {
+ if (output.Count > 15)
+ {
+ output.RemoveAt(0);
+ }
+
+ output.Add(String.Format("{0} - {1}", e.Key.ToString(), e.Pressed.ToString()));
+ }
}
}
diff --git a/WindowsPhone/KeyboardTest/KeyboardTest/Spectrum.keyboard b/WindowsPhone/KeyboardTest/KeyboardTest/Spectrum.keyboard Binary files differindex 2607d6c..c0574cf 100644 --- a/WindowsPhone/KeyboardTest/KeyboardTest/Spectrum.keyboard +++ b/WindowsPhone/KeyboardTest/KeyboardTest/Spectrum.keyboard diff --git a/WindowsPhone/KeyboardTest/KeyboardTestContent/Courier New.spritefont b/WindowsPhone/KeyboardTest/KeyboardTestContent/Courier New.spritefont new file mode 100644 index 0000000..ebefa68 --- /dev/null +++ b/WindowsPhone/KeyboardTest/KeyboardTestContent/Courier New.spritefont @@ -0,0 +1,52 @@ +<?xml version="1.0" encoding="utf-8"?>
+<!--
+This file contains an xml description of a font, and will be read by the XNA
+Framework Content Pipeline. Follow the comments to customize the appearance
+of the font in your game, and to change the characters which are available to draw
+with.
+-->
+<XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics">
+ <Asset Type="Graphics:FontDescription">
+
+ <!--
+ Modify this string to change the font that will be imported.
+ -->
+ <FontName>Courier New</FontName>
+
+ <!--
+ Size is a float value, measured in points. Modify this value to change
+ the size of the font.
+ -->
+ <Size>28</Size>
+
+ <!--
+ Spacing is a float value, measured in pixels. Modify this value to change
+ the amount of spacing in between characters.
+ -->
+ <Spacing>2</Spacing>
+
+ <!--
+ Style controls the style of the font. Valid entries are "Regular", "Bold", "Italic",
+ and "Bold, Italic", and are case sensitive.
+ -->
+ <Style>Regular</Style>
+
+ <!--
+ CharacterRegions control what letters are available in the font. Every
+ character from Start to End will be built and made available for drawing. The
+ default range is from 32 to 127, which is the basic Latin character set. The
+ characters are ordered according to the Unicode standard.
+ Change these regions to make more or fewer letters available. To save
+ graphics memory and decrease build time, try to use as few letters as possible.
+ You can specify additional regions by adding more <CharacterRegion> tags.
+ Alternatively, you can use a custom processor to add specific characters. See
+ the documentation for more information.
+ -->
+ <CharacterRegions>
+ <CharacterRegion>
+ <Start> </Start>
+ <End>~</End>
+ </CharacterRegion>
+ </CharacterRegions>
+ </Asset>
+</XnaContent>
\ No newline at end of file diff --git a/WindowsPhone/KeyboardTest/KeyboardTestContent/KeyboardTestContent.contentproj b/WindowsPhone/KeyboardTest/KeyboardTestContent/KeyboardTestContent.contentproj index 3ed205e..390995e 100644 --- a/WindowsPhone/KeyboardTest/KeyboardTestContent/KeyboardTestContent.contentproj +++ b/WindowsPhone/KeyboardTest/KeyboardTestContent/KeyboardTestContent.contentproj @@ -36,6 +36,13 @@ <Processor>TextureProcessor</Processor>
</Compile>
</ItemGroup>
+ <ItemGroup>
+ <Compile Include="Courier New.spritefont">
+ <Name>Courier New</Name>
+ <Importer>FontDescriptionImporter</Importer>
+ <Processor>FontDescriptionProcessor</Processor>
+ </Compile>
+ </ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\$(XnaFrameworkVersion)\Microsoft.Xna.GameStudio.ContentPipeline.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
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
+ }
+}
|