Skip to content

Commit 6bcc9fd

Browse files
committed
Add gamepad input support
1 parent 2bbedbb commit 6bcc9fd

File tree

10 files changed

+1190
-164
lines changed

10 files changed

+1190
-164
lines changed

SerialPrograms/CMakeLists.txt

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -294,15 +294,18 @@ if (WIN32)
294294
MAP_IMPORTED_CONFIG_MINSIZEREL RELEASE
295295
)
296296

297-
target_link_libraries(
298-
SerialProgramsLib PRIVATE
299-
tesseractPA.lib
300-
OpenCV_lib
301-
ONNX_lib
302-
ONNX_Providers_lib
303-
dpp_lib
304-
discord_lib
305-
)
297+
target_link_libraries(
298+
SerialProgramsLib PRIVATE
299+
tesseractPA.lib
300+
OpenCV_lib
301+
ONNX_lib
302+
ONNX_Providers_lib
303+
dpp_lib
304+
discord_lib
305+
xinput
306+
dinput8
307+
dxguid
308+
)
306309

307310
target_compile_definitions(
308311
SerialProgramsLib PUBLIC
@@ -705,4 +708,4 @@ if (QT_MAJOR GREATER_EQUAL 6)
705708
endif()
706709

707710
# Add command-line executable (GUI-free) from subdirectory
708-
include(Source/CommandLine/CommandLineExecutable.cmake)
711+
include(Source/CommandLine/CommandLineExecutable.cmake)

SerialPrograms/Source/ControllerInput/ControllerInput.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
*
55
*/
66

7-
#include <vector>
8-
#include "ControllerInput.h"
9-
10-
#include "Keyboard/GlobalKeyboardHidTracker.h"
7+
#include <vector>
8+
#include "ControllerInput.h"
9+
10+
#include "Gamepad/GlobalGamepadTracker.h"
11+
#include "Keyboard/GlobalKeyboardHidTracker.h"
1112

1213
namespace PokemonAutomation{
1314

@@ -18,11 +19,12 @@ class GlobalInputSources{
1819
GlobalInputSources(){
1920
//
2021
// When we add more input types, add them here.
21-
//
22-
23-
m_input_sources.emplace_back(&global_keyboard_tracker());
24-
25-
}
22+
//
23+
24+
m_input_sources.emplace_back(&global_gamepad_tracker());
25+
m_input_sources.emplace_back(&global_keyboard_tracker());
26+
27+
}
2628

2729
void stop(){
2830
for (ControllerInputSource* source : m_input_sources){
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* Gamepad Input State
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#include "GamepadInput_State.h"
8+
9+
namespace PokemonAutomation{
10+
11+
12+
13+
14+
15+
}
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/* Gamepad Input State
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#ifndef PokemonAutomation_ControllerInput_Gamepad_GamepadInput_State_H
8+
#define PokemonAutomation_ControllerInput_Gamepad_GamepadInput_State_H
9+
10+
#include <stdint.h>
11+
#include "ControllerInput/ControllerInput.h"
12+
13+
namespace PokemonAutomation{
14+
15+
16+
17+
enum GamepadInputButton : uint32_t{
18+
GAMEPAD_BUTTON_NONE = 0,
19+
GAMEPAD_BUTTON_SOUTH = ((uint32_t)1 << 0), // A on Xbox, B on Switch.
20+
GAMEPAD_BUTTON_EAST = ((uint32_t)1 << 1), // B on Xbox, A on Switch.
21+
GAMEPAD_BUTTON_WEST = ((uint32_t)1 << 2), // X on Xbox, Y on Switch.
22+
GAMEPAD_BUTTON_NORTH = ((uint32_t)1 << 3), // Y on Xbox, X on Switch.
23+
GAMEPAD_BUTTON_DPAD_UP = ((uint32_t)1 << 4),
24+
GAMEPAD_BUTTON_DPAD_RIGHT = ((uint32_t)1 << 5),
25+
GAMEPAD_BUTTON_DPAD_DOWN = ((uint32_t)1 << 6),
26+
GAMEPAD_BUTTON_DPAD_LEFT = ((uint32_t)1 << 7),
27+
GAMEPAD_BUTTON_LEFT_SHOULDER = ((uint32_t)1 << 8),
28+
GAMEPAD_BUTTON_RIGHT_SHOULDER = ((uint32_t)1 << 9),
29+
GAMEPAD_BUTTON_LEFT_TRIGGER = ((uint32_t)1 << 10),
30+
GAMEPAD_BUTTON_RIGHT_TRIGGER = ((uint32_t)1 << 11),
31+
GAMEPAD_BUTTON_SELECT = ((uint32_t)1 << 12), // Back/View.
32+
GAMEPAD_BUTTON_START = ((uint32_t)1 << 13), // Start/Menu.
33+
GAMEPAD_BUTTON_LEFT_THUMB = ((uint32_t)1 << 14),
34+
GAMEPAD_BUTTON_RIGHT_THUMB = ((uint32_t)1 << 15),
35+
GAMEPAD_BUTTON_GUIDE = ((uint32_t)1 << 16), // Xbox logo button.
36+
};
37+
38+
inline constexpr GamepadInputButton empty_value(const GamepadInputButton*){
39+
return GAMEPAD_BUTTON_NONE;
40+
}
41+
inline constexpr bool is_empty(GamepadInputButton x){
42+
return x == GAMEPAD_BUTTON_NONE;
43+
}
44+
inline constexpr GamepadInputButton operator|(GamepadInputButton x, GamepadInputButton y){
45+
return (GamepadInputButton)((uint32_t)x | (uint32_t)y);
46+
}
47+
inline constexpr void operator|=(GamepadInputButton& x, GamepadInputButton y){
48+
x = (GamepadInputButton)((uint32_t)x | (uint32_t)y);
49+
}
50+
inline constexpr GamepadInputButton operator&(GamepadInputButton x, GamepadInputButton y){
51+
return (GamepadInputButton)((uint32_t)x & (uint32_t)y);
52+
}
53+
inline constexpr void operator&=(GamepadInputButton& x, GamepadInputButton y){
54+
x = (GamepadInputButton)((uint32_t)x & (uint32_t)y);
55+
}
56+
57+
58+
59+
60+
class GamepadInputState final : public ControllerInputState{
61+
public:
62+
GamepadInputState(GamepadInputState&& x)
63+
: ControllerInputState(ControllerInputType::StandardGamepad)
64+
, m_buttons(x.m_buttons)
65+
, m_left_x(x.m_left_x)
66+
, m_left_y(x.m_left_y)
67+
, m_right_x(x.m_right_x)
68+
, m_right_y(x.m_right_y)
69+
{}
70+
void operator=(GamepadInputState&& x){
71+
if (this == &x){
72+
return;
73+
}
74+
m_buttons = x.m_buttons;
75+
m_left_x = x.m_left_x;
76+
m_left_y = x.m_left_y;
77+
m_right_x = x.m_right_x;
78+
m_right_y = x.m_right_y;
79+
}
80+
81+
82+
public:
83+
GamepadInputState()
84+
: ControllerInputState(ControllerInputType::StandardGamepad)
85+
{}
86+
87+
88+
virtual void clear() override{
89+
m_buttons = GAMEPAD_BUTTON_NONE;
90+
m_left_x = 0;
91+
m_left_y = 0;
92+
m_right_x = 0;
93+
m_right_y = 0;
94+
}
95+
virtual bool is_neutral() const override{
96+
return m_buttons == GAMEPAD_BUTTON_NONE
97+
&& m_left_x == 0
98+
&& m_left_y == 0
99+
&& m_right_x == 0
100+
&& m_right_y == 0;
101+
}
102+
virtual bool operator==(const ControllerInputState& state) const override{
103+
const GamepadInputState* x = dynamic_cast<const GamepadInputState*>(&state);
104+
if (x == nullptr){
105+
return false;
106+
}
107+
return m_buttons == x->m_buttons
108+
&& m_left_x == x->m_left_x
109+
&& m_left_y == x->m_left_y
110+
&& m_right_x == x->m_right_x
111+
&& m_right_y == x->m_right_y;
112+
}
113+
114+
115+
public:
116+
GamepadInputButton buttons() const{
117+
return m_buttons;
118+
}
119+
bool button(GamepadInputButton button) const{
120+
return !is_empty(m_buttons & button);
121+
}
122+
123+
int16_t left_x() const{
124+
return m_left_x;
125+
}
126+
int16_t left_y() const{
127+
return m_left_y;
128+
}
129+
int16_t right_x() const{
130+
return m_right_x;
131+
}
132+
int16_t right_y() const{
133+
return m_right_y;
134+
}
135+
136+
void set_buttons(GamepadInputButton buttons){
137+
m_buttons = buttons;
138+
}
139+
void set_sticks(int16_t left_x, int16_t left_y, int16_t right_x, int16_t right_y){
140+
m_left_x = left_x;
141+
m_left_y = left_y;
142+
m_right_x = right_x;
143+
m_right_y = right_y;
144+
}
145+
void set(
146+
GamepadInputButton buttons,
147+
int16_t left_x, int16_t left_y,
148+
int16_t right_x, int16_t right_y
149+
){
150+
m_buttons = buttons;
151+
m_left_x = left_x;
152+
m_left_y = left_y;
153+
m_right_x = right_x;
154+
m_right_y = right_y;
155+
}
156+
157+
158+
private:
159+
GamepadInputButton m_buttons = GAMEPAD_BUTTON_NONE;
160+
int16_t m_left_x = 0;
161+
int16_t m_left_y = 0;
162+
int16_t m_right_x = 0;
163+
int16_t m_right_y = 0;
164+
};
165+
166+
167+
168+
169+
}
170+
#endif

0 commit comments

Comments
 (0)