summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Noddybox.Emulation.Xna.Input/Joystick/JoystickDriver.cs260
-rw-r--r--src/Noddybox.Emulation.Xna.Input/Joystick/JoystickState.cs37
2 files changed, 195 insertions, 102 deletions
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
/// </summary>
public class JoystickDriver
{
+ #region Event arguments
+
+ /// <summary>
+ /// Event data when the digital joystick state changes
+ /// </summary>
+ public class DigitalJoystickEventArgs : EventArgs
+ {
+ /// <summary>
+ /// The joystick state.
+ /// </summary>
+ public DigitalJoystickState State {get; private set;}
+
+ public bool[] Buttons {get; private set;}
+
+ private DigitalJoystickEventArgs(DigitalJoystickState state, bool[] buttons)
+ {
+ State = state;
+ Buttons = buttons;
+ }
+ }
+
+ /// <summary>
+ /// Event data when the analogue joystick state changes
+ /// </summary>
+ public class AnalogueJoystickEventArgs : EventArgs
+ {
+ /// <summary>
+ /// The joystick state.
+ /// </summary>
+ 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
/// <summary>
- /// Get the current state of the joystick and fire buttons.
+ /// Updates the joystick.
/// </summary>
- 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
/// <summary>
- /// Updates the keyboard. Note that this will consume all the key press events.
+ /// Stops consuming inputs.
/// </summary>
- 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;
}
}
/// <summary>
- /// Draw the keyboard
+ /// Starts consuming inputs.
+ /// </summary>
+ public void StartJoystickUpdates()
+ {
+ if (!subscribed)
+ {
+ manager.TouchEvent += TouchScreenHandler;
+ subscribed = true;
+ }
+ }
+
+ /// <summary>
+ /// Draw the joystick
/// </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));
- // }
- //}
+ 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
/// <summary>
/// Constructor.
/// </summary>
+ /// <param name="manager">The input manager to attach to.</param>
/// <param name="graphics">The graphics device to associate textures with.</param>
+ /// <param name="joystickType">The type of joystick.</param>
/// <param name="backgroundImage">The image for the joystick background.</param>
/// <param name="joystickImage">The image for the joystick knob.</param>
/// <param name="buttonImage">The image for a joystick button.</param>
- /// <param name="position">Where to draw and offset the joystick to.</param>
- public JoystickDriver(GraphicsDevice graphics, Texture2D backgroundImage, Texture2D joystickImage, Texture2D buttonImage, Vector2 position)
+ /// <param name="backgroundPosition">Where to draw and offset the joystick background to.</param>
+ /// <param name="joystickPosition">Where to centre the joystick. Note this is absolute.</param>
+ /// <param name="buttonPositions">Positions for the buttons.</param>
+ /// <param name="deadzone">Deadzone radius where joystick movement is not picked up.</param>
+ /// <param name="stickSize">The maximum radius that the joystick can move.</param>
+ 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
diff --git a/src/Noddybox.Emulation.Xna.Input/Joystick/JoystickState.cs b/src/Noddybox.Emulation.Xna.Input/Joystick/JoystickState.cs
index d57ebaa..3933127 100644
--- a/src/Noddybox.Emulation.Xna.Input/Joystick/JoystickState.cs
+++ b/src/Noddybox.Emulation.Xna.Input/Joystick/JoystickState.cs
@@ -28,16 +28,33 @@ namespace Noddybox.Emulation.Xna.Input.Joystick
/// <summary>
/// Defines the state of the joystick.
/// </summary>
- public enum JoystickState
+ [Flags]
+ public enum DigitalJoystickState
{
- None = 0x00,
- Up = 0x01,
- Down = 0x02,
- Left = 0x04,
- Right = 0x08,
- Fire1 = 0x10,
- Fire2 = 0x20,
- Fire3 = 0x40,
- Fire4 = 0x80
+ Centre = 0x00,
+ Up = 0x01,
+ Down = 0x02,
+ Left = 0x04,
+ Right = 0x08
+ }
+
+ /// <summary>
+ /// Defines the type of joystick.
+ /// </summary>
+ public enum JoystickType
+ {
+ Analogue,
+ Digital
+ }
+
+ /// <summary>
+ /// Defines the locked axis of the joystick.
+ /// </summary>
+ public enum JoystickLock
+ {
+ EightWay,
+ FourWay,
+ TwoWayHorizontal,
+ TwoWayVertical
}
}