summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan C <ianc@noddybox.co.uk>2012-05-28 22:46:59 +0000
committerIan C <ianc@noddybox.co.uk>2012-05-28 22:46:59 +0000
commit643c13f3ebd3240aa58f83a35a66909fdd9545d9 (patch)
treef342069f0a639d55e20f5913e758336932cdcf2d
parent60b52b009c2f74cea7d5ffb0ad62cbbc55c41729 (diff)
Added test code to handle buttons to make sure drawing is OK.
-rw-r--r--WindowsPhone/JoystickTest/JoystickTest/Game1.cs41
-rw-r--r--WindowsPhone/JoystickTest/JoystickTestContent/JoystickTestContent.contentproj17
-rw-r--r--WindowsPhone/JoystickTest/JoystickTestContent/button.pngbin0 -> 6594 bytes
-rw-r--r--WindowsPhone/JoystickTest/JoystickTestContent/joystick.pngbin0 -> 16006 bytes
-rw-r--r--WindowsPhone/JoystickTest/JoystickTestContent/joystick_background.pngbin0 -> 109311 bytes
-rw-r--r--src/Noddybox.Emulation.Xna.Input/Joystick/JoystickDriver.cs260
-rw-r--r--src/Noddybox.Emulation.Xna.Input/Joystick/JoystickState.cs37
7 files changed, 241 insertions, 114 deletions
diff --git a/WindowsPhone/JoystickTest/JoystickTest/Game1.cs b/WindowsPhone/JoystickTest/JoystickTest/Game1.cs
index a5f8be3..53560a7 100644
--- a/WindowsPhone/JoystickTest/JoystickTest/Game1.cs
+++ b/WindowsPhone/JoystickTest/JoystickTest/Game1.cs
@@ -9,6 +9,8 @@ using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
+using Noddybox.Emulation.Xna.Input;
+using Noddybox.Emulation.Xna.Input.Joystick;
namespace JoystickTest
{
@@ -19,6 +21,12 @@ namespace JoystickTest
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
+ InputManager manager;
+ JoystickDriver digital;
+ JoystickDriver analogue;
+ Texture2D background;
+ Texture2D button;
+ Texture2D joystick;
public Game1()
{
@@ -45,8 +53,6 @@ namespace JoystickTest
/// </summary>
protected override void Initialize()
{
- // TODO: Add your initialization logic here
-
base.Initialize();
}
@@ -56,10 +62,21 @@ namespace JoystickTest
/// </summary>
protected override void LoadContent()
{
- // Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
- // TODO: use this.Content to load your game content here
+ background = Content.Load<Texture2D>("joystick_background");
+ joystick = Content.Load<Texture2D>("joystick");
+ button = Content.Load<Texture2D>("button");
+
+ manager = new InputManager();
+
+ digital = new JoystickDriver(manager, GraphicsDevice, JoystickType.Digital, background, joystick, button,
+ Vector2.Zero, new Vector2(100), new Vector2[2] {new Vector2(300, 50), new Vector2(300, 150)},
+ 10, 100);
+
+ analogue = new JoystickDriver(manager, GraphicsDevice, JoystickType.Analogue, background, joystick, button,
+ new Vector2(0, 300), new Vector2(100), new Vector2[1] {new Vector2(300, 150)},
+ 10, 100);
}
/// <summary>
@@ -78,12 +95,7 @@ namespace JoystickTest
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
- // Allows the game to exit
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit();
-
- // TODO: Add your update logic here
-
+ manager.Update();
base.Update(gameTime);
}
@@ -93,9 +105,14 @@ namespace JoystickTest
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
- GraphicsDevice.Clear(Color.CornflowerBlue);
+ GraphicsDevice.Clear(Color.White);
+
+ spriteBatch.Begin();
+
+ digital.Draw(spriteBatch);
+ analogue.Draw(spriteBatch);
- // TODO: Add your drawing code here
+ spriteBatch.End();
base.Draw(gameTime);
}
diff --git a/WindowsPhone/JoystickTest/JoystickTestContent/JoystickTestContent.contentproj b/WindowsPhone/JoystickTest/JoystickTestContent/JoystickTestContent.contentproj
index 4ae643c..abe9430 100644
--- a/WindowsPhone/JoystickTest/JoystickTestContent/JoystickTestContent.contentproj
+++ b/WindowsPhone/JoystickTest/JoystickTestContent/JoystickTestContent.contentproj
@@ -29,6 +29,23 @@
<Reference Include="Microsoft.Xna.Framework.Content.Pipeline.AudioImporters, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=MSIL" />
<Reference Include="Microsoft.Xna.Framework.Content.Pipeline.VideoImporters, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=MSIL" />
</ItemGroup>
+ <ItemGroup>
+ <Compile Include="button.png">
+ <Name>button</Name>
+ <Importer>TextureImporter</Importer>
+ <Processor>TextureProcessor</Processor>
+ </Compile>
+ <Compile Include="joystick.png">
+ <Name>joystick</Name>
+ <Importer>TextureImporter</Importer>
+ <Processor>TextureProcessor</Processor>
+ </Compile>
+ <Compile Include="joystick_background.png">
+ <Name>joystick_background</Name>
+ <Importer>TextureImporter</Importer>
+ <Processor>TextureProcessor</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/WindowsPhone/JoystickTest/JoystickTestContent/button.png b/WindowsPhone/JoystickTest/JoystickTestContent/button.png
new file mode 100644
index 0000000..b2e483c
--- /dev/null
+++ b/WindowsPhone/JoystickTest/JoystickTestContent/button.png
Binary files differ
diff --git a/WindowsPhone/JoystickTest/JoystickTestContent/joystick.png b/WindowsPhone/JoystickTest/JoystickTestContent/joystick.png
new file mode 100644
index 0000000..cb24378
--- /dev/null
+++ b/WindowsPhone/JoystickTest/JoystickTestContent/joystick.png
Binary files differ
diff --git a/WindowsPhone/JoystickTest/JoystickTestContent/joystick_background.png b/WindowsPhone/JoystickTest/JoystickTestContent/joystick_background.png
new file mode 100644
index 0000000..c7ac0d3
--- /dev/null
+++ b/WindowsPhone/JoystickTest/JoystickTestContent/joystick_background.png
Binary files differ
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
}
}