From 643c13f3ebd3240aa58f83a35a66909fdd9545d9 Mon Sep 17 00:00:00 2001 From: Ian C Date: Mon, 28 May 2012 22:46:59 +0000 Subject: Added test code to handle buttons to make sure drawing is OK. --- .../Joystick/JoystickDriver.cs | 260 +++++++++++++-------- 1 file changed, 168 insertions(+), 92 deletions(-) (limited to 'src/Noddybox.Emulation.Xna.Input/Joystick/JoystickDriver.cs') diff --git a/src/Noddybox.Emulation.Xna.Input/Joystick/JoystickDriver.cs b/src/Noddybox.Emulation.Xna.Input/Joystick/JoystickDriver.cs index 049d057..dc83831 100644 --- a/src/Noddybox.Emulation.Xna.Input/Joystick/JoystickDriver.cs +++ b/src/Noddybox.Emulation.Xna.Input/Joystick/JoystickDriver.cs @@ -31,145 +31,221 @@ namespace Noddybox.Emulation.Xna.Input.Joystick /// public class JoystickDriver { + #region Event arguments + + /// + /// Event data when the digital joystick state changes + /// + public class DigitalJoystickEventArgs : EventArgs + { + /// + /// The joystick state. + /// + public DigitalJoystickState State {get; private set;} + + public bool[] Buttons {get; private set;} + + private DigitalJoystickEventArgs(DigitalJoystickState state, bool[] buttons) + { + State = state; + Buttons = buttons; + } + } + + /// + /// Event data when the analogue joystick state changes + /// + public class AnalogueJoystickEventArgs : EventArgs + { + /// + /// The joystick state. + /// + public Vector2 State {get; private set;} + + public bool[] Buttons {get; private set;} + + private AnalogueJoystickEventArgs(Vector2 state, bool[] buttons) + { + State = state; + Buttons = buttons; + } + } + + #endregion + #region Private data + private InputManager manager; + private bool subscribed; + private Texture2D backgroundImage; private Texture2D joystickImage; private Texture2D buttonImage; - private Vector2 position; - private JoystickState state; - #endregion + private DigitalJoystickState digital; + private Vector2 analogue; + private bool[] button; - #region Private members + private Vector2 backgroundPosition; + private Vector2 joystickPosition; + private Vector2[] buttonPositions; + private float deadzone; + private float stickSize; + private int numButtons; + + private Vector2 joystickOffset; + private Vector2 buttonOffset; + private int joystickSize; + private int buttonSize; + + private JoystickLock joylock; #endregion - #region Public properties + #region Private members /// - /// Get the current state of the joystick and fire buttons. + /// Updates the joystick. /// - public JoystickState State {get {return state;}} + public void TouchScreenHandler(object sender, InputManager.TouchLocationEventArgs e) + { + if (e.Location.State == TouchLocationState.Pressed) + { + for(int f = 0; !e.Handled && f < numButtons; f++) + { + if ((buttonPositions[f] - e.Location.Position).Length() < buttonSize) + { + e.Handled = true; + button[f] = true; + } + } + } + else if (e.Location.State == TouchLocationState.Released) + { + for(int f = 0; !e.Handled && f < numButtons; f++) + { + if ((buttonPositions[f] - e.Location.Position).Length() < buttonSize) + { + e.Handled = true; + button[f] = false; + } + } + } + } #endregion #region Public members /// - /// Updates the keyboard. Note that this will consume all the key press events. + /// Stops consuming inputs. /// - public void Update() + public void StopJoytstickUpdates() { - foreach (TouchLocation t in TouchPanel.GetState()) + if (subscribed) { - // 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}); - // } - // } + manager.TouchEvent -= TouchScreenHandler; + subscribed = false; } } /// - /// Draw the keyboard + /// Starts consuming inputs. + /// + public void StartJoystickUpdates() + { + if (!subscribed) + { + manager.TouchEvent += TouchScreenHandler; + subscribed = true; + } + } + + /// + /// Draw the joystick /// /// 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)); - // } - //} + spriteBatch.Draw(backgroundImage, backgroundPosition, Color.White); + + spriteBatch.Draw(joystickImage, joystickPosition + analogue, + null, Color.White, 0f, joystickOffset, 1f, SpriteEffects.None, 0f); + + for(int f = 0; f < numButtons; f++) + { + spriteBatch.Draw(buttonImage, buttonPositions[f], + null, button[f] ? Color.LightGray : Color.White, 0f, buttonOffset, button[f] ? 0.9f : 1f, SpriteEffects.None, 0f); + } } #endregion + #region Events + + #endregion + #region Constructors /// /// Constructor. /// + /// The input manager to attach to. /// The graphics device to associate textures with. + /// The type of joystick. /// 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) + /// Where to draw and offset the joystick background to. + /// Where to centre the joystick. Note this is absolute. + /// Positions for the buttons. + /// Deadzone radius where joystick movement is not picked up. + /// The maximum radius that the joystick can move. + public JoystickDriver(InputManager manager, GraphicsDevice graphics, JoystickType joystickType, + Texture2D backgroundImage, Texture2D joystickImage, Texture2D buttonImage, + Vector2 backgroundPosition, Vector2 joystickPosition, Vector2[] buttonPositions, + float deadzone, float stickSize) { + this.manager = manager; this.backgroundImage = backgroundImage; this.joystickImage = joystickImage; this.buttonImage = buttonImage; - this.position = position; - this.state = JoystickState.None; + + this.backgroundPosition = backgroundPosition; + this.joystickPosition = joystickPosition + backgroundPosition; + + this.joystickSize = Math.Min(joystickImage.Width, joystickImage.Height) / 2; + this.joystickOffset = new Vector2(joystickImage.Width / 2f, joystickImage.Height / 2f); + + if (buttonImage != null) + { + this.buttonSize = Math.Min(buttonImage.Width, buttonImage.Height) / 2; + this.buttonOffset = new Vector2(buttonImage.Width / 2f, buttonImage.Height / 2f); + this.numButtons = buttonPositions.Length; + + this.button = new bool[numButtons]; + this.buttonPositions = new Vector2[numButtons]; + + for(int f = 0; f < numButtons; f++) + { + this.buttonPositions[f] = backgroundPosition + buttonPositions[f] + buttonOffset; + } + } + else + { + this.buttonSize = 0; + this.buttonOffset = Vector2.Zero; + this.numButtons = 0; + } + + this.deadzone = deadzone; + this.stickSize = stickSize; + + this.digital = DigitalJoystickState.Centre; + this.joylock = JoystickLock.EightWay; + this.analogue = Vector2.Zero; + + StartJoystickUpdates(); } #endregion -- cgit v1.2.3