-
Notifications
You must be signed in to change notification settings - Fork 4
/
InputEvents.hpp
140 lines (111 loc) · 3.94 KB
/
InputEvents.hpp
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
#pragma once
#include <SDL2/SDL.h>
#if !defined(_3DS) && !defined(_3DS_MOCK)
// 3ds has no sdl keycodes
typedef SDL_Keycode CST_Keycode;
#else
typedef int CST_Keycode;
#endif
typedef uint16_t CST_Keymod;
#include <functional>
#include <string>
#define TOTAL_BUTTONS 18
// clang-format off
#define LEFT_BUTTON 0b00000000000001
#define RIGHT_BUTTON 0b00000000000010
#define UP_BUTTON 0b00000000000100
#define DOWN_BUTTON 0b00000000001000
#define START_BUTTON 0b00000000010000
#define B_BUTTON 0b00000000100000
#define A_BUTTON 0b00000001000000
#define ZL_BUTTON 0b00000010000000
#define SELECT_BUTTON 0b00000100000000
#define L_BUTTON 0b00001000000000
#define R_BUTTON 0b00010000000000
#define X_BUTTON 0b00100000000000
#define Y_BUTTON 0b01000000000000
#define ZR_BUTTON 0b10000000000000
// SDL enums should line up with the actual controls
// uses switch+wiiu mappings, see: https://github.com/rw-r-r-0644/sdl2-wiiu/blob/master/SDL2-wiiu/src/joystick/wiiu/SDL_sysjoystick.c#L38
#define SDL_A SDL_CONTROLLER_BUTTON_A
#define SDL_B SDL_CONTROLLER_BUTTON_B
#define SDL_X SDL_CONTROLLER_BUTTON_X
#define SDL_Y SDL_CONTROLLER_BUTTON_Y
#define SDL_PLUS SDL_CONTROLLER_BUTTON_RIGHTSHOULDER
#define SDL_L SDL_CONTROLLER_BUTTON_START
#define SDL_R SDL_CONTROLLER_BUTTON_LEFTSTICK
#define SDL_ZL SDL_CONTROLLER_BUTTON_RIGHTSTICK
#define SDL_ZR SDL_CONTROLLER_BUTTON_LEFTSHOULDER
#define SDL_MINUS SDL_CONTROLLER_BUTTON_DPAD_UP
#define SDL_UP SDL_CONTROLLER_BUTTON_DPAD_LEFT
#ifdef __WIIU__
#define SDL_DOWN SDL_CONTROLLER_BUTTON_MAX
#else
#define SDL_DOWN SDL_CONTROLLER_BUTTON_MISC1
#endif
#define SDL_LEFT SDL_CONTROLLER_BUTTON_DPAD_DOWN
#define SDL_RIGHT SDL_CONTROLLER_BUTTON_DPAD_RIGHT
#define SDL_LEFT_STICK (SDL_GameControllerButton)16
#define SDL_UP_STICK (SDL_GameControllerButton)17
#define SDL_RIGHT_STICK (SDL_GameControllerButton)18
#define SDL_DOWN_STICK (SDL_GameControllerButton)19
// clang-format on
struct GamepadInfo {
unsigned int* buttons;
std::string* names;
std::string prefix;
std::string controller_type;
public:
GamepadInfo(unsigned int* buttons, std::string* names, std::string prefix, std::string controller_type)
: buttons(buttons), names(names), prefix(prefix), controller_type(controller_type) {}
GamepadInfo()
: buttons(nullptr), names(nullptr), prefix(""), controller_type("")
{
}
};
class InputEvents
{
public:
InputEvents();
/// whether or not a button is pressed during this cycle
bool held(int buttons);
bool pressed(int buttons);
bool released(int buttons);
/// whether or not a touch is detected within the specified rect in this cycle
bool touchIn(int x, int width, int y, int height);
/// update which buttons are pressed
bool processSDLEvents();
bool update();
bool allowTouch = true;
bool isScrolling = false;
// whether or not the current event is one of a few known ones
bool isTouchDown();
bool isTouchUp();
bool isTouchDrag();
bool isTouch();
bool isScroll();
bool isKeyDown();
bool isKeyUp();
// additional key processing info
bool processDirectionalButtons();
int directionForKeycode();
void toggleHeldButtons();
/// joystick device events processing
void processJoystickHotplugging(SDL_Event *event);
CST_Keycode keyCode = -1;
CST_Keymod mod = -1;
SDL_Event event; // underlying SDL event
bool held_directions[4] = { false, false, false, false };
Uint32 held_type;
int rapidFireRate = 12; // fire duplicate events if curframe mod rapidFireRate is 0 (higher = slower)
int curFrame = 0;
static bool bypassKeyEvents;
static GamepadInfo& getLastGamepadInfo();
static std::string lastGamepadKey;
std::function<void()> quitaction = NULL; //Called for an SDL_Quit event, usually caused by a SIGINT
float wheelScroll = 0;
int yPos = 0;
int xPos = 0;
bool noop = false;
Uint32 type;
};