Permalink
Browse files

Gamepad support implemented for Win/Lin/OSX

  • Loading branch information...
1 parent cef31c6 commit 8d09d7dbcb8347e064ca39f2d2c889525014ee64 @Ragath Ragath committed Nov 14, 2011
View
4 MonoGame.Framework.Linux.sln
@@ -1,6 +1,6 @@

-Microsoft Visual Studio Solution File, Format Version 10.00
-# Visual Studio 2008
+Microsoft Visual Studio Solution File, Format Version 11.00
+# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MonoGame.Framework.Linux", "MonoGame.Framework\MonoGame.Framework.Linux.csproj", "{35253CE1-C864-4CD3-8249-4D1319748E8F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lidgren.Network.Linux", "ThirdParty\Lidgren.Network\Lidgren.Network.Linux.csproj", "{AE483C29-042E-4226-BA52-D247CE7676DA}"
View
41 MonoGame.Framework/Input/GamePadButtons.cs
@@ -44,91 +44,96 @@ namespace Microsoft.Xna.Framework.Input
{
public struct GamePadButtons
{
- private Buttons _buttons;
-
- public GamePadButtons(Buttons buttons)
- {
- _buttons = buttons;
- }
+ internal Buttons buttons;
public ButtonState A
{
get
{
- return ((_buttons & Buttons.A) == Buttons.A) ? ButtonState.Pressed : ButtonState.Released;
+ return ((buttons & Buttons.A) == Buttons.A) ? ButtonState.Pressed : ButtonState.Released;
}
}
public ButtonState B
{
get
{
- return ((_buttons & Buttons.B) == Buttons.B) ? ButtonState.Pressed : ButtonState.Released;
+ return ((buttons & Buttons.B) == Buttons.B) ? ButtonState.Pressed : ButtonState.Released;
}
}
public ButtonState Back
{
get
{
- return ((_buttons & Buttons.Back) == Buttons.Back) ? ButtonState.Pressed : ButtonState.Released;
+ return ((buttons & Buttons.Back) == Buttons.Back) ? ButtonState.Pressed : ButtonState.Released;
}
}
public ButtonState X
{
get
{
- return ((_buttons & Buttons.X) == Buttons.X) ? ButtonState.Pressed : ButtonState.Released;
+ return ((buttons & Buttons.X) == Buttons.X) ? ButtonState.Pressed : ButtonState.Released;
}
}
public ButtonState Y
{
get
{
- return ((_buttons & Buttons.Y) == Buttons.Y) ? ButtonState.Pressed : ButtonState.Released;
+ return ((buttons & Buttons.Y) == Buttons.Y) ? ButtonState.Pressed : ButtonState.Released;
}
}
public ButtonState Start
{
get
{
- return ((_buttons & Buttons.Start) == Buttons.Start) ? ButtonState.Pressed : ButtonState.Released;
+ return ((buttons & Buttons.Start) == Buttons.Start) ? ButtonState.Pressed : ButtonState.Released;
}
}
public ButtonState LeftShoulder
{
get
{
- return ((_buttons & Buttons.LeftShoulder) == Buttons.LeftShoulder) ? ButtonState.Pressed : ButtonState.Released;
+ return ((buttons & Buttons.LeftShoulder) == Buttons.LeftShoulder) ? ButtonState.Pressed : ButtonState.Released;
}
}
public ButtonState LeftStick
{
get
{
- return ((_buttons & Buttons.LeftStick) == Buttons.LeftStick) ? ButtonState.Pressed : ButtonState.Released;
+ return ((buttons & Buttons.LeftStick) == Buttons.LeftStick) ? ButtonState.Pressed : ButtonState.Released;
}
}
public ButtonState RightShoulder
{
get
{
- return ((_buttons & Buttons.RightShoulder) == Buttons.RightShoulder) ? ButtonState.Pressed : ButtonState.Released;
+ return ((buttons & Buttons.RightShoulder) == Buttons.RightShoulder) ? ButtonState.Pressed : ButtonState.Released;
}
}
public ButtonState RightStick
{
get
{
- return ((_buttons & Buttons.RightStick) == Buttons.RightStick) ? ButtonState.Pressed : ButtonState.Released;
+ return ((buttons & Buttons.RightStick) == Buttons.RightStick) ? ButtonState.Pressed : ButtonState.Released;
}
}
public ButtonState BigButton
{
get
{
- return ((_buttons & Buttons.BigButton) == Buttons.BigButton) ? ButtonState.Pressed : ButtonState.Released;
+ return ((buttons & Buttons.BigButton) == Buttons.BigButton) ? ButtonState.Pressed : ButtonState.Released;
}
}
+
+ public GamePadButtons(Buttons buttons)
+ {
+ this.buttons = buttons;
+ }
+ internal GamePadButtons(params Buttons[] buttons)
+ : this()
+ {
+ foreach (Buttons b in buttons)
+ this.buttons |= b;
+ }
}
-
}
View
99 MonoGame.Framework/Input/GamePadDPad.cs
@@ -1,4 +1,4 @@
-// #region License
+#region License
// /*
// Microsoft Public License (Ms-PL)
// MonoGame - Copyright © 2009 The MonoGame Team
@@ -36,53 +36,54 @@
// permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular
// purpose and non-infringement.
// */
-// #endregion License
-//
-
-using System;
-
-namespace Microsoft.Xna.Framework.Input
+#endregion License
+
+using System;
+
+namespace Microsoft.Xna.Framework.Input
{
- public struct GamePadDPad
+ public struct GamePadDPad
{
- public GamePadDPad (
- ButtonState upValue,
- ButtonState downValue,
- ButtonState leftValue,
- ButtonState rightValue )
- {
- }
-
- public ButtonState Down
- {
- get
- {
- return ButtonState.Released;
- }
- }
-
- public ButtonState Left
- {
- get
- {
- return ButtonState.Released;
- }
- }
-
- public ButtonState Right
- {
- get
- {
- return ButtonState.Released;
- }
- }
-
- public ButtonState Up
- {
- get
- {
- return ButtonState.Released;
- }
- }
- }
-}
+ public ButtonState Down
+ {
+ get;
+ internal set;
+ }
+ public ButtonState Left
+ {
+ get;
+ internal set;
+ }
+ public ButtonState Right
+ {
+ get;
+ internal set;
+ }
+ public ButtonState Up
+ {
+ get;
+ internal set;
+ }
+
+ public GamePadDPad(ButtonState upValue, ButtonState downValue, ButtonState leftValue, ButtonState rightValue)
+ : this()
+ {
+ Up = upValue;
+ Down = downValue;
+ Left = leftValue;
+ Right = rightValue;
+ }
+ internal GamePadDPad(Buttons b)
+ : this()
+ {
+ if ((b & Buttons.DPadDown) == Buttons.DPadDown)
+ Down = ButtonState.Pressed;
+ if ((b & Buttons.DPadLeft) == Buttons.DPadLeft)
+ Left = ButtonState.Pressed;
+ if ((b & Buttons.DPadRight) == Buttons.DPadRight)
+ Right = ButtonState.Pressed;
+ if ((b & Buttons.DPadUp) == Buttons.DPadUp)
+ Up = ButtonState.Pressed;
+ }
+ }
+}
View
318 MonoGame.Framework/Input/GamePadState.cs
@@ -1,168 +1,200 @@
-#region License
-/*
-Microsoft Public License (Ms-PL)
-MonoGame - Copyright © 2009 The MonoGame Team
-
-All rights reserved.
-
-This license governs use of the accompanying software. If you use the software, you accept this license. If you do not
-accept the license, do not use the software.
-
-1. Definitions
-The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under
-U.S. copyright law.
-
-A "contribution" is the original software, or any additions or changes to the software.
-A "contributor" is any person that distributes its contribution under this license.
-"Licensed patents" are a contributor's patent claims that read directly on its contribution.
-
-2. Grant of Rights
-(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3,
-each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.
-(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3,
-each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.
-
-3. Conditions and Limitations
-(A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
-(B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software,
-your patent license from such contributor to the software ends automatically.
-(C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution
-notices that are present in the software.
-(D) If you distribute any portion of the software in source code form, you may do so only under this license by including
-a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object
-code form, you may only do so under a license that complies with this license.
-(E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees
-or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent
-permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular
-purpose and non-infringement.
-*/
-#endregion License
-
-using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework;
using System;
namespace Microsoft.Xna.Framework.Input
{
- public struct GamePadState
+ //
+ // Summary:
+ // Represents specific information about the state of an Xbox 360 Controller,
+ // including the current state of buttons and sticks. Reference page contains
+ // links to related code samples.
+ public struct GamePadState
{
- private GamePadThumbSticks _thumbs;
- private Buttons _buttons;
- private GamePadDPad _dPad;
- private GamePadTriggers _triggers;
-
- internal GamePadState(Buttons buttons, Vector2 LeftStick, Vector2 RightStick)
- {
- _buttons = buttons;
- _thumbs = new GamePadThumbSticks(LeftStick,RightStick);
- }
-
- public GamePadState(GamePadThumbSticks thumbs, GamePadTriggers triggers, GamePadButtons gamePadButtons, GamePadDPad dPad)
+ //
+ // Summary:
+ // Indicates whether the Xbox 360 Controller is connected. Reference page contains
+ // links to related code samples.
+ public bool IsConnected
+ {
+ get;
+ internal set;
+ }
+ //
+ // Summary:
+ // Gets the packet number associated with this state. Reference page contains
+ // links to related code samples.
+ public int PacketNumber
{
- _thumbs = thumbs;
- _triggers = triggers;
- ConvertGamePadButtonsToButtons(ref gamePadButtons, out _buttons);
- _dPad = dPad;
+ get;
+ internal set;
}
-
+
+ //
+ // Summary:
+ // Returns a structure that identifies what buttons on the Xbox 360 controller
+ // are pressed. Reference page contains links to related code samples.
public GamePadButtons Buttons
{
- get
- {
- return new GamePadButtons(_buttons);
- }
+ get;
+ internal set;
}
-
- public bool IsConnected
+ //
+ // Summary:
+ // Returns a structure that identifies what directions of the directional pad
+ // on the Xbox 360 Controller are pressed.
+ public GamePadDPad DPad
{
- get
- {
- return true;
- }
+ get;
+ internal set;
}
-
+ //
+ // Summary:
+ // Returns a structure that indicates the position of the Xbox 360 Controller
+ // sticks (thumbsticks).
public GamePadThumbSticks ThumbSticks
{
- get
- {
- return this._thumbs;
- }
+ get;
+ internal set;
+ }
+ //
+ // Summary:
+ // Returns a structure that identifies the position of triggers on the Xbox
+ // 360 controller.
+ public GamePadTriggers Triggers
+ {
+ get;
+ internal set;
}
-
- public bool IsButtonDown(Microsoft.Xna.Framework.Input.Buttons button)
+
+ //
+ // Summary:
+ // Initializes a new instance of the GamePadState class using the specified
+ // GamePadThumbSticks, GamePadTriggers, GamePadButtons, and GamePadDPad.
+ //
+ // Parameters:
+ // thumbSticks:
+ // Initial thumbstick state.
+ //
+ // triggers:
+ // Initial trigger state.
+ //
+ // buttons:
+ // Initial button state.
+ //
+ // dPad:
+ // Initial directional pad state.
+ public GamePadState(GamePadThumbSticks thumbSticks, GamePadTriggers triggers, GamePadButtons buttons, GamePadDPad dPad)
+ : this()
+ {
+ ThumbSticks = thumbSticks;
+ Triggers = triggers;
+ Buttons = buttons;
+ DPad = dPad;
+ }
+ //
+ // Summary:
+ // Initializes a new instance of the GamePadState class with the specified stick,
+ // trigger, and button values.
+ //
+ // Parameters:
+ // leftThumbStick:
+ // Left stick value. Each axis is clamped between −1.0 and 1.0.
+ //
+ // rightThumbStick:
+ // Right stick value. Each axis is clamped between −1.0 and 1.0.
+ //
+ // leftTrigger:
+ // Left trigger value. This value is clamped between 0.0 and 1.0.
+ //
+ // rightTrigger:
+ // Right trigger value. This value is clamped between 0.0 and 1.0.
+ //
+ // buttons:
+ // Array or parameter list of Buttons to initialize as pressed.
+ public GamePadState(Vector2 leftThumbStick, Vector2 rightThumbStick, float leftTrigger, float rightTrigger, params Buttons[] buttons)
+ : this(new GamePadThumbSticks(leftThumbStick, rightThumbStick), new GamePadTriggers(leftTrigger, rightTrigger), new GamePadButtons(buttons), new GamePadDPad())
{
- return ((_buttons & button) == button);
}
- public bool IsButtonUp(Microsoft.Xna.Framework.Input.Buttons button)
+ //
+ // Summary:
+ // Determines whether specified input device buttons are pressed in this GamePadState.
+ //
+ // Parameters:
+ // button:
+ // Buttons to query. Specify a single button, or combine multiple buttons using
+ // a bitwise OR operation.
+ public bool IsButtonDown(Buttons button)
+ {
+ return (Buttons.buttons & button) == button;
+ }
+ //
+ // Summary:
+ // Determines whether specified input device buttons are up (not pressed) in
+ // this GamePadState.
+ //
+ // Parameters:
+ // button:
+ // Buttons to query. Specify a single button, or combine multiple buttons using
+ // a bitwise OR operation.
+ public bool IsButtonUp(Buttons button)
{
- return !this.IsButtonDown(button);
+ return (Buttons.buttons & button) != button;
}
-
- public GamePadDPad DPad
- {
- get
- {
- return _dPad;
- }
- }
-
- public GamePadTriggers Triggers
- {
- get
- {
- return _triggers;
- }
- }
- internal static void ConvertGamePadButtonsToButtons(ref GamePadButtons gamePadButtons, out Buttons buttons)
+ //
+ // Summary:
+ // Determines whether two GamePadState instances are not equal.
+ //
+ // Parameters:
+ // left:
+ // Object on the left of the equal sign.
+ //
+ // right:
+ // Object on the right of the equal sign.
+ public static bool operator !=(GamePadState left, GamePadState right)
+ {
+ return !left.Equals(right);
+ }
+ //
+ // Summary:
+ // Determines whether two GamePadState instances are equal.
+ //
+ // Parameters:
+ // left:
+ // Object on the left of the equal sign.
+ //
+ // right:
+ // Object on the right of the equal sign.
+ public static bool operator ==(GamePadState left, GamePadState right)
{
- buttons = new Buttons();
- if (gamePadButtons.A == ButtonState.Pressed)
- {
- buttons |= Microsoft.Xna.Framework.Input.Buttons.A;
- }
- if (gamePadButtons.B == ButtonState.Pressed)
- {
- buttons |= Microsoft.Xna.Framework.Input.Buttons.B;
- }
- if (gamePadButtons.X == ButtonState.Pressed)
- {
- buttons |= Microsoft.Xna.Framework.Input.Buttons.X;
- }
- if (gamePadButtons.Y == ButtonState.Pressed)
- {
- buttons |= Microsoft.Xna.Framework.Input.Buttons.Y;
- }
- if (gamePadButtons.Back == ButtonState.Pressed)
- {
- buttons |= Microsoft.Xna.Framework.Input.Buttons.Back;
- }
- if (gamePadButtons.Start == ButtonState.Pressed)
- {
- buttons |= Microsoft.Xna.Framework.Input.Buttons.Start;
- }
- if (gamePadButtons.BigButton == ButtonState.Pressed)
- {
- buttons |= Microsoft.Xna.Framework.Input.Buttons.BigButton;
- }
- if (gamePadButtons.LeftShoulder == ButtonState.Pressed)
- {
- buttons |= Microsoft.Xna.Framework.Input.Buttons.LeftShoulder;
- }
- if (gamePadButtons.RightShoulder == ButtonState.Pressed)
- {
- buttons |= Microsoft.Xna.Framework.Input.Buttons.RightShoulder;
- }
- if (gamePadButtons.LeftStick == ButtonState.Pressed)
- {
- buttons |= Microsoft.Xna.Framework.Input.Buttons.LeftStick;
- }
- if (gamePadButtons.RightStick == ButtonState.Pressed)
- {
- buttons |= Microsoft.Xna.Framework.Input.Buttons.RightStick;
- }
+ return left.Equals(right);
+ }
+ //
+ // Summary:
+ // Returns a value that indicates whether the current instance is equal to a
+ // specified object.
+ //
+ // Parameters:
+ // obj:
+ // Object with which to make the comparison.
+ public override bool Equals(object obj)
+ {
+ return base.Equals(obj);
+ }
+ //
+ // Summary:
+ // Gets the hash code for this instance.
+ public override int GetHashCode()
+ {
+ return base.GetHashCode();
+ }
+ //
+ // Summary:
+ // Retrieves a string representation of this object.
+ public override string ToString()
+ {
+ return base.ToString();
}
}
-
}
-
View
102 MonoGame.Framework/Input/GamePadThumbSticks.cs
@@ -45,31 +45,103 @@ namespace Microsoft.Xna.Framework.Input
{
public struct GamePadThumbSticks
{
- private Vector2 _left;
- private Vector2 _right;
+ public enum GateType
+ {
+ None,
+ Round,
+ Square
+ };
+ public static GateType Gate = GateType.Round;
+
+ Vector2 left;
+ Vector2 right;
- public GamePadThumbSticks(Vector2 LeftPosition, Vector2 RightPosition)
- {
- _left = LeftPosition;
- _right = RightPosition;
- }
-
public Vector2 Left
{
- get
+ get
{
- return this._left;
+ return left;
+ }
+ internal set
+ {
+ switch (Gate)
+ {
+ case GateType.None:
+ left = value;
+ break;
+ case GateType.Round:
+ if (value.LengthSquared () > 1f)
+ left = Vector2.Normalize (value);
+ else
+ left = value;
+ break;
+ case GateType.Square:
+ left = new Vector2(MathHelper.Clamp(value.X, -1f, 1f), MathHelper.Clamp(value.Y, -1f, 1f));
+ break;
+ default:
+ left = Vector2.Zero;
+ break;
+ }
}
}
-
public Vector2 Right
{
- get
+ get
+ {
+ return right;
+ }
+ internal set
{
- return this._right;
+ switch (Gate)
+ {
+ case GateType.None:
+ right = value;
+ break;
+ case GateType.Round:
+ if (value.LengthSquared () > 1f)
+ right = Vector2.Normalize (value);
+ else
+ right = value;
+ break;
+ case GateType.Square:
+ right = new Vector2(MathHelper.Clamp(value.X, -1f, 1f), MathHelper.Clamp(value.Y, -1f, 1f));
+ break;
+ default:
+ right = Vector2.Zero;
+ break;
+ }
}
}
- }
-}
+ public GamePadThumbSticks(Vector2 leftPosition, Vector2 rightPosition):this()
+ {
+ Left = leftPosition;
+ Right = rightPosition;
+ }
+ internal void ApplyDeadZone(GamePadDeadZone dz, float size)
+ {
+ switch (dz)
+ {
+ case GamePadDeadZone.None:
+ break;
+ case GamePadDeadZone.IndependentAxes:
+ if (Math.Abs(left.X) < size)
+ left.X = 0f;
+ if (Math.Abs(left.Y) < size)
+ left.Y = 0f;
+ if (Math.Abs(right.X) < size)
+ right.X = 0f;
+ if (Math.Abs(right.Y) < size)
+ right.Y = 0f;
+ break;
+ case GamePadDeadZone.Circular:
+ if (left.LengthSquared() < size * size)
+ left = Vector2.Zero;
+ if (right.LengthSquared() < size * size)
+ right = Vector2.Zero;
+ break;
+ }
+ }
+ }
+}
View
48 MonoGame.Framework/Input/GamePadTriggers.cs
@@ -1,4 +1,4 @@
-// #region License
+#region License
// /*
// Microsoft Public License (Ms-PL)
// MonoGame - Copyright © 2009 The MonoGame Team
@@ -36,35 +36,31 @@
// permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular
// purpose and non-infringement.
// */
-// #endregion License
-//
-
-using System;
+#endregion License
+using System;
+
namespace Microsoft.Xna.Framework.Input
{
public struct GamePadTriggers
{
- public GamePadTriggers (
- float leftTrigger,
- float rightTrigger)
- {
- }
-
- public float Left
- {
- get
- {
- return 0.0f;
- }
- }
-
- public float Right
- {
- get
- {
- return 0.0f;
- }
- }
+ float left, right;
+
+ public float Left
+ {
+ get { return left; }
+ internal set { left = MathHelper.Clamp(value, 0f, 1f); }
+ }
+ public float Right
+ {
+ get { return right; }
+ internal set { right = MathHelper.Clamp(value, 0f, 1f); }
+ }
+
+ public GamePadTriggers(float leftTrigger, float rightTrigger):this()
+ {
+ Left = leftTrigger;
+ Right = rightTrigger;
+ }
}
}
View
480 MonoGame.Framework/Linux/Input/GamePad.cs
@@ -1,259 +1,263 @@
-#region License
-/*
-Microsoft Public License (Ms-PL)
-MonoGame - Copyright © 2009 The MonoGame Team
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.IO;
+using GamepadConfigLib;
+using Tao.Sdl;
+using System.Xml.Serialization;
-All rights reserved.
+namespace Microsoft.Xna.Framework.Input
+{
+ //
+ // Summary:
+ // Allows retrieval of user interaction with an Xbox 360 Controller and setting
+ // of controller vibration motors. Reference page contains links to related
+ // code samples.
+ public static class GamePad
+ {
-This license governs use of the accompanying software. If you use the software, you accept this license. If you do not
-accept the license, do not use the software.
+ static Settings settings;
+ static Settings Settings
+ {
+ get
+ {
+ if (settings == null)
+ {
+ settings = LoadConfigs("Settings.xml");
+ }
+ if (settings == null)
+ throw new Exception("Gamepad settings broke");
+ else
+ {
+ Init();
+ return settings;
+ }
+ }
+ }
+ static Settings LoadConfigs(string filename)
+ {
+ Settings e;
+ try
+ {
+ using (Stream s = File.OpenRead(filename))
+ {
+ XmlSerializer x = new XmlSerializer(typeof(Settings));
+ e = (Settings)x.Deserialize(s);
+ }
+ }
+ catch
+ {
+ return null;
+ }
+ for (int i = 0; i < 4; i++)
+ if (e[i] == null)
+ e[i] = new PadConfig();
+ return e;
+ }
-1. Definitions
-The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under
-U.S. copyright law.
+ static IntPtr[] devices = new IntPtr[4];
-A "contribution" is the original software, or any additions or changes to the software.
-A "contributor" is any person that distributes its contribution under this license.
-"Licensed patents" are a contributor's patent claims that read directly on its contribution.
+ static void Init()
+ {
+ Joystick.Init();
+ for (int i = 0; i < 4; i++)
+ {
+ PadConfig pc = settings[i];
+ if (pc.JoystickName == Sdl.SDL_JoystickName(pc.ID))
+ {
+ devices[i] = Sdl.SDL_JoystickOpen(pc.ID);
+ }
+ }
+ }
+ static void Cleanup()
+ {
+ Joystick.Cleanup();
+ }
-2. Grant of Rights
-(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3,
-each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.
-(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3,
-each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.
+ static IntPtr GetDevice(PlayerIndex index)
+ {
+ return devices[(int)index];
+ }
+ static PadConfig GetConfig(PlayerIndex index)
+ {
+ return Settings[(int)index];
+ }
-3. Conditions and Limitations
-(A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
-(B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software,
-your patent license from such contributor to the software ends automatically.
-(C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution
-notices that are present in the software.
-(D) If you distribute any portion of the software in source code form, you may do so only under this license by including
-a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object
-code form, you may only do so under a license that complies with this license.
-(E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees
-or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent
-permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular
-purpose and non-infringement.
-*/
-#endregion License
+ static Buttons StickToButtons(Vector2 stick, float DeadZoneSize)
+ {
+ Buttons b = (Buttons)0;
-using System;
-using System.Collections.Generic;
+ if (stick.X > DeadZoneSize)
+ b |= Buttons.LeftThumbstickRight;
+ if (stick.X < -DeadZoneSize)
+ b |= Buttons.LeftThumbstickLeft;
+ if (stick.Y > DeadZoneSize)
+ b |= Buttons.LeftThumbstickUp;
+ if (stick.Y < -DeadZoneSize)
+ b |= Buttons.LeftThumbstickDown;
-
-
+ return b;
+ }
+ static Buttons ReadButtons(IntPtr device, PadConfig c, float deadZoneSize)
+ {
+ short DeadZone = (short)(deadZoneSize * short.MaxValue);
+ Buttons b = (Buttons)0;
-using Microsoft.Xna.Framework;
-using Microsoft.Xna.Framework.Graphics;
+ if (c.Button_A.ReadBool(device, DeadZone))
+ b |= Buttons.A;
+ if (c.Button_B.ReadBool(device, DeadZone))
+ b |= Buttons.B;
+ if (c.Button_X.ReadBool(device, DeadZone))
+ b |= Buttons.X;
+ if (c.Button_Y.ReadBool(device, DeadZone))
+ b |= Buttons.Y;
+ if (c.Button_LB.ReadBool(device, DeadZone))
+ b |= Buttons.LeftShoulder;
+ if (c.Button_RB.ReadBool(device, DeadZone))
+ b |= Buttons.RightShoulder;
+ if (c.Button_Back.ReadBool(device, DeadZone))
+ b |= Buttons.Back;
+ if (c.Button_Start.ReadBool(device, DeadZone))
+ b |= Buttons.Start;
-namespace Microsoft.Xna.Framework.Input
-{
- public class GamePad
- {
- private static GamePad _instance;
- private float _thumbStickRadius = 20*20;
- private bool _visible;
- private List<ButtonDefinition> _buttonsDefinitions;
- private ThumbStickDefinition _leftThumbDefinition,_rightThumbDefinition;
- private Color _alphaColor = Color.DarkGray;
- private int _buttons;
- private Vector2 _leftStick, _rightStick;
-
- protected GamePad()
- {
- _visible = true;
- _buttonsDefinitions = new List<ButtonDefinition>();
-
- // Set the transparency Level
- _alphaColor.A = 100;
-
- Reset();
- }
-
- internal static GamePad Instance
- {
- get
- {
- if (_instance == null)
- {
- _instance = new GamePad();
- }
- return _instance;
- }
- }
-
- public void Reset()
- {
- _buttons = 0;
- _leftStick = Vector2.Zero;
- _rightStick = Vector2.Zero;
-
- // reset thumbsticks
- if (_leftThumbDefinition != null)
- {
- _leftThumbDefinition.Offset = Vector2.Zero;
- }
- if (_rightThumbDefinition != null)
- {
- _rightThumbDefinition.Offset = Vector2.Zero;
- }
- }
-
- public static bool Visible
- {
- get
- {
- return GamePad.Instance._visible;
- }
- set
- {
- GamePad.Instance.Reset();
- GamePad.Instance._visible = value;
- }
- }
-
- public static GamePadCapabilities GetCapabilities(PlayerIndex playerIndex)
+ if (c.LeftStick.Press.ReadBool(device, DeadZone))
+ b |= Buttons.LeftStick;
+ if (c.RightStick.Press.ReadBool(device, DeadZone))
+ b |= Buttons.RightStick;
+
+ if (c.Dpad.Up.ReadBool(device, DeadZone))
+ b |= Buttons.DPadUp;
+ if (c.Dpad.Down.ReadBool(device, DeadZone))
+ b |= Buttons.DPadDown;
+ if (c.Dpad.Left.ReadBool(device, DeadZone))
+ b |= Buttons.DPadLeft;
+ if (c.Dpad.Right.ReadBool(device, DeadZone))
+ b |= Buttons.DPadRight;
+
+ return b;
+ }
+ static Buttons ReadButtons(IntPtr device, PadConfig c, float deadZoneSize, Vector2 leftStick, Vector2 rightStick)
{
- GamePadCapabilities capabilities = new GamePadCapabilities();
- capabilities.IsConnected = (playerIndex == PlayerIndex.One);
- capabilities.HasAButton = true;
- capabilities.HasBButton = true;
- capabilities.HasXButton = true;
- capabilities.HasYButton = true;
- capabilities.HasBackButton = true;
- capabilities.HasLeftXThumbStick = true;
- capabilities.HasLeftYThumbStick = true;
- capabilities.HasRightXThumbStick = true;
- capabilities.HasRightYThumbStick = true;
-
- return capabilities;
+ Buttons b = ReadButtons(device, c, deadZoneSize);
+
+ b |= StickToButtons(leftStick, deadZoneSize);
+ b |= StickToButtons(rightStick, deadZoneSize);
+
+ return b;
}
- public static GamePadState GetState(PlayerIndex playerIndex)
+ static GamePadState ReadState(PlayerIndex index, GamePadDeadZone deadZone)
{
- /* if (playerIndex != PlayerIndex.One)
- {
- throw new NotSupportedException("Only one player!");
- }*/
-
- return new GamePadState((Buttons)GamePad.Instance._buttons,GamePad.Instance._leftStick,GamePad.Instance._rightStick);
+ const float DeadZoneSize = 0.25f;
+ IntPtr device = GetDevice(index);
+ PadConfig c = GetConfig(index);
+
+ GamePadThumbSticks sticks = new GamePadThumbSticks(new Vector2(c.LeftStick.ReadAxisPair(device)), new Vector2(c.RightStick.ReadAxisPair(device)));
+ sticks.ApplyDeadZone(deadZone, DeadZoneSize);
+ GamePadTriggers triggers = new GamePadTriggers(c.LeftTrigger.ReadFloat(device), c.RightTrigger.ReadFloat(device));
+ GamePadButtons buttons = new GamePadButtons(ReadButtons(device, c, DeadZoneSize));
+ GamePadDPad dpad = new GamePadDPad(buttons.buttons);
+
+ GamePadState g = new GamePadState(sticks, triggers, buttons, dpad);
+ return g;
}
+ //
+ // Summary:
+ // Retrieves the capabilities of an Xbox 360 Controller.
+ //
+ // Parameters:
+ // playerIndex:
+ // Index of the controller to query.
+ public static GamePadCapabilities GetCapabilities(PlayerIndex playerIndex)
+ {
+ IntPtr d = GetDevice(playerIndex);
+ PadConfig c = GetConfig(playerIndex);
+
+ if (c.JoystickName == null || c.JoystickName == string.Empty)
+ return new GamePadCapabilities();
+
+ return new GamePadCapabilities()
+ {
+ IsConnected = d != IntPtr.Zero,
+ HasAButton = c.Button_A.Type != InputType.None,
+ HasBButton = c.Button_B.Type != InputType.None,
+ HasXButton = c.Button_X.Type != InputType.None,
+ HasYButton = c.Button_Y.Type != InputType.None,
+ HasBackButton = c.Button_Back.Type != InputType.None,
+ HasStartButton = c.Button_Start.Type != InputType.None,
+ HasDPadDownButton = c.Dpad.Down.Type != InputType.None,
+ HasDPadLeftButton = c.Dpad.Left.Type != InputType.None,
+ HasDPadRightButton = c.Dpad.Right.Type != InputType.None,
+ HasDPadUpButton = c.Dpad.Up.Type != InputType.None,
+ HasLeftShoulderButton = c.Button_LB.Type != InputType.None,
+ HasRightShoulderButton = c.Button_RB.Type != InputType.None,
+ HasLeftStickButton = c.LeftStick.Press.Type != InputType.None,
+ HasRightStickButton = c.RightStick.Press.Type != InputType.None,
+ HasLeftTrigger = c.LeftTrigger.Type != InputType.None,
+ HasRightTrigger = c.RightTrigger.Type != InputType.None,
+ HasLeftXThumbStick = c.LeftStick.X.Type != InputType.None,
+ HasLeftYThumbStick = c.LeftStick.Y.Type != InputType.None,
+ HasRightXThumbStick = c.RightStick.X.Type != InputType.None,
+ HasRightYThumbStick = c.RightStick.Y.Type != InputType.None,
+
+ HasLeftVibrationMotor = false,
+ HasRightVibrationMotor = false,
+ HasVoiceSupport = false,
+ HasBigButton = false
+ };
+ }
+ //
+ // Summary:
+ // Gets the current state of a game pad controller. Reference page contains
+ // links to related code samples.
+ //
+ // Parameters:
+ // playerIndex:
+ // Player index for the controller you want to query.
+ public static GamePadState GetState(PlayerIndex playerIndex)
+ {
+ return GetState(playerIndex, GamePadDeadZone.IndependentAxes);
+ }
+ //
+ // Summary:
+ // Gets the current state of a game pad controller, using a specified dead zone
+ // on analog stick positions. Reference page contains links to related code
+ // samples.
+ //
+ // Parameters:
+ // playerIndex:
+ // Player index for the controller you want to query.
+ //
+ // deadZoneMode:
+ // Enumerated value that specifies what dead zone type to use.
+ public static GamePadState GetState(PlayerIndex playerIndex, GamePadDeadZone deadZoneMode)
+ {
+ Sdl.SDL_JoystickUpdate();
+ return ReadState(playerIndex, deadZoneMode);
+ }
+ //
+ // Summary:
+ // Sets the vibration motor speeds on an Xbox 360 Controller. Reference page
+ // contains links to related code samples.
+ //
+ // Parameters:
+ // playerIndex:
+ // Player index that identifies the controller to set.
+ //
+ // leftMotor:
+ // The speed of the left motor, between 0.0 and 1.0. This motor is a low-frequency
+ // motor.
+ //
+ // rightMotor:
+ // The speed of the right motor, between 0.0 and 1.0. This motor is a high-frequency
+ // motor.
public static bool SetVibration(PlayerIndex playerIndex, float leftMotor, float rightMotor)
- {
- if (playerIndex != PlayerIndex.One)
- {
- throw new NotSupportedException("Only one player!");
- }
-
- // TODO SystemSound.Vibrate.PlaySystemSound();
- return true;
+ {
+ return false;
}
-
- public static ThumbStickDefinition LeftThumbStickDefinition
- {
- get
- {
- return Instance._leftThumbDefinition;
- }
- set
- {
- Instance._leftThumbDefinition = value;
- }
- }
-
- public static ThumbStickDefinition RightThumbStickDefinition
- {
- get
- {
- return Instance._rightThumbDefinition;
- }
- set
- {
- Instance._rightThumbDefinition = value;
- }
- }
-
- private bool CheckButtonHit(ButtonDefinition theButton, Vector2 location)
- {
- Rectangle buttonRect = new Rectangle((int) theButton.Position.X,(int)theButton.Position.Y,theButton.TextureRect.Width, theButton.TextureRect.Height);
- return buttonRect.Contains(location);
- }
-
- private bool CheckThumbStickHit(ThumbStickDefinition theStick, Vector2 location)
- {
- Vector2 stickPosition = theStick.Position + theStick.Offset;
- Rectangle thumbRect = new Rectangle((int) stickPosition.X,(int)stickPosition.Y,theStick.TextureRect.Width, theStick.TextureRect.Height);
- return thumbRect.Contains(location);
- }
-
- private bool UpdateButton (ButtonDefinition button, Vector2 location)
- {
- bool hitInButton = CheckButtonHit (button, location);
-
- if (hitInButton)
- {
- _buttons |= (int)button.Type;
- }
- return hitInButton;
- }
-
- #region render virtual gamepad
-
- public static List<ButtonDefinition> ButtonsDefinitions
- {
- get
- {
- return Instance._buttonsDefinitions;
- }
- }
-
- public static void Draw(GameTime gameTime, SpriteBatch batch )
- {
- Instance.Render(gameTime,batch);
- }
-
- internal void Render(GameTime gameTime, SpriteBatch batch)
- {
- // render buttons
- foreach (ButtonDefinition button in _buttonsDefinitions)
- {
- RenderButton(button, batch);
- }
-
- // Render the thumbsticks
- if (_leftThumbDefinition != null)
- {
- RenderThumbStick(_leftThumbDefinition, batch);
- }
- if (_rightThumbDefinition != null)
- {
- RenderThumbStick(_rightThumbDefinition, batch);
- }
- }
-
- private void RenderButton(ButtonDefinition theButton, SpriteBatch batch)
- {
- if (batch == null)
- {
- throw new InvalidOperationException("SpriteBatch not set.");
- }
- batch.Draw(theButton.Texture,theButton.Position,theButton.TextureRect,_alphaColor);
- }
-
- private void RenderThumbStick(ThumbStickDefinition theStick, SpriteBatch batch)
- {
- if (batch == null)
- {
- throw new InvalidOperationException("SpriteBatch not set.");
- }
- batch.Draw(theStick.Texture,theStick.Position + theStick.Offset,theStick.TextureRect,_alphaColor);
- }
-
- #endregion
- }
-
+ }
}
View
502 MonoGame.Framework/MacOS/Input/GamePad.cs
@@ -1,281 +1,263 @@
-#region License
-/*
-Microsoft Public License (Ms-PL)
-MonoGame - Copyright © 2009 The MonoGame Team
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.IO;
+using GamepadConfigLib;
+using Tao.Sdl;
+using System.Xml.Serialization;
-All rights reserved.
+namespace Microsoft.Xna.Framework.Input
+{
+ //
+ // Summary:
+ // Allows retrieval of user interaction with an Xbox 360 Controller and setting
+ // of controller vibration motors. Reference page contains links to related
+ // code samples.
+ public static class GamePad
+ {
-This license governs use of the accompanying software. If you use the software, you accept this license. If you do not
-accept the license, do not use the software.
+ static Settings settings;
+ static Settings Settings
+ {
+ get
+ {
+ if (settings == null)
+ {
+ settings = LoadConfigs("Settings.xml");
+ }
+ if (settings == null)
+ throw new Exception("Gamepad settings broke");
+ else
+ {
+ Init();
+ return settings;
+ }
+ }
+ }
+ static Settings LoadConfigs(string filename)
+ {
+ Settings e;
+ try
+ {
+ using (Stream s = File.OpenRead(filename))
+ {
+ XmlSerializer x = new XmlSerializer(typeof(Settings));
+ e = (Settings)x.Deserialize(s);
+ }
+ }
+ catch
+ {
+ return null;
+ }
+ for (int i = 0; i < 4; i++)
+ if (e[i] == null)
+ e[i] = new PadConfig();
+ return e;
+ }
-1. Definitions
-The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under
-U.S. copyright law.
+ static IntPtr[] devices = new IntPtr[4];
-A "contribution" is the original software, or any additions or changes to the software.
-A "contributor" is any person that distributes its contribution under this license.
-"Licensed patents" are a contributor's patent claims that read directly on its contribution.
+ static void Init()
+ {
+ Joystick.Init();
+ for (int i = 0; i < 4; i++)
+ {
+ PadConfig pc = settings[i];
+ if (pc.JoystickName == Sdl.SDL_JoystickName(pc.ID))
+ {
+ devices[i] = Sdl.SDL_JoystickOpen(pc.ID);
+ }
+ }
+ }
+ static void Cleanup()
+ {
+ Joystick.Cleanup();
+ }
-2. Grant of Rights
-(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3,
-each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.
-(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3,
-each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.
+ static IntPtr GetDevice(PlayerIndex index)
+ {
+ return devices[(int)index];
+ }
+ static PadConfig GetConfig(PlayerIndex index)
+ {
+ return Settings[(int)index];
+ }
-3. Conditions and Limitations
-(A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
-(B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software,
-your patent license from such contributor to the software ends automatically.
-(C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution
-notices that are present in the software.
-(D) If you distribute any portion of the software in source code form, you may do so only under this license by including
-a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object
-code form, you may only do so under a license that complies with this license.
-(E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees
-or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent
-permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular
-purpose and non-infringement.
-*/
-#endregion License
+ static Buttons StickToButtons(Vector2 stick, float DeadZoneSize)
+ {
+ Buttons b = (Buttons)0;
-using System;
-using System.Collections.Generic;
+ if (stick.X > DeadZoneSize)
+ b |= Buttons.LeftThumbstickRight;
+ if (stick.X < -DeadZoneSize)
+ b |= Buttons.LeftThumbstickLeft;
+ if (stick.Y > DeadZoneSize)
+ b |= Buttons.LeftThumbstickUp;
+ if (stick.Y < -DeadZoneSize)
+ b |= Buttons.LeftThumbstickDown;
-using MonoMac.AppKit;
-using MonoMac.OpenGL;
+ return b;
+ }
+ static Buttons ReadButtons(IntPtr device, PadConfig c, float deadZoneSize)
+ {
+ short DeadZone = (short)(deadZoneSize * short.MaxValue);
+ Buttons b = (Buttons)0;
-using Microsoft.Xna.Framework;
-using Microsoft.Xna.Framework.Graphics;
+ if (c.Button_A.ReadBool(device, DeadZone))
+ b |= Buttons.A;
+ if (c.Button_B.ReadBool(device, DeadZone))
+ b |= Buttons.B;
+ if (c.Button_X.ReadBool(device, DeadZone))
+ b |= Buttons.X;
+ if (c.Button_Y.ReadBool(device, DeadZone))
+ b |= Buttons.Y;
+ if (c.Button_LB.ReadBool(device, DeadZone))
+ b |= Buttons.LeftShoulder;
+ if (c.Button_RB.ReadBool(device, DeadZone))
+ b |= Buttons.RightShoulder;
+ if (c.Button_Back.ReadBool(device, DeadZone))
+ b |= Buttons.Back;
+ if (c.Button_Start.ReadBool(device, DeadZone))
+ b |= Buttons.Start;
-namespace Microsoft.Xna.Framework.Input
-{
- public class GamePad
- {
- private static GamePad _instance;
- private float _thumbStickRadius = 20*20;
- private bool _visible;
- private List<ButtonDefinition> _buttonsDefinitions;
- private ThumbStickDefinition _leftThumbDefinition,_rightThumbDefinition;
- private Color _alphaColor = Color.DarkGray;
- private int _buttons;
- private Vector2 _leftStick, _rightStick;
-
- protected GamePad()
- {
- _visible = true;
- _buttonsDefinitions = new List<ButtonDefinition>();
-
- // Set the transparency Level
- _alphaColor.A = 100;
-
- Reset();
- }
-
- internal static GamePad Instance
- {
- get
- {
- if (_instance == null)
- {
- _instance = new GamePad();
- }
- return _instance;
- }
- }
-
- public void Reset()
- {
- _buttons = 0;
- _leftStick = Vector2.Zero;
- _rightStick = Vector2.Zero;
-
- // reset thumbsticks
- if (_leftThumbDefinition != null)
- {
- _leftThumbDefinition.Offset = Vector2.Zero;
- }
- if (_rightThumbDefinition != null)
- {
- _rightThumbDefinition.Offset = Vector2.Zero;
- }
- }
-
- public static bool Visible
- {
- get
- {
- return GamePad.Instance._visible;
- }
- set
- {
- GamePad.Instance.Reset();
- GamePad.Instance._visible = value;
- }
- }
-
- public static GamePadCapabilities GetCapabilities(PlayerIndex playerIndex)
+ if (c.LeftStick.Press.ReadBool(device, DeadZone))
+ b |= Buttons.LeftStick;
+ if (c.RightStick.Press.ReadBool(device, DeadZone))
+ b |= Buttons.RightStick;
+
+ if (c.Dpad.Up.ReadBool(device, DeadZone))
+ b |= Buttons.DPadUp;
+ if (c.Dpad.Down.ReadBool(device, DeadZone))
+ b |= Buttons.DPadDown;
+ if (c.Dpad.Left.ReadBool(device, DeadZone))
+ b |= Buttons.DPadLeft;
+ if (c.Dpad.Right.ReadBool(device, DeadZone))
+ b |= Buttons.DPadRight;
+
+ return b;
+ }
+ static Buttons ReadButtons(IntPtr device, PadConfig c, float deadZoneSize, Vector2 leftStick, Vector2 rightStick)
{
- GamePadCapabilities capabilities = new GamePadCapabilities();
- capabilities.IsConnected = (playerIndex == PlayerIndex.One);
- capabilities.HasAButton = true;
- capabilities.HasBButton = true;
- capabilities.HasXButton = true;
- capabilities.HasYButton = true;
- capabilities.HasBackButton = true;
- capabilities.HasLeftXThumbStick = true;
- capabilities.HasLeftYThumbStick = true;
- capabilities.HasRightXThumbStick = true;
- capabilities.HasRightYThumbStick = true;
-
- return capabilities;
+ Buttons b = ReadButtons(device, c, deadZoneSize);
+
+ b |= StickToButtons(leftStick, deadZoneSize);
+ b |= StickToButtons(rightStick, deadZoneSize);
+
+ return b;
}
- public static GamePadState GetState(PlayerIndex playerIndex)
+ static GamePadState ReadState(PlayerIndex index, GamePadDeadZone deadZone)
{
- /* if (playerIndex != PlayerIndex.One)
- {
- throw new NotSupportedException("Only one player!");
- }*/
-
- return new GamePadState((Buttons)GamePad.Instance._buttons,GamePad.Instance._leftStick,GamePad.Instance._rightStick);
+ const float DeadZoneSize = 0.25f;
+ IntPtr device = GetDevice(index);
+ PadConfig c = GetConfig(index);
+
+ GamePadThumbSticks sticks = new GamePadThumbSticks(new Vector2(c.LeftStick.ReadAxisPair(device)), new Vector2(c.RightStick.ReadAxisPair(device)));
+ sticks.ApplyDeadZone(deadZone, DeadZoneSize);
+ GamePadTriggers triggers = new GamePadTriggers(c.LeftTrigger.ReadFloat(device), c.RightTrigger.ReadFloat(device));
+ GamePadButtons buttons = new GamePadButtons(ReadButtons(device, c, DeadZoneSize));
+ GamePadDPad dpad = new GamePadDPad(buttons.buttons);
+
+ GamePadState g = new GamePadState(sticks, triggers, buttons, dpad);
+ return g;
}
+ //
+ // Summary:
+ // Retrieves the capabilities of an Xbox 360 Controller.
+ //
+ // Parameters:
+ // playerIndex:
+ // Index of the controller to query.
+ public static GamePadCapabilities GetCapabilities(PlayerIndex playerIndex)
+ {
+ IntPtr d = GetDevice(playerIndex);
+ PadConfig c = GetConfig(playerIndex);
+
+ if (c.JoystickName == null || c.JoystickName == string.Empty)
+ return new GamePadCapabilities();
+
+ return new GamePadCapabilities()
+ {
+ IsConnected = d != IntPtr.Zero,
+ HasAButton = c.Button_A.Type != InputType.None,
+ HasBButton = c.Button_B.Type != InputType.None,
+ HasXButton = c.Button_X.Type != InputType.None,
+ HasYButton = c.Button_Y.Type != InputType.None,
+ HasBackButton = c.Button_Back.Type != InputType.None,
+ HasStartButton = c.Button_Start.Type != InputType.None,
+ HasDPadDownButton = c.Dpad.Down.Type != InputType.None,
+ HasDPadLeftButton = c.Dpad.Left.Type != InputType.None,
+ HasDPadRightButton = c.Dpad.Right.Type != InputType.None,
+ HasDPadUpButton = c.Dpad.Up.Type != InputType.None,
+ HasLeftShoulderButton = c.Button_LB.Type != InputType.None,
+ HasRightShoulderButton = c.Button_RB.Type != InputType.None,
+ HasLeftStickButton = c.LeftStick.Press.Type != InputType.None,
+ HasRightStickButton = c.RightStick.Press.Type != InputType.None,
+ HasLeftTrigger = c.LeftTrigger.Type != InputType.None,
+ HasRightTrigger = c.RightTrigger.Type != InputType.None,
+ HasLeftXThumbStick = c.LeftStick.X.Type != InputType.None,
+ HasLeftYThumbStick = c.LeftStick.Y.Type != InputType.None,
+ HasRightXThumbStick = c.RightStick.X.Type != InputType.None,
+ HasRightYThumbStick = c.RightStick.Y.Type != InputType.None,
+
+ HasLeftVibrationMotor = false,
+ HasRightVibrationMotor = false,
+ HasVoiceSupport = false,
+ HasBigButton = false
+ };
+ }
+ //
+ // Summary:
+ // Gets the current state of a game pad controller. Reference page contains
+ // links to related code samples.
+ //
+ // Parameters:
+ // playerIndex:
+ // Player index for the controller you want to query.
+ public static GamePadState GetState(PlayerIndex playerIndex)
+ {
+ return GetState(playerIndex, GamePadDeadZone.IndependentAxes);
+ }
+ //
+ // Summary:
+ // Gets the current state of a game pad controller, using a specified dead zone
+ // on analog stick positions. Reference page contains links to related code
+ // samples.
+ //
+ // Parameters:
+ // playerIndex:
+ // Player index for the controller you want to query.
+ //
+ // deadZoneMode:
+ // Enumerated value that specifies what dead zone type to use.
+ public static GamePadState GetState(PlayerIndex playerIndex, GamePadDeadZone deadZoneMode)
+ {
+ Sdl.SDL_JoystickUpdate();
+ return ReadState(playerIndex, deadZoneMode);
+ }
+ //
+ // Summary:
+ // Sets the vibration motor speeds on an Xbox 360 Controller. Reference page
+ // contains links to related code samples.
+ //
+ // Parameters:
+ // playerIndex:
+ // Player index that identifies the controller to set.
+ //
+ // leftMotor:
+ // The speed of the left motor, between 0.0 and 1.0. This motor is a low-frequency
+ // motor.
+ //
+ // rightMotor:
+ // The speed of the right motor, between 0.0 and 1.0. This motor is a high-frequency
+ // motor.
public static bool SetVibration(PlayerIndex playerIndex, float leftMotor, float rightMotor)
- {
- if (playerIndex != PlayerIndex.One)
- {
- throw new NotSupportedException("Only one player!");
- }
-
- // TODO SystemSound.Vibrate.PlaySystemSound();
- return true;
+ {
+ return false;
}
-
- public static ThumbStickDefinition LeftThumbStickDefinition
- {
- get
- {
- return Instance._leftThumbDefinition;
- }
- set
- {
- Instance._leftThumbDefinition = value;
- }
- }
-
- public static ThumbStickDefinition RightThumbStickDefinition
- {
- get
- {
- return Instance._rightThumbDefinition;
- }
- set
- {
- Instance._rightThumbDefinition = value;
- }
- }
-
- private bool CheckButtonHit(ButtonDefinition theButton, Vector2 location)
- {
- Rectangle buttonRect = new Rectangle((int) theButton.Position.X,(int)theButton.Position.Y,theButton.TextureRect.Width, theButton.TextureRect.Height);
- return buttonRect.Contains(location);
- }
-
- private bool CheckThumbStickHit(ThumbStickDefinition theStick, Vector2 location)
- {
- Vector2 stickPosition = theStick.Position + theStick.Offset;
- Rectangle thumbRect = new Rectangle((int) stickPosition.X,(int)stickPosition.Y,theStick.TextureRect.Width, theStick.TextureRect.Height);
- return thumbRect.Contains(location);
- }
-
- internal void TouchesBegan( MonoMac.Foundation.NSSet touches, MonoMac.AppKit.NSEvent e)
- {
- // Reset State
- Reset();
-
- }
-
- internal void TouchesCancelled( MonoMac.Foundation.NSSet touches, MonoMac.AppKit.NSEvent e)
- {
- // do nothing
- }
-
- internal void TouchesMoved( MonoMac.Foundation.NSSet touches, MonoMac.AppKit.NSEvent e)
- {
-
- }
-
- internal void TouchesEnded( MonoMac.Foundation.NSSet touches, MonoMac.AppKit.NSEvent e)
- {
-
- }
-
- private bool UpdateButton (ButtonDefinition button, Vector2 location)
- {
- bool hitInButton = CheckButtonHit (button, location);
-
- if (hitInButton)
- {
- _buttons |= (int)button.Type;
- }
- return hitInButton;
- }
-
- #region render virtual gamepad
-
- public static List<ButtonDefinition> ButtonsDefinitions
- {
- get
- {
- return Instance._buttonsDefinitions;
- }
- }
-
- public static void Draw(GameTime gameTime, SpriteBatch batch )
- {
- Instance.Render(gameTime,batch);
- }
-
- internal void Render(GameTime gameTime, SpriteBatch batch)
- {
- // render buttons
- foreach (ButtonDefinition button in _buttonsDefinitions)
- {
- RenderButton(button, batch);
- }
-
- // Render the thumbsticks
- if (_leftThumbDefinition != null)
- {
- RenderThumbStick(_leftThumbDefinition, batch);
- }
- if (_rightThumbDefinition != null)
- {
- RenderThumbStick(_rightThumbDefinition, batch);
- }
- }
-
- private void RenderButton(ButtonDefinition theButton, SpriteBatch batch)
- {
- if (batch == null)
- {
- throw new InvalidOperationException("SpriteBatch not set.");
- }
- batch.Draw(theButton.Texture,theButton.Position,theButton.TextureRect,_alphaColor);
- }
-
- private void RenderThumbStick(ThumbStickDefinition theStick, SpriteBatch batch)
- {
- if (batch == null)
- {
- throw new InvalidOperationException("SpriteBatch not set.");
- }
- batch.Draw(theStick.Texture,theStick.Position + theStick.Offset,theStick.TextureRect,_alphaColor);
- }
-
- #endregion
- }
-
+ }
}
View
485 MonoGame.Framework/Windows/Input/GamePad.cs
@@ -1,256 +1,263 @@
-#region License
-/*
-Microsoft Public License (Ms-PL)
-MonoGame - Copyright © 2009 The MonoGame Team
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.IO;
+using GamepadConfigLib;
+using Tao.Sdl;
+using System.Xml.Serialization;
-All rights reserved.
+namespace Microsoft.Xna.Framework.Input
+{
+ //
+ // Summary:
+ // Allows retrieval of user interaction with an Xbox 360 Controller and setting
+ // of controller vibration motors. Reference page contains links to related
+ // code samples.
+ public static class GamePad
+ {
-This license governs use of the accompanying software. If you use the software, you accept this license. If you do not
-accept the license, do not use the software.
+ static Settings settings;
+ static Settings Settings
+ {
+ get
+ {
+ if (settings == null)
+ {
+ settings = LoadConfigs("Settings.xml");
+ }
+ if (settings == null)
+ throw new Exception("Gamepad settings broke");
+ else
+ {
+ Init();
+ return settings;
+ }
+ }
+ }
+ static Settings LoadConfigs(string filename)
+ {
+ Settings e;
+ try
+ {
+ using (Stream s = File.OpenRead(filename))
+ {
+ XmlSerializer x = new XmlSerializer(typeof(Settings));
+ e = (Settings)x.Deserialize(s);
+ }
+ }
+ catch
+ {
+ return null;
+ }
+ for (int i = 0; i < 4; i++)
+ if (e[i] == null)
+ e[i] = new PadConfig();
+ return e;
+ }
-1. Definitions
-The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under
-U.S. copyright law.
+ static IntPtr[] devices = new IntPtr[4];
-A "contribution" is the original software, or any additions or changes to the software.
-A "contributor" is any person that distributes its contribution under this license.
-"Licensed patents" are a contributor's patent claims that read directly on its contribution.
+ static void Init()
+ {
+ Joystick.Init();
+ for (int i = 0; i < 4; i++)
+ {
+ PadConfig pc = settings[i];
+ if (pc.JoystickName == Sdl.SDL_JoystickName(pc.ID))
+ {
+ devices[i] = Sdl.SDL_JoystickOpen(pc.ID);
+ }
+ }
+ }
+ static void Cleanup()
+ {
+ Joystick.Cleanup();
+ }
-2. Grant of Rights
-(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3,
-each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.
-(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3,
-each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.
+ static IntPtr GetDevice(PlayerIndex index)
+ {
+ return devices[(int)index];
+ }
+ static PadConfig GetConfig(PlayerIndex index)
+ {
+ return Settings[(int)index];
+ }
-3. Conditions and Limitations
-(A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
-(B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software,
-your patent license from such contributor to the software ends automatically.
-(C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution
-notices that are present in the software.
-(D) If you distribute any portion of the software in source code form, you may do so only under this license by including
-a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object
-code form, you may only do so under a license that complies with this license.
-(E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees
-or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent
-permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular
-purpose and non-infringement.
-*/
-#endregion License
+ static Buttons StickToButtons(Vector2 stick, float DeadZoneSize)
+ {
+ Buttons b = (Buttons)0;
-using Microsoft.Xna.Framework;
-using System;
-using Microsoft.Xna.Framework.Graphics;
-using OpenTK.Graphics.ES11;
-using System.Collections.Generic;
+ if (stick.X > DeadZoneSize)
+ b |= Buttons.LeftThumbstickRight;
+ if (stick.X < -DeadZoneSize)
+ b |= Buttons.LeftThumbstickLeft;
+ if (stick.Y > DeadZoneSize)
+ b |= Buttons.LeftThumbstickUp;
+ if (stick.Y < -DeadZoneSize)
+ b |= Buttons.LeftThumbstickDown;
-namespace Microsoft.Xna.Framework.Input
-{
- public class GamePad
- {
- private static GamePad _instance;
- private float _thumbStickRadius = 20*20;
- private bool _visible;
- private List<ButtonDefinition> _buttonsDefinitions;
- private ThumbStickDefinition _leftThumbDefinition,_rightThumbDefinition;
- private Color _alphaColor = Color.DarkGray;
- private int _buttons;
- private Vector2 _leftStick, _rightStick;
-
- protected GamePad()
- {
- _visible = true;
- _buttonsDefinitions = new List<ButtonDefinition>();
-
- // Set the transparency Level
- _alphaColor.A = 100;
-
- Reset();
- }
-
- internal static GamePad Instance
- {
- get
- {
- if (_instance == null)
- {
- _instance = new GamePad();
- }
- return _instance;
- }
- }
-
- public void Reset()
- {
- _buttons = 0;
- _leftStick = Vector2.Zero;
- _rightStick = Vector2.Zero;
-
- // reset thumbsticks
- if (_leftThumbDefinition != null)
- {
- _leftThumbDefinition.Offset = Vector2.Zero;
- }
- if (_rightThumbDefinition != null)
- {
- _rightThumbDefinition.Offset = Vector2.Zero;
- }
- }
-
- public static bool Visible
- {
- get
- {
- return GamePad.Instance._visible;
- }
- set
- {
- GamePad.Instance.Reset();
- GamePad.Instance._visible = value;
- }
- }
-
- public static GamePadCapabilities GetCapabilities(PlayerIndex playerIndex)
+ return b;
+ }
+ static Buttons ReadButtons(IntPtr device, PadConfig c, float deadZoneSize)
+ {
+ short DeadZone = (short)(deadZoneSize * short.MaxValue);
+ Buttons b = (Buttons)0;
+
+ if (c.Button_A.ReadBool(device, DeadZone))
+ b |= Buttons.A;
+ if (c.Button_B.ReadBool(device, DeadZone))
+ b |= Buttons.B;
+ if (c.Button_X.ReadBool(device, DeadZone))
+ b |= Buttons.X;
+ if (c.Button_Y.ReadBool(device, DeadZone))
+ b |= Buttons.Y;
+
+ if (c.Button_LB.ReadBool(device, DeadZone))
+ b |= Buttons.LeftShoulder;
+ if (c.Button_RB.ReadBool(device, DeadZone))
+ b |= Buttons.RightShoulder;
+
+ if (c.Button_Back.ReadBool(device, DeadZone))
+ b |= Buttons.Back;
+ if (c.Button_Start.ReadBool(device, DeadZone))
+ b |= Buttons.Start;
+
+ if (c.LeftStick.Press.ReadBool(device, DeadZone))
+ b |= Buttons.LeftStick;
+ if (c.RightStick.Press.ReadBool(device, DeadZone))
+ b |= Buttons.RightStick;
+
+ if (c.Dpad.Up.ReadBool(device, DeadZone))
+ b |= Buttons.DPadUp;
+ if (c.Dpad.Down.ReadBool(device, DeadZone))
+ b |= Buttons.DPadDown;
+ if (c.Dpad.Left.ReadBool(device, DeadZone))
+ b |= Buttons.DPadLeft;
+ if (c.Dpad.Right.ReadBool(device, DeadZone))
+ b |= Buttons.DPadRight;
+
+ return b;
+ }
+ static Buttons ReadButtons(IntPtr device, PadConfig c, float deadZoneSize, Vector2 leftStick, Vector2 rightStick)
{
- GamePadCapabilities capabilities = new GamePadCapabilities();
- capabilities.IsConnected = (playerIndex == PlayerIndex.One);
- capabilities.HasAButton = true;
- capabilities.HasBButton = true;
- capabilities.HasXButton = true;
- capabilities.HasYButton = true;
- capabilities.HasBackButton = true;
- capabilities.HasLeftXThumbStick = true;
- capabilities.HasLeftYThumbStick = true;
- capabilities.HasRightXThumbStick = true;
- capabilities.HasRightYThumbStick = true;
-
- return capabilities;
+ Buttons b = ReadButtons(device, c, deadZoneSize);
+
+ b |= StickToButtons(leftStick, deadZoneSize);
+ b |= StickToButtons(rightStick, deadZoneSize);
+
+ return b;
}
- public static GamePadState GetState(PlayerIndex playerIndex)
+ static GamePadState ReadState(PlayerIndex index, GamePadDeadZone deadZone)
{
- /* if (playerIndex != PlayerIndex.One)
- {
- throw new NotSupportedException("Only one player!");
- }*/
-
- return new GamePadState((Buttons)GamePad.Instance._buttons,GamePad.Instance._leftStick,GamePad.Instance._rightStick);
+ const float DeadZoneSize = 0.25f;
+ IntPtr device = GetDevice(index);
+ PadConfig c = GetConfig(index);
+
+ GamePadThumbSticks sticks = new GamePadThumbSticks(new Vector2(c.LeftStick.ReadAxisPair(device)), new Vector2(c.RightStick.ReadAxisPair(device)));
+ sticks.ApplyDeadZone(deadZone, DeadZoneSize);
+ GamePadTriggers triggers = new GamePadTriggers(c.LeftTrigger.ReadFloat(device), c.RightTrigger.ReadFloat(device));
+ GamePadButtons buttons = new GamePadButtons(ReadButtons(device, c, DeadZoneSize));
+ GamePadDPad dpad = new GamePadDPad(buttons.buttons);
+
+ GamePadState g = new GamePadState(sticks, triggers, buttons, dpad);
+ return g;
}
+ //
+ // Summary:
+ // Retrieves the capabilities of an Xbox 360 Controller.
+ //
+ // Parameters:
+ // playerIndex:
+ // Index of the controller to query.
+ public static GamePadCapabilities GetCapabilities(PlayerIndex playerIndex)
+ {
+ IntPtr d = GetDevice(playerIndex);
+ PadConfig c = GetConfig(playerIndex);
+
+ if (c.JoystickName == null || c.JoystickName == string.Empty)
+ return new GamePadCapabilities();
+
+ return new GamePadCapabilities()
+ {
+ IsConnected = d != IntPtr.Zero,
+ HasAButton = c.Button_A.Type != InputType.None,
+ HasBButton = c.Button_B.Type != InputType.None,
+ HasXButton = c.Button_X.Type != InputType.None,
+ HasYButton = c.Button_Y.Type != InputType.None,
+ HasBackButton = c.Button_Back.Type != InputType.None,
+ HasStartButton = c.Button_Start.Type != InputType.None,
+ HasDPadDownButton = c.Dpad.Down.Type != InputType.None,
+ HasDPadLeftButton = c.Dpad.Left.Type != InputType.None,
+ HasDPadRightButton = c.Dpad.Right.Type != InputType.None,
+ HasDPadUpButton = c.Dpad.Up.Type != InputType.None,
+ HasLeftShoulderButton = c.Button_LB.Type != InputType.None,
+ HasRightShoulderButton = c.Button_RB.Type != InputType.None,
+ HasLeftStickButton = c.LeftStick.Press.Type != InputType.None,
+ HasRightStickButton = c.RightStick.Press.Type != InputType.None,
+ HasLeftTrigger = c.LeftTrigger.Type != InputType.None,
+ HasRightTrigger = c.RightTrigger.Type != InputType.None,
+ HasLeftXThumbStick = c.LeftStick.X.Type != InputType.None,
+ HasLeftYThumbStick = c.LeftStick.Y.Type != InputType.None,
+ HasRightXThumbStick = c.RightStick.X.Type != InputType.None,
+ HasRightYThumbStick = c.RightStick.Y.Type != InputType.None,
+
+ HasLeftVibrationMotor = false,
+ HasRightVibrationMotor = false,
+ HasVoiceSupport = false,
+ HasBigButton = false
+ };
+ }
+ //
+ // Summary:
+ // Gets the current state of a game pad controller. Reference page contains
+ // links to related code samples.
+ //
+ // Parameters:
+ // playerIndex:
+ // Player index for the controller you want to query.
+ public static GamePadState GetState(PlayerIndex playerIndex)
+ {
+ return GetState(playerIndex, GamePadDeadZone.IndependentAxes);
+ }
+ //
+ // Summary:
+ // Gets the current state of a game pad controller, using a specified dead zone
+ // on analog stick positions. Reference page contains links to related code
+ // samples.
+ //
+ // Parameters:
+ // playerIndex:
+ // Player index for the controller you want to query.
+ //
+ // deadZoneMode:
+ // Enumerated value that specifies what dead zone type to use.
+ public static GamePadState GetState(PlayerIndex playerIndex, GamePadDeadZone deadZoneMode)
+ {
+ Sdl.SDL_JoystickUpdate();
+ return ReadState(playerIndex, deadZoneMode);
+ }
+ //
+ // Summary:
+ // Sets the vibration motor speeds on an Xbox 360 Controller. Reference page
+ // contains links to related code samples.
+ //
+ // Parameters:
+ // playerIndex:
+ // Player index that identifies the controller to set.
+ //
+ // leftMotor:
+ // The speed of the left motor, between 0.0 and 1.0. This motor is a low-frequency
+ // motor.
+ //
+ // rightMotor:
+ // The speed of the right motor, between 0.0 and 1.0. This motor is a high-frequency
+ // motor.
public static bool SetVibration(PlayerIndex playerIndex, float leftMotor, float rightMotor)
- {
- if (playerIndex != PlayerIndex.One)
- {
- throw new NotSupportedException("Only one player!");
- }
-
- //SystemSound.Vibrate.PlaySystemSound();
- return true;
+ {
+ return false;
}
-
- public static ThumbStickDefinition LeftThumbStickDefinition
- {
- get
- {
- return Instance._leftThumbDefinition;
- }
- set
- {
- Instance._leftThumbDefinition = value;
- }
- }
-
- public static ThumbStickDefinition RightThumbStickDefinition
- {
- get
- {
- return Instance._rightThumbDefinition;
- }
- set
- {
- Instance._rightThumbDefinition = value;
- }
- }
-
- private bool CheckButtonHit(ButtonDefinition theButton, Vector2 location)
- {
- Rectangle buttonRect = new Rectangle((int) theButton.Position.X,(int)theButton.Position.Y,theButton.TextureRect.Width, theButton.TextureRect.Height);
- return buttonRect.Contains(location);
- }
-
- private bool CheckThumbStickHit(ThumbStickDefinition theStick, Vector2 location)
- {
- Vector2 stickPosition = theStick.Position + theStick.Offset;
- Rectangle thumbRect = new Rectangle((int) stickPosition.X,(int)stickPosition.Y,theStick.TextureRect.Width, theStick.TextureRect.Height);
- return thumbRect.Contains(location);
- }
-
-
-
- private bool UpdateButton (ButtonDefinition button, Vector2 location)
- {
- bool hitInButton = CheckButtonHit (button, location);
-
- if (hitInButton)
- {
- _buttons |= (int)button.Type;
- }
- return hitInButton;
- }
-
- #region render virtual gamepad
-
- public static List<ButtonDefinition> ButtonsDefinitions
- {
- get
- {
- return Instance._buttonsDefinitions;
- }
- }
-
- public static void Draw(GameTime gameTime, SpriteBatch batch )
- {
- Instance.Render(gameTime,batch);
- }
-
- internal void Render(GameTime gameTime, SpriteBatch batch)
- {
- // render buttons
- foreach (ButtonDefinition button in _buttonsDefinitions)
- {
- RenderButton(button, batch);
- }
-
- // Render the thumbsticks
- if (_leftThumbDefinition != null)
- {
- RenderThumbStick(_leftThumbDefinition, batch);
- }
- if (_rightThumbDefinition != null)
- {
- RenderThumbStick(_rightThumbDefinition, batch);
- }
- }
-
- private void RenderButton(ButtonDefinition theButton, SpriteBatch batch)
- {
- if (batch == null)
- {
- throw new InvalidOperationException("SpriteBatch not set.");
- }
- batch.Draw(theButton.Texture,theButton.Position,theButton.TextureRect,_alphaColor);
- }
-
- private void RenderThumbStick(ThumbStickDefinition theStick, SpriteBatch batch)
- {
- if (batch == null)
- {
- throw new InvalidOperationException("SpriteBatch not set.");
- }
- batch.Draw(theStick.Texture,theStick.Position + theStick.Offset,theStick.TextureRect,_alphaColor);
- }
-
- #endregion
- }
-
+ }
}

0 comments on commit 8d09d7d

Please sign in to comment.