Repository navigation
Expand file tree
/
Copy pathNintendoSwitch_VirtualControllerState.cpp
More file actions
140 lines (103 loc) · 3.23 KB
/
Copy pathNintendoSwitch_VirtualControllerState.cpp
File metadata and controls
140 lines (103 loc) · 3.23 KB
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
/* Virtual Controller State
*
* From: https://github.com/PokemonAutomation/
*
*/
//#include "Common/Cpp/Json/JsonObject.h"
#include "Procon/NintendoSwitch_ProControllerState.h"
#include "Joycon/NintendoSwitch_JoyconState.h"
#include "NintendoSwitch_VirtualControllerState.h"
//#include <iostream>
//using std::cout;
//using std::endl;
namespace PokemonAutomation{
namespace NintendoSwitch{
using namespace std::chrono_literals;
void ProControllerDeltas::operator+=(const ProControllerDeltas& x){
buttons |= x.buttons;
dpad_x += x.dpad_x;
dpad_y += x.dpad_y;
left_x += x.left_x;
left_y += x.left_y;
right_x += x.right_x;
right_y += x.right_y;
}
bool ProControllerDeltas::to_state(ProControllerState& state) const{
bool neutral;
state.buttons = buttons;
neutral = buttons == 0;
state.dpad = DPAD_NONE;
if (dpad_x != 0 || dpad_y != 0){
neutral = false;
do{
if (dpad_x == 0){
state.dpad = dpad_y > 0 ? DPAD_DOWN : DPAD_UP;
break;
}
if (dpad_y == 0){
state.dpad = dpad_x > 0 ? DPAD_RIGHT : DPAD_LEFT;
break;
}
if (dpad_x < 0 && dpad_y < 0){
state.dpad = DPAD_UP_LEFT;
break;
}
if (dpad_x < 0 && dpad_y > 0){
state.dpad = DPAD_DOWN_LEFT;
break;
}
if (dpad_x > 0 && dpad_y > 0){
state.dpad = DPAD_DOWN_RIGHT;
break;
}
if (dpad_x > 0 && dpad_y < 0){
state.dpad = DPAD_UP_RIGHT;
break;
}
}while (false);
}
state.left_joystick.x = 0;
state.left_joystick.y = 0;
if (left_x != 0 || left_y != 0){
neutral = false;
int mag = std::abs(left_x) > std::abs(left_y)
? std::abs(left_x)
: std::abs(left_y);
state.left_joystick.x = (double)left_x / mag;
state.left_joystick.y = -(double)left_y / mag;
}
state.right_joystick.x = 0;
state.right_joystick.y = 0;
if (right_x != 0 || right_y != 0){
neutral = false;
int mag = std::abs(right_x) > std::abs(right_y)
? std::abs(right_x)
: std::abs(right_y);
state.right_joystick.x = (double)right_x / mag;
state.right_joystick.y = -(double)right_y / mag;
}
return neutral;
}
void JoyconDeltas::operator+=(const JoyconDeltas& x){
buttons |= x.buttons;
joystick_x += x.joystick_x;
joystick_y += x.joystick_y;
}
bool JoyconDeltas::to_state(JoyconState& state) const{
bool neutral;
state.buttons = buttons;
neutral = buttons == 0;
state.joystick.x = 0;
state.joystick.y = 0;
if (joystick_x != 0 || joystick_y != 0){
neutral = false;
int mag = std::abs(joystick_x) > std::abs(joystick_y)
? std::abs(joystick_x)
: std::abs(joystick_y);
state.joystick.x = (double)joystick_x / mag;
state.joystick.y = -(double)joystick_y / mag;
}
return neutral;
}
}
}