A game about forced loneliness, made by TACStudios
1#if UNITY_WEBGL || UNITY_EDITOR
2using UnityEngine.InputSystem.Layouts;
3using UnityEngine.InputSystem.WebGL.LowLevel;
4using UnityEngine.InputSystem.LowLevel;
5using UnityEngine.InputSystem.Utilities;
6using System;
7
8namespace UnityEngine.InputSystem.WebGL
9{
10#if UNITY_DISABLE_DEFAULT_INPUT_PLUGIN_INITIALIZATION
11 public
12#else
13 internal
14#endif
15 static class WebGLSupport
16 {
17 private const string InterfaceName = "WebGL";
18 public static void Initialize()
19 {
20 // We only turn gamepads with the "standard" mapping into actual Gamepads.
21 InputSystem.RegisterLayout<WebGLGamepad>(
22 matches: new InputDeviceMatcher()
23 .WithInterface(InterfaceName)
24 .WithDeviceClass("Gamepad")
25 .WithCapability("mapping", "standard"));
26
27 InputSystem.onFindLayoutForDevice += OnFindLayoutForDevice;
28 }
29
30 internal static string OnFindLayoutForDevice(ref InputDeviceDescription description,
31 string matchedLayout, InputDeviceExecuteCommandDelegate executeCommandDelegate)
32 {
33 // If the device isn't a WebGL device, we're not interested.
34 if (string.Compare(description.interfaceName, InterfaceName, StringComparison.InvariantCultureIgnoreCase) != 0)
35 return null;
36
37 // If it was matched by the standard mapping, we don't need to fall back to generating a layout.
38 if (!string.IsNullOrEmpty(matchedLayout) && matchedLayout != "Gamepad")
39 return null;
40
41 var deviceMatcher = InputDeviceMatcher.FromDeviceDescription(description);
42
43 var layout = new WebGLLayoutBuilder {capabilities = WebGLDeviceCapabilities.FromJson(description.capabilities)};
44 InputSystem.RegisterLayoutBuilder(() => layout.Build(),
45 description.product, "Joystick", deviceMatcher);
46
47 return description.product;
48 }
49
50 [Serializable]
51 private class WebGLLayoutBuilder
52 {
53 public WebGLDeviceCapabilities capabilities;
54
55 public InputControlLayout Build()
56 {
57 var builder = new InputControlLayout.Builder
58 {
59 type = typeof(WebGLJoystick),
60 extendsLayout = "Joystick",
61 stateFormat = new FourCC('H', 'T', 'M', 'L')
62 };
63
64 // Best guess: Treat first two axes as stick
65 uint offset = 0;
66 if (capabilities.numAxes >= 2)
67 {
68 var stickName = "Stick";
69 builder.AddControl(stickName)
70 .WithLayout("Stick")
71 .WithByteOffset(offset)
72 .WithSizeInBits(64)
73 .WithFormat(InputStateBlock.FormatFloat);
74
75 builder.AddControl(stickName + "/x")
76 .WithLayout("Axis")
77 .WithByteOffset(offset)
78 .WithSizeInBits(32)
79 .WithFormat(InputStateBlock.FormatFloat);
80
81 builder.AddControl(stickName + "/y")
82 .WithLayout("Axis")
83 .WithByteOffset(offset + 4)
84 .WithParameters("invert")
85 .WithSizeInBits(32)
86 .WithFormat(InputStateBlock.FormatFloat);
87
88 //Need to handle Up/Down/Left/Right
89 builder.AddControl(stickName + "/up")
90 .WithLayout("Button")
91 .WithParameters("clamp=1,clampMin=-1,clampMax=0,invert")
92 .WithByteOffset(offset + 4)
93 .WithSizeInBits(32)
94 .WithFormat(InputStateBlock.FormatFloat);
95
96 builder.AddControl(stickName + "/down")
97 .WithLayout("Button")
98 .WithParameters("clamp=1,clampMin=0,clampMax=1")
99 .WithByteOffset(offset + 4)
100 .WithSizeInBits(32)
101 .WithFormat(InputStateBlock.FormatFloat);
102
103 builder.AddControl(stickName + "/left")
104 .WithLayout("Button")
105 .WithParameters("clamp=1,clampMin=-1,clampMax=0,invert")
106 .WithByteOffset(offset)
107 .WithSizeInBits(32)
108 .WithFormat(InputStateBlock.FormatFloat);
109
110 builder.AddControl(stickName + "/right")
111 .WithLayout("Button")
112 .WithParameters("clamp=1,clampMin=0,clampMax=1")
113 .WithByteOffset(offset)
114 .WithSizeInBits(32)
115 .WithFormat(InputStateBlock.FormatFloat);
116
117 offset += 8;
118 }
119
120 for (var axis = 2; axis < capabilities.numAxes; axis++)
121 {
122 builder.AddControl($"Axis {axis - 1}")
123 .WithLayout("Axis")
124 .WithByteOffset(offset)
125 .WithSizeInBits(32)
126 .WithFormat(InputStateBlock.FormatFloat);
127 offset += 4;
128 }
129
130 var buttonStartOffset = offset;
131
132 for (var button = 0; button < capabilities.numButtons; button++)
133 {
134 builder.AddControl($"Button {button + 1}")
135 .WithLayout("Button")
136 .WithByteOffset(offset)
137 .WithSizeInBits(32)
138 .WithFormat(InputStateBlock.FormatFloat);
139 offset += 4;
140 }
141
142 builder.AddControl("Trigger")
143 .WithLayout("AnyKey")
144 .WithByteOffset(buttonStartOffset)
145 .IsSynthetic(true)
146 .WithSizeInBits((uint)(32 * capabilities.numButtons))
147 .WithFormat(InputStateBlock.FormatBit);
148
149 return builder.Build();
150 }
151 }
152 }
153}
154#endif // UNITY_WEBGL || UNITY_EDITOR