-
Notifications
You must be signed in to change notification settings - Fork 88
/
XInputSceneController.cs
249 lines (218 loc) · 9.19 KB
/
XInputSceneController.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
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.UI.Controls;
using SharpDX.XInput;
using System;
using Microsoft.UI.Xaml;
namespace XInputHelper
{
// Usage:
// Add namespace using
// xmlns:xinput="using:XInputHelper"
//
// Add inside SceneView:
// <xinput:XInputSceneController.Controller>
// <xinput:XInputSceneController />
// </xinput:XInputSceneController.Controller>
//
//
//
//
//
//
//
public class XInputSceneController
{
private SceneView sceneView;
private DispatcherTimer timer;
private SharpDX.XInput.Controller controller;
private bool relativeToDirection;
private DateTime time;
public XInputSceneController()
{
controller = new SharpDX.XInput.Controller(SharpDX.XInput.UserIndex.One);
}
private void SetSceneView(SceneView sceneView)
{
if (this.sceneView != null)
{
//Clean up
this.sceneView.Loaded -= _sceneView_Loaded;
this.sceneView.Unloaded -= _sceneView_Unloaded;
Stop();
}
this.sceneView = sceneView;
if (sceneView != null)
{
this.sceneView.Loaded += _sceneView_Loaded;
this.sceneView.Unloaded += _sceneView_Unloaded;
Start(); //We should only start if already loaded, but no way to detect that currently
}
}
private void Start()
{
if (timer == null)
{
timer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(1.0 / 200) };
timer.Tick += timer_Tick;
time = DateTime.Now;
timer.Start();
}
}
private void Stop()
{
if (timer != null)
{
timer.Tick -= timer_Tick;
timer.Stop();
timer = null;
}
}
private void timer_Tick(object sender, object e)
{
UpdateView();
}
private void UpdateView()
{
if (sceneView == null)
return;
if (controller == null || !controller.IsConnected)
return;
//The Camera might change whil doing all the calculations...
var originalCamera = sceneView.Camera;
if (originalCamera == null)
return;
var newCamera = originalCamera;
var state = controller.GetState();
var time2 = DateTime.Now;
bool isRightShoulderPressed = state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.RightShoulder);
//Choose if staying at the same alitude or not
if (isRightShoulderPressed)
{
var elapsed = time2 - time;
if (elapsed != TimeSpan.Zero && elapsed > TimeSpan.FromSeconds(0.25))
{
timer.Stop();
timer.Start();
relativeToDirection = !relativeToDirection;
time = time2;
}
}
else
time = time2;
double LeftThumbStickX = 0;
double LeftThumbStickY = 0;
double RightThumbStickX = 0;
double RightThumbStickY = 0;
var gamePad = new NormalizedGamepad(state.Gamepad);
LeftThumbStickX = gamePad.LeftThumbXNormalized;
LeftThumbStickY = gamePad.LeftThumbYNormalized;
RightThumbStickX = gamePad.RightThumbXNormalized;
RightThumbStickY = gamePad.RightThumbYNormalized;
if ((LeftThumbStickX != 0) || (LeftThumbStickY != 0) || (RightThumbStickX != 0) || (RightThumbStickY != 0))
{
//Move sideways (crab move), so calling move forward after modifying heading and tilt, then resetting heading and tilt
if (LeftThumbStickX != 0)
{
//Distance depends on the altitude, but must have minimum values (one way and the other) to not be almost nothing when altitude gets to 0)
var distance = (double)LeftThumbStickX * newCamera.Location.Z / 40;
if (distance > 0 && distance < 0.75)
distance = 0.75;
if (distance < 0 && distance > -0.75)
distance = -0.75;
var pitch = newCamera.Pitch;
newCamera = newCamera.RotateTo(newCamera.Heading, 90, newCamera.Roll).RotateTo(90, 0, 0).MoveForward(distance).RotateTo(-90, 0, 0);
newCamera = newCamera.RotateTo(newCamera.Heading, pitch, newCamera.Roll);
}
var distance2 = (double)LeftThumbStickY * newCamera.Location.Z / 40;
if (distance2 > 0 && distance2 < 1)
distance2 = 1;
if (distance2 < 0 && distance2 > -1)
distance2 = -1;
//Move in the direction of the camera (modify the elevation)
newCamera = newCamera.MoveForward(distance2).RotateTo((double)RightThumbStickX, newCamera.Pitch > 2 ? -(double)RightThumbStickY : RightThumbStickY < 0 ? -(double)RightThumbStickY : 0, 0);
//If move in the direction of the camera but staying at the same elevation.
if (!relativeToDirection)
newCamera = newCamera.MoveTo(new MapPoint(newCamera.Location.X, newCamera.Location.Y, originalCamera.Location.Z, newCamera.Location.SpatialReference));
}
//Triggers are just elevating the camera
double elevationDistance = (double)(state.Gamepad.RightTrigger - state.Gamepad.LeftTrigger);
if ((elevationDistance != 0) && (newCamera.Location.Z != double.NaN))
{
var distance = elevationDistance * Math.Abs(newCamera.Location.Z) / 10000;
newCamera = newCamera.Elevate(distance > 1 ? distance : distance < 1 ? distance : distance < 0 ? -1 : 1);
}
if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadUp))
newCamera = newCamera.RotateTo(0, newCamera.Pitch, newCamera.Roll);
if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadDown))
newCamera = newCamera.RotateTo(180, newCamera.Pitch, newCamera.Roll);
if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadLeft))
newCamera = newCamera.RotateTo(270, newCamera.Pitch, newCamera.Roll);
if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadRight))
newCamera = newCamera.RotateTo(90, newCamera.Pitch, newCamera.Roll);
if (!CamerasAreEqual(originalCamera, newCamera))
{
sceneView.SetViewpointCamera(newCamera);
}
}
private void _sceneView_Unloaded(object sender, RoutedEventArgs e)
{
Stop();
}
private void _sceneView_Loaded(object sender, RoutedEventArgs e)
{
Start();
}
public static XInputSceneController GetController(DependencyObject obj)
{
return (XInputSceneController)obj.GetValue(ControllerProperty);
}
public static void SetController(DependencyObject obj, XInputSceneController value)
{
obj.SetValue(ControllerProperty, value);
}
public static readonly DependencyProperty ControllerProperty =
DependencyProperty.RegisterAttached("Controller", typeof(XInputSceneController), typeof(XInputSceneController), new PropertyMetadata(null, OnControllerPropertyChanged));
private static void OnControllerPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var oldCtrl = e.OldValue as XInputSceneController;
var newCtrl = e.NewValue as XInputSceneController;
if (oldCtrl != null)
oldCtrl.SetSceneView(null);
if (newCtrl != null)
newCtrl.SetSceneView(d as SceneView);
}
private static bool CamerasAreEqual(Camera lhs, Camera rhs)
{
if (ReferenceEquals(lhs, rhs))
{
return true;
}
if (lhs == null || rhs == null)
{
return false;
}
if (lhs.Location == null && rhs.Location == null)
{
return true;
}
if (lhs.Location == null || rhs.Location == null)
{
return false;
}
if (lhs.Location is MapPoint ll && rhs.Location is MapPoint rl && doublesAreEqual(ll.X, rl.X)
&& doublesAreEqual(ll.Y, rl.Y) && doublesAreEqual(ll.Z, rl.Z)
&& doublesAreEqual(lhs.Pitch, rhs.Pitch) && doublesAreEqual(lhs.Heading, rhs.Heading)
&& doublesAreEqual(lhs.Roll, rhs.Roll))
{
return true;
}
return false;
}
private static bool doublesAreEqual(double lhs, double rhs)
{
const float eps = 1e-5f;
return Math.Abs(lhs - rhs) < eps;
}
}
}