A game about forced loneliness, made by TACStudios
1#if UNITY_WEBGL || UNITY_EDITOR || PACKAGE_DOCS_GENERATION
2using System;
3using System.ComponentModel;
4using UnityEngine.InputSystem.Layouts;
5using UnityEngine.InputSystem.LowLevel;
6using UnityEngine.InputSystem.WebGL.LowLevel;
7using UnityEngine.InputSystem.Utilities;
8
9namespace UnityEngine.InputSystem.WebGL.LowLevel
10{
11 internal unsafe struct WebGLGamepadState : IInputStateTypeInfo
12 {
13 public const int NumAxes = 4;
14 public const int NumButtons = 16;
15 private const int ButtonOffset = NumAxes * 4;
16
17 // Stick default format is already two floats so all we need to do is move the sticks and
18 // put inverts on Y.
19 [InputControl(name = "leftStick", offset = 0)]
20 [InputControl(name = "rightStick", offset = 8)]
21 [InputControl(name = "leftStick/y", parameters = "invert")]
22 [InputControl(name = "leftStick/up", parameters = "clamp=2,clampMin=0,clampMax=1,invert")]
23 [InputControl(name = "leftStick/down", parameters = "clamp=2,clampMin=-1,clampMax=0,invert=false")]
24 [InputControl(name = "rightStick/y", parameters = "invert")]
25 [InputControl(name = "rightStick/up", parameters = "clamp=2,clampMin=0,clampMax=1,invert")]
26 [InputControl(name = "rightStick/down", parameters = "clamp=2,clampMin=-1,clampMax=0,invert=false")]
27 // All the buttons we need to bump from single bits to full floats and reset bit offsets.
28 [InputControl(name = "buttonSouth", offset = ButtonOffset + 0 * 4, bit = 0, format = "FLT")]
29 [InputControl(name = "buttonEast", offset = ButtonOffset + 1 * 4, bit = 0, format = "FLT")]
30 [InputControl(name = "buttonWest", offset = ButtonOffset + 2 * 4, bit = 0, format = "FLT")]
31 [InputControl(name = "buttonNorth", offset = ButtonOffset + 3 * 4, bit = 0, format = "FLT")]
32 [InputControl(name = "leftShoulder", offset = ButtonOffset + 4 * 4, bit = 0, format = "FLT")]
33 [InputControl(name = "rightShoulder", offset = ButtonOffset + 5 * 4, bit = 0, format = "FLT")]
34 [InputControl(name = "leftTrigger", offset = ButtonOffset + 6 * 4, bit = 0, format = "FLT")]
35 [InputControl(name = "rightTrigger", offset = ButtonOffset + 7 * 4, bit = 0, format = "FLT")]
36 [InputControl(name = "select", offset = ButtonOffset + 8 * 4, bit = 0, format = "FLT")]
37 [InputControl(name = "start", offset = ButtonOffset + 9 * 4, bit = 0, format = "FLT")]
38 [InputControl(name = "leftStickPress", offset = ButtonOffset + 10 * 4, bit = 0, format = "FLT")]
39 [InputControl(name = "rightStickPress", offset = ButtonOffset + 11 * 4, bit = 0, format = "FLT")]
40 [InputControl(name = "dpad", offset = ButtonOffset + 12 * 4, bit = 0, sizeInBits = 4 * 4 * 8)]
41 [InputControl(name = "dpad/up", offset = 0, bit = 0, format = "FLT")]
42 [InputControl(name = "dpad/down", offset = 4, bit = 0, format = "FLT")]
43 [InputControl(name = "dpad/left", offset = 8, bit = 0, format = "FLT")]
44 [InputControl(name = "dpad/right", offset = 12, bit = 0, format = "FLT")]
45 public fixed float values[NumButtons + NumAxes];
46
47 public float leftTrigger
48 {
49 get => GetValue(NumAxes + 6);
50 set => SetValue(NumAxes + 6, value);
51 }
52
53 public float rightTrigger
54 {
55 get => GetValue(NumAxes + 7);
56 set => SetValue(NumAxes + 7, value);
57 }
58
59 public Vector2 leftStick
60 {
61 get => new Vector2(GetValue(0), GetValue(1));
62 set
63 {
64 SetValue(0, value.x);
65 SetValue(1, value.y);
66 }
67 }
68
69 public Vector2 rightStick
70 {
71 get => new Vector2(GetValue(2), GetValue(3));
72 set
73 {
74 SetValue(2, value.x);
75 SetValue(3, value.y);
76 }
77 }
78
79 public FourCC format
80 {
81 get { return new FourCC('H', 'T', 'M', 'L'); }
82 }
83
84 public WebGLGamepadState WithButton(GamepadButton button, float value = 1)
85 {
86 int index;
87 switch (button)
88 {
89 case GamepadButton.South: index = 0; break;
90 case GamepadButton.East: index = 1; break;
91 case GamepadButton.West: index = 2; break;
92 case GamepadButton.North: index = 3; break;
93 case GamepadButton.LeftShoulder: index = 4; break;
94 case GamepadButton.RightShoulder: index = 5; break;
95 case GamepadButton.Select: index = 8; break;
96 case GamepadButton.Start: index = 9; break;
97 case GamepadButton.LeftStick: index = 10; break;
98 case GamepadButton.RightStick: index = 11; break;
99 case GamepadButton.DpadUp: index = 12; break;
100 case GamepadButton.DpadDown: index = 13; break;
101 case GamepadButton.DpadLeft: index = 14; break;
102 case GamepadButton.DpadRight: index = 15; break;
103
104 default:
105 throw new InvalidEnumArgumentException(nameof(button), (int)button, typeof(GamepadButton));
106 }
107
108 SetValue(NumAxes + index, value);
109 return this;
110 }
111
112 private float GetValue(int index)
113 {
114 fixed(float* valuePtr = values)
115 return valuePtr[index];
116 }
117
118 private void SetValue(int index, float value)
119 {
120 fixed(float* valuePtr = values)
121 valuePtr[index] = value;
122 }
123 }
124
125 [Serializable]
126 internal struct WebGLDeviceCapabilities
127 {
128 public int numAxes;
129 public int numButtons;
130 public string mapping;
131
132 public string ToJson()
133 {
134 return JsonUtility.ToJson(this);
135 }
136
137 public static WebGLDeviceCapabilities FromJson(string json)
138 {
139 if (string.IsNullOrEmpty(json))
140 throw new ArgumentNullException(nameof(json));
141 return JsonUtility.FromJson<WebGLDeviceCapabilities>(json);
142 }
143 }
144}
145
146namespace UnityEngine.InputSystem.WebGL
147{
148 /// <summary>
149 /// Gamepad on WebGL that uses the "standard" mapping.
150 /// </summary>
151 /// <seealso href="https://w3c.github.io/gamepad/#remapping"/>
152 [InputControlLayout(stateType = typeof(WebGLGamepadState), displayName = "WebGL Gamepad (\"standard\" mapping)")]
153 public class WebGLGamepad : Gamepad
154 {
155 }
156}
157#endif // UNITY_WEBGL || UNITY_EDITOR