Author: rloach
Date: 2007-01-14 02:59:32 -0500 (Sun, 14 Jan 2007)
New Revision: 70970
Modified:
trunk/Mono.Xna/examples/SimpleExample/SimpleExampleGame.cs
trunk/Mono.Xna/src/Microsoft.Xna.Framework/Input/GamePad.cs
trunk/Mono.Xna/src/Microsoft.Xna.Framework/Input/GamePadCapabilities.cs
trunk/Mono.Xna/src/Microsoft.Xna.Framework/Input/GamePadDPad.cs
trunk/Mono.Xna/src/Microsoft.Xna.Framework/Input/GamePadState.cs
trunk/Mono.Xna/src/Microsoft.Xna.Framework/Input/GamePadThumbSticks.cs
Log:
Implement GamePad, fix SimpleExample.
Modified: trunk/Mono.Xna/examples/SimpleExample/SimpleExampleGame.cs
===================================================================
--- trunk/Mono.Xna/examples/SimpleExample/SimpleExampleGame.cs 2007-01-13
23:28:00 UTC (rev 70969)
+++ trunk/Mono.Xna/examples/SimpleExample/SimpleExampleGame.cs 2007-01-14
07:59:32 UTC (rev 70970)
@@ -25,19 +25,30 @@
*/
#endregion License
+#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
+using Microsoft.Xna.Framework.Storage;
+#endregion
namespace SimpleExample
{
/// <summary>
- /// This is the main type for your game
+ /// The SimpleExample is what you're presented with when you
+ /// run File -> New Project -> Windows Game in Microsoft Game
+ /// Studio Express.
/// </summary>
- public class SimpleExampleGame : Game
+ /// <remarks>
+ /// When run, you'll see an empty blue screen. This code can
+ /// be run in both Mono.Xna and Microsoft XNA without changing
+ /// any code.
+ /// </remarks>
+ public class SimpleExampleGame: Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
ContentManager content;
@@ -118,25 +129,13 @@
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing
values.</param>
- private int count = 0;
- DateTime start;
protected override void Draw(GameTime gameTime)
{
- if (count++ == 0)
- start = DateTime.Now;
+ graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
- graphics.GraphicsDevice.Clear(this.colors[index++ %
colors.Length]);
// TODO: Add your drawing code here
- if (count == 10000)
- {
- TimeSpan s = DateTime.Now - start;
- float fps = (float)count / (float)s.TotalSeconds;
- }
base.Draw(gameTime);
}
-
- private int index = 0;
- private Color[] colors = new Color[] { Color.AliceBlue, Color.Red,
Color.Yellow, Color.Green, Color.Indigo, Color.LightSteelBlue };
}
-}
+}
\ No newline at end of file
Modified: trunk/Mono.Xna/src/Microsoft.Xna.Framework/Input/GamePad.cs
===================================================================
--- trunk/Mono.Xna/src/Microsoft.Xna.Framework/Input/GamePad.cs 2007-01-13
23:28:00 UTC (rev 70969)
+++ trunk/Mono.Xna/src/Microsoft.Xna.Framework/Input/GamePad.cs 2007-01-14
07:59:32 UTC (rev 70970)
@@ -5,6 +5,8 @@
http://www.taoframework.com
All rights reserved.
+Authors: Rob Loach (http://www.robloach.net)
+
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
@@ -34,30 +36,133 @@
namespace Microsoft.Xna.Framework.Input
{
+ /// <summary>
+ /// Allows retrieval of user interaction with an Xbox 360 Controller and
setting of controller vibration motors.
+ /// </summary>
public static class GamePad
{
+ // The joysticks and associated states
+ private static Joystick[] joystick = new Joystick[4];
+ private static GamePadState[] state = new GamePadState[4];
+
static GamePad()
{
+ // Register the number of joysticks that are available
+ int numJoysticks = (Joysticks.NumberOfJoysticks > 4) ? 4 :
Joysticks.NumberOfJoysticks;
+ for (int i = 0; i < numJoysticks; i++)
+ {
+ joystick[i] = Joysticks.OpenJoystick(i);
+ state[i].isConnected = true;
+ System.Diagnostics.Debug.WriteLine("Joystick Registered: " +
joystick[i].Name + " (" + joystick[i].NumberOfAxes + " Axises, " +
joystick[i].NumberOfBalls + " Balls, " + joystick[i].NumberOfButtons + "
Buttons, " + joystick[i].NumberOfHats + " Hats)");
+ }
+
+ // Register the joystick events
+ Events.JoystickButtonDown += new
JoystickButtonEventHandler(Events_JoystickButton);
+ Events.JoystickButtonUp += new
JoystickButtonEventHandler(Events_JoystickButton);
+ Events.JoystickAxisMotion += new
JoystickAxisEventHandler(Events_JoystickAxisMotion);
+ Events.JoystickBallMotion += new
JoystickBallEventHandler(Events_JoystickBallMotion);
+ Events.JoystickHatMotion += new
JoystickHatEventHandler(Events_JoystickHatMotion);
}
- private static GamePadState[] gamePadState = new GamePadState[4];
+ static void Events_JoystickHatMotion(object sender,
JoystickHatEventArgs e)
+ {
+ // DPad Movement
+ System.Diagnostics.Debug.WriteLine("Player " + e.Device + " Hat
Motion #" + e.HatIndex + "," + e.HatValue);
+ state[e.Device].dPad.up = ((e.HatValue & 1) == 1) ?
ButtonState.Pressed : ButtonState.Released;
+ state[e.Device].dPad.right = ((e.HatValue & 2) == 2) ?
ButtonState.Pressed : ButtonState.Released;
+ state[e.Device].dPad.down = ((e.HatValue & 4) == 4) ?
ButtonState.Pressed : ButtonState.Released;
+ state[e.Device].dPad.left = ((e.HatValue & 8) == 8) ?
ButtonState.Pressed : ButtonState.Released;
+
+ IncreasePacketNumber(ref state[e.Device].packetNumber);
+ }
+
+ static void Events_JoystickBallMotion(object sender,
JoystickBallEventArgs e)
+ {
+ System.Diagnostics.Debug.WriteLine("Player " + e.Device + " Ball
Motion #" + e.Ball + ": " + e.RelativeX + "," + e.RelativeY);
+
+ // TODO: Is the ball joystick event even needed?
+ }
+
+ static void Events_JoystickAxisMotion(object sender,
JoystickAxisEventArgs e)
+ {
+ System.Diagnostics.Debug.WriteLine("Player " + e.Device + " Axis
Motion #" + e.AxisIndex + ": " + e.AxisValue);
+
+ if (e.AxisIndex == 0) // X Axis
+ state[e.Device].thumbSticks.left.X = e.AxisValue;
+ else // Y Axis
+ state[e.Device].thumbSticks.left.Y = e.AxisValue;
+
+ IncreasePacketNumber(ref state[e.Device].packetNumber);
+ // TODO: Find out how to get the right thumbstick values.
+ }
+
+ static void Events_JoystickButton(object sender,
JoystickButtonEventArgs e)
+ {
+ // Button pushed or released
+ System.Diagnostics.Debug.WriteLine("Player " + e.Device + " Button
Action " + e.ButtonPressed + " #" + e.Button);
+
+ ButtonState buttonstate = e.ButtonPressed ? ButtonState.Pressed :
ButtonState.Released;
+ switch (e.Button)
+ {
+ case 0: state[e.Device].buttons.b = buttonstate; break;
+ case 1: state[e.Device].buttons.a = buttonstate; break;
+ case 2: state[e.Device].buttons.x = buttonstate; break;
+ case 3: state[e.Device].buttons.y = buttonstate; break;
+ case 4: state[e.Device].buttons.leftShoulder = buttonstate;
break;
+ case 5: state[e.Device].buttons.rightShoulder = buttonstate;
break;
+ case 6: state[e.Device].buttons.back = buttonstate; break;
+ case 7: state[e.Device].buttons.start = buttonstate; break;
+ case 8: state[e.Device].buttons.leftStick = buttonstate; break;
+ case 9: state[e.Device].buttons.rightStick = buttonstate;
break;
+ }
+
+ IncreasePacketNumber(ref state[e.Device].packetNumber);
+ }
+
public static GamePadCapabilities GetCapabilities(PlayerIndex
playerIndex)
{
- throw new NotImplementedException();
+ GamePadCapabilities caps = new GamePadCapabilities();
+ Joystick stick = joystick[(int)playerIndex];
+ if (stick != null)
+ {
+ caps.hasAButton = stick.NumberOfButtons > 1;
+ caps.hasBButton = stick.NumberOfButtons > 0;
+ caps.hasDPadDownButton = caps.hasDPadLeftButton =
caps.hasDPadRightButton = caps.hasDPadUpButton = stick.NumberOfHats > 0;
+ caps.hasLeftShoulderButton = stick.NumberOfButtons > 4;
+ caps.hasRightShoulderButton = stick.NumberOfButtons > 5;
+ caps.hasStartButton = stick.NumberOfButtons > 7;
+ caps.hasXButton = stick.NumberOfButtons > 2;
+ caps.hasYButton = stick.NumberOfButtons > 3;
+ caps.isConnected = true;
+ }
+ return caps;
}
+
public static GamePadState GetState(PlayerIndex playerIndex)
{
return GamePad.GetState(playerIndex,
GamePadDeadZone.IndependentAxes);
}
+
public static GamePadState GetState(PlayerIndex playerIndex,
GamePadDeadZone deadZoneMode)
{
// TODO: Consider the dead zone of the gamepads.
- return gamePadState[(int)playerIndex];
+ return state[(int)playerIndex];
}
+
public static bool SetVibration(PlayerIndex playerIndex, float
leftMotor, float rightMotor)
{
throw new NotImplementedException();
}
+
+ /// <summary>
+ /// Increases the packet number of the given player's device.
+ /// </summary>
+ /// <param name="player">The device number to increase.</param>
+ private static void IncreasePacketNumber(ref int packetNumber)
+ {
+ if (++packetNumber >= int.MaxValue)
+ packetNumber = int.MinValue;
+ }
}
}
Modified:
trunk/Mono.Xna/src/Microsoft.Xna.Framework/Input/GamePadCapabilities.cs
===================================================================
--- trunk/Mono.Xna/src/Microsoft.Xna.Framework/Input/GamePadCapabilities.cs
2007-01-13 23:28:00 UTC (rev 70969)
+++ trunk/Mono.Xna/src/Microsoft.Xna.Framework/Input/GamePadCapabilities.cs
2007-01-14 07:59:32 UTC (rev 70970)
@@ -31,11 +31,18 @@
{
public struct GamePadCapabilities
{
+ internal GamePadType gamePadType;
+ internal bool hasAButton, hasBButton, hasXButton, hasYButton,
+ hasBackButton, hasStartButton,
+ hasLeftShoulderButton, hasRightShoulderButton,
+ hasDPadLeftButton, hasDPadDownButton, hasDPadRightButton,
hasDPadUpButton,
+ isConnected, hasVoiceSupport;
+
public GamePadType GamePadType
{
get
{
- throw new NotImplementedException();
+ return gamePadType;
}
}
@@ -43,7 +50,7 @@
{
get
{
- throw new NotImplementedException();
+ return hasAButton;
}
}
@@ -51,7 +58,7 @@
{
get
{
- throw new NotImplementedException();
+ return hasBackButton;
}
}
@@ -59,7 +66,7 @@
{
get
{
- throw new NotImplementedException();
+ return hasBButton;
}
}
@@ -67,7 +74,7 @@
{
get
{
- throw new NotImplementedException();
+ return hasDPadDownButton;
}
}
@@ -75,7 +82,7 @@
{
get
{
- throw new NotImplementedException();
+ return hasDPadLeftButton;
}
}
@@ -83,7 +90,7 @@
{
get
{
- throw new NotImplementedException();
+ return hasDPadRightButton;
}
}
@@ -91,7 +98,7 @@
{
get
{
- throw new NotImplementedException();
+ return hasDPadUpButton;
}
}
@@ -99,7 +106,7 @@
{
get
{
- throw new NotImplementedException();
+ return hasLeftShoulderButton;
}
}
@@ -107,7 +114,7 @@
{
get
{
- throw new NotImplementedException();
+ return hasRightShoulderButton;
}
}
@@ -115,7 +122,7 @@
{
get
{
- throw new NotImplementedException();
+ return hasStartButton;
}
}
@@ -123,7 +130,7 @@
{
get
{
- throw new NotImplementedException();
+ return hasVoiceSupport;
}
}
@@ -131,7 +138,7 @@
{
get
{
- throw new NotImplementedException();
+ return hasXButton;
}
}
@@ -139,14 +146,14 @@
{
get
{
- throw new NotImplementedException();
+ return hasYButton;
}
}
public bool IsConnected
{
get
{
- throw new NotImplementedException();
+ return isConnected;
}
}
}
Modified: trunk/Mono.Xna/src/Microsoft.Xna.Framework/Input/GamePadDPad.cs
===================================================================
--- trunk/Mono.Xna/src/Microsoft.Xna.Framework/Input/GamePadDPad.cs
2007-01-13 23:28:00 UTC (rev 70969)
+++ trunk/Mono.Xna/src/Microsoft.Xna.Framework/Input/GamePadDPad.cs
2007-01-14 07:59:32 UTC (rev 70970)
@@ -5,6 +5,8 @@
http://www.taoframework.com
All rights reserved.
+Authors: Rob Loach (http://www.robloach.net)
+
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
@@ -29,27 +31,31 @@
namespace Microsoft.Xna.Framework.Input
{
-
+ /// <summary>
+ /// Identifies which directions on the directional pad of an Xbox 360
Controller are being pressed.
+ /// </summary>
public struct GamePadDPad
{
+ internal ButtonState down, left, right, up;
-
public static bool operator !=(GamePadDPad left, GamePadDPad right)
{
- throw new NotImplementedException();
+ return !(left == right);
}
public static bool operator ==(GamePadDPad left, GamePadDPad right)
{
- throw new NotImplementedException();
+ return (left.Down == right.Down)
+ && (left.Up == right.Up)
+ && (left.Left == right.Left)
+ && (left.Right == right.Right);
}
-
public ButtonState Down
{
get
{
- throw new NotImplementedException();
+ return down;
}
}
@@ -57,7 +63,7 @@
{
get
{
- throw new NotImplementedException();
+ return left;
}
}
@@ -65,7 +71,7 @@
{
get
{
- throw new NotImplementedException();
+ return right;
}
}
@@ -73,14 +79,13 @@
{
get
{
- throw new NotImplementedException();
+ return up;
}
}
-
public override bool Equals(object obj)
{
- throw new NotImplementedException();
+ return (obj is GamePadDPad) ? false : (GamePadDPad)obj == this;
}
public override int GetHashCode()
@@ -90,7 +95,18 @@
public override string ToString()
{
- throw new NotImplementedException();
+ string output = string.Empty;
+ if (this.up == ButtonState.Pressed)
+ output += (output.Length == 0) ? "Up" : " Up";
+ if (this.down == ButtonState.Pressed)
+ output += (output.Length == 0) ? "Down" : " Down";
+ if (this.left == ButtonState.Pressed)
+ output += (output.Length == 0) ? "Left" : " Left";
+ if (this.right == ButtonState.Pressed)
+ output += (output.Length == 0) ? "Right" : " Right";
+ if (output.Length == 0)
+ output = "None";
+ return string.Format("{{DPad:{0}}}", output);
}
}
}
Modified: trunk/Mono.Xna/src/Microsoft.Xna.Framework/Input/GamePadState.cs
===================================================================
--- trunk/Mono.Xna/src/Microsoft.Xna.Framework/Input/GamePadState.cs
2007-01-13 23:28:00 UTC (rev 70969)
+++ trunk/Mono.Xna/src/Microsoft.Xna.Framework/Input/GamePadState.cs
2007-01-14 07:59:32 UTC (rev 70970)
@@ -31,9 +31,12 @@
{
public struct GamePadState
{
- private bool isConnected;
- private GamePadDPad dPad;
- private GamePadButtons buttons;
+ internal bool isConnected;
+ internal GamePadDPad dPad;
+ internal GamePadButtons buttons;
+ internal GamePadThumbSticks thumbSticks;
+ internal GamePadTriggers triggers;
+ internal int packetNumber;
public static bool operator !=(GamePadState left, GamePadState right)
{
@@ -58,7 +61,7 @@
{
get
{
- throw new NotImplementedException();
+ return dPad;
}
}
@@ -74,7 +77,7 @@
{
get
{
- throw new NotImplementedException();
+ return packetNumber;
}
}
@@ -82,7 +85,7 @@
{
get
{
- throw new NotImplementedException();
+ return thumbSticks;
}
}
@@ -90,7 +93,7 @@
{
get
{
- throw new NotImplementedException();
+ return triggers;
}
}
@@ -107,7 +110,7 @@
public override string ToString()
{
- throw new NotImplementedException();
+ return string.Format("{{IsConnected:{0}}}", isConnected);
}
}
}
Modified: trunk/Mono.Xna/src/Microsoft.Xna.Framework/Input/GamePadThumbSticks.cs
===================================================================
--- trunk/Mono.Xna/src/Microsoft.Xna.Framework/Input/GamePadThumbSticks.cs
2007-01-13 23:28:00 UTC (rev 70969)
+++ trunk/Mono.Xna/src/Microsoft.Xna.Framework/Input/GamePadThumbSticks.cs
2007-01-14 07:59:32 UTC (rev 70970)
@@ -5,6 +5,8 @@
http://www.taoframework.com
All rights reserved.
+Authors: Rob Loach (http://www.robloach.net)
+
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
@@ -29,10 +31,13 @@
namespace Microsoft.Xna.Framework.Input
{
+ /// <summary>
+ /// Structure that represents the position of left and right sticks
(thumbsticks) on an Xbox 360 Controller.
+ /// </summary>
public struct GamePadThumbSticks
{
+ internal Vector2 left, right;
-
public static bool operator !=(GamePadThumbSticks left,
GamePadThumbSticks right)
{
throw new NotImplementedException();
@@ -48,7 +53,7 @@
{
get
{
- throw new NotImplementedException();
+ return left;
}
}
@@ -56,7 +61,7 @@
{
get
{
- throw new NotImplementedException();
+ return right;
}
}
@@ -73,7 +78,7 @@
public override string ToString()
{
- throw new NotImplementedException();
+ return string.Format("{{Left:{0} Right:{1}}}", left, right);
}
}
}
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches