methods for working on controller binds

doc parts of the InputController class
This commit is contained in:
minenice55 2023-07-08 23:42:05 -04:00
parent aeddb144e0
commit cf31db9f87
3 changed files with 199 additions and 58 deletions

View file

@ -98,7 +98,7 @@ namespace HeavenStudio.InputSystem
{
public class InputJoyshock : InputController
{
static string[] joyShockNames =
static readonly string[] joyShockNames =
{
"Unknown",
"Joy-Con (L)",
@ -108,7 +108,7 @@ namespace HeavenStudio.InputSystem
"DualSense"
};
static int[] dsPlayerColours = new[]
static readonly int[] dsPlayerColours = new[]
{
0xd41817,
0x04d4fa,
@ -119,8 +119,7 @@ namespace HeavenStudio.InputSystem
0x888888
};
//TODO: see if single joy-con mappings differ from a normal pad (they don't!)
int[] defaultMappings = new[]
static readonly int[] defaultMappings = new[]
{
ButtonMaskUp,
ButtonMaskDown,
@ -135,7 +134,7 @@ namespace HeavenStudio.InputSystem
ButtonMaskPlus,
-1
};
int[] defaultMappingsL = new[]
static readonly int[] defaultMappingsL = new[]
{
-1,
-1,
@ -150,7 +149,7 @@ namespace HeavenStudio.InputSystem
ButtonMaskMinus,
-1
};
int[] defaultMappingsR = new[]
static readonly int[] defaultMappingsR = new[]
{
-1,
-1,
@ -189,6 +188,7 @@ namespace HeavenStudio.InputSystem
JSL_SETTINGS joySettings;
InputJoyshock otherHalf;
bool isPair;
public struct JoyshockButtonState
{
@ -215,20 +215,21 @@ namespace HeavenStudio.InputSystem
int GetButtonForSplitType(int action)
{
if (currentBindings.ControllerName == null) return -1;
if (action < 0 || action >= BINDS_MAX) return -1;
ControlBindings actionMap = currentBindings;
if (otherHalf == null)
{
switch (splitType)
{
case SplitLeft:
return defaultMappingsL[action];
case SplitRight:
return defaultMappingsR[action];
return actionMap.Pad[action];
default:
return defaultMappings[action];
}
}
return defaultMappings[action];
return actionMap.Pad[action];
}
public static void JslEventInit()
@ -239,7 +240,7 @@ namespace HeavenStudio.InputSystem
static void JslEventCallback(int handle, JOY_SHOCK_STATE state, JOY_SHOCK_STATE lastState,
IMU_STATE imuState, IMU_STATE lastImuState, float deltaTime)
{
if (!joyshocks.ContainsKey(handle)) return;
if (joyshocks == null || !joyshocks.ContainsKey(handle)) return;
InputJoyshock js = joyshocks[handle];
if (js == null) return;
if (js.inputStack == null) return;
@ -272,14 +273,14 @@ namespace HeavenStudio.InputSystem
joyTouchStateLast = new TOUCH_STATE();
//FUTURE: remappable controls
joySettings = JslGetControllerInfoAndSettings(joyshockHandle);
type = joySettings.controllerType;
joyshockName = joyShockNames[type];
splitType = joySettings.splitType;
currentBindings = GetDefaultBindings();
joyshocks.Add(joyshockHandle, this);
}
@ -400,6 +401,46 @@ namespace HeavenStudio.InputSystem
return false;
}
public override ControlBindings GetDefaultBindings()
{
ControlBindings binds = new ControlBindings();
switch (type)
{
case TypeJoyConLeft:
binds.Pad = defaultMappingsL;
binds.ControllerName = "Joy-Con (L)";
break;
case TypeJoyConRight:
binds.Pad = defaultMappingsR;
binds.ControllerName = "Joy-Con (R)";
break;
case TypeProController:
binds.Pad = defaultMappings;
binds.ControllerName = "Pro Controller";
break;
case TypeDualShock4:
binds.Pad = defaultMappings;
binds.ControllerName = "DualShock 4";
break;
case TypeDualSense:
binds.Pad = defaultMappings;
binds.ControllerName = "DualSense";
break;
}
return binds;
}
public override void ResetBindings()
{ }
public override ControlBindings GetCurrentBindings()
{
return currentBindings;
}
public override void SetCurrentBindings(ControlBindings newBinds)
{ }
public override int GetLastButtonDown()
{
for (int i = 0; i < buttonStates.Length; i++)

View file

@ -40,21 +40,23 @@ namespace HeavenStudio.InputSystem
.Where(k => ((int)k < (int)KeyCode.Mouse0))
.ToArray();
//FUTURE: remappable controls
//KeyCode[] mappings = new KeyCode[Enum.GetNames(typeof(ButtonsPad)).Length];
KeyCode[] defaultMappings = new KeyCode[]
static readonly ControlBindings defaultBindings = new()
{
KeyCode.W, // dpad up
KeyCode.S, // dpad down
KeyCode.A, // dpad left
KeyCode.D, // dpad right
KeyCode.K, // south face button
KeyCode.J, // east face button
KeyCode.I, // west face button
KeyCode.U, // north face button
KeyCode.C, // left shoulder button
KeyCode.N, // right shoulder button
KeyCode.Escape, // start button
ControllerName = "Keyboard",
Pad = new int[]
{
(int)KeyCode.W,
(int)KeyCode.S,
(int)KeyCode.A,
(int)KeyCode.D,
(int)KeyCode.K,
(int)KeyCode.J,
(int)KeyCode.I,
(int)KeyCode.U,
(int)KeyCode.C,
(int)KeyCode.N,
(int)KeyCode.Escape,
},
};
InputDirection hatDirectionCurrent;
@ -62,7 +64,7 @@ namespace HeavenStudio.InputSystem
public override void InitializeController()
{
//FUTURE: remappable controls
currentBindings = GetDefaultBindings();
}
public override void UpdateState()
@ -90,6 +92,22 @@ namespace HeavenStudio.InputSystem
return false;
}
public override ControlBindings GetDefaultBindings()
{
return defaultBindings;
}
public override void ResetBindings()
{ }
public override ControlBindings GetCurrentBindings()
{
return currentBindings;
}
public override void SetCurrentBindings(ControlBindings newBinds)
{ }
public override int GetLastButtonDown()
{
return 0;
@ -110,19 +128,19 @@ namespace HeavenStudio.InputSystem
public override bool GetButton(int button)
{
return Input.GetKey(defaultMappings[button]);
return Input.GetKey((KeyCode)defaultBindings.Pad[button]);
}
public override bool GetButtonDown(int button, out double dt)
{
dt = 0;
return Input.GetKeyDown(defaultMappings[button]);
return Input.GetKeyDown((KeyCode)defaultBindings.Pad[button]);
}
public override bool GetButtonUp(int button, out double dt)
{
dt = 0;
return Input.GetKeyUp(defaultMappings[button]);
return Input.GetKeyUp((KeyCode)defaultBindings.Pad[button]);
}
public override float GetAxis(InputAxis axis)
@ -136,13 +154,13 @@ namespace HeavenStudio.InputSystem
switch (direction)
{
case InputDirection.Up:
return Input.GetKey(defaultMappings[0]);
return Input.GetKey((KeyCode)defaultBindings.Pad[0]);
case InputDirection.Down:
return Input.GetKey(defaultMappings[1]);
return Input.GetKey((KeyCode)defaultBindings.Pad[1]);
case InputDirection.Left:
return Input.GetKey(defaultMappings[2]);
return Input.GetKey((KeyCode)defaultBindings.Pad[2]);
case InputDirection.Right:
return Input.GetKey(defaultMappings[3]);
return Input.GetKey((KeyCode)defaultBindings.Pad[3]);
default:
return false;
}
@ -154,13 +172,13 @@ namespace HeavenStudio.InputSystem
switch (direction)
{
case InputDirection.Up:
return Input.GetKeyDown(defaultMappings[0]);
return Input.GetKeyDown((KeyCode)defaultBindings.Pad[0]);
case InputDirection.Down:
return Input.GetKeyDown(defaultMappings[1]);
return Input.GetKeyDown((KeyCode)defaultBindings.Pad[1]);
case InputDirection.Left:
return Input.GetKeyDown(defaultMappings[2]);
return Input.GetKeyDown((KeyCode)defaultBindings.Pad[2]);
case InputDirection.Right:
return Input.GetKeyDown(defaultMappings[3]);
return Input.GetKeyDown((KeyCode)defaultBindings.Pad[3]);
default:
return false;
}
@ -172,13 +190,13 @@ namespace HeavenStudio.InputSystem
switch (direction)
{
case InputDirection.Up:
return Input.GetKeyUp(defaultMappings[0]);
return Input.GetKeyUp((KeyCode)defaultBindings.Pad[0]);
case InputDirection.Down:
return Input.GetKeyUp(defaultMappings[1]);
return Input.GetKeyUp((KeyCode)defaultBindings.Pad[1]);
case InputDirection.Left:
return Input.GetKeyUp(defaultMappings[2]);
return Input.GetKeyUp((KeyCode)defaultBindings.Pad[2]);
case InputDirection.Right:
return Input.GetKeyUp(defaultMappings[3]);
return Input.GetKeyUp((KeyCode)defaultBindings.Pad[3]);
default:
return false;
}

View file

@ -127,8 +127,7 @@ namespace HeavenStudio.InputSystem
// FUTURE: Move Style needs to be implemented per-game (maybe implement checks for common actions?)
protected int currentInputFlags = 0;
protected int lastInputFlags = 0;
protected ControlBindings currentBindings;
protected int? playerNum;
protected int directionStateCurrent = 0;
@ -142,22 +141,105 @@ namespace HeavenStudio.InputSystem
public abstract bool GetIsConnected();
public abstract bool GetIsPoorConnection();
// public abstract int[] GetDefaultMappings(ControlStyles style);
// public abstract int[] GetCurrentMappings(ControlStyles style);
// public abstract int[] SetCurrentMappings(ControlStyles style);
/// <summary>
/// Gets the controller's default mappings
/// </summary>
/// <returns></returns>
public abstract ControlBindings GetDefaultBindings();
public abstract int GetLastButtonDown(); // Get the last button down
public abstract KeyCode GetLastKeyDown(); // Get the last key down (used for keyboards and other devices that use Keycode)
public abstract bool GetButton(int button); // is button currently pressed?
public abstract bool GetButtonDown(int button, out double dt); // is button just pressed?
public abstract bool GetButtonUp(int button, out double dt); // is button just released?
public abstract float GetAxis(InputAxis axis); // Get the value of an axis
public abstract bool GetHatDirection(InputDirection direction); // is direction active?
public abstract bool GetHatDirectionDown(InputDirection direction, out double dt); // direction just became active?
public abstract bool GetHatDirectionUp(InputDirection direction, out double dt); // direction just became inactive?
/// <summary>
/// Resets the controller's mappings to default
/// </summary>
public abstract void ResetBindings();
public abstract void SetPlayer(int? playerNum); // Set the player number (starts at 1, set to -1 or null for no player)
public abstract int? GetPlayer(); // Get the player number (null if no player)
/// <summary>
/// Gets the controller's current mappings
/// </summary>
/// <returns></returns>
public abstract ControlBindings GetCurrentBindings();
/// <summary>
/// Sets the controller's current mappings
/// </summary>
/// <param name="newBinds"></param>
public abstract void SetCurrentBindings(ControlBindings newBinds);
/// <summary>
/// Gets the last pressed button
/// </summary>
/// <returns></returns>
public abstract int GetLastButtonDown();
/// <summary>
/// Gets the last pressed key (keyboards)
/// </summary>
/// <returns></returns>
public abstract KeyCode GetLastKeyDown();
/// <summary>
/// True if the given button is being held
/// </summary>
/// <param name="button"></param>
/// <returns></returns>
public abstract bool GetButton(int button);
/// <summary>
/// True if the button was just pressed this Update
/// </summary>
/// <param name="button"></param>
/// <param name="dt">time since the reported event, use to compensate for controller delays</param>
/// <returns></returns>
public abstract bool GetButtonDown(int button, out double dt);
/// <summary>
/// True if the button was just released this Update
/// </summary>
/// <param name="button"></param>
/// <param name="dt">time since the reported event, use to compensate for controller delays</param>
/// <returns></returns>
public abstract bool GetButtonUp(int button, out double dt);
/// <summary>
/// Get the value of an analogue axis
/// </summary>
/// <param name="axis"></param>
/// <returns></returns>
public abstract float GetAxis(InputAxis axis);
/// <summary>
/// True if the current direction is active
/// </summary>
/// <param name="direction"></param>
/// <returns></returns>
public abstract bool GetHatDirection(InputDirection direction);
/// <summary>
/// True if the current direction just became active this Update
/// </summary>
/// <param name="direction"></param>
/// <param name="dt">time since the reported event, use to compensate for controller delays</param>
/// <returns></returns>
public abstract bool GetHatDirectionDown(InputDirection direction, out double dt);
/// <summary>
/// True if the current direction just became inactive this Update
/// </summary>
/// <param name="direction"></param>
/// <param name="dt">time since the reported event, use to compensate for controller delays</param>
/// <returns></returns>
public abstract bool GetHatDirectionUp(InputDirection direction, out double dt);
/// <summary>
/// Sets the player number (starts at 1, set to -1 or null for no player)
/// </summary>
/// <param name="playerNum"></param>
public abstract void SetPlayer(int? playerNum);
/// <summary>
/// Gets the player number (starts at 1, -1 or null for no player)
/// </summary>
/// <returns></returns>
public abstract int? GetPlayer();
//public abstract Sprite GetDisplayIcon(); //"big icon" for the controller in the settings menu
//public abstract Sprite GetPlaybackIcon(); //"small icon" for the controller during playback