forked from gradientspace/frame3Sharp
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathInputExtension.cs
298 lines (235 loc) · 9.85 KB
/
InputExtension.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
using System;
using UnityEngine;
using g3;
namespace f3
{
// InputTrigger converts an Axis input (eg like a trigger on a gamepad) to
// a Pressed/Down/Released-style button, based on "down" and "up" thresholds
public class InputTrigger
{
float fPrevValue = 0, fCurValue = 0;
Func<float> valueF;
float fValueUpThresh, fValueDownThresh;
bool in_pressed;
bool pressed_this_frame;
bool released_this_frame;
public InputTrigger(Func<float> valueFunction, float fDownThresh = 0.98f, float fUpThresh = 0.1f) {
valueF = valueFunction;
fValueDownThresh = fDownThresh;
fValueUpThresh = fUpThresh;
fPrevValue = fCurValue = 0;
in_pressed = false;
}
public void Update() {
fPrevValue = fCurValue;
fCurValue = valueF();
pressed_this_frame = released_this_frame = false;
if ( in_pressed == false && fPrevValue < fValueDownThresh && fCurValue >= fValueDownThresh ) {
in_pressed = true;
pressed_this_frame = true;
} else if ( in_pressed == true && fPrevValue > fValueUpThresh && fCurValue <= fValueUpThresh ) {
in_pressed = false;
released_this_frame = true;
}
}
public bool Pressed
{ get { return pressed_this_frame; } }
public bool Down
{ get { return in_pressed; } }
public bool Released
{ get { return released_this_frame; } }
public static readonly InputTrigger NoTrigger = new InputTrigger(() => { return 0; });
}
public class InputJoystick
{
Func<Vector2f> positionF;
public InputJoystick(Func<Vector2f> positionFunc)
{
positionF = positionFunc;
}
public Vector2f Position
{
get { return positionF(); }
}
public static readonly InputJoystick NoStick = new InputJoystick( () => { return Vector2f.Zero; } );
}
public class InputMouse
{
Func<Vector2f> deltaF;
Func<Vector2f> positionF;
Func<float> wheelF;
public InputMouse(Func<Vector2f> deltaFunc, Func<Vector2f> positionFunc, Func<float> wheelFunc)
{
deltaF = deltaFunc;
positionF = positionFunc;
wheelF = wheelFunc;
}
public Vector2f Position
{
get { return positionF(); }
}
public Vector2f PositionDelta
{
get { return deltaF(); }
}
public float WheelDelta
{
get { return wheelF(); }
}
public static readonly InputMouse NoMouse = new InputMouse(
() => { return Vector2f.Zero; }, () => { return Vector2f.Zero; }, () => { return 0; });
}
public class InputButton
{
Func<bool> pressedF, downF, releasedF;
public InputButton(Func<bool> pressedFunc, Func<bool> downFunc, Func<bool> releasedFunc)
{
pressedF = pressedFunc;
downF = downFunc;
releasedF = releasedFunc;
}
public bool Pressed {
get { return pressedF(); }
}
public bool Down {
get { return downF(); }
}
public bool Released {
get { return releasedF(); }
}
public static readonly InputButton NoButton = new InputButton(
() => { return false; },
() => { return false; },
() => { return false; });
}
public class InputExtension
{
static InputExtension Singleton = null;
public static InputExtension Get {
get { if (Singleton == null) Singleton = new InputExtension(); return Singleton; }
}
public InputMouse Mouse;
public InputTrigger GamepadLeft;
public InputTrigger GamepadRight;
public InputJoystick GamepadLeftStick;
public InputJoystick GamepadRightStick;
public InputButton GamepadA, GamepadB, GamepadX, GamepadY;
public InputButton GamepadLeftShoulder, GamepadRightShoulder;
public InputTrigger SpatialLeftTrigger;
public InputTrigger SpatialRightTrigger;
public InputTrigger SpatialLeftShoulder;
public InputTrigger SpatialRightShoulder;
public void Start()
{
// configure mouse
if (UnityUtil.InputAxisExists("Mouse X") && UnityUtil.InputAxisExists("Mouse Y")) {
Func<float> wheelFunc = () => { return 0; };
if (UnityUtil.InputAxisExists("Mouse ScrollWheel"))
wheelFunc = () => { return Input.GetAxis("Mouse ScrollWheel"); };
Mouse = new InputMouse(
() => { return new Vector2f(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); },
() => { return new Vector2f(Input.mousePosition.x, Input.mousePosition.y); },
wheelFunc);
} else {
Mouse = InputMouse.NoMouse;
}
// configure gamepad
if (UnityUtil.InputAxisExists("LeftTrigger")) {
GamepadLeft = new InputTrigger(() => {
return Input.GetAxis("LeftTrigger");
});
} else {
GamepadLeft = InputTrigger.NoTrigger;
}
if (UnityUtil.InputAxisExists("RightTrigger")) {
GamepadRight = new InputTrigger(() => {
return Input.GetAxis("RightTrigger");
});
} else {
GamepadRight = InputTrigger.NoTrigger;
}
if ( UnityUtil.InputAxisExists("Joystick X") && UnityUtil.InputAxisExists("Joystick Y") ) {
GamepadLeftStick = new InputJoystick(() => {
return new Vector2f(Input.GetAxis("Joystick X"), Input.GetAxis("Joystick Y"));
});
} else {
GamepadLeftStick = InputJoystick.NoStick;
}
if (UnityUtil.InputAxisExists("Joystick2 X") && UnityUtil.InputAxisExists("Joystick2 Y")) {
GamepadRightStick = new InputJoystick(() => {
return new Vector2f(Input.GetAxis("Joystick2 X"), Input.GetAxis("Joystick2 Y"));
});
} else {
GamepadRightStick = InputJoystick.NoStick;
}
if ( UnityUtil.InputButtonExists("ButtonA") ) {
GamepadA = new InputButton(
() => { return Input.GetButtonDown("ButtonA"); },
() => { return Input.GetButton("ButtonA"); },
() => { return Input.GetButtonUp("ButtonA"); });
} else {
GamepadA = InputButton.NoButton;
}
if (UnityUtil.InputButtonExists("ButtonB")) {
GamepadB = new InputButton(
() => { return Input.GetButtonDown("ButtonB"); },
() => { return Input.GetButton("ButtonB"); },
() => { return Input.GetButtonUp("ButtonB"); });
} else {
GamepadB = InputButton.NoButton;
}
if (UnityUtil.InputButtonExists("ButtonX")) {
GamepadX = new InputButton(
() => { return Input.GetButtonDown("ButtonX"); },
() => { return Input.GetButton("ButtonX"); },
() => { return Input.GetButtonUp("ButtonX"); });
} else {
GamepadX = InputButton.NoButton;
}
if (UnityUtil.InputButtonExists("ButtonY")) {
GamepadY = new InputButton(
() => { return Input.GetButtonDown("ButtonY"); },
() => { return Input.GetButton("ButtonY"); },
() => { return Input.GetButtonUp("ButtonY"); });
} else {
GamepadY = InputButton.NoButton;
}
if (UnityUtil.InputButtonExists("LeftShoulder")) {
GamepadLeftShoulder = new InputButton(
() => { return Input.GetButtonDown("LeftShoulder"); },
() => { return Input.GetButton("LeftShoulder"); },
() => { return Input.GetButtonUp("LeftShoulder"); });
} else {
GamepadLeftShoulder = InputButton.NoButton;
}
if (UnityUtil.InputButtonExists("RightShoulder")) {
GamepadRightShoulder = new InputButton(
() => { return Input.GetButtonDown("RightShoulder"); },
() => { return Input.GetButton("RightShoulder"); },
() => { return Input.GetButtonUp("RightShoulder"); });
} else {
GamepadRightShoulder = InputButton.NoButton;
}
// configure spatial controller triggers
float fDownThresh = (gs.VRPlatform.CurrentVRDevice == gs.VRPlatform.Device.HTCVive) ? 0.8f : 0.9f;
SpatialLeftTrigger =
new InputTrigger(() => { return gs.VRPlatform.LeftTrigger; }, fDownThresh, 0.1f);
SpatialRightTrigger =
new InputTrigger(() => { return gs.VRPlatform.RightTrigger; }, fDownThresh, 0.1f);
SpatialLeftShoulder =
new InputTrigger(() => { return gs.VRPlatform.LeftSecondaryTrigger; }, fDownThresh, 0.1f);
SpatialRightShoulder =
new InputTrigger(() => { return gs.VRPlatform.RightSecondaryTrigger; }, fDownThresh, 0.1f);
}
// call this from a MonoBehavior Update before you use the Input extension methods
public void Update()
{
GamepadLeft.Update();
GamepadRight.Update();
SpatialLeftTrigger.Update();
SpatialRightTrigger.Update();
SpatialLeftShoulder.Update();
SpatialRightShoulder.Update();
}
}
}