From df8eab9483a3b697a70446062e139e95c402674b Mon Sep 17 00:00:00 2001 From: Ian C Date: Tue, 22 May 2012 22:05:17 +0000 Subject: Added some base source files for the joystick and joystick test. --- .../JoystickDriver.cs | 177 +++++++++++++++++++++ .../JoystickState.cs | 43 +++++ 2 files changed, 220 insertions(+) create mode 100644 src/Noddybox.Emulation.Xna.Joystick/JoystickDriver.cs create mode 100644 src/Noddybox.Emulation.Xna.Joystick/JoystickState.cs (limited to 'src') diff --git a/src/Noddybox.Emulation.Xna.Joystick/JoystickDriver.cs b/src/Noddybox.Emulation.Xna.Joystick/JoystickDriver.cs new file mode 100644 index 0000000..f0d2b63 --- /dev/null +++ b/src/Noddybox.Emulation.Xna.Joystick/JoystickDriver.cs @@ -0,0 +1,177 @@ +// 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 . +// +// Copyright (c) 2012 Ian Cowburn +// +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Input.Touch; + +namespace Noddybox.Emulation.Xna.Joystick +{ + /// + /// Displays and drives the joystick. Unlike the keyboard this is not + /// not done event driven, but as a clasic Joystick interface. + /// + public class JoystickDriver + { + #region Private data + + private Texture2D backgroundImage; + private Texture2D joystickImage; + private Texture2D buttonImage; + private Vector2 position; + private JoystickState state; + + #endregion + + #region Private members + + #endregion + + #region Public properties + + /// + /// Get the current state of the joystick and fire buttons. + /// + public JoystickState State {get {return state;}} + + #endregion + + #region Public members + + /// + /// Updates the keyboard. Note that this will consume all the key press events. + /// + 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; + + + // if (t.State == TouchLocationState.Pressed) + // { + // for(f = 0; f < keymapSize && key == null; f++) + // { + // if (keymapPos[f].Contains(x, y)) + // { + // key = keymapState[f]; + // } + // } + + // if (key != null) + // { + // key.TouchId = t.Id; + + // if (key.IsSticky) + // { + // key.IsPressed = !key.IsPressed; + // } + // else + // { + // key.IsPressed = true; + // } + + // if (KeyEvent != null) + // { + // KeyEvent(this, new KeyPressEventArgs() {Key = key.Symbol, Pressed = key.IsPressed}); + // } + // } + // } + // else if (t.State == TouchLocationState.Released) + // { + // for(f = 0; f < keymapSize && key == null; f++) + // { + // if (keymapState[f].TouchId == t.Id) + // { + // key = keymapState[f]; + // } + // } + + // if (key != null) + // { + // if (!key.IsSticky) + // { + // key.IsPressed = false; + + // 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}); + // } + // } + } + } + + /// + /// Draw the keyboard + /// + /// + 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 Constructors + + /// + /// Constructor. + /// + /// The graphics device to associate textures with. + /// The image for the joystick background. + /// The image for the joystick knob. + /// The image for a joystick button. + /// Where to draw and offset the joystick to. + public JoystickDriver(GraphicsDevice graphics, Texture2D backgroundImage, Texture2D joystickImage, Texture2D buttonImage, Vector2 position) + { + this.backgroundImage = backgroundImage; + this.joystickImage = joystickImage; + this.buttonImage = buttonImage; + this.position = position; + this.state = JoystickState.None; + } + + #endregion + } +} diff --git a/src/Noddybox.Emulation.Xna.Joystick/JoystickState.cs b/src/Noddybox.Emulation.Xna.Joystick/JoystickState.cs new file mode 100644 index 0000000..97219bc --- /dev/null +++ b/src/Noddybox.Emulation.Xna.Joystick/JoystickState.cs @@ -0,0 +1,43 @@ +// 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 . +// +// Copyright (c) 2012 Ian Cowburn +// +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Input.Touch; + +namespace Noddybox.Emulation.Xna.Joystick +{ + /// + /// Defines the state of the joystick. + /// + public enum JoystickState + { + None = 0x00, + Up = 0x01, + Down = 0x02, + Left = 0x04, + Right = 0x08, + Fire1 = 0x10, + Fire2 = 0x20, + Fire3 = 0x40, + Fire4 = 0x80 + } +} -- cgit v1.2.3