Chesto 0.9
A declarative and element-based library for creating GUIs on homebrew'd consoles
InputEvents.hpp
1#pragma once
2
3#include <SDL2/SDL.h>
4
5#if !defined(_3DS) && !defined(_3DS_MOCK)
6// 3ds has no sdl keycodes
7typedef SDL_Keycode CST_Keycode;
8#else
9typedef int CST_Keycode;
10#endif
11
12typedef uint16_t CST_Keymod;
13
14#include <functional>
15#include <string>
16
17#define TOTAL_BUTTONS 18
18
19// clang-format off
20#define LEFT_BUTTON 0b00000000000001
21#define RIGHT_BUTTON 0b00000000000010
22#define UP_BUTTON 0b00000000000100
23#define DOWN_BUTTON 0b00000000001000
24#define START_BUTTON 0b00000000010000
25#define B_BUTTON 0b00000000100000
26#define A_BUTTON 0b00000001000000
27#define ZL_BUTTON 0b00000010000000
28#define SELECT_BUTTON 0b00000100000000
29#define L_BUTTON 0b00001000000000
30#define R_BUTTON 0b00010000000000
31#define X_BUTTON 0b00100000000000
32#define Y_BUTTON 0b01000000000000
33#define ZR_BUTTON 0b10000000000000
34
35// SDL enums should line up with the actual controls
36// 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
37#define SDL_A SDL_CONTROLLER_BUTTON_A
38#define SDL_B SDL_CONTROLLER_BUTTON_B
39#define SDL_X SDL_CONTROLLER_BUTTON_X
40#define SDL_Y SDL_CONTROLLER_BUTTON_Y
41
42#define SDL_PLUS SDL_CONTROLLER_BUTTON_RIGHTSHOULDER
43#define SDL_L SDL_CONTROLLER_BUTTON_START
44#define SDL_R SDL_CONTROLLER_BUTTON_LEFTSTICK
45#define SDL_ZL SDL_CONTROLLER_BUTTON_RIGHTSTICK
46#define SDL_ZR SDL_CONTROLLER_BUTTON_LEFTSHOULDER
47#define SDL_MINUS SDL_CONTROLLER_BUTTON_DPAD_UP
48
49#define SDL_UP SDL_CONTROLLER_BUTTON_DPAD_LEFT
50#define SDL_DOWN SDL_CONTROLLER_BUTTON_MISC1
51#define SDL_LEFT SDL_CONTROLLER_BUTTON_DPAD_DOWN
52#define SDL_RIGHT SDL_CONTROLLER_BUTTON_DPAD_RIGHT
53
54#define SDL_LEFT_STICK (SDL_GameControllerButton)16
55#define SDL_UP_STICK (SDL_GameControllerButton)17
56#define SDL_RIGHT_STICK (SDL_GameControllerButton)18
57#define SDL_DOWN_STICK (SDL_GameControllerButton)19
58// clang-format on
59
61 unsigned int* buttons;
62 std::string* names;
63 std::string prefix;
64 std::string controller_type;
65
66public:
67 GamepadInfo(unsigned int* buttons, std::string* names, std::string prefix, std::string controller_type)
68 : buttons(buttons), names(names), prefix(prefix), controller_type(controller_type) {}
70 : buttons(nullptr), names(nullptr), prefix(""), controller_type("")
71 {
72 }
73};
74
76{
77public:
79
81 bool held(int buttons);
82 bool pressed(int buttons);
83 bool released(int buttons);
84
86 bool touchIn(int x, int width, int y, int height);
87
89 bool processSDLEvents();
90 bool update();
91
92 bool allowTouch = true;
93 bool isScrolling = false;
94
95 // whether or not the current event is one of a few known ones
96 bool isTouchDown();
97 bool isTouchUp();
98 bool isTouchDrag();
99 bool isTouch();
100
101 bool isScroll();
102 bool isKeyDown();
103 bool isKeyUp();
104
105 // additional key processing info
106 bool processDirectionalButtons();
107 int directionForKeycode();
108 void toggleHeldButtons();
109
111 void processJoystickHotplugging(SDL_Event *event);
112
113 CST_Keycode keyCode = -1;
114 CST_Keymod mod = -1;
115 SDL_Event event; // underlying SDL event
116
117 bool held_directions[4] = { false, false, false, false };
118 Uint32 held_type;
119
120 int rapidFireRate = 12; // fire duplicate events if curframe mod rapidFireRate is 0 (higher = slower)
121 int curFrame = 0;
122
123 static bool bypassKeyEvents;
124 static GamepadInfo& getLastGamepadInfo();
125 static std::string lastGamepadKey;
126
127 std::function<void()> quitaction = NULL; //Called for an SDL_Quit event, usually caused by a SIGINT
128
129 float wheelScroll = 0;
130
131 int yPos = 0;
132 int xPos = 0;
133 bool noop = false;
134
135 Uint32 type;
136};
bool touchIn(int x, int width, int y, int height)
whether or not a touch is detected within the specified rect in this cycle
bool processSDLEvents()
update which buttons are pressed
Definition: InputEvents.cpp:75
bool held(int buttons)
whether or not a button is pressed during this cycle
void processJoystickHotplugging(SDL_Event *event)
joystick device events processing