From a47153f2f6c893f8133fe3255c849eac553e2256 Mon Sep 17 00:00:00 2001 From: minenice55 Date: Mon, 25 Jul 2022 18:36:32 -0400 Subject: [PATCH] playstation controller lightbar colours - fix controller feature flags not actually being flags --- .../ControllerTypes/InputJoyshock.cs | 74 ++++++++++++++++++- Assets/Scripts/InputSystem/InputController.cs | 38 +++++----- .../SettingsDialog/Tabs/ControllerSettings.cs | 64 +++++++++------- Assets/Scripts/Util/BitwiseUtils.cs | 8 ++ 4 files changed, 135 insertions(+), 49 deletions(-) diff --git a/Assets/Scripts/InputSystem/ControllerTypes/InputJoyshock.cs b/Assets/Scripts/InputSystem/ControllerTypes/InputJoyshock.cs index 4eb7d4a6..f1924b56 100644 --- a/Assets/Scripts/InputSystem/ControllerTypes/InputJoyshock.cs +++ b/Assets/Scripts/InputSystem/ControllerTypes/InputJoyshock.cs @@ -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 @@ -154,7 +167,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 +186,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 +372,7 @@ namespace HeavenStudio.InputSystem { this.playerNum = null; JslSetPlayerNumber(joyshockHandle, 0); + JslSetLightColour(joyshockHandle, 0); return; } this.playerNum = playerNum; @@ -372,6 +385,8 @@ namespace HeavenStudio.InputSystem } } JslSetPlayerNumber(joyshockHandle, ledMask); + lightbarColour = GetLightbarColourForPlayer((int) this.playerNum); + JslSetLightColour(joyshockHandle, lightbarColour); } public override int? GetPlayer() @@ -379,6 +394,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; diff --git a/Assets/Scripts/InputSystem/InputController.cs b/Assets/Scripts/InputSystem/InputController.cs index f6ff8911..7f63ebc2 100644 --- a/Assets/Scripts/InputSystem/InputController.cs +++ b/Assets/Scripts/InputSystem/InputController.cs @@ -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 diff --git a/Assets/Scripts/LevelEditor/SettingsDialog/Tabs/ControllerSettings.cs b/Assets/Scripts/LevelEditor/SettingsDialog/Tabs/ControllerSettings.cs index 2c30343a..8be6d452 100644 --- a/Assets/Scripts/LevelEditor/SettingsDialog/Tabs/ControllerSettings.cs +++ b/Assets/Scripts/LevelEditor/SettingsDialog/Tabs/ControllerSettings.cs @@ -72,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)); @@ -102,14 +102,21 @@ namespace HeavenStudio.Editor ShowControllerIcon(newController); InputController.InputFeatures features = newController.GetFeatures(); - if (features.HasFlag(InputController.InputFeatures.Extra_SplitControllerLeft) || features.HasFlag(InputController.InputFeatures.Extra_SplitControllerRight)) - { + 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 - { + 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); } @@ -127,7 +134,6 @@ namespace HeavenStudio.Editor if (!isAutoSearching) { pairSearchLabel.SetActive(true); isPairSearching = true; - pairingLabel.text = "Joy-Con (L / R) Selected\nPairing Second Joy-Con..."; } } @@ -135,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."; } } @@ -157,6 +163,7 @@ namespace HeavenStudio.Editor optionData.text = vals[i].GetDeviceName(); dropDownData.Add(optionData); } + controllersDropdown.ClearOptions(); controllersDropdown.AddOptions(dropDownData); controllersDropdown.value = 0; } @@ -185,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); } diff --git a/Assets/Scripts/Util/BitwiseUtils.cs b/Assets/Scripts/Util/BitwiseUtils.cs index bd0c03ac..62ec17cb 100644 --- a/Assets/Scripts/Util/BitwiseUtils.cs +++ b/Assets/Scripts/Util/BitwiseUtils.cs @@ -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; + } } } \ No newline at end of file