-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFingers.cs
448 lines (383 loc) · 13.5 KB
/
Fingers.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
using System;
using System.Numerics; // Vector
using System.Windows.Forms; // SystemInformation
using System.Diagnostics; // Debug
using System.Threading.Tasks;
public enum RingStatus
{
SEARCHING = 1,
CONNECTING = 2,
CONNECTED = 4,
}
public class Fingers
{
// DCS translates mouse movement over its window into a 2D plane in the game world - you can see
// this window when you bring up the ESC menu where it is fixed in place, while it follows the
// head position at other times (this is why you get two different mouse behaviors). A mouse
// cursor in a corner of the DCS window always translates into the same position in-game,
// regardless of the player's FOV or the size/ratio of the main DCS window, so we need to
// compensate for the stretching DCS does to map input over its desktop window of varying size and
// aspect ratio to its inner window of fixed size and aspect ratio.
//
// This virtual screen appears to be 100 degrees wide with 16:9 aspect ratio
private static float inputScreenRatio = 16f / 10f;
private static float inputScreenWidthDegrees = 135f;
private static float inputScreenHeightDegrees = inputScreenWidthDegrees / inputScreenRatio;
// When choosing between two hands, how many degrees we should bias towards sticking with the
// currently tracked hand.
private static float overlap = 1;
// Scroll tracking
// DCS uses both individual events (for discrete things like channel selectors) as well as amounts
// for analog things like brightness controls - need to strike a balance between making one too
// sensitive and the other too insensitive
private static int ScrollDetentAmount = 750;
// Internal variables
// The inputScreenRatio setup is translated using inputAngleScale, which is set in
// HandleDCSWindow; here are some reasonable default values
Vector2 inputAngleScale = new Vector2(16.6f, 16.6f);
Vector4 DCSWindow = new Vector4(0, 0, 0, 0);
// The last hand we tracked
HandData currentHand;
public FingersApp.MainWindow ui;
Vector2 screenCenter;
Vector2 resetPoint;
ulong leftRingAddr = 0;
ulong rightRingAddr = 0;
uint leftRingBatt = 0;
uint rightRingBatt = 0;
bool leftButtonDown = false;
bool rightButtonDown = false;
long lastClicked = 0;
bool cursorEnabled = true;
float verticalOffset = 0;
LoopListener loop;
LeapHandler leap;
public Fingers(FingersApp.MainWindow mainWindow)
{
ui = mainWindow;
// Configuration variables
screenCenter = new Vector2(SystemInformation.PrimaryMonitorSize.Width / 2, SystemInformation.PrimaryMonitorSize.Height / 2);
resetPoint = new Vector2(screenCenter.X, screenCenter.Y * (float)1.25);
DCS.Monitor(this);
loop = new LoopListener(this);
leap = new LeapHandler(this);
SetLeapProfile(FingersApp.Properties.Settings.Default.LeapProfile, true);
SetSensitivity(FingersApp.Properties.Settings.Default.Sensitivity);
ui.Dispatcher.Invoke(() => { ui.SetSensitivityDisplay(FingersApp.Properties.Settings.Default.Sensitivity); });
// Keep this process running
Console.ReadLine();
}
public async Task<bool> Closing()
{
await loop.Closing();
return true;
}
public void HandleDCSWindow(Vector4 dim)
{
DCSWindow = dim;
Debug.WriteLine("DCS Window Size Adjustment: {0}", dim);
UpdateAngleScale();
}
public void UpdateAngleScale()
{
inputAngleScale = new Vector2(DCSWindow.W / inputScreenWidthDegrees,
DCSWindow.Z / inputScreenHeightDegrees);
Debug.WriteLine("New inputAngleScale: {0}", inputAngleScale);
}
public void SetSensitivity(float sens)
{
Debug.WriteLine("New sens: {0}", sens);
inputScreenWidthDegrees = sens;
inputScreenHeightDegrees = inputScreenWidthDegrees / inputScreenRatio;
UpdateAngleScale();
if (sens != FingersApp.Properties.Settings.Default.Sensitivity)
{
FingersApp.Properties.Settings.Default.Sensitivity = sens;
FingersApp.Properties.Settings.Default.Save();
}
}
// Get the angle of the position (in HandData position) relative to the eye in degrees
public Vector2 GetRelativeAngle(ref Vector3 pos)
{
Vector2 angle;
angle.X = (float)Math.Atan2(pos.X, pos.Z) * 180 / (float)Math.PI;
angle.Y = -(float)Math.Atan2(pos.Y, pos.Z) * 180 / (float)Math.PI;
return angle;
}
public Vector2 GetScreenPosition(Vector3 pos)
{
Vector2 angle = GetRelativeAngle(ref pos);
return new Vector2(
angle.X * inputAngleScale.X,
(angle.Y + verticalOffset) * inputAngleScale.Y
);
}
public bool IsDragging()
{
return (leftButtonDown || rightButtonDown);
}
public HandData GetActiveHand(HandData left, HandData right)
{
HandData activeHand = new HandData() { isActive = false };
// Match to existing hand if we're in some kind of drag event (so we don't snap to another
// hand partway through).
if (IsDragging() && currentHand.isActive)
{
if (currentHand.isLeft && left.isActive)
return left;
else if (!currentHand.isLeft && right.isActive)
return right;
else
return activeHand;
}
if (!left.isActive && right.isActive)
return right;
else if (left.isActive && !right.isActive)
return left;
else if (left.isActive && right.isActive)
{
// Figure out which hand to focus on by distance away from the middle
float l = Math.Abs(GetRelativeAngle(ref left.pos).X);
float r = Math.Abs(GetRelativeAngle(ref right.pos).X);
// If we're currently tracking a hand, bias away from the other hand by a certain amount of
// angle (defined in overlap)
if (currentHand.isActive && currentHand.isLeft)
r += overlap;
else if (currentHand.isActive && !currentHand.isLeft)
l += overlap;
return (l < r) ? left : right;
}
return activeHand;
}
public void HandleLeapConnected()
{
ui.Dispatcher.Invoke(() =>
{
ui.SetLeapStatus("Connected");
});
}
public void HandleLeapDisconnected()
{
ui.Dispatcher.Invoke(() =>
{
ui.SetLeapStatus("Connecting...");
});
}
public void HandleHands(HandData left, HandData right)
{
HandData activeHand = GetActiveHand(left, right);
if (!activeHand.isActive && currentHand.isActive)
{
DisengageHand();
return;
}
currentHand = activeHand;
Vector2 screenPos = GetScreenPosition(activeHand.pos);
SetCursorPos(screenPos);
}
// Called when the current hand stops being active; currently we leave left/right mouse buttons
// down because a user might have them down for other reasons (e.g. holding temporary switches)
private void DisengageHand()
{
}
private void ToggleVerticalOffset()
{
verticalOffset = (verticalOffset == 0) ? 12 : 0;
}
private void ToggleCursorEnabled()
{
cursorEnabled = !cursorEnabled;
}
private long GetTime()
{
return DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
}
public void HandleLoopConnecting(ulong addr)
{
if (leftRingAddr != 0 && rightRingAddr != 0)
{
return;
}
else if (leftRingAddr != 0 || FingersApp.Properties.Settings.Default.RightRingID == addr)
{
if (rightRingAddr != addr) ui.Dispatcher.Invoke(() =>
{
ui.SetRightRingStatus(RingStatus.CONNECTING, 0, 0);
});
}
else if (leftRingAddr != addr)
{
ui.Dispatcher.Invoke(() =>
{
ui.SetLeftRingStatus(RingStatus.CONNECTING, 0, 0);
});
}
}
public void HandleLoopConnected(ulong addr)
{
String addrString = addr.ToString("X");
if (leftRingAddr != 0 || addr.Equals(FingersApp.Properties.Settings.Default.RightRingID))
{
rightRingAddr = addr;
FingersApp.Properties.Settings.Default.RightRingID = addr;
FingersApp.Properties.Settings.Default.Save();
}
else
{
leftRingAddr = addr;
FingersApp.Properties.Settings.Default.LeftRingID = addr;
FingersApp.Properties.Settings.Default.Save();
}
UpdateRingStatus();
}
public void HandleLoopDisconnected(ulong addr)
{
String addrString = addr.ToString("X");
if (leftRingAddr == addr)
{
leftRingAddr = 0;
leftRingBatt = 0;
}
if (rightRingAddr == addr)
{
rightRingAddr = 0;
rightRingBatt = 0;
}
UpdateRingStatus();
}
public void SetLeapProfile(String name, bool updateUI)
{
if (name.Equals("Joculus"))
{
leap.SetProfile(5, 0, 0, 0, -18, 100);
}
else if (name.Equals("Generic"))
{
leap.SetProfile(0, 0, 0, 0, 0, 90);
}
else
{
name = "Pimax";
leap.SetProfile(10, 0, 0, 0, -45, 68);
}
if (updateUI)
{
ui.Dispatcher.Invoke(() =>
{
ui.SelectLeapProfile(name);
});
}
FingersApp.Properties.Settings.Default.LeapProfile = name;
FingersApp.Properties.Settings.Default.Save();
}
private void UpdateRingStatus()
{
if (leftRingAddr > 0)
ui.Dispatcher.Invoke(() => { ui.SetLeftRingStatus(RingStatus.CONNECTED, leftRingAddr, leftRingBatt); });
else
ui.Dispatcher.Invoke(() => { ui.SetLeftRingStatus(RingStatus.SEARCHING, 0, 0); });
if (rightRingAddr > 0)
ui.Dispatcher.Invoke(() => { ui.SetRightRingStatus(RingStatus.CONNECTED, rightRingAddr, rightRingBatt); });
else
ui.Dispatcher.Invoke(() => { ui.SetRightRingStatus(RingStatus.SEARCHING, 0, 0); });
}
public void SwapRings()
{
FingersApp.Properties.Settings.Default.LeftRingID = rightRingAddr;
FingersApp.Properties.Settings.Default.RightRingID = leftRingAddr;
leftRingAddr = FingersApp.Properties.Settings.Default.LeftRingID;
rightRingAddr = FingersApp.Properties.Settings.Default.RightRingID;
FingersApp.Properties.Settings.Default.Save();
uint t = leftRingBatt;
leftRingBatt = rightRingBatt;
rightRingBatt = t;
UpdateRingStatus();
}
public void HandleLoopBattery(ulong addr, uint batt)
{
if (addr == leftRingAddr)
leftRingBatt = batt;
else if (addr == rightRingAddr)
rightRingBatt = batt;
UpdateRingStatus();
}
public void HandleLoopEvent(LoopButton b, Boolean pressed, ulong addr)
{
Debug.WriteLine("{0}: {1} {2}", addr.ToString("X"), b, pressed ? "pressed" : "released");
if (addr == rightRingAddr)
{
if (b == LoopButton.FWD) b = LoopButton.BACK;
else if (b == LoopButton.BACK) b = LoopButton.FWD;
}
ui.Dispatcher.Invoke(() =>
{
ui.SetButtonStatus(b, pressed, (addr == rightRingAddr));
});
/*
if (pressed && (b == LoopButton.FWD || !cursorEnabled))
{
ToggleCursorEnabled();
return;
}
*/
if (b == LoopButton.FWD && pressed)
{
ToggleVerticalOffset();
}
else if (b == LoopButton.CENTER && pressed)
{
Winput.MouseButton(Winput.MouseEventF.LeftDown);
leftButtonDown = true;
lastClicked = GetTime();
}
else if (b == LoopButton.CENTER && !pressed)
{
Winput.MouseButton(Winput.MouseEventF.LeftUp);
leftButtonDown = false;
}
else if (b == LoopButton.BACK && pressed)
{
Winput.MouseButton(Winput.MouseEventF.RightDown);
rightButtonDown = true;
lastClicked = GetTime();
}
else if (b == LoopButton.BACK && !pressed)
{
Winput.MouseButton(Winput.MouseEventF.RightUp);
rightButtonDown = false;
}
else if (b == LoopButton.UP && pressed)
{
Scroll(ScrollDetentAmount);
}
else if (b == LoopButton.DOWN && pressed)
{
Scroll(-ScrollDetentAmount);
}
}
private void Scroll(int amount)
{
Winput.ScrollMouse(amount);
}
private void SetCursorPos(Vector2 pos)
{
if (!cursorEnabled)
return;
// Could use ClipCursor, but direct control is good
int x = (int)(screenCenter.X + pos.X);
int y = (int)(screenCenter.Y + pos.Y);
if (DCSWindow.W == 0)
{ }
else if (x < DCSWindow.X)
x = (int)DCSWindow.X;
else if (x > DCSWindow.X + DCSWindow.W)
x = (int)(DCSWindow.X + DCSWindow.W);
if (DCSWindow.Z == 0)
{ }
else if (y < DCSWindow.Y)
y = (int)DCSWindow.Y;
else if (y >= DCSWindow.Y + DCSWindow.Z)
y = (int)(DCSWindow.Y + DCSWindow.Z - 1);
Winput.SetCursorPosition(x, y);
}
}