more generalized directinput handling.

all axes are handled as boolbuttons with hardcoded deadzones.
all boolbuttons are handled as expected.
"slider"s are not handled because i had nothing to test against.
button names for axes have changed; button names for boolbuttons have not.
This commit is contained in:
goyuken 2012-09-29 19:16:37 +00:00
parent 76619babc1
commit 4fc714d0ad
2 changed files with 107 additions and 56 deletions

View File

@ -47,8 +47,6 @@ namespace BizHawk.MultiClient
private readonly Guid guid;
private readonly Joystick joystick;
private JoystickState state = new JoystickState();
private bool[] buttons;
private int[] pov;
private GamePad(string name, Guid guid, Joystick joystick)
{
@ -56,6 +54,7 @@ namespace BizHawk.MultiClient
this.guid = guid;
this.joystick = joystick;
Update();
InitializeCallbacks();
}
public void Update()
@ -67,74 +66,122 @@ namespace BizHawk.MultiClient
state = joystick.GetCurrentState();
if (Result.Last.IsFailure)
// do something?
return;
}
buttons = state.GetButtons();
pov = state.GetPointOfViewControllers();
/// <summary>FOR DEBUGGING ONLY</summary>
public JoystickState GetInternalState()
{
return state;
}
public string Name { get { return name; } }
public Guid Guid { get { return guid; } }
public float X { get { return state.X / 1000f; } }
public float Y { get { return state.Y / 1000f; } }
public float Z { get { return state.Z / 1000f; } }
public bool[] Buttons { get { return buttons; } }
public bool Up
public string ButtonName(int index)
{
get
return names[index];
}
public bool Pressed(int index)
{
return actions[index]();
}
public int NumButtons { get; private set; }
List<string> names = new List<string>();
List<Func<bool>> actions = new List<Func<bool>>();
void AddItem(string name, Func<bool> callback)
{
names.Add(name);
actions.Add(callback);
NumButtons++;
}
void InitializeCallbacks()
{
const int dzp = 400;
const int dzn = -400;
names.Clear();
actions.Clear();
NumButtons = 0;
AddItem("AccelerationX+", () => state.AccelerationX >= dzp);
AddItem("AccelerationX-", () => state.AccelerationX <= dzn);
AddItem("AccelerationY+", () => state.AccelerationY >= dzp);
AddItem("AccelerationY-", () => state.AccelerationY <= dzn);
AddItem("AccelerationZ+", () => state.AccelerationZ >= dzp);
AddItem("AccelerationZ-", () => state.AccelerationZ <= dzn);
AddItem("AngularAccelerationX+", () => state.AngularAccelerationX >= dzp);
AddItem("AngularAccelerationX-", () => state.AngularAccelerationX <= dzn);
AddItem("AngularAccelerationY+", () => state.AngularAccelerationY >= dzp);
AddItem("AngularAccelerationY-", () => state.AngularAccelerationY <= dzn);
AddItem("AngularAccelerationZ+", () => state.AngularAccelerationZ >= dzp);
AddItem("AngularAccelerationZ-", () => state.AngularAccelerationZ <= dzn);
AddItem("AngularVelocityX+", () => state.AngularVelocityX >= dzp);
AddItem("AngularVelocityX-", () => state.AngularVelocityX <= dzn);
AddItem("AngularVelocityY+", () => state.AngularVelocityY >= dzp);
AddItem("AngularVelocityY-", () => state.AngularVelocityY <= dzn);
AddItem("AngularVelocityZ+", () => state.AngularVelocityZ >= dzp);
AddItem("AngularVelocityZ-", () => state.AngularVelocityZ <= dzn);
AddItem("ForceX+", () => state.ForceX >= dzp);
AddItem("ForceX-", () => state.ForceX <= dzn);
AddItem("ForceY+", () => state.ForceY >= dzp);
AddItem("ForceY-", () => state.ForceY <= dzn);
AddItem("ForceZ+", () => state.ForceZ >= dzp);
AddItem("ForceZ-", () => state.ForceZ <= dzn);
AddItem("RotationX+", () => state.RotationX >= dzp);
AddItem("RotationX-", () => state.RotationX <= dzn);
AddItem("RotationY+", () => state.RotationY >= dzp);
AddItem("RotationY-", () => state.RotationY <= dzn);
AddItem("RotationZ+", () => state.RotationZ >= dzp);
AddItem("RotationZ-", () => state.RotationZ <= dzn);
AddItem("TorqueX+", () => state.TorqueX >= dzp);
AddItem("TorqueX-", () => state.TorqueX <= dzn);
AddItem("TorqueY+", () => state.TorqueY >= dzp);
AddItem("TorqueY-", () => state.TorqueY <= dzn);
AddItem("TorqueZ+", () => state.TorqueZ >= dzp);
AddItem("TorqueZ-", () => state.TorqueZ <= dzn);
AddItem("VelocityX+", () => state.VelocityX >= dzp);
AddItem("VelocityX-", () => state.VelocityX <= dzn);
AddItem("VelocityY+", () => state.VelocityY >= dzp);
AddItem("VelocityY-", () => state.VelocityY <= dzn);
AddItem("VelocityZ+", () => state.VelocityZ >= dzp);
AddItem("VelocityZ-", () => state.VelocityZ <= dzn);
AddItem("X+", () => state.X >= dzp);
AddItem("X-", () => state.X <= dzn);
AddItem("Y+", () => state.Y >= dzp);
AddItem("Y-", () => state.Y <= dzn);
AddItem("Z+", () => state.Z >= dzp);
AddItem("Z-", () => state.Z <= dzn);
// i don't know what the "Slider"s do, so they're omitted for the moment
for (int i = 0; i < state.GetButtons().Length; i++)
{
if (state.Y < -250 || state.RotationY < -250)
return true;
foreach (int p in pov)
if (p.In(0, 4500, 31500))
return true;
return false;
int j = i;
AddItem(string.Format("B{0}", i + 1), () => state.IsPressed(j));
}
for (int i = 0; i < state.GetPointOfViewControllers().Length; i++)
{
int j = i;
AddItem(string.Format("POV{0}U", i + 1),
() => { int t = state.GetPointOfViewControllers()[j]; return (t >= 0 && t <= 4500) || (t >= 31500 && t < 36000); });
AddItem(string.Format("POV{0}D", i + 1),
() => { int t = state.GetPointOfViewControllers()[j]; return t >= 13500 && t <= 22500; });
AddItem(string.Format("POV{0}L", i + 1),
() => { int t = state.GetPointOfViewControllers()[j]; return t >= 22500 && t <= 31500; });
AddItem(string.Format("POV{0}R", i + 1),
() => { int t = state.GetPointOfViewControllers()[j]; return t >= 4500 && t <= 13500; });
}
}
public bool Down
{
get
{
if (state.Y > 250 || state.RotationY > 250)
return true;
foreach (int p in pov)
if (p.In(13500, 18000, 22500))
return true;
return false;
}
}
public bool Left
{
get
{
if (state.X < -250 || state.RotationX < -250)
return true;
foreach (int p in pov)
if (p.In(22500, 27000, 31500))
return true;
return false;
}
}
public bool Right
{
get
{
if (state.X > 250 || state.RotationX > 250)
return true;
foreach (int p in pov)
if (p.In(4500, 9000, 13500))
return true;
return false;
}
}
/// <summary>
/// Note that this does not appear to work at this time. I probably need to have more infos.
/// </summary>
public void SetVibration(int left, int right)
@ -157,4 +204,4 @@ namespace BizHawk.MultiClient
effect.Start(1);
}
}
}
}

View File

@ -224,6 +224,7 @@ namespace BizHawk.MultiClient
{
var pad = GamePad.Devices[i];
string jname = "J" + (i + 1) + " ";
/*
HandleButton(jname + "Up", pad.Up);
HandleButton(jname + "Down", pad.Down);
HandleButton(jname + "Left", pad.Left);
@ -231,6 +232,9 @@ namespace BizHawk.MultiClient
for (int b = 0; b < pad.Buttons.Length; b++)
HandleButton(jname + "B" + (b + 1), pad.Buttons[b]);
*/
for (int b = 0; b < pad.NumButtons; b++)
HandleButton(jname + pad.ButtonName(b), pad.Pressed(b));
}
bool swallow = !Global.MainForm.AllowInput;