-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoveLookController.cpp
1005 lines (869 loc) · 29.7 KB
/
MoveLookController.cpp
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
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
//// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
//// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//// PARTICULAR PURPOSE.
////
//// Copyright (c) Microsoft Corporation. All rights reserved
#include "pch.h"
#include "MoveLookController.h"
#include "DirectXSample.h"
using namespace Windows::UI::Core;
using namespace Windows::UI::Input;
using namespace Windows::UI;
using namespace Windows::Foundation;
using namespace Microsoft::WRL;
using namespace DirectX;
using namespace Windows::Devices::Input;
using namespace Windows::System;
#define ROTATION_GAIN 0.008f // Sensitivity adjustment for look controller.
#define MOVEMENT_GAIN 2.f // Sensitivity adjustment for move controller.
//----------------------------------------------------------------------
MoveLookController::MoveLookController():
m_autoFire(true),
m_isControllerConnected(false)
{
}
//----------------------------------------------------------------------
// Set up the Controls supported by this controller.
void MoveLookController::Initialize(
_In_ CoreWindow^ window
)
{
window->PointerPressed +=
ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &MoveLookController::OnPointerPressed);
window->PointerMoved +=
ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &MoveLookController::OnPointerMoved);
window->PointerReleased +=
ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &MoveLookController::OnPointerReleased);
window->PointerExited +=
ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &MoveLookController::OnPointerExited);
window->KeyDown +=
ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &MoveLookController::OnKeyDown);
window->KeyUp +=
ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &MoveLookController::OnKeyUp);
// There is a separate handler for mouse only relative mouse movement events.
MouseDevice::GetForCurrentView()->MouseMoved +=
ref new TypedEventHandler<MouseDevice^, MouseEventArgs^>(this, &MoveLookController::OnMouseMoved);
ResetState();
m_state = MoveLookControllerState::None;
m_pitch = 0.0f;
m_yaw = 0.0f;
}
//----------------------------------------------------------------------
bool MoveLookController::IsPauseRequested()
{
switch (m_state)
{
case MoveLookControllerState::Active:
UpdateGameController();
if (m_pausePressed)
{
#ifdef MOVELOOKCONTROLLER_TRACE
DebugTrace(L"IsPauseRequested == true\n");
#endif
m_pausePressed = false;
return true;
}
else
{
return false;
}
}
return false;
}
//----------------------------------------------------------------------
bool MoveLookController::IsFiring()
{
if (m_state == MoveLookControllerState::Active)
{
if (m_autoFire)
{
return (m_fireInUse || (m_mouseInUse && m_mouseLeftInUse) || m_xinputTriggerInUse);
}
else
{
if (m_firePressed)
{
m_firePressed = false;
return true;
}
}
}
return false;
}
//----------------------------------------------------------------------
bool MoveLookController::IsPressComplete()
{
switch (m_state)
{
case MoveLookControllerState::WaitForInput:
UpdateGameController();
if (m_buttonPressed)
{
#ifdef MOVELOOKCONTROLLER_TRACE
DebugTrace(L"IsPressComplete == true\n");
#endif
m_buttonPressed = false;
return true;
}
else
{
return false;
}
break;
}
return false;
}
//----------------------------------------------------------------------
void MoveLookController::OnPointerPressed(
_In_ CoreWindow^ /* sender */,
_In_ PointerEventArgs^ args
)
{
PointerPoint^ point = args->CurrentPoint;
uint32 pointerID = point->PointerId;
Point pointerPosition = point->Position;
PointerPointProperties^ pointProperties = point->Properties;
auto pointerDevice = point->PointerDevice;
auto pointerDeviceType = pointerDevice->PointerDeviceType;
XMFLOAT2 position = XMFLOAT2(pointerPosition.X, pointerPosition.Y);
#ifdef MOVELOOKCONTROLLER_TRACE
DebugTrace(L"%-7s (%d) at (%4.0f, %4.0f)", L"Pressed", pointerID, position.x, position.y);
#endif
switch (m_state)
{
case MoveLookControllerState::WaitForInput:
if (position.x > m_buttonUpperLeft.x &&
position.x < m_buttonLowerRight.x &&
position.y > m_buttonUpperLeft.y &&
position.y < m_buttonLowerRight.y)
{
// Wait until button released before setting variable.
m_buttonPointerID = pointerID;
m_buttonInUse = true;
#ifdef MOVELOOKCONTROLLER_TRACE
DebugTrace(L"\tWaitForInput(%d) - BUTTON in USE", pointerID);
#endif
}
break;
case MoveLookControllerState::Active:
switch (pointerDeviceType)
{
case Windows::Devices::Input::PointerDeviceType::Touch:
if (position.x > m_moveUpperLeft.x &&
position.x < m_moveLowerRight.x &&
position.y > m_moveUpperLeft.y &&
position.y < m_moveLowerRight.y)
{
// This pointer is in the move control.
if (!m_moveInUse)
{
// There is not an active pointer in this control yet.
// Process a DPad touch down event.
m_moveFirstDown = position; // Save location of initial contact.
m_movePointerID = pointerID; // Store the pointer using this control.
m_moveInUse = true;
}
}
else if (position.x > m_fireUpperLeft.x &&
position.x < m_fireLowerRight.x &&
position.y > m_fireUpperLeft.y &&
position.y < m_fireLowerRight.y)
{
// This pointer is in the fire control.
if (!m_fireInUse)
{
m_fireLastPoint = position;
m_firePointerID = pointerID;
m_fireInUse = true;
if (!m_autoFire)
{
m_firePressed = true;
}
}
}
else
{
if (!m_lookInUse)
{
// There is not an active pointer in this control yet.
m_lookLastPoint = position; // Save point for later move.
m_lookPointerID = pointerID; // Store the pointer using this control.
m_lookLastDelta.x = m_lookLastDelta.y = 0; // These are for smoothing.
m_lookInUse = true;
}
}
break;
default:
bool rightButton = pointProperties->IsRightButtonPressed;
bool leftButton = pointProperties->IsLeftButtonPressed;
if (!m_autoFire && (!m_mouseLeftInUse && leftButton))
{
m_firePressed = true;
}
if (!m_mouseInUse)
{
m_mouseInUse = true;
m_mouseLastPoint = position;
m_mousePointerID = pointerID;
m_mouseLeftInUse = leftButton;
m_mouseRightInUse = rightButton;
m_lookLastDelta.x = m_lookLastDelta.y = 0; // These are for smoothing.
}
else
{
#ifdef MOVELOOKCONTROLLER_TRACE
DebugTrace(L"\tWARNING: OnPointerPressed() Mouse aleady in use (%d-%s%s) and new event id: %d %s%s",
m_mousePointerID,
m_mouseLeftInUse ? "L" : "",
m_mouseRightInUse ? "R" : "",
pointerID,
leftButton ? "L" : "",
rightButton ? "R" : ""
);
#endif
}
break;
}
#ifdef MOVELOOKCONTROLLER_TRACE
DebugTrace(
L"\t%s%s%s %s%s%s",
m_moveInUse ? L"Move " : L"",
m_lookInUse ? L"Look " : L"",
m_fireInUse ? L"Fire " : L"",
m_mouseInUse ? L"Mouse:" : L"",
m_mouseLeftInUse ? L"L" : L"-",
m_mouseRightInUse ? L"R" : L"-"
);
#endif
break;
}
#ifdef MOVELOOKCONTROLLER_TRACE
DebugTrace(L"\n");
#endif
return;
}
//----------------------------------------------------------------------
void MoveLookController::OnPointerMoved(
_In_ CoreWindow^ /* sender */,
_In_ PointerEventArgs^ args
)
{
PointerPoint^ point = args->CurrentPoint;
uint32 pointerID = point->PointerId;
Point pointerPosition = point->Position;
PointerPointProperties^ pointProperties = point->Properties;
auto pointerDevice = point->PointerDevice;
XMFLOAT2 position = XMFLOAT2(pointerPosition.X, pointerPosition.Y); // convert to allow math
#ifdef MOVELOOKCONTROLLER_TRACE
DebugTrace(L"%-7s (%d) at (%4.0f, %4.0f)", L"Moved", pointerID, position.x, position.y);
#endif
switch (m_state)
{
case MoveLookControllerState::Active:
// Decide which control this pointer is operating.
if (pointerID == m_movePointerID)
{
m_movePointerPosition = position; // Save the current position.
}
else if (pointerID == m_lookPointerID)
{
// Look control.
XMFLOAT2 pointerDelta;
pointerDelta.x = position.x - m_lookLastPoint.x; // How far did pointer move?
pointerDelta.y = position.y - m_lookLastPoint.y;
XMFLOAT2 rotationDelta;
rotationDelta.x = pointerDelta.x * ROTATION_GAIN; // Scale for control sensitivity.
rotationDelta.y = pointerDelta.y * ROTATION_GAIN;
m_lookLastPoint = position; // Save for next time through.
#ifdef MOVELOOKCONTROLLER_TRACE
DebugTrace(L"\tDelta (%4.0f, %4.0f)", pointerDelta.x, pointerDelta.y);
#endif
// Update our orientation based on the command.
m_pitch -= rotationDelta.y;
m_yaw += rotationDelta.x;
// Limit pitch to straight up or straight down.
float limit = XM_PI / 2.0f - 0.01f;
m_pitch = __max(-limit, m_pitch);
m_pitch = __min(+limit, m_pitch);
// Keep longitude in sane range by wrapping.
if (m_yaw > XM_PI)
{
m_yaw -= XM_PI * 2.0f;
}
else if (m_yaw < -XM_PI)
{
m_yaw += XM_PI * 2.0f;
}
}
else if (pointerID == m_firePointerID)
{
m_fireLastPoint = position;
}
else if (pointerID == m_mousePointerID)
{
m_mouseLeftInUse = pointProperties->IsLeftButtonPressed;
m_mouseRightInUse = pointProperties->IsRightButtonPressed;
m_mouseLastPoint = position; // Save for next time through.
}
// Mouse movement is handled via a separate relative mouse movement handler (OnMouseMoved).
break;
}
#ifdef MOVELOOKCONTROLLER_TRACE
DebugTrace(L"\n");
#endif
}
//----------------------------------------------------------------------
void MoveLookController::OnMouseMoved(
_In_ MouseDevice^ /* mouseDevice */,
_In_ MouseEventArgs^ args
)
{
// Handle Mouse Input via dedicated relative movement handler.
switch (m_state)
{
case MoveLookControllerState::Active:
XMFLOAT2 mouseDelta;
mouseDelta.x = static_cast<float>(args->MouseDelta.X);
mouseDelta.y = static_cast<float>(args->MouseDelta.Y);
XMFLOAT2 rotationDelta;
rotationDelta.x = mouseDelta.x * ROTATION_GAIN; // Scale for control sensitivity.
rotationDelta.y = mouseDelta.y * ROTATION_GAIN;
// Update our orientation based on the command.
m_pitch -= rotationDelta.y;
m_yaw += rotationDelta.x;
// Limit pitch to straight up or straight down.
float limit = XM_PI / 2.0f - 0.01f;
m_pitch = __max(-limit, m_pitch);
m_pitch = __min(+limit, m_pitch);
// Keep longitude in sane range by wrapping.
if (m_yaw > XM_PI)
{
m_yaw -= XM_PI * 2.0f;
}
else if (m_yaw < -XM_PI)
{
m_yaw += XM_PI * 2.0f;
}
break;
}
}
//----------------------------------------------------------------------
void MoveLookController::OnPointerReleased(
_In_ CoreWindow^ /* sender */,
_In_ PointerEventArgs^ args
)
{
PointerPoint^ point = args->CurrentPoint;
uint32 pointerID = point->PointerId;
Point pointerPosition = point->Position;
PointerPointProperties^ pointProperties = point->Properties;
XMFLOAT2 position = XMFLOAT2(pointerPosition.X, pointerPosition.Y);
#ifdef MOVELOOKCONTROLLER_TRACE
DebugTrace(L"%-7s (%d) at (%4.0f, %4.0f)\n", L"Release", pointerID, position.x, position.y);
#endif
switch (m_state)
{
case MoveLookControllerState::WaitForInput:
if (m_buttonInUse && (pointerID == m_buttonPointerID))
{
m_buttonInUse = false;
m_buttonPressed = true;
#ifdef MOVELOOKCONTROLLER_TRACE
DebugTrace(L"\tWaitForInput: ButtonPressed = true\n");
#endif
}
break;
case MoveLookControllerState::Active:
if (pointerID == m_movePointerID)
{
m_velocity = XMFLOAT3(0, 0, 0); // Stop on release.
m_moveInUse = false;
m_movePointerID = 0;
}
else if (pointerID == m_lookPointerID)
{
m_lookInUse = false;
m_lookPointerID = 0;
}
else if (pointerID == m_firePointerID)
{
m_fireInUse = false;
m_firePointerID = 0;
}
else if (pointerID == m_mousePointerID)
{
bool rightButton = pointProperties->IsRightButtonPressed;
bool leftButton = pointProperties->IsLeftButtonPressed;
m_mouseInUse = false;
// Don't clear the mouse Pointer ID so that Move events still result in Look changes.
m_mouseLeftInUse = leftButton;
m_mouseRightInUse = rightButton;
}
break;
}
}
//----------------------------------------------------------------------
void MoveLookController::OnPointerExited(
_In_ CoreWindow^ /* sender */,
_In_ PointerEventArgs^ args
)
{
PointerPoint^ point = args->CurrentPoint;
uint32 pointerID = point->PointerId;
Point pointerPosition = point->Position;
PointerPointProperties^ pointProperties = point->Properties;
XMFLOAT2 position = XMFLOAT2(pointerPosition.X, pointerPosition.Y);
#ifdef MOVELOOKCONTROLLER_TRACE
DebugTrace(L"%-7s (%d) at (%4.0f, %4.0f)\n", L"Exit", pointerID, position.x, position.y);
#endif
switch (m_state)
{
case MoveLookControllerState::WaitForInput:
if (m_buttonInUse && (pointerID == m_buttonPointerID))
{
m_buttonInUse = false;
m_buttonPressed = false;
#ifdef MOVELOOKCONTROLLER_TRACE
DebugTrace(L"\tWaitForPress: ButtonPressed = false -- Got Exit Event\n");
#endif
}
break;
case MoveLookControllerState::Active:
if (pointerID == m_movePointerID)
{
m_velocity = XMFLOAT3(0, 0, 0); // Stop on release.
m_moveInUse = false;
m_movePointerID = 0;
}
else if (pointerID == m_lookPointerID)
{
m_lookInUse = false;
m_lookPointerID = 0;
}
else if (pointerID == m_firePointerID)
{
m_fireInUse = false;
m_firePointerID = 0;
}
else if (pointerID == m_mousePointerID)
{
m_mouseInUse = false;
m_mousePointerID = 0;
m_mouseLeftInUse = false;
m_mouseRightInUse = false;
}
break;
}
}
//----------------------------------------------------------------------
void MoveLookController::OnKeyDown(
_In_ CoreWindow^ /* sender */,
_In_ KeyEventArgs^ args
)
{
Windows::System::VirtualKey Key;
Key = args->VirtualKey;
// Figure out the command from the keyboard.
if (Key == VirtualKey::W)
m_forward = true;
if (Key == VirtualKey::S)
m_back = true;
if (Key == VirtualKey::A)
m_left = true;
if (Key == VirtualKey::D)
m_right = true;
if (Key == VirtualKey::Space)
m_up = true;
if (Key == VirtualKey::X)
m_down = true;
if (Key == VirtualKey::P)
m_pause = true;
}
//----------------------------------------------------------------------
void MoveLookController::OnKeyUp(
_In_ CoreWindow^ /* sender */,
_In_ KeyEventArgs^ args
)
{
Windows::System::VirtualKey Key;
Key = args->VirtualKey;
// Figure out the command from the keyboard.
if (Key == VirtualKey::W)
m_forward = false;
if (Key == VirtualKey::S)
m_back = false;
if (Key == VirtualKey::A)
m_left = false;
if (Key == VirtualKey::D)
m_right = false;
if (Key == VirtualKey::Space)
m_up = false;
if (Key == VirtualKey::X)
m_down = false;
if (Key == VirtualKey::P)
{
if (m_pause)
{
// Trigger pause only once on button release.
m_pausePressed = true;
m_pause = false;
}
}
}
//----------------------------------------------------------------------
void MoveLookController::ResetState()
{
// Reset the state of the controller.
// Disable any active pointer IDs to stop all interaction.
m_buttonPressed = false;
m_pausePressed = false;
m_buttonInUse = false;
m_moveInUse = false;
m_lookInUse = false;
m_fireInUse = false;
m_mouseInUse = false;
m_mouseLeftInUse = false;
m_mouseRightInUse = false;
m_movePointerID = 0;
m_lookPointerID = 0;
m_firePointerID = 0;
m_mousePointerID = 0;
m_velocity = XMFLOAT3(0.0f, 0.0f, 0.0f);
m_xinputStartButtonInUse = false;
m_xinputTriggerInUse = false;
m_moveCommand = XMFLOAT3(0.0f, 0.0f, 0.0f);
m_forward = false;
m_back = false;
m_left = false;
m_right = false;
m_up = false;
m_down = false;
m_pause = false;
}
//----------------------------------------------------------------------
void MoveLookController::SetMoveRect (
_In_ XMFLOAT2 upperLeft,
_In_ XMFLOAT2 lowerRight
)
{
#ifdef MOVELOOKCONTROLLER_TRACE
DebugTrace(L"MoveRect (%d, %d) to (%d, %d)", upperLeft.x, upperLeft.y, lowerRight.x, lowerRight.y);
#endif
m_moveUpperLeft = upperLeft;
m_moveLowerRight = lowerRight;
}
//----------------------------------------------------------------------
void MoveLookController::SetFireRect (
_In_ XMFLOAT2 upperLeft,
_In_ XMFLOAT2 lowerRight
)
{
#ifdef MOVELOOKCONTROLLER_TRACE
DebugTrace(L"FireRect (%d, %d) to (%d, %d)", upperLeft.x, upperLeft.y, lowerRight.x, lowerRight.y);
#endif
m_fireUpperLeft = upperLeft;
m_fireLowerRight = lowerRight;
}
//----------------------------------------------------------------------
void MoveLookController::WaitForPress(
_In_ XMFLOAT2 upperLeft,
_In_ XMFLOAT2 lowerRight
)
{
#ifdef MOVELOOKCONTROLLER_TRACE
DebugTrace(L"WaitForPress (%.1f, %.1f) to (%.1f, %.1f)\n", upperLeft.x, upperLeft.y, lowerRight.x, lowerRight.y);
#endif
ResetState();
m_state = MoveLookControllerState::WaitForInput;
m_buttonUpperLeft = upperLeft;
m_buttonLowerRight = lowerRight;
// Turn on mouse cursor.
CoreWindow::GetForCurrentThread()->PointerCursor = ref new CoreCursor(CoreCursorType::Arrow, 0);
}
//----------------------------------------------------------------------
void MoveLookController::WaitForPress()
{
#ifdef MOVELOOKCONTROLLER_TRACE
DebugTrace(L"WaitForPress (null rect)\n");
#endif
ResetState();
m_state = MoveLookControllerState::WaitForInput;
m_buttonUpperLeft.x = 0.0f;
m_buttonUpperLeft.y = 0.0f;
m_buttonLowerRight.x = 0.0f;
m_buttonLowerRight.y = 0.0f;
// Turn on mouse cursor.
CoreWindow::GetForCurrentThread()->PointerCursor = ref new CoreCursor(CoreCursorType::Arrow, 0);
}
//----------------------------------------------------------------------
XMFLOAT3 MoveLookController::Velocity()
{
return m_velocity;
}
//----------------------------------------------------------------------
XMFLOAT3 MoveLookController::LookDirection()
{
XMFLOAT3 lookDirection;
float r = cosf(m_pitch); // In the plane.
lookDirection.y = sinf(m_pitch); // Vertical.
lookDirection.z = r * cosf(m_yaw); // Fwd-back.
lookDirection.x = r * sinf(m_yaw); // Left-right.
return lookDirection;
}
//----------------------------------------------------------------------
float MoveLookController::Pitch()
{
return m_pitch;
}
//----------------------------------------------------------------------
void MoveLookController::Pitch(_In_ float pitch)
{
m_pitch = pitch;
}
//----------------------------------------------------------------------
float MoveLookController::Yaw()
{
return m_yaw;
}
//----------------------------------------------------------------------
void MoveLookController::Yaw(_In_ float yaw)
{
m_yaw = yaw;
}
//----------------------------------------------------------------------
void MoveLookController::Active(_In_ bool active)
{
ResetState();
#ifdef MOVELOOKCONTROLLER_TRACE
DebugTrace(L"Active = %s\n", active ? L"true" : L"false");
#endif
if (active)
{
m_state = MoveLookControllerState::Active;
// Turn mouse cursor off (hidden).
//CoreWindow::GetForCurrentThread()->PointerCursor = nullptr;
}
else
{
m_state = MoveLookControllerState::None;
// Turn mouse cursor on.
auto window = CoreWindow::GetForCurrentThread();
if (window)
{
// Protect case where there isn't a window associated with the current thread.
// This happens on initialization.
window->PointerCursor = ref new CoreCursor(CoreCursorType::Arrow, 0);
}
}
}
//----------------------------------------------------------------------
bool MoveLookController::Active()
{
if (m_state == MoveLookControllerState::Active)
{
return true;
}
else
{
return false;
}
}
//----------------------------------------------------------------------
void MoveLookController::AutoFire(_In_ bool autoFire)
{
m_autoFire = autoFire;
}
//----------------------------------------------------------------------
bool MoveLookController::AutoFire()
{
return m_autoFire;
}
//----------------------------------------------------------------------
void MoveLookController::Update()
{
UpdateGameController();
if (m_moveInUse)
{
// Move control.
XMFLOAT2 pointerDelta;
pointerDelta.x = m_movePointerPosition.x - m_moveFirstDown.x;
pointerDelta.y = m_movePointerPosition.y - m_moveFirstDown.y;
// Figure out the command from the virtual joystick.
XMFLOAT3 commandDirection = XMFLOAT3(0.0f, 0.0f, 0.0f);
if (fabsf(pointerDelta.x) > 16.0f) // Leave 32 pixel-wide dead spot for being still.
m_moveCommand.x -= pointerDelta.x/fabsf(pointerDelta.x);
if (fabsf(pointerDelta.y) > 16.0f)
m_moveCommand.y -= pointerDelta.y/fabsf(pointerDelta.y);
}
// Poll our state bits set by the keyboard input events.
if (m_forward)
{
m_moveCommand.y += 1.0f;
}
if (m_back)
{
m_moveCommand.y -= 1.0f;
}
if (m_left)
{
m_moveCommand.x += 1.0f;
}
if (m_right)
{
m_moveCommand.x -= 1.0f;
}
if (m_up)
{
m_moveCommand.z += 1.0f;
}
if (m_down)
{
m_moveCommand.z -= 1.0f;
}
// Make sure that 45deg cases are not faster.
if (fabsf(m_moveCommand.x) > 0.1f ||
fabsf(m_moveCommand.y) > 0.1f ||
fabsf(m_moveCommand.z) > 0.1f)
{
XMStoreFloat3(&m_moveCommand, XMVector3Normalize(XMLoadFloat3(&m_moveCommand)));
}
// Rotate command to align with our direction (world coordinates).
XMFLOAT3 wCommand;
wCommand.x = m_moveCommand.x * cosf(m_yaw) - m_moveCommand.y * sinf(m_yaw);
wCommand.y = m_moveCommand.x * sinf(m_yaw) + m_moveCommand.y * cosf(m_yaw);
wCommand.z = m_moveCommand.z;
// Scale for sensitivity adjustment.
// Our velocity is based on the command. Y is up.
m_velocity.x = -wCommand.x * MOVEMENT_GAIN;
m_velocity.z = wCommand.y * MOVEMENT_GAIN;
m_velocity.y = wCommand.z * MOVEMENT_GAIN;
// Clear movement input accumulator for use during next frame.
m_moveCommand = XMFLOAT3(0.0f, 0.0f, 0.0f);
}
//----------------------------------------------------------------------
void MoveLookController::UpdateGameController()
{
if (!m_isControllerConnected)
{
// Check for controller connection by trying to get the capabilties.
DWORD capsResult = XInputGetCapabilities(0, XINPUT_FLAG_GAMEPAD, &m_xinputCaps);
if (capsResult != ERROR_SUCCESS)
{
return;
}
// Device is connected.
m_isControllerConnected = true;
m_xinputStartButtonInUse = false;
m_xinputTriggerInUse = false;
}
DWORD stateResult = XInputGetState(0, &m_xinputState);
if (stateResult != ERROR_SUCCESS)
{
// Device is no longer connected.
m_isControllerConnected = false;
}
switch (m_state)
{
case MoveLookControllerState::WaitForInput:
if (m_xinputState.Gamepad.wButtons & XINPUT_GAMEPAD_START)
{
m_xinputStartButtonInUse = true;
}
else if (m_xinputStartButtonInUse)
{
// Trigger once only on button release.
m_xinputStartButtonInUse = false;
m_buttonPressed = true;
}
break;
case MoveLookControllerState::Active:
if (m_xinputState.Gamepad.wButtons & XINPUT_GAMEPAD_START)
{
m_xinputStartButtonInUse = true;
}
else if (m_xinputStartButtonInUse)
{
// Trigger once only on button release.
m_xinputStartButtonInUse = false;
m_pausePressed = true;
}
// Use the Left Thumb joystick on the Xbox controller to control
// the eye point position control.
// The controller input goes from -32767 to 32767. We will normalize
// this from -1 to 1 and keep a dead spot in the middle to avoid drift.
if (m_xinputState.Gamepad.sThumbLX > XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE ||
m_xinputState.Gamepad.sThumbLX < -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE)
{
float x = (float)m_xinputState.Gamepad.sThumbLX/32767.0f;
m_moveCommand.x -= x / fabsf(x);
}
if (m_xinputState.Gamepad.sThumbLY > XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE ||
m_xinputState.Gamepad.sThumbLY < -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE)
{
float y = (float)m_xinputState.Gamepad.sThumbLY/32767.0f;
m_moveCommand.y += y / fabsf(y);
}
// Use the Right Thumb Joystick on the Xbox controller to control
// the look at control.
// The controller input goes from -32767 to 32767. We will normalize
// this from -1 to 1 and keep a dead spot in the middle to avoid drift.
XMFLOAT2 pointerDelta;
if (m_xinputState.Gamepad.sThumbRX > XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE ||
m_xinputState.Gamepad.sThumbRX < -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE)
{
pointerDelta.x = (float)m_xinputState.Gamepad.sThumbRX/32767.0f;
pointerDelta.x = pointerDelta.x * pointerDelta.x * pointerDelta.x;
}
else
{
pointerDelta.x = 0.0f;
}
if (m_xinputState.Gamepad.sThumbRY > XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE ||
m_xinputState.Gamepad.sThumbRY < -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE)
{
pointerDelta.y = (float)m_xinputState.Gamepad.sThumbRY/32767.0f;
pointerDelta.y = pointerDelta.y * pointerDelta.y * pointerDelta.y;
}
else
{
pointerDelta.y = 0.0f;
}
XMFLOAT2 rotationDelta;
rotationDelta.x = pointerDelta.x * 0.08f; // Scale for control sensitivity.
rotationDelta.y = pointerDelta.y * 0.08f;
// Update our orientation based on the command.
m_pitch += rotationDelta.y;
m_yaw += rotationDelta.x;
// Limit pitch to straight up or straight down.
m_pitch = __max(-XM_PI / 2.0f, m_pitch);
m_pitch = __min(+XM_PI / 2.0f, m_pitch);
// Check the state of the Right Trigger button. This is used to indicate fire control.
if (m_xinputState.Gamepad.bRightTrigger > XINPUT_GAMEPAD_TRIGGER_THRESHOLD)
{
if (!m_autoFire && !m_xinputTriggerInUse)
{
m_firePressed = true;
}
m_xinputTriggerInUse = true;
}
else
{
m_xinputTriggerInUse = false;
}
break;
}
}
//----------------------------------------------------------------------
#ifdef MOVELOOKCONTROLLER_TRACE
void MoveLookController::DebugTrace(const wchar_t *format, ...)
{
// Generate the message string.
va_list args;
va_start(args, format);
wchar_t message[1024];
vswprintf_s(message, 1024, format, args);