Skip to content
This repository has been archived by the owner on Jan 5, 2022. It is now read-only.

Commit

Permalink
Applying deadzone calculations to Xinput logic (Windows platform)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jibran Ibrahim Syed committed Dec 21, 2016
1 parent 337a2b4 commit ec72b8b
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 2 deletions.
63 changes: 61 additions & 2 deletions XboxCtrlrInput/Assets/Plugins/XboxCtrlrInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -751,8 +751,9 @@ public static float GetAxis(XboxAxis axis)
{
r = XInputGetAxisState(ctrlrState.ThumbSticks, axis);
}

r = XInputApplyDeadzone(r, axis, XboxController.All);
}

else
{
string axisCode = DetermineAxisCode(axis, 0);
Expand Down Expand Up @@ -799,8 +800,9 @@ public static float GetAxis(XboxAxis axis, XboxController controller)
{
r = XInputGetAxisState(ctrlrState.ThumbSticks, axis);
}

r = XInputApplyDeadzone(r, axis, controller);
}

else
{
string axisCode = DetermineAxisCode(axis, controllerNumber);
Expand Down Expand Up @@ -1672,6 +1674,51 @@ private static bool XInputStillInCurrFrame()
return r;
}

private static float XInputApplyDeadzone(float rawAxisValue, XboxAxis axis, XboxController controller)
{
float finalValue = rawAxisValue;
float deadzone = 0.0f;

// Find the deadzone
switch(axis)
{
case XboxAxis.LeftStickX:
deadzone = XciHandler.Instance.Deadzones.LeftStickX[(int) controller];
break;
case XboxAxis.LeftStickY:
deadzone = XciHandler.Instance.Deadzones.LeftStickY[(int) controller];
break;
case XboxAxis.RightStickX:
deadzone = XciHandler.Instance.Deadzones.RightStickX[(int) controller];
break;
case XboxAxis.RightStickY:
deadzone = XciHandler.Instance.Deadzones.RightStickY[(int) controller];
break;
case XboxAxis.LeftTrigger:
deadzone = XciHandler.Instance.Deadzones.LeftTrigger[(int) controller];
break;
case XboxAxis.RightTrigger:
deadzone = XciHandler.Instance.Deadzones.RightTrigger[(int) controller];
break;
}


// Clear axis value if less than the deadzone
if(Mathf.Abs(rawAxisValue) < deadzone)
{
finalValue = 0.0f;
}
// Remap the axis value from interval [0,1] to [deadzone,1]
else
{
finalValue = (Mathf.Abs(rawAxisValue) * (1 - deadzone)) + deadzone;
finalValue = finalValue * Mathf.Sign(rawAxisValue);
}


return finalValue;
}


// -------------------------- Handler Script -------------------

Expand All @@ -1690,6 +1737,7 @@ private class XciHandler : MonoBehaviour
public bool u3dTrigger3RightIsTouched = false;
public bool u3dTrigger4LeftIsTouched = false;
public bool u3dTrigger4RightIsTouched = false;
private XciAxisDeadzoneData deadZones = null;

void Awake()
{
Expand All @@ -1700,6 +1748,9 @@ void Awake()

XciHandler.instance = this;

this.deadZones = new XciAxisDeadzoneData();
this.deadZones.Init(XciInputManagerReader.Instance.InputManager);

// Lives for the life of the game
DontDestroyOnLoad(this.gameObject);
}
Expand All @@ -1717,6 +1768,14 @@ void OnApplicationFocus(bool isWindowInFocusNow)
}
}

public XciAxisDeadzoneData Deadzones
{
get
{
return this.deadZones;
}
}

