-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
MainWindow.xaml.cs
911 lines (761 loc) · 36.9 KB
/
MainWindow.xaml.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
using MathNet.Numerics.LinearAlgebra;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Net;
using System.Text.Json;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
namespace VRC_OSC_ExternallyTrackedObject
{
internal class AvatarParams
{
public string Activate { get; set; } = "/avatar/parameters/OSCTrackingEnabled";
public string PositionX { get; set; } = "/avatar/parameters/OscTrackedPosX";
public string PositionY { get; set; } = "/avatar/parameters/OscTrackedPosY";
public string PositionZ { get; set; } = "/avatar/parameters/OscTrackedPosZ";
public string RotationX { get; set; } = "/avatar/parameters/OscTrackedRotX";
public string RotationY { get; set; } = "/avatar/parameters/OscTrackedRotY";
public string RotationZ { get; set; } = "/avatar/parameters/OscTrackedRotZ";
}
public class AvatarCalibration
{
public double Scale { get; set; } = 1;
public double TranslationX { get; set; } = 0;
public double TranslationY { get; set; } = 0;
public double TranslationZ { get; set; } = 0;
public double RotationX { get; set; } = 0;
public double RotationY { get; set; } = 0;
public double RotationZ { get; set; } = 0;
public void CopyFrom(AvatarCalibration other) {
Scale = other.Scale;
TranslationX = other.TranslationX;
TranslationY = other.TranslationY;
TranslationZ = other.TranslationZ;
RotationX = other.RotationX;
RotationY = other.RotationY;
RotationZ = other.RotationZ;
}
public static AvatarCalibration FromMatrix(Matrix<float> mat44)
{
var rotation = MathUtils.extractRotationsFromMatrix44(mat44);
var translation = MathUtils.extractTranslationFromMatrix44(mat44);
var scaleVec = MathUtils.extractScaleFromMatrix44(mat44);
var calibration = new AvatarCalibration();
calibration.Scale = scaleVec.AbsoluteMinimum(); // the scale should be almost exactly the same for all dimensions so let's just pick the smallest one
calibration.TranslationX = translation[0];
calibration.TranslationY = translation[1];
calibration.TranslationZ = translation[2];
calibration.RotationX = rotation[0];
calibration.RotationY = rotation[1];
calibration.RotationZ = rotation[2];
return calibration;
}
}
internal class AvatarConfig
{
public string Name { get; set; } = "";
public AvatarParams Parameters { get; set; } = new AvatarParams();
public AvatarCalibration Calibration { get; set; } = new AvatarCalibration();
}
internal class Configuration
{
public bool Autostart { get; set; } = false;
public bool AllowAllDevices { get; set; } = false;
public string OscInputAddress { get; set; } = "127.0.0.1:9001";
public string OscOutputAddress { get; set; } = "127.0.0.1:9000";
public string? ControllerSerial { get; set; }
public string? TrackerSerial { get; set; }
public Dictionary<string, AvatarConfig> Avatars { get; set; } = new Dictionary<string, AvatarConfig>();
}
internal class AvatarListItem
{
public string Name { get; set; }
public string Id { get; set; }
public string DisplayName
{
get { return Name + " (" + Id + ")"; }
}
public AvatarListItem(string id, string name)
{
Id = id;
Name = name;
}
}
internal class DeviceListItem
{
public string Serial { get; set; }
public bool Exists { get; set; }
public string DisplayName
{
get { return Serial + ((!Exists) ? " (Not found)" : ""); }
}
public DeviceListItem(string serial, bool exists = true)
{
Serial = serial;
Exists = exists;
}
}
//public class MainWindowProperties : INotifyPropertyChanged
//{
// public event PropertyChangedEventHandler PropertyChanged;
// protected virtual void OnPropertyChanged(string propertyName)
// {
// PropertyChangedEventHandler handler = PropertyChanged;
// if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
// }
// protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
// {
// if (EqualityComparer<T>.Default.Equals(field, value)) return false;
// field = value;
// OnPropertyChanged(propertyName);
// return true;
// }
// private bool allInputsEnabled;
// public bool AllInputsEnabled
// {
// get { return allInputsEnabled; }
// set { SetField(ref allInputsEnabled, value); }
// }
//}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Configuration CurrentConfig = new Configuration();
private ObservableCollection<AvatarListItem> AvatarList = new ObservableCollection<AvatarListItem>();
private ObservableCollection<DeviceListItem> ControllerList = new ObservableCollection<DeviceListItem>();
private ObservableCollection<DeviceListItem> TrackerList = new ObservableCollection<DeviceListItem>();
//public MainWindowProperties DisplayProperties { get; set; }
private OpenVRManager OpenVRManager = new OpenVRManager();
private OscManager OscManager = new OscManager();
private string? CurrentAvatarId;
private Matrix<float>? CurrentInverseCalibrationMatrix = null;
private Matrix<float>? CurrentInverseCalibrationMatrixNoScale = null;
public static float MAX_RELATIVE_DISTANCE = 1; // this is the max distance the tracker can be away from the controller. this is important for scaling the value since vrchat wants a value between -1 and 1
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
AvatarDropdown.ItemsSource = AvatarList;
AvatarListBox.ItemsSource = AvatarList;
ControllerDropdown.ItemsSource = ControllerList;
TrackerDropdown.ItemsSource = TrackerList;
OSCInputAddress.InputText = CurrentConfig.OscInputAddress;
OSCOutputAddress.InputText = CurrentConfig.OscOutputAddress;
if (AvatarList.Count > 0)
{
AvatarDropdown.SelectedIndex = 0;
}
this.OpenVRManager.CalibrationUpdate += OnCalibrationUpdate;
this.OpenVRManager.TrackingData += OnTrackingData;
this.OscManager.AvatarChanged += OnAvatarChanged;
this.OscManager.TrackingActiveChanged += OnTrackingActiveChanged;
this.OscManager.ThreadCrashed += OnOscThreadCrashed;
}
public bool Init()
{
bool initSuccess = this.OpenVRManager.InitOverlay();
if (!initSuccess) return false;
UpdateControllersAndTrackers();
return true;
}
public void ProcessStartupConfig()
{
if (CurrentConfig.Autostart)
{
StartTracking();
}
}
// this will be called within the calibration thread so we need to inject the event back into the UI thread
private void OnCalibrationUpdate(object? sender, EventArgs args)
{
Dictionary<CalibrationField, LabeledInput> fields = new Dictionary<CalibrationField, LabeledInput>()
{
{ CalibrationField.POSX, CalibrationPosX },
{ CalibrationField.POSY, CalibrationPosY },
{ CalibrationField.POSZ, CalibrationPosZ },
{ CalibrationField.ROTX, CalibrationRotX },
{ CalibrationField.ROTY, CalibrationRotY },
{ CalibrationField.ROTZ, CalibrationRotZ },
{ CalibrationField.SCALE, CalibrationScale },
};
var calibrationUpdateArgs = (CalibrationUpdateArgs)args;
var dispatcher = Application.Current.Dispatcher;
dispatcher.BeginInvoke(new Action(() =>
{
switch(calibrationUpdateArgs.Type)
{
case CalibrationUpdateArgs.CalibrationUpdateType.ACTIVE_FIELD:
CalibrationScale.Highlighted = false;
CalibrationPosX.Highlighted = false;
CalibrationPosY.Highlighted = false;
CalibrationPosZ.Highlighted = false;
CalibrationRotX.Highlighted = false;
CalibrationRotY.Highlighted = false;
CalibrationRotZ.Highlighted = false;
fields[calibrationUpdateArgs.Field].Highlighted = true;
break;
case CalibrationUpdateArgs.CalibrationUpdateType.CALIBRATION_VALUE:
if (CurrentAvatarId != null)
{
CurrentConfig.Avatars[CurrentAvatarId].Calibration.CopyFrom(calibrationUpdateArgs.CalibrationValues!);
}
CalibrationPosX.InputText = NumberToInput(calibrationUpdateArgs.CalibrationValues!.TranslationX);
CalibrationPosY.InputText = NumberToInput(calibrationUpdateArgs.CalibrationValues!.TranslationY);
CalibrationPosZ.InputText = NumberToInput(calibrationUpdateArgs.CalibrationValues!.TranslationZ);
CalibrationRotX.InputText = NumberToInput(calibrationUpdateArgs.CalibrationValues!.RotationX);
CalibrationRotY.InputText = NumberToInput(calibrationUpdateArgs.CalibrationValues!.RotationY);
CalibrationRotZ.InputText = NumberToInput(calibrationUpdateArgs.CalibrationValues!.RotationZ);
CalibrationScale.InputText = NumberToInput(calibrationUpdateArgs.CalibrationValues!.Scale);
break;
}
}));
}
private string NumberToInput(double number)
{
var dec = Convert.ToDecimal(FixZero(number));
return Math.Round(dec, 6).ToString(CultureInfo.InvariantCulture);
}
// will be called within the OVR tracking thread,
// but we can send the OSC messages directly from here, so no injecting it into the UI thread neccessary
private void OnTrackingData(object? sender, EventArgs args)
{
var trackingEventArgs = (TrackingDataArgs)args;
if (CurrentInverseCalibrationMatrix == null || trackingEventArgs.Controller == null) return;
var controllerInverse = trackingEventArgs.Controller.Inverse();
var _controllerToTracker = controllerInverse * trackingEventArgs.Tracker;
var controllerToTracker = CurrentInverseCalibrationMatrix * _controllerToTracker;
var controllerToTrackerNS = CurrentInverseCalibrationMatrixNoScale * _controllerToTracker;
var relativeTranslate = MathUtils.extractTranslationFromMatrix44(controllerToTracker);
var relativeRotation = MathUtils.extractRotationsFromMatrix(controllerToTrackerNS.Inverse().SubMatrix(0, 3, 0, 3));
if (Math.Abs(relativeTranslate[0]) >= MAX_RELATIVE_DISTANCE
|| Math.Abs(relativeTranslate[1]) >= MAX_RELATIVE_DISTANCE
|| Math.Abs(relativeTranslate[2]) >= MAX_RELATIVE_DISTANCE)
{
relativeTranslate[0] = 0;
relativeTranslate[1] = 0;
relativeTranslate[2] = 0;
}
float rotationX = -relativeRotation[0] / (float)Math.PI;
float rotationY = relativeRotation[1] / (float)Math.PI;
float rotationZ = relativeRotation[2] / (float)Math.PI;
this.OscManager.SendValues(
-relativeTranslate[0] / MAX_RELATIVE_DISTANCE,
relativeTranslate[1] / MAX_RELATIVE_DISTANCE,
relativeTranslate[2] / MAX_RELATIVE_DISTANCE,
rotationX,
rotationY,
rotationZ
);
}
// this is called from the OSC thread, so we need to move the data to the UI thread first
private void OnTrackingActiveChanged(object? sender, EventArgs args)
{
var eventArgs = (TrackingActiveChangedArgs)args;
var dispatcher = Application.Current.Dispatcher;
dispatcher.BeginInvoke(new Action(() =>
{
if (eventArgs.Active)
{
CurrentStatusLabel.Content = "active";
}
else if (eventArgs.AvatarKnown)
{
CurrentStatusLabel.Content = "inactive (disabled)";
}
else
{
CurrentStatusLabel.Content = "inactive (unknown avatar)";
}
}));
}
// this is called from the OSC thread, so we need to move the data to the UI thread first
private void OnAvatarChanged(object? sender, EventArgs args)
{
var avatarChangeArgs = (AvatarChangedArgs)args;
var dispatcher = Application.Current.Dispatcher;
dispatcher.BeginInvoke(new Action(() =>
{
Debug.WriteLine("Avatar changed to " + avatarChangeArgs.Id);
if (CurrentConfig.Avatars.ContainsKey(avatarChangeArgs.Id))
{
Debug.WriteLine("we know this avatar, creating inverse calibration matrix");
var calibration = CurrentConfig.Avatars[avatarChangeArgs.Id].Calibration;
CurrentInverseCalibrationMatrix = MathUtils.createTransformMatrix44(
(float)calibration.RotationX, (float)calibration.RotationY, (float)calibration.RotationZ,
(float)calibration.TranslationX, (float)calibration.TranslationY, (float)calibration.TranslationZ,
(float)calibration.Scale, (float)calibration.Scale, (float)calibration.Scale
).Inverse();
CurrentInverseCalibrationMatrixNoScale = MathUtils.createTransformMatrix44(
(float)calibration.RotationX, (float)calibration.RotationY, (float)calibration.RotationZ,
(float)calibration.TranslationX, (float)calibration.TranslationY, (float)calibration.TranslationZ,
1.0f, 1.0f, 1.0f
).Inverse();
}
}));
}
private void OnOscThreadCrashed(object? sender, EventArgs args)
{
var dispatcher = Application.Current.Dispatcher;
dispatcher.BeginInvoke(new Action(() =>
{
ShowTrackingStopped();
this.OpenVRManager.StopThread();
this.OscManager.Stop();
}));
}
private void CopySettingsToConfig()
{
CurrentConfig.OscInputAddress = OSCInputAddress.InputText;
CurrentConfig.OscOutputAddress = OSCOutputAddress.InputText;
CurrentConfig.Autostart = AutostartCheckbox.IsChecked == true;
}
private void Btn_saveConfig(object sender, RoutedEventArgs e)
{
if (CurrentAvatarId != null)
{
CopyInputValuesToConfig(CurrentConfig.Avatars[CurrentAvatarId]);
}
CopySettingsToConfig();
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "JSON file|*.json";
if (saveFileDialog.ShowDialog() == true)
{
string jsonString = JsonSerializer.Serialize(CurrentConfig, new JsonSerializerOptions() { WriteIndented = true });
File.WriteAllText(saveFileDialog.FileName, jsonString);
}
}
public void LoadConfig(string path)
{
string jsonString = File.ReadAllText(path);
if (jsonString == null) return;
Configuration? config = new Configuration();
try
{
config = JsonSerializer.Deserialize<Configuration>(jsonString);
}
catch (JsonException ex)
{
MessageBox.Show("Could not parse config file: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
if (config == null) return;
CurrentAvatarId = null;
CurrentConfig = config;
if (CurrentConfig.ControllerSerial != null && CurrentConfig.ControllerSerial.Length > 0) {
bool found = false;
for (int i = 0; i < ControllerList.Count; i++)
{
if (ControllerList[i].Serial == CurrentConfig.ControllerSerial)
{
ControllerDropdown.SelectedIndex = i;
found = true;
break;
}
}
if (!found)
{
ControllerList.Add(new DeviceListItem(CurrentConfig.ControllerSerial, false));
ControllerDropdown.SelectedValue = CurrentConfig.ControllerSerial;
}
}
if (CurrentConfig.TrackerSerial != null && CurrentConfig.TrackerSerial.Length > 0)
{
bool found = false;
for (int i = 0; i < TrackerList.Count; i++)
{
if (TrackerList[i].Serial == CurrentConfig.TrackerSerial)
{
TrackerDropdown.SelectedIndex = i;
found = true;
break;
}
}
if (!found)
{
TrackerList.Add(new DeviceListItem(CurrentConfig.TrackerSerial, false));
TrackerDropdown.SelectedValue = CurrentConfig.TrackerSerial;
}
}
OSCInputAddress.InputText = CurrentConfig.OscInputAddress;
OSCOutputAddress.InputText = CurrentConfig.OscOutputAddress;
AutostartCheckbox.IsChecked = CurrentConfig.Autostart;
AvatarList.Clear();
foreach (var avi in CurrentConfig.Avatars)
{
AvatarList.Add(new AvatarListItem(avi.Key, avi.Value.Name));
}
if (AvatarList.Count > 0)
{
AvatarDropdown.SelectedIndex = 0;
}
}
private void Btn_openConfig(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "JSON file|*.json";
if (openFileDialog.ShowDialog() == true)
{
LoadConfig(openFileDialog.FileName);
}
}
private void Btn_addAvatar(object sender, RoutedEventArgs e)
{
if (NewAvatarId.InputText == "" || NewAvatarId.InputText == null)
{
MessageBox.Show("The id field can not be empty", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
if (NewAvatarName.InputText == "" || NewAvatarName.InputText == null)
{
MessageBox.Show("The name field can not be empty", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
var avatarId = NewAvatarId.InputText;
var avatarName = NewAvatarName.InputText;
var config = new AvatarConfig();
config.Name = avatarName;
CurrentConfig.Avatars.Add(avatarId, config);
var listItem = new AvatarListItem(avatarId, avatarName);
AvatarList.Add(listItem);
NewAvatarId.InputText = "";
NewAvatarName.InputText = "";
if (AvatarList.Count == 1)
{
AvatarDropdown.SelectedIndex = 0;
}
}
private void Btn_deleteAvatar(object sender, RoutedEventArgs e)
{
if (AvatarListBox.SelectedItem == null) return;
CurrentConfig.Avatars.Remove(((AvatarListItem)AvatarListBox.SelectedItem).Id);
AvatarList.Remove((AvatarListItem)AvatarListBox.SelectedItem);
}
private void AvatarDropdown_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (AvatarDropdown.SelectedItem == null)
{
ClearAndDisableAllInputs();
return;
}
// first, store all the current input values in the config object
if (CurrentAvatarId != null && CurrentConfig.Avatars.ContainsKey(CurrentAvatarId))
{
var currentAvatarConfig = CurrentConfig.Avatars[CurrentAvatarId];
CopyInputValuesToConfig(currentAvatarConfig);
}
// afterwards switch everything over to the new avatar
CurrentAvatarId = ((AvatarListItem)AvatarDropdown.SelectedItem).Id;
var avatarConfig = CurrentConfig.Avatars[CurrentAvatarId];
FillAndEnableAllInputs(avatarConfig);
}
private void ClearAndDisableAllInputs()
{
ParamTrigger.InputText = "";
ParamTrigger.IsEnabled = false;
ParamPosX.InputText = "";
ParamPosX.IsEnabled = false;
ParamPosY.InputText = "";
ParamPosY.IsEnabled = false;
ParamPosZ.InputText = "";
ParamPosZ.IsEnabled = false;
ParamRotX.InputText = "";
ParamRotX.IsEnabled = false;
ParamRotY.InputText = "";
ParamRotY.IsEnabled = false;
ParamRotZ.InputText = "";
ParamRotZ.IsEnabled = false;
CalibrationScale.InputText = "";
CalibrationScale.IsEnabled = false;
CalibrationPosX.InputText = "";
CalibrationPosX.IsEnabled = false;
CalibrationPosY.InputText = "";
CalibrationPosY.IsEnabled = false;
CalibrationPosZ.InputText = "";
CalibrationPosZ.IsEnabled = false;
CalibrationRotX.InputText = "";
CalibrationRotX.IsEnabled = false;
CalibrationRotY.InputText = "";
CalibrationRotY.IsEnabled = false;
CalibrationRotZ.InputText = "";
CalibrationRotZ.IsEnabled = false;
StartTrackingButton.IsEnabled = false;
StartCalibrationButton.IsEnabled = false;
}
private void FillAllCalibrationInputs(AvatarCalibration calibration)
{
CalibrationScale.InputText = calibration.Scale.ToString(CultureInfo.InvariantCulture);
CalibrationPosX.InputText = calibration.TranslationX.ToString(CultureInfo.InvariantCulture);
CalibrationPosY.InputText = calibration.TranslationY.ToString(CultureInfo.InvariantCulture);
CalibrationPosZ.InputText = calibration.TranslationZ.ToString(CultureInfo.InvariantCulture);
CalibrationRotX.InputText = calibration.RotationX.ToString(CultureInfo.InvariantCulture);
CalibrationRotY.InputText = calibration.RotationY.ToString(CultureInfo.InvariantCulture);
CalibrationRotZ.InputText = calibration.RotationZ.ToString(CultureInfo.InvariantCulture);
}
private void ControlAllCalibrationInputs(bool enabled)
{
CalibrationScale.IsEnabled = enabled;
CalibrationPosX.IsEnabled = enabled;
CalibrationPosY.IsEnabled = enabled;
CalibrationPosZ.IsEnabled = enabled;
CalibrationRotX.IsEnabled = enabled;
CalibrationRotY.IsEnabled = enabled;
CalibrationRotZ.IsEnabled = enabled;
}
private void FillAllParameterInputs(AvatarParams parameters)
{
ParamTrigger.InputText = parameters.Activate;
ParamPosX.InputText = parameters.PositionX;
ParamPosY.InputText = parameters.PositionY;
ParamPosZ.InputText = parameters.PositionZ;
ParamRotX.InputText = parameters.RotationX;
ParamRotY.InputText = parameters.RotationY;
ParamRotZ.InputText = parameters.RotationZ;
}
private void ControlAllParameterInputs(bool enabled)
{
ParamTrigger.IsEnabled = enabled;
ParamPosX.IsEnabled = enabled;
ParamPosY.IsEnabled = enabled;
ParamPosZ.IsEnabled = enabled;
ParamRotX.IsEnabled = enabled;
ParamRotY.IsEnabled = enabled;
ParamRotZ.IsEnabled = enabled;
}
private void FillAndEnableAllInputs(AvatarConfig config)
{
FillAllParameterInputs(config.Parameters);
ControlAllParameterInputs(true);
FillAllCalibrationInputs(config.Calibration);
ControlAllCalibrationInputs(true);
StartTrackingButton.IsEnabled = true;
StartCalibrationButton.IsEnabled = true;
}
private void CopyInputValuesToConfig(AvatarConfig config)
{
CopyCalibrationValuesToConfig(config);
CopyParametersToConfig(config);
}
private void CopyCalibrationValuesToConfig(AvatarConfig config)
{
config.Calibration.Scale = float.Parse(CalibrationScale.InputText, CultureInfo.InvariantCulture);
config.Calibration.TranslationX = float.Parse(CalibrationPosX.InputText, CultureInfo.InvariantCulture);
config.Calibration.TranslationY = float.Parse(CalibrationPosY.InputText, CultureInfo.InvariantCulture);
config.Calibration.TranslationZ = float.Parse(CalibrationPosZ.InputText, CultureInfo.InvariantCulture);
config.Calibration.RotationX = float.Parse(CalibrationRotX.InputText, CultureInfo.InvariantCulture);
config.Calibration.RotationY = float.Parse(CalibrationRotY.InputText, CultureInfo.InvariantCulture);
config.Calibration.RotationZ = float.Parse(CalibrationRotZ.InputText, CultureInfo.InvariantCulture);
}
private void CopyParametersToConfig(AvatarConfig config)
{
config.Parameters.Activate = ParamTrigger.InputText;
config.Parameters.PositionX = ParamPosX.InputText;
config.Parameters.PositionY = ParamPosY.InputText;
config.Parameters.PositionZ = ParamPosZ.InputText;
config.Parameters.RotationX = ParamRotX.InputText;
config.Parameters.RotationY = ParamRotY.InputText;
config.Parameters.RotationZ = ParamRotZ.InputText;
}
private void StartCalibrationButton_Click(object sender, RoutedEventArgs e)
{
if (this.OpenVRManager.IsAnyThreadRunning())
{
StartCalibrationButton.Content = "Start Calibration";
StartTrackingButton.IsEnabled = true;
AvatarDropdown.IsEnabled = true;
TrackingTab.IsEnabled = true;
AvatarsTab.IsEnabled = true;
this.OpenVRManager.StopThread();
// clear highlight as well
CalibrationScale.Highlighted = false;
CalibrationPosX.Highlighted = false;
CalibrationPosY.Highlighted = false;
CalibrationPosZ.Highlighted = false;
CalibrationRotX.Highlighted = false;
CalibrationRotY.Highlighted = false;
CalibrationRotZ.Highlighted = false;
}
else
{
if (ControllerDropdown.SelectedItem == null)
{
MessageBox.Show("No controller selected", "Controller missing", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
if (AvatarDropdown.SelectedItem == null)
{
MessageBox.Show("No avatar selected. You need to select an avatar to calibrate the values for.", "Avatar missing", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
string currentController = ((DeviceListItem)ControllerDropdown.SelectedItem).Serial;
string currentAvatar = ((AvatarListItem)AvatarDropdown.SelectedItem).Id;
// load values from inputs
CopyCalibrationValuesToConfig(CurrentConfig.Avatars[currentAvatar]);
// copy the current calibration to the calibration thread
// updates are then sent by the calibration thread via events
AvatarCalibration calibration = CurrentConfig.Avatars[currentAvatar].Calibration;
StartCalibrationButton.Content = "Stop Calibration";
StartTrackingButton.IsEnabled = false;
AvatarDropdown.IsEnabled = false;
TrackingTab.IsEnabled = false;
AvatarsTab.IsEnabled = false;
this.OpenVRManager.StartCalibrationThread(currentController, calibration);
}
}
private void StartTrackingButton_Click(object sender, RoutedEventArgs e)
{
if (this.OpenVRManager.IsAnyThreadRunning())
{
ShowTrackingStopped();
this.OpenVRManager.StopThread();
this.OscManager.Stop();
}
else
{
string currentAvatar = ((AvatarListItem)AvatarDropdown.SelectedItem).Id;
// load values from inputs
CopyInputValuesToConfig(CurrentConfig.Avatars[currentAvatar]);
StartTracking();
}
}
private void ShowTrackingStopped()
{
StartTrackingButton.Content = "Start Tracking";
CurrentStatusLabel.Content = "inactive";
StartCalibrationButton.IsEnabled = true;
AvatarDropdown.IsEnabled = true;
CalibrationTab.IsEnabled = true;
AvatarsTab.IsEnabled = true;
}
private void StartTracking()
{
if (ControllerDropdown.SelectedItem == null)
{
MessageBox.Show("No controller selected", "Controller missing", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
if (TrackerDropdown.SelectedItem == null)
{
MessageBox.Show("No tracker selected", "Tracker missing", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
if (OSCInputAddress.InputText == null || OSCInputAddress.InputText.Length == 0)
{
MessageBox.Show("No input address provided", "Input address missing", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
if (OSCOutputAddress.InputText == null || OSCOutputAddress.InputText.Length == 0)
{
MessageBox.Show("No output address provided", "Output address missing", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
string inputAddress;
int inputPort;
string outputAddress;
int outputPort;
if (IPEndPoint.TryParse(OSCInputAddress.InputText, out IPEndPoint? endpoint))
{
inputAddress = endpoint.Address.ToString();
inputPort = endpoint.Port;
}
else
{
MessageBox.Show("Could not parse input address. Make sure it has the format <address>:<port>", "Invalid input address", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
if (IPEndPoint.TryParse(OSCOutputAddress.InputText, out endpoint))
{
outputAddress = endpoint.Address.ToString();
outputPort = endpoint.Port;
}
else
{
MessageBox.Show("Could not parse output address. Make sure it has the format <address>:<port>", "Invalid input address", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
string currentController = ((DeviceListItem)ControllerDropdown.SelectedItem).Serial;
string currentTracker = ((DeviceListItem)TrackerDropdown.SelectedItem).Serial;
bool trackingStarted = this.OpenVRManager.StartTrackingThread(currentController, currentTracker);
if (!trackingStarted) return;
StartTrackingButton.Content = "Stop Tracking";
StartCalibrationButton.IsEnabled = false;
AvatarDropdown.IsEnabled = false;
CalibrationTab.IsEnabled = false;
AvatarsTab.IsEnabled = false;
this.OscManager.Start(inputAddress, inputPort, outputAddress, outputPort, CurrentConfig.Avatars);
}
private void MainWindowName_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (!this.OpenVRManager.IsCalibrationThreadRunning()) return;
if (e.Key == System.Windows.Input.Key.Up ||
e.Key == System.Windows.Input.Key.Down ||
e.Key == System.Windows.Input.Key.Left ||
e.Key == System.Windows.Input.Key.Right)
{
this.OpenVRManager.InjectKeyPress(e.Key);
}
e.Handled = true;
}
private void TrackerDropdown_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender == null) return;
ComboBox? senderComboBox = sender as ComboBox;
if (senderComboBox == null) return;
object selectedValue = senderComboBox.SelectedValue;
if (selectedValue == null) return;
CurrentConfig.TrackerSerial = (string)selectedValue;
}
private void ControllerDropdown_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender == null) return;
ComboBox? senderComboBox = sender as ComboBox;
if (senderComboBox == null) return;
object selectedValue = senderComboBox.SelectedValue;
if (selectedValue == null) return;
CurrentConfig.ControllerSerial = (string)selectedValue;
}
private void UpdateControllersAndTrackers()
{
this.OpenVRManager.UpdateControllers();
this.ControllerList.Clear();
List<string> controllers = (CurrentConfig.AllowAllDevices) ? this.OpenVRManager.GetAllDevices() : this.OpenVRManager.GetControllers();
foreach (string controllerId in controllers)
{
this.ControllerList.Add(new DeviceListItem(controllerId));
}
this.TrackerList.Clear();
List<string> trackers = (CurrentConfig.AllowAllDevices) ? this.OpenVRManager.GetAllDevices() : this.OpenVRManager.GetTrackers();
foreach (string trackerId in trackers)
{
this.TrackerList.Add(new DeviceListItem(trackerId));
}
}
private void DevicesRefresh_Click(object sender, RoutedEventArgs e)
{
var currentController = ControllerDropdown.SelectedItem as DeviceListItem;
var currentControllerSerial = (currentController != null) ? currentController.Serial : null;
var currentTracker = TrackerDropdown.SelectedItem as DeviceListItem;
var currentTrackerSerial = (currentTracker != null) ? currentTracker.Serial : null;
UpdateControllersAndTrackers();
if (currentControllerSerial != null)
{
ControllerDropdown.SelectedValue = currentControllerSerial;
}
if (currentTrackerSerial != null)
{
TrackerDropdown.SelectedValue = currentTrackerSerial;
}
}
private double FixZero(double input)
{
return (input < 0.00001 && input > -0.00001) ? 0 : input;
}
}
}