Merge pull request #138 from minenice55/controllers-update1

Controllers Support Update 1
This commit is contained in:
minenice55 2022-07-26 08:57:38 -04:00 committed by GitHub
commit af3980cd4e
4 changed files with 175 additions and 103 deletions

View file

@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
@ -19,6 +20,17 @@ namespace HeavenStudio.InputSystem
"DualSense"
};
static int[] dsPlayerColours = new[]
{
0xd41817,
0x04d4fa,
0x05ff08,
0xffdd01,
0xe906c9,
0xcc6020,
0x888888
};
//TODO: see if single joy-con mappings differ from a normal pad (they don't!)
int[] mappings = new[]
{
@ -68,6 +80,7 @@ namespace HeavenStudio.InputSystem
int joyshockHandle;
int type;
int splitType;
int lightbarColour;
string joyshockName;
//buttons, sticks, triggers
@ -107,6 +120,14 @@ namespace HeavenStudio.InputSystem
joyBtStateLast = joyBtStateCurrent;
joyBtStateCurrent = JslGetSimpleState(joyshockHandle);
//gyro and accelerometer
joyImuStateLast = joyImuStateCurrent;
joyImuStateCurrent = JslGetIMUState(joyshockHandle);
//touchpad
joyTouchStateLast = joyTouchStateCurrent;
joyTouchStateCurrent = JslGetTouchState(joyshockHandle);
//stick direction state
//split controllers will need to be rotated to compensate
//left rotates counterclockwise, right rotates clockwise, all by 90 degrees
@ -154,7 +175,7 @@ namespace HeavenStudio.InputSystem
public override InputFeatures GetFeatures()
{
InputFeatures features = InputFeatures.Style_Pad | InputFeatures.Style_Baton;
InputFeatures features = InputFeatures.Readable_MotionSensor | InputFeatures.Extra_Rumble | InputFeatures.Style_Pad | InputFeatures.Style_Baton;
switch (type)
{
case TypeJoyConLeft:
@ -173,7 +194,6 @@ namespace HeavenStudio.InputSystem
features |= InputFeatures.Readable_AnalogueTriggers | InputFeatures.Readable_Pointer | InputFeatures.Writable_PlayerLED | InputFeatures.Writable_LightBar;
break;
}
features |= InputFeatures.Readable_MotionSensor | InputFeatures.Extra_Rumble | InputFeatures.Style_Pad | InputFeatures.Style_Baton | InputFeatures.Style_Touch;
return features;
}
@ -360,6 +380,7 @@ namespace HeavenStudio.InputSystem
{
this.playerNum = null;
JslSetPlayerNumber(joyshockHandle, 0);
JslSetLightColour(joyshockHandle, 0);
return;
}
this.playerNum = playerNum;
@ -372,6 +393,8 @@ namespace HeavenStudio.InputSystem
}
}
JslSetPlayerNumber(joyshockHandle, ledMask);
lightbarColour = GetLightbarColourForPlayer((int) this.playerNum);
JslSetLightColour(joyshockHandle, lightbarColour);
}
public override int? GetPlayer()
@ -379,6 +402,61 @@ namespace HeavenStudio.InputSystem
return this.playerNum;
}
public Color GetBodyColor()
{
if (otherHalf != null)
{
// gets the colour of the right controller if is split
return BitwiseUtils.IntToRgb(splitType == SplitRight ? JslGetControllerColour(joyshockHandle) : JslGetControllerColour(GetOtherHalf().GetHandle()));
}
return BitwiseUtils.IntToRgb(JslGetControllerColour(joyshockHandle));
}
public Color GetButtonColor()
{
return BitwiseUtils.IntToRgb(JslGetControllerButtonColour(joyshockHandle));
}
public Color GetLeftGripColor()
{
if (otherHalf != null)
{
return BitwiseUtils.IntToRgb(splitType == SplitLeft ? JslGetControllerColour(joyshockHandle) : JslGetControllerColour(GetOtherHalf().GetHandle()));
}
return BitwiseUtils.IntToRgb(JslGetControllerLeftGripColour(joyshockHandle));
}
public Color GetRightGripColor()
{
if (otherHalf != null)
{
return BitwiseUtils.IntToRgb(splitType == SplitRight ? JslGetControllerColour(joyshockHandle) : JslGetControllerColour(GetOtherHalf().GetHandle()));
}
return BitwiseUtils.IntToRgb(JslGetControllerRightGripColour(joyshockHandle));
}
public Color GetLightbarColour()
{
return BitwiseUtils.IntToRgb(lightbarColour);
}
public void SetLightbarColour(Color color)
{
lightbarColour = BitwiseUtils.RgbToInt(color);
JslSetLightColour(joyshockHandle, lightbarColour);
}
public static int GetLightbarColourForPlayer(int playerNum = 0)
{
if (playerNum < 0)
{
return dsPlayerColours[dsPlayerColours.Length - 1];
}
playerNum = Math.Min(playerNum, dsPlayerColours.Length - 1);
return dsPlayerColours[playerNum];
}
public int GetHandle()
{
return joyshockHandle;

View file

@ -64,31 +64,31 @@ namespace HeavenStudio.InputSystem
public enum InputFeatures
{
//readable properties
Readable_ShellColour,
Readable_ButtonColour,
Readable_LeftGripColour,
Readable_RightGripColour,
Readable_AnalogueTriggers,
Readable_StringInput,
Readable_Pointer,
Readable_MotionSensor,
Readable_ShellColour = 1 << 0,
Readable_ButtonColour = 1 << 1,
Readable_LeftGripColour = 1 << 2,
Readable_RightGripColour = 1 << 3,
Readable_AnalogueTriggers = 1 << 4,
Readable_StringInput = 1 << 5,
Readable_Pointer = 1 << 6,
Readable_MotionSensor = 1 << 7,
//writable properties
Writable_PlayerLED,
Writable_LightBar,
Writable_Chroma,
Writable_Speaker,
Writable_PlayerLED = 1 << 8,
Writable_LightBar = 1 << 9,
Writable_Chroma = 1 << 10,
Writable_Speaker = 1 << 11,
//other / "special" properties
Extra_SplitControllerLeft,
Extra_SplitControllerRight,
Extra_Rumble,
Extra_HDRumble,
Extra_SplitControllerLeft = 1 << 12,
Extra_SplitControllerRight = 1 << 13,
Extra_Rumble = 1 << 14,
Extra_HDRumble = 1 << 15,
//supported control styles
Style_Pad,
Style_Baton,
Style_Touch
Style_Pad = 1 << 16,
Style_Baton = 1 << 17,
Style_Touch = 1 << 18,
};
//Following enums are specific to Heaven Studio, can be removed in other applications

View file

@ -40,36 +40,8 @@ namespace HeavenStudio.Editor
{
InputController lastController = PlayerInput.GetInputController(1);
InputController newController = PlayerInput.GetInputControllers()[controllersDropdown.value];
lastController.SetPlayer(-1);
newController.SetPlayer(1);
if (typeof(InputJoyshock) == lastController.GetType()) {
InputJoyshock con = (InputJoyshock) lastController;
con.UnAssignOtherHalf();
}
if (typeof(InputJoyshock) == newController.GetType()) {
InputJoyshock con = (InputJoyshock) newController;
StartCoroutine(SelectionVibrate(con));
con.UnAssignOtherHalf();
}
currentControllerLabel.text = "Current Controller: " + newController.GetDeviceName();
ShowControllerIcon(newController);
InputController.InputFeatures features = newController.GetFeatures();
if (features.HasFlag(InputController.InputFeatures.Extra_SplitControllerLeft) || features.HasFlag(InputController.InputFeatures.Extra_SplitControllerRight))
{
pairSelectLR = !features.HasFlag(InputController.InputFeatures.Extra_SplitControllerLeft);
pairSearchItem.SetActive(true);
StartPairSearch();
}
else
{
pairSearchItem.SetActive(false);
CancelPairSearch();
}
AssignController(newController, lastController);
});
}
@ -79,38 +51,11 @@ namespace HeavenStudio.Editor
foreach (var controller in controllers) {
if (controller.GetLastButtonDown() > 0 || controller.GetLastKeyDown() > 0) {
InputController lastController = PlayerInput.GetInputController(1);
lastController.SetPlayer(-1);
controller.SetPlayer(1);
isAutoSearching = false;
autoSearchLabel.SetActive(false);
AssignController(controller, lastController);
controllersDropdown.value = PlayerInput.GetInputControllerId(1);
if (typeof(InputJoyshock) == lastController.GetType()) {
((InputJoyshock)lastController).UnAssignOtherHalf();
}
if (typeof(InputJoyshock) == controller.GetType()) {
InputJoyshock con = (InputJoyshock) controller;
StartCoroutine(SelectionVibrate(con));
con.UnAssignOtherHalf();
}
currentControllerLabel.text = "Current Controller: " + controller.GetDeviceName();
ShowControllerIcon(controller);
InputController.InputFeatures features = controller.GetFeatures();
if (features.HasFlag(InputController.InputFeatures.Extra_SplitControllerLeft) || features.HasFlag(InputController.InputFeatures.Extra_SplitControllerRight))
{
pairSelectLR = !features.HasFlag(InputController.InputFeatures.Extra_SplitControllerLeft);
pairSearchItem.SetActive(true);
StartPairSearch();
}
else
{
pairSearchItem.SetActive(false);
CancelPairSearch();
}
}
}
}
@ -127,7 +72,7 @@ namespace HeavenStudio.Editor
isPairSearching = false;
pairSearchLabel.SetActive(false);
currentControllerLabel.text = "Current Controller: " + controller.GetDeviceName();
pairingLabel.text = "Joy-Con (L / R) Selected\nPairing Successful!";
pairingLabel.text = "Joy-Con Pair Selected\nPairing Successful!";
ShowControllerIcon(controller);
StartCoroutine(SelectionVibrate(con));
@ -137,6 +82,46 @@ namespace HeavenStudio.Editor
}
}
void AssignController(InputController newController, InputController lastController)
{
lastController.SetPlayer(-1);
newController.SetPlayer(1);
if (typeof(InputJoyshock) == lastController.GetType()) {
InputJoyshock con = (InputJoyshock) lastController;
con.UnAssignOtherHalf();
}
if (typeof(InputJoyshock) == newController.GetType()) {
InputJoyshock con = (InputJoyshock) newController;
StartCoroutine(SelectionVibrate(con));
con.UnAssignOtherHalf();
}
currentControllerLabel.text = "Current Controller: " + newController.GetDeviceName();
ShowControllerIcon(newController);
InputController.InputFeatures features = newController.GetFeatures();
if (features.HasFlag(InputController.InputFeatures.Extra_SplitControllerLeft)) {
pairingLabel.text = "Joy-Con (L) Selected\nPress A to pair with Joy-Con (R)";
pairSelectLR = !features.HasFlag(InputController.InputFeatures.Extra_SplitControllerLeft);
pairSearchItem.SetActive(true);
StartPairSearch();
}
else if (features.HasFlag(InputController.InputFeatures.Extra_SplitControllerRight)) {
pairingLabel.text = "Joy-Con (R) Selected\nPress A to pair with Joy-Con (L)";
pairSelectLR = !features.HasFlag(InputController.InputFeatures.Extra_SplitControllerLeft);
pairSearchItem.SetActive(true);
StartPairSearch();
}
else {
CancelPairSearch();
pairSearchItem.SetActive(false);
}
}
public void StartAutoSearch() {
if (!isPairSearching)
{
@ -149,7 +134,6 @@ namespace HeavenStudio.Editor
if (!isAutoSearching) {
pairSearchLabel.SetActive(true);
isPairSearching = true;
pairingLabel.text = "Joy-Con (L / R) Selected\nPairing Second Joy-Con...";
}
}
@ -157,7 +141,7 @@ namespace HeavenStudio.Editor
if (isPairSearching) {
pairSearchLabel.SetActive(false);
isPairSearching = false;
pairingLabel.text = "Joy-Con (L / R) Selected\nPairing was cancelled.";
pairingLabel.text = "Joy-Con Selected\nPairing was cancelled.";
}
}
@ -179,6 +163,7 @@ namespace HeavenStudio.Editor
optionData.text = vals[i].GetDeviceName();
dropDownData.Add(optionData);
}
controllersDropdown.ClearOptions();
controllersDropdown.AddOptions(dropDownData);
controllersDropdown.value = 0;
}
@ -207,55 +192,56 @@ namespace HeavenStudio.Editor
case "Joy-Con (L)":
case "Joy-Con (R)":
InputJoyshock joy = (InputJoyshock) controller;
controllerMat.SetColor("_BodyColor", BitwiseUtils.IntToRgb(JslGetControllerColour(joy.GetHandle())));
controllerMat.SetColor("_BtnColor", BitwiseUtils.IntToRgb(JslGetControllerButtonColour(joy.GetHandle())));
controllerMat.SetColor("_BodyColor", joy.GetBodyColor());
controllerMat.SetColor("_BtnColor", joy.GetButtonColor());
controllerMat.SetColor("_LGripColor", ColorUtility.TryParseHtmlString("#2F353A", out colour) ? colour : Color.white);
controllerMat.SetColor("_RGripColor", ColorUtility.TryParseHtmlString("#2F353A", out colour) ? colour : Color.white);
break;
case "Joy-Con Pair":
joy = (InputJoyshock) controller;
int joySide = JslGetControllerSplitType(joy.GetHandle());
controllerMat.SetColor("_BodyColor", BitwiseUtils.IntToRgb(joySide == SplitRight ? JslGetControllerButtonColour(joy.GetHandle()) : JslGetControllerButtonColour(joy.GetOtherHalf().GetHandle())));
controllerMat.SetColor("_BtnColor", BitwiseUtils.IntToRgb(joySide == SplitLeft ? JslGetControllerButtonColour(joy.GetHandle()) : JslGetControllerButtonColour(joy.GetOtherHalf().GetHandle())));
controllerMat.SetColor("_LGripColor", BitwiseUtils.IntToRgb(joySide == SplitLeft ? JslGetControllerColour(joy.GetHandle()) : JslGetControllerColour(joy.GetOtherHalf().GetHandle())));
controllerMat.SetColor("_RGripColor", BitwiseUtils.IntToRgb(joySide == SplitRight ? JslGetControllerColour(joy.GetHandle()) : JslGetControllerColour(joy.GetOtherHalf().GetHandle())));
controllerMat.SetColor("_BodyColor", joySide == SplitRight ? joy.GetButtonColor() : joy.GetOtherHalf().GetButtonColor());
controllerMat.SetColor("_BtnColor", joySide == SplitLeft ? joy.GetButtonColor() : joy.GetOtherHalf().GetButtonColor());
controllerMat.SetColor("_LGripColor", joy.GetLeftGripColor());
controllerMat.SetColor("_RGripColor", joy.GetRightGripColor());
break;
case "Pro Controller":
joy = (InputJoyshock) controller;
controllerMat.SetColor("_BodyColor", BitwiseUtils.IntToRgb(JslGetControllerColour(joy.GetHandle())));
controllerMat.SetColor("_BtnColor", BitwiseUtils.IntToRgb(JslGetControllerButtonColour(joy.GetHandle())));
controllerMat.SetColor("_LGripColor", BitwiseUtils.IntToRgb(JslGetControllerLeftGripColour(joy.GetHandle())));
controllerMat.SetColor("_RGripColor", BitwiseUtils.IntToRgb(JslGetControllerRightGripColour(joy.GetHandle())));
controllerMat.SetColor("_BodyColor", joy.GetBodyColor());
controllerMat.SetColor("_BtnColor", joy.GetButtonColor());
controllerMat.SetColor("_LGripColor", joy.GetLeftGripColor());
controllerMat.SetColor("_RGripColor", joy.GetRightGripColor());
break;
//TODO: dualshock 4 and dualsense lightbar colour support
case "DualShock 4":
joy = (InputJoyshock) controller;
controllerMat.SetColor("_BodyColor", ColorUtility.TryParseHtmlString("#E1E2E4", out colour) ? colour : Color.white);
controllerMat.SetColor("_BtnColor", ColorUtility.TryParseHtmlString("#414246", out colour) ? colour : Color.white);
controllerMat.SetColor("_LGripColor", ColorUtility.TryParseHtmlString("#1E6EFA", out colour) ? colour : Color.white);
controllerMat.SetColor("_RGripColor", ColorUtility.TryParseHtmlString("#1E6EFA", out colour) ? colour : Color.white);
controllerMat.SetColor("_LGripColor", joy.GetLightbarColour());
controllerMat.SetColor("_RGripColor", joy.GetLightbarColour());
break;
case "DualSense":
joy = (InputJoyshock) controller;
controllerMat.SetColor("_BodyColor", ColorUtility.TryParseHtmlString("#DEE0EB", out colour) ? colour : Color.white);
controllerMat.SetColor("_BtnColor", ColorUtility.TryParseHtmlString("#272D39", out colour) ? colour : Color.white);
controllerMat.SetColor("_LGripColor", ColorUtility.TryParseHtmlString("#1E6EFA", out colour) ? colour : Color.white);
controllerMat.SetColor("_RGripColor", ColorUtility.TryParseHtmlString("#1E6EFA", out colour) ? colour : Color.white);
controllerMat.SetColor("_LGripColor", joy.GetLightbarColour());
controllerMat.SetColor("_RGripColor", joy.GetLightbarColour());
break;
default:
controllerMat.SetColor("_BodyColor", new Color(1, 1, 1, 1));
controllerMat.SetColor("_BtnColor", new Color(1, 1, 1, 1));
controllerMat.SetColor("_LGripColor", new Color(1, 1, 1, 1));
controllerMat.SetColor("_RGripColor", new Color(1, 1, 1, 1));
controllerMat.SetColor("_BodyColor", Color.white);
controllerMat.SetColor("_BtnColor", Color.white);
controllerMat.SetColor("_LGripColor", Color.white);
controllerMat.SetColor("_RGripColor", Color.white);
break;
}
}
IEnumerator SelectionVibrate(InputJoyshock controller)
{
JslSetRumbleFrequency(controller.GetHandle(), 0.4f, 0.3f, 80f, 160f);
JslSetRumbleFrequency(controller.GetHandle(), 0.4f, 0.4f, 80f, 160f);
yield return new WaitForSeconds(0.15f);
JslSetRumbleFrequency(controller.GetHandle(), 0f, 0f, 0f, 0f);
yield return new WaitForSeconds(0.05f);
JslSetRumbleFrequency(controller.GetHandle(), 0.45f, 0.45f, 160f, 320f);
JslSetRumbleFrequency(controller.GetHandle(), 0.5f, 0.5f, 160f, 320f);
yield return new WaitForSeconds(0.25f);
JslSetRumbleFrequency(controller.GetHandle(), 0f, 0f, 0f, 0f);
}

View file

@ -59,5 +59,13 @@ namespace HeavenStudio.Util
var blue = ( value >> 0 ) & 255;
return new Color(red/255f, green/255f, blue/255f);
}
public static int RgbToInt(Color color)
{
var red = (int)(color.r * 255);
var green = (int)(color.g * 255);
var blue = (int)(color.b * 255);
return (red << 16) | (green << 8) | blue;
}
}
}