private void ResetTriggerTouches()
{
this.u3dTrigger0LeftIsTouched = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using UnityEngine;

namespace XboxCtrlrInput
{
/// <summary>
/// Contains deadzone data of every axis for every joystick number (see XboxController enum).
/// Mostly for use with XInput (Windows API)
/// </summary>
public class XciAxisDeadzoneData
{
private float[] leftStickX = new float[5];
private float[] leftStickY = new float[5];
private float[] rightStickX = new float[5];
private float[] rightStickY = new float[5];
private float[] leftTrigger = new float[5];
private float[] rightTrigger = new float[5];


public void Init(XciInputManagerClone inputManager)
{
this.leftStickX[0] = inputManager.SearchInputByName("XboxAxisXJoy0").dead;
this.leftStickX[1] = inputManager.SearchInputByName("XboxAxisXJoy1").dead;
this.leftStickX[2] = inputManager.SearchInputByName("XboxAxisXJoy2").dead;
this.leftStickX[3] = inputManager.SearchInputByName("XboxAxisXJoy3").dead;
this.leftStickX[4] = inputManager.SearchInputByName("XboxAxisXJoy4").dead;

this.leftStickY[0] = inputManager.SearchInputByName("XboxAxisYJoy0").dead;
this.leftStickY[1] = inputManager.SearchInputByName("XboxAxisYJoy1").dead;
this.leftStickY[2] = inputManager.SearchInputByName("XboxAxisYJoy2").dead;
this.leftStickY[3] = inputManager.SearchInputByName("XboxAxisYJoy3").dead;
this.leftStickY[4] = inputManager.SearchInputByName("XboxAxisYJoy4").dead;

this.rightStickX[0] = inputManager.SearchInputByName("XboxAxis4Joy0").dead;
this.rightStickX[1] = inputManager.SearchInputByName("XboxAxis4Joy1").dead;
this.rightStickX[2] = inputManager.SearchInputByName("XboxAxis4Joy2").dead;
this.rightStickX[3] = inputManager.SearchInputByName("XboxAxis4Joy3").dead;
this.rightStickX[4] = inputManager.SearchInputByName("XboxAxis4Joy4").dead;

this.rightStickY[0] = inputManager.SearchInputByName("XboxAxis5Joy0").dead;
this.rightStickY[1] = inputManager.SearchInputByName("XboxAxis5Joy1").dead;
this.rightStickY[2] = inputManager.SearchInputByName("XboxAxis5Joy2").dead;
this.rightStickY[3] = inputManager.SearchInputByName("XboxAxis5Joy3").dead;
this.rightStickY[4] = inputManager.SearchInputByName("XboxAxis5Joy4").dead;

this.leftTrigger[0] = inputManager.SearchInputByName("XboxAxis3Joy0").dead;
this.leftTrigger[1] = inputManager.SearchInputByName("XboxAxis3Joy1").dead;
this.leftTrigger[2] = inputManager.SearchInputByName("XboxAxis3Joy2").dead;
this.leftTrigger[3] = inputManager.SearchInputByName("XboxAxis3Joy3").dead;
this.leftTrigger[4] = inputManager.SearchInputByName("XboxAxis3Joy4").dead;

this.rightTrigger[0] = inputManager.SearchInputByName("XboxAxis3Joy0").dead;
this.rightTrigger[1] = inputManager.SearchInputByName("XboxAxis3Joy1").dead;
this.rightTrigger[2] = inputManager.SearchInputByName("XboxAxis3Joy2").dead;
this.rightTrigger[3] = inputManager.SearchInputByName("XboxAxis3Joy3").dead;
this.rightTrigger[4] = inputManager.SearchInputByName("XboxAxis3Joy4").dead;
}


public float[] LeftStickX
{
get
{
return this.leftStickX;
}
}

public float[] LeftStickY
{
get
{
return this.leftStickY;
}
}

public float[] RightStickX
{
get
{
return this.rightStickX;
}
}

public float[] RightStickY
{
get
{
return this.rightStickY;
}
}

public float[] LeftTrigger
{
get
{
return this.leftTrigger;
}
}

public float[] RightTrigger
{
get
{
return this.rightTrigger;
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,29 @@ public InputManagerEntry this[int index]
}
}

/// <summary>
/// Searchs by the name of the input.
/// </summary>
public InputManagerEntry SearchInputByName(string entryName)
{
InputManagerEntry foundEntry = null;

InputManagerEntry currentEntry = null;
int numEntries = this.NumberOfEntries;
for(int i = 0; i < numEntries; i++)
{
currentEntry = this.inputManagerEntries[i];

if(currentEntry.name.Equals(entryName))
{
foundEntry = currentEntry;
break;
}
}

return foundEntry;
}

/// <summary>
/// WARNING: Clears entire Input Manager Clone
/// </summary>
Expand Down

0 comments on commit ec72b8b

Please sign in to comment.