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

Commit

Permalink
Added a new version of XCI.IsPluggedIn() which takes an enum XboxCont…
Browse files Browse the repository at this point in the history
…roller instead of an int. Made the old IsPluggedIn() method and XboxController.All deprecated
  • Loading branch information
Jibran Syed committed Jun 27, 2017
1 parent 4da5d42 commit fe0865a
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 16 deletions.
99 changes: 84 additions & 15 deletions XboxCtrlrInput/Assets/Plugins/XboxCtrlrInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,19 @@ namespace XboxCtrlrInput
/// <summary>
/// List of enumerated identifiers for Xbox controllers.
/// </summary>
/// <remarks>
/// Do NOT change enum values
/// </remarks>
public enum XboxController
{
All = 0,
/// <summary>
/// "All" Controllers is deprecated! Use "Any" instead.
/// </summary>
[System.Obsolete("Instead use XboxController.Any")]
All = 0,

Any = 0,

First = 1,
Second = 2,
Third = 3,
Expand Down Expand Up @@ -129,7 +139,7 @@ public static bool GetButton(XboxButton button, XboxController controller)
if (button.IsDPad())
return GetDPad(button.ToDPad(), controller);

if (controller == XboxController.All)
if (controller == XboxController.Any)
return GetButton(button);

int controllerNumber = (int)controller;
Expand Down Expand Up @@ -221,7 +231,7 @@ public static bool GetButtonDown(XboxButton button, XboxController controller)
if (button.IsDPad())
return GetDPadDown(button.ToDPad(), controller);

if (controller == XboxController.All)
if (controller == XboxController.Any)
return GetButtonDown(button);

int controllerNumber = (int)controller;
Expand Down Expand Up @@ -320,7 +330,7 @@ public static bool GetButtonUp(XboxButton button, XboxController controller)
if (button.IsDPad())
return GetDPadUp(button.ToDPad(), controller);

if (controller == XboxController.All)
if (controller == XboxController.Any)
return GetButtonUp(button);

int controllerNumber = (int)controller;
Expand Down Expand Up @@ -435,7 +445,7 @@ public static bool GetDPad(XboxDPad padDirection)
/// </param>
public static bool GetDPad(XboxDPad padDirection, XboxController controller)
{
if (controller == XboxController.All)
if (controller == XboxController.Any)
return GetDPad(padDirection);

int controllerNumber = (int)controller;
Expand Down Expand Up @@ -566,7 +576,7 @@ public static bool GetDPadUp(XboxDPad padDirection)
/// </param>
public static bool GetDPadUp(XboxDPad padDirection, XboxController controller)
{
if (controller == XboxController.All)
if (controller == XboxController.Any)
return GetDPadUp(padDirection);

int controllerNumber = (int)controller;
Expand Down Expand Up @@ -695,7 +705,7 @@ public static bool GetDPadDown(XboxDPad padDirection)
/// </param>
public static bool GetDPadDown(XboxDPad padDirection, XboxController controller)
{
if (controller == XboxController.All)
if (controller == XboxController.Any)
return GetDPadDown(padDirection);

int controllerNumber = (int)controller;
Expand Down Expand Up @@ -805,7 +815,7 @@ public static float GetAxis(XboxAxis axis)
/// </param>
public static float GetAxis(XboxAxis axis, XboxController controller)
{
if (controller == XboxController.All)
if (controller == XboxController.Any)
return GetAxis(axis);

int controllerNumber = (int)controller;
Expand Down Expand Up @@ -897,7 +907,7 @@ public static float GetAxisRaw(XboxAxis axis)
/// </param>
public static float GetAxisRaw(XboxAxis axis, XboxController controller)
{
if (controller == XboxController.All)
if (controller == XboxController.Any)
return GetAxisRaw(axis);

int controllerNumber = (int)controller;
Expand Down Expand Up @@ -998,12 +1008,14 @@ public static void DEBUG_LogControllerNames()

// From @xoorath
/// <summary>
/// Determines if the controller is plugged in the specified controllerNumber.
/// DEPRECATED:
/// Determines if the controller is plugged in the specified controllerNumber.
/// CAUTION: Only works on Windows Native (Desktop and Editor)!
/// </summary>
/// <param name="controllerNumber">
/// An identifier for the specific controller on which to test the axis. An int between 1 and 4.
/// </param>
[System.Obsolete("Instead use IsPluggedIn(XboxController)")]
public static bool IsPluggedIn(int controllerNumber)
{
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
Expand All @@ -1025,15 +1037,72 @@ public static bool IsPluggedIn(int controllerNumber)
}


// Based off @xoorath 's implementation
/// <summary>
/// Determines if the controller is plugged in the specified controllerNumber.
/// Passing Any will evaluate if any controller is connected.
/// CAUTION: Only works on Windows Native (Desktop and Editor)!
/// </summary>
/// <param name="controllerNumber">
/// An identifier for the specific controller on which to test the axis.
/// Passing Any will evaluate if any controller is connected.
/// </param>
public static bool IsPluggedIn(XboxController controllerNumber)
{
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
{
if (OnWindowsNative())
{
if (!XInputStillInCurrFrame())
{
XInputUpdateAllStates();
}

GamePadState ctrlrState;


// If inquiring about any controller

// TODO: Remove condition for XboxController.All when the time comes
// Users of XCI: Feel free to remove XboxController.All if you like having no warnings
if(controllerNumber == XboxController.Any || controllerNumber == XboxController.All)
{
// Examine all controllers it see if any are connected
for(int i = 1; i <= 4; i++)
{
ctrlrState = XInputGetPaticularState(i);
if(ctrlrState.IsConnected)
{
return true;
}
}
}


// If specifying a specific controller

// Note: Casting only works if XboxController enums are values 1 to 4 (First, Second, Third, Fourth)
ctrlrState = XInputGetPaticularState((int)controllerNumber);

return ctrlrState.IsConnected;
}
}
#endif

// NOT IMPLEMENTED for other platforms
return false;
}

////
// ------------- Private -------------- //
////

// ------------ Methods --------------- //

private static bool OnMac()

////
// ------------- Private -------------- //
////

// ------------ Methods --------------- //

private static bool OnMac()
{
// All Mac mappings are based off TattieBogle Xbox Controller drivers
// http://tattiebogle.net/index.php/ProjectRoot/Xbox360Controller/OsxDriver
Expand Down
13 changes: 12 additions & 1 deletion XboxCtrlrInput/Assets/XboxCtrlrInputExample/MovePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,18 @@ void Start ()
}

XCI.DEBUG_LogControllerNames();
}

// This code only works on Windows
if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor)
{
Debug.Log("Windows Only:: Any Controller Plugged in: " + XCI.IsPluggedIn(XboxController.Any).ToString());

Debug.Log("Windows Only:: Controller 1 Plugged in: " + XCI.IsPluggedIn(XboxController.First).ToString());
Debug.Log("Windows Only:: Controller 2 Plugged in: " + XCI.IsPluggedIn(XboxController.Second).ToString());
Debug.Log("Windows Only:: Controller 3 Plugged in: " + XCI.IsPluggedIn(XboxController.Third).ToString());
Debug.Log("Windows Only:: Controller 4 Plugged in: " + XCI.IsPluggedIn(XboxController.Fourth).ToString());
}
}

}

Expand Down

0 comments on commit fe0865a

Please sign in to comment.