Virtu/Virtu/Xna/Services/XnaGamePortService.cs
Sean Fausett 365e5723c1 Deleted all svn:eol-style properties on files.
--HG--
extra : convert_revision : svn%3Affd33b8c-2492-42e0-bdc5-587b920b7d6d/trunk%4050811
2010-08-28 11:34:14 +00:00

65 lines
2.6 KiB
C#

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
namespace Jellyfish.Virtu.Services
{
public sealed class XnaGamePortService : GamePortService
{
public XnaGamePortService(Machine machine) :
base(machine)
{
}
public override void Update() // main thread
{
_lastState = _state;
_state = GamePad.GetState(PlayerIndex.One);
if (_state.IsConnected && (_state != _lastState))
{
var left = _state.ThumbSticks.Left;
var right = _state.ThumbSticks.Right;
var dpad = _state.DPad;
Paddle0 = (int)((1 + left.X) * PaddleScale);
Paddle1 = (int)((1 - left.Y) * PaddleScale); // invert y
Paddle2 = (int)((1 + right.X) * PaddleScale);
Paddle3 = (int)((1 - right.Y) * PaddleScale); // invert y
Joystick0 = GetJoystick(ref left, ref dpad);
Joystick1 = GetJoystick(ref right);
IsButton0Down = ((_state.Buttons.A == ButtonState.Pressed) || (_state.Buttons.LeftShoulder == ButtonState.Pressed));
IsButton1Down = ((_state.Buttons.B == ButtonState.Pressed) || (_state.Buttons.RightShoulder == ButtonState.Pressed));
IsButton2Down = (_state.Buttons.X == ButtonState.Pressed);
}
}
private static Joystick GetJoystick(ref Vector2 thumbstick)
{
bool isUp = (thumbstick.Y > JoystickDeadZone);
bool isLeft = (thumbstick.X < -JoystickDeadZone);
bool isRight = (thumbstick.X > JoystickDeadZone);
bool isDown = (thumbstick.Y < -JoystickDeadZone);
return new Joystick(isUp, isLeft, isRight, isDown);
}
private static Joystick GetJoystick(ref Vector2 thumbstick, ref GamePadDPad dpad)
{
bool isUp = ((thumbstick.Y > JoystickDeadZone) || (dpad.Up == ButtonState.Pressed));
bool isLeft = ((thumbstick.X < -JoystickDeadZone) || (dpad.Left == ButtonState.Pressed));
bool isRight = ((thumbstick.X > JoystickDeadZone) || (dpad.Right == ButtonState.Pressed));
bool isDown = ((thumbstick.Y < -JoystickDeadZone) || (dpad.Down == ButtonState.Pressed));
return new Joystick(isUp, isLeft, isRight, isDown);
}
private const int PaddleScale = 128;
private const float JoystickDeadZone = 0.5f;
private GamePadState _state;
private GamePadState _lastState;
}
}