-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputStateHandler.cs
425 lines (374 loc) · 14 KB
/
InputStateHandler.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InputStateHandler : MonoBehaviour
{
// -------------------------------------------------------------------------------------------------
// original Tap Strap 2 attributes START
// (code from TapInputTest)
public Text LogText;
private ITapInput tapInputManager;
private bool mouseHIDEnabled;
private string connectedTapIdentifier = "";
//
// original Tap Strap 2 attributes END
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
// added attributes START
//
// filename and path for used Json
// (.../<User>/AppData/LocalLow/<Company>/<ProjectName> folder)
string filename = "input_states.json";
string path;
// used for testing (save produce Json)
// JsonStates testJsonStates;
// used for testing (save produce Json)
// SingleState testSingleState;
//
JsonStates inputStates;
// removed constructor (was used for testing structure)
// JsonStates inputStates = new JsonStates();
// all states read
JsonStates outputStates;
// current state
SingleState currentState;
// SingleState currentState = new SingleState();
//
// Added attributes END
// -------------------------------------------------------------------------------------------------
// Start is called before the first frame update
void Start()
{
InitialReadData();
tapStrapStuffOnStart();
}
// -------------------------------------------------------------------------------------------------
// CHANGED Lines of code of the Tap Strap 2 Unity plugin (some slightly adapted - see comments)
// START
// all lines of code executed in the original Tap Strap 2 Unity plugin on start
void tapStrapStuffOnStart()
{
tapInputManager = TapInputManager.Instance;
tapInputManager.OnTapInputReceived += onTapped;
tapInputManager.OnTapConnected += onTapConnected;
tapInputManager.OnTapDisconnected += onTapDisconnected;
tapInputManager.OnMouseInputReceived += onMoused;
tapInputManager.OnAirGestureInputReceived += onAirGestureInputReceived;
tapInputManager.OnTapChangedAirGestureState += onTapChangedState;
tapInputManager.OnRawSensorDataReceived += onRawSensorDataReceived;
tapInputManager.EnableDebug();
tapInputManager.SetDefaultControllerWithMouseHIDMode(true);
mouseHIDEnabled = false;
}
private void Log(string text)
{
if (LogText != null)
{
LogText.text += string.Format("{0}\n", text);
}
Debug.Log(text);
}
void onMoused(string identifier, int vx, int vy, bool isMouse)
{
Log("onMoused" + identifier + ", velocity = (" + vx + "," + vy + "), isMouse " + isMouse);
}
void onTapped(string identifier, int combination)
{
bool[] arr = TapCombination.toFingers(combination);
Log("onTapped : " + identifier + ", " + combination);
// added modeIdentifier - turn received Tap combination into an integer (used for mapID)
int modeIdentifier = 0;
if (arr[0]) { modeIdentifier += 1; }
if (arr[1]) { modeIdentifier += 2; }
if (arr[2]) { modeIdentifier += 4; }
if (arr[3]) { modeIdentifier += 8; }
if (arr[4]) { modeIdentifier += 16; }
actionProcessing(modeIdentifier);
}
void onAirGestureInputReceived(string tapIdentifier, TapAirGesture gesture)
{
// added modeIdentifier - turn received Tap combination into an integer (used for mapID)
int modeIdentifier = 31;
switch (gesture)
{
case TapAirGesture.OneFingerUp:
modeIdentifier += 1;
break;
case TapAirGesture.TwoFingersUp:
modeIdentifier += 2;
break;
case TapAirGesture.OneFingerDown:
modeIdentifier += 3;
break;
case TapAirGesture.TwoFingersDown:
modeIdentifier += 4;
break;
case TapAirGesture.OneFingerLeft:
modeIdentifier += 5;
break;
case TapAirGesture.TwoFingersLeft:
modeIdentifier += 6;
break;
case TapAirGesture.OnefingerRight:
modeIdentifier += 7;
break;
case TapAirGesture.TwoFingersRight:
modeIdentifier += 8;
break;
case TapAirGesture.IndexToThumbTouch:
modeIdentifier += 9;
break;
case TapAirGesture.MiddleToThumbTouch:
modeIdentifier += 10;
break;
default:
break;
}
actionProcessing(modeIdentifier);
// Log("OnAirGestureInputReceived: " + tapIdentifier + ", " + gesture.ToString());
}
//
// changed lines END
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
// Additional code I added START
//
// processes hardware input to new inputs/ outputs
void actionProcessing(int modeIdentifier)
{
// look for mapID connected to received input
int iterateMapID = 0;
SingleInputMapping mapAction = new SingleInputMapping(0, false, null, null);
foreach (SingleInputMapping im in currentState.inputMapping)
{
iterateMapID = im.mapID;
if (iterateMapID == modeIdentifier)
{
mapAction = im;
break;
}
}
if (mapAction.inputAction != null)
{
// This is where the to be used input action has been successfully determined
// INTERFACE for application should then be created here!
Debug.Log("Called action: " + mapAction.inputAction);
}
// transfer to the mode specified in the mapping
if (mapAction.transferMode)
{
ReadData(mapAction.transferToMode);
}
}
// accessed when the state mod should be change - looks for new/next mode
// and saves it to currentState variable
void ReadData(string transferToState)
{
string iterateStateID;
List<SingleState> singleStateList = outputStates.singleStates;
foreach (SingleState n in singleStateList)
{
iterateStateID = n.stateID;
if (iterateStateID.Equals(transferToState))
{
currentState = n;
Log("Mode changed to " + currentState.stateID);
break;
}
}
if (currentState == null)
{
Debug.Log("No <" + transferToState + "> state found in given Json.");
}
}
// read file and determine current state
void InitialReadData()
{
// path is set here
path = Application.persistentDataPath + "/" + filename;
Debug.Log(path);
try
{
if (System.IO.File.Exists(path))
{
string jsonData = System.IO.File.ReadAllText(path);
outputStates = JsonUtility.FromJson<JsonStates>(jsonData);
// Debug.Log(inputStates.singleStates);
List<SingleState> singleStateList = outputStates.singleStates;
string currentStateID;
foreach (SingleState n in singleStateList)
{
currentStateID = n.stateID;
if (currentStateID.Equals("default"))
{
currentState = n;
Debug.Log("State found successfull.");
break;
}
}
if (currentState == null)
{
Debug.Log("No <default> state found in given Json.");
}
}
else
{
Debug.Log("File does not exist.");
}
}
catch (System.Exception e)
{
Debug.Log("Unknown exception: " + e.Message);
}
}
//
// Additional code I added END
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
// Unused code START
//
// Update is called once per frame
//void Update()
//{
// testing with keyboard
// keyboardTesting()
//}
//void keyboardTesting()
//{
// if (Input.GetKeyDown(KeyCode.S))
// {
//saveData();
//}
// if (Input.GetKeyDown(KeyCode.R))
// {
//actionProcessing(1);
//}
// if (Input.GetKeyDown(KeyCode.Keypad0))
// {
//actionProcessing(2);
//}
// if (Input.GetKeyDown(KeyCode.Keypad1))
// {
//actionProcessing(3);
//}
// if (Input.GetKeyDown(KeyCode.Keypad2))
// {
//actionProcessing(4);
//}
// if (Input.GetKeyDown(KeyCode.Keypad3))
// {
//actionProcessing(5);
//}
// if (Input.GetKeyDown(KeyCode.Keypad4))
// {
//actionProcessing(6);
//}
// if (Input.GetKeyDown(KeyCode.Keypad5))
// {
//actionProcessing(7);
//}
// if (Input.GetKeyDown(KeyCode.Keypad6))
// {
//actionProcessing(8);
//}
// if (Input.GetKeyDown(KeyCode.Keypad7))
// {
//actionProcessing(9);
//}
// if (Input.GetKeyDown(KeyCode.Keypad8))
// {
//actionProcessing(10);
//}
// if (Input.GetKeyDown(KeyCode.Keypad9))
// {
//actionProcessing(11);
//}
//}
//void saveData()
//{
//createJson();
//testIt();
//string testJson = JsonUtility.ToJson(inputStates, true);
//System.IO.File.WriteAllText(path, testJson);
//}
//void createJson()
//{
// List<SingleInputMapping> testSingleList = new List<SingleInputMapping>();
//List<SingleState> testJsonList = new List<SingleState>();
//testJsonList.Add(new SingleState("default", createSingleInputMapping()));
//testJsonList.Add(new SingleState("state B", createSingleInputMapping()));
//testJsonStates = new JsonStates(testJsonList);
//}
//List<SingleInputMapping> createSingleInputMapping()
//{
//List<SingleInputMapping> testMapping = new List<SingleInputMapping>();
//testMapping.Add(new SingleInputMapping(0, true, "0", ""));
// for (int i = 1; i < 32; i++)
// {
//testMapping.Add(new SingleInputMapping(i, false, i.ToString(), "-"));
//}
// return testMapping;
//}
// delete this later or put into comment!
//void testIt()
//{
//inputStates = testJsonStates;
//}
//
// Unused code END
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
// Unedited original Tap Strap 2 Unity plugin code START
//
void onTapConnected(string identifier, string name, int fw)
{
Debug.Log("onTapConnected : " + identifier + ", " + name + ", FW: " + fw);
Log("onTapConnected : " + identifier + ", " + name);
this.connectedTapIdentifier = identifier;
}
void onTapDisconnected(string identifier)
{
Debug.Log("UNITY TAP CALLBACK --- onTapDisconnected : " + identifier);
Log("UNITY TAP CALLBACK --- onTapDisconnected : " + identifier);
if (identifier.Equals(this.connectedTapIdentifier))
{
this.connectedTapIdentifier = "";
}
}
void onTapChangedState(string tapIdentifier, bool isAirGesture)
{
// Log("onTapChangedState: " + tapIdentifier + ", " + isAirGesture.ToString());
}
void onRawSensorDataReceived(string tapIdentifier, RawSensorData data)
{
//RawSensorData Object has a timestamp, type and an array points(x,y,z).
if (data.type == RawSensorData.DataType.Device)
{
// Fingers accelerometer.
// Each point in array represents the accelerometer value of a finger (thumb, index, middle, ring, pinky).
Vector3 thumb = data.GetPoint(RawSensorData.iDEV_THUMB);
if (thumb != null)
{
// Do something with thumb.x, thumb.y, thumb.z
}
// Etc... use indexes: RawSensorData.iDEV_THUMB, RawSensorData.iDEV_INDEX, RawSensorData.iDEV_MIDDLE, RawSensorData.iDEV_RING, RawSensorData.iDEV_PINKY
}
else if (data.type == RawSensorData.DataType.IMU)
{
// Refers to an additional accelerometer on the Thumb sensor and a Gyro (placed on the thumb unit as well).
Vector3 gyro = data.GetPoint(RawSensorData.iIMU_GYRO);
if (gyro != null)
{
// Do something with gyro.x, gyro.y, gyro.z
}
// Etc... use indexes: RawSensorData.iIMU_GYRO, RawSensorData.iIMU_ACCELEROMETER
}
// -------------------------------------------------
// -- Please refer readme.md for more information --
// -------------------------------------------------
}
//
// Unedited original Tap Strap 2 Unity plugin code END
// -------------------------------------------------------------------------------------------------
}