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

Commit fe0865a

Browse files
author
Jibran Syed
committed
Added a new version of XCI.IsPluggedIn() which takes an enum XboxController instead of an int. Made the old IsPluggedIn() method and XboxController.All deprecated
1 parent 4da5d42 commit fe0865a

File tree

2 files changed

+96
-16
lines changed

2 files changed

+96
-16
lines changed

XboxCtrlrInput/Assets/Plugins/XboxCtrlrInput.cs

Lines changed: 84 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,19 @@ namespace XboxCtrlrInput
1313
/// <summary>
1414
/// List of enumerated identifiers for Xbox controllers.
1515
/// </summary>
16+
/// <remarks>
17+
/// Do NOT change enum values
18+
/// </remarks>
1619
public enum XboxController
1720
{
18-
All = 0,
21+
/// <summary>
22+
/// "All" Controllers is deprecated! Use "Any" instead.
23+
/// </summary>
24+
[System.Obsolete("Instead use XboxController.Any")]
25+
All = 0,
26+
27+
Any = 0,
28+
1929
First = 1,
2030
Second = 2,
2131
Third = 3,
@@ -129,7 +139,7 @@ public static bool GetButton(XboxButton button, XboxController controller)
129139
if (button.IsDPad())
130140
return GetDPad(button.ToDPad(), controller);
131141

132-
if (controller == XboxController.All)
142+
if (controller == XboxController.Any)
133143
return GetButton(button);
134144

135145
int controllerNumber = (int)controller;
@@ -221,7 +231,7 @@ public static bool GetButtonDown(XboxButton button, XboxController controller)
221231
if (button.IsDPad())
222232
return GetDPadDown(button.ToDPad(), controller);
223233

224-
if (controller == XboxController.All)
234+
if (controller == XboxController.Any)
225235
return GetButtonDown(button);
226236

227237
int controllerNumber = (int)controller;
@@ -320,7 +330,7 @@ public static bool GetButtonUp(XboxButton button, XboxController controller)
320330
if (button.IsDPad())
321331
return GetDPadUp(button.ToDPad(), controller);
322332

323-
if (controller == XboxController.All)
333+
if (controller == XboxController.Any)
324334
return GetButtonUp(button);
325335

326336
int controllerNumber = (int)controller;
@@ -435,7 +445,7 @@ public static bool GetDPad(XboxDPad padDirection)
435445
/// </param>
436446
public static bool GetDPad(XboxDPad padDirection, XboxController controller)
437447
{
438-
if (controller == XboxController.All)
448+
if (controller == XboxController.Any)
439449
return GetDPad(padDirection);
440450

441451
int controllerNumber = (int)controller;
@@ -566,7 +576,7 @@ public static bool GetDPadUp(XboxDPad padDirection)
566576
/// </param>
567577
public static bool GetDPadUp(XboxDPad padDirection, XboxController controller)
568578
{
569-
if (controller == XboxController.All)
579+
if (controller == XboxController.Any)
570580
return GetDPadUp(padDirection);
571581

572582
int controllerNumber = (int)controller;
@@ -695,7 +705,7 @@ public static bool GetDPadDown(XboxDPad padDirection)
695705
/// </param>
696706
public static bool GetDPadDown(XboxDPad padDirection, XboxController controller)
697707
{
698-
if (controller == XboxController.All)
708+
if (controller == XboxController.Any)
699709
return GetDPadDown(padDirection);
700710

701711
int controllerNumber = (int)controller;
@@ -805,7 +815,7 @@ public static float GetAxis(XboxAxis axis)
805815
/// </param>
806816
public static float GetAxis(XboxAxis axis, XboxController controller)
807817
{
808-
if (controller == XboxController.All)
818+
if (controller == XboxController.Any)
809819
return GetAxis(axis);
810820

811821
int controllerNumber = (int)controller;
@@ -897,7 +907,7 @@ public static float GetAxisRaw(XboxAxis axis)
897907
/// </param>
898908
public static float GetAxisRaw(XboxAxis axis, XboxController controller)
899909
{
900-
if (controller == XboxController.All)
910+
if (controller == XboxController.Any)
901911
return GetAxisRaw(axis);
902912

903913
int controllerNumber = (int)controller;
@@ -998,12 +1008,14 @@ public static void DEBUG_LogControllerNames()
9981008

9991009
// From @xoorath
10001010
/// <summary>
1001-
/// Determines if the controller is plugged in the specified controllerNumber.
1011+
/// DEPRECATED:
1012+
/// Determines if the controller is plugged in the specified controllerNumber.
10021013
/// CAUTION: Only works on Windows Native (Desktop and Editor)!
10031014
/// </summary>
10041015
/// <param name="controllerNumber">
10051016
/// An identifier for the specific controller on which to test the axis. An int between 1 and 4.
10061017
/// </param>
1018+
[System.Obsolete("Instead use IsPluggedIn(XboxController)")]
10071019
public static bool IsPluggedIn(int controllerNumber)
10081020
{
10091021
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
@@ -1025,15 +1037,72 @@ public static bool IsPluggedIn(int controllerNumber)
10251037
}
10261038

10271039

1040+
// Based off @xoorath 's implementation
1041+
/// <summary>
1042+
/// Determines if the controller is plugged in the specified controllerNumber.
1043+
/// Passing Any will evaluate if any controller is connected.
1044+
/// CAUTION: Only works on Windows Native (Desktop and Editor)!
1045+
/// </summary>
1046+
/// <param name="controllerNumber">
1047+
/// An identifier for the specific controller on which to test the axis.
1048+
/// Passing Any will evaluate if any controller is connected.
1049+
/// </param>
1050+
public static bool IsPluggedIn(XboxController controllerNumber)
1051+
{
1052+
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
1053+
{
1054+
if (OnWindowsNative())
1055+
{
1056+
if (!XInputStillInCurrFrame())
1057+
{
1058+
XInputUpdateAllStates();
1059+
}
1060+
1061+
GamePadState ctrlrState;
1062+
1063+
1064+
// If inquiring about any controller
1065+
1066+
// TODO: Remove condition for XboxController.All when the time comes
1067+
// Users of XCI: Feel free to remove XboxController.All if you like having no warnings
1068+
if(controllerNumber == XboxController.Any || controllerNumber == XboxController.All)
1069+
{
1070+
// Examine all controllers it see if any are connected
1071+
for(int i = 1; i <= 4; i++)
1072+
{
1073+
ctrlrState = XInputGetPaticularState(i);
1074+
if(ctrlrState.IsConnected)
1075+
{
1076+
return true;
1077+
}
1078+
}
1079+
}
1080+
1081+
1082+
// If specifying a specific controller
1083+
1084+
// Note: Casting only works if XboxController enums are values 1 to 4 (First, Second, Third, Fourth)
1085+
ctrlrState = XInputGetPaticularState((int)controllerNumber);
1086+
1087+
return ctrlrState.IsConnected;
1088+
}
1089+
}
1090+
#endif
10281091

1092+
// NOT IMPLEMENTED for other platforms
1093+
return false;
1094+
}
10291095

1030-
////
1031-
// ------------- Private -------------- //
1032-
////
10331096

1034-
// ------------ Methods --------------- //
10351097

1036-
private static bool OnMac()
1098+
1099+
////
1100+
// ------------- Private -------------- //
1101+
////
1102+
1103+
// ------------ Methods --------------- //
1104+
1105+
private static bool OnMac()
10371106
{
10381107
// All Mac mappings are based off TattieBogle Xbox Controller drivers
10391108
// http://tattiebogle.net/index.php/ProjectRoot/Xbox360Controller/OsxDriver

XboxCtrlrInput/Assets/XboxCtrlrInputExample/MovePlayer.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,18 @@ void Start ()
7171
}
7272

7373
XCI.DEBUG_LogControllerNames();
74-
}
74+
75+
// This code only works on Windows
76+
if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor)
77+
{
78+
Debug.Log("Windows Only:: Any Controller Plugged in: " + XCI.IsPluggedIn(XboxController.Any).ToString());
79+
80+
Debug.Log("Windows Only:: Controller 1 Plugged in: " + XCI.IsPluggedIn(XboxController.First).ToString());
81+
Debug.Log("Windows Only:: Controller 2 Plugged in: " + XCI.IsPluggedIn(XboxController.Second).ToString());
82+
Debug.Log("Windows Only:: Controller 3 Plugged in: " + XCI.IsPluggedIn(XboxController.Third).ToString());
83+
Debug.Log("Windows Only:: Controller 4 Plugged in: " + XCI.IsPluggedIn(XboxController.Fourth).ToString());
84+
}
85+
}
7586

7687
}
7788

0 commit comments

Comments
 (0)