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
60namespace Chesto {
61
63 unsigned int* buttons;
64 std::string* names;
65 std::string prefix;
66 std::string controller_type;
67
68public:
69 GamepadInfo(unsigned int* buttons, std::string* names, std::string prefix, std::string controller_type)
70 : buttons(buttons), names(names), prefix(prefix), controller_type(controller_type) {}
72 : buttons(nullptr), names(nullptr), prefix(""), controller_type("")
73 {
74 }
75};
76
78{
79public:
81
83 bool held(int buttons);
84 bool pressed(int buttons);
85 bool released(int buttons);
86
88 bool touchIn(int x, int width, int y, int height);
89
91 bool processSDLEvents();
92 bool update();
93
94 bool allowTouch = true;
95 bool isScrolling = false;
96
97 // whether or not the current event is one of a few known ones
98 bool isTouchDown();
99 bool isTouchUp();
100 bool isTouchDrag();
101 bool isTouch();
102
103 bool isScroll();
104 bool isKeyDown();
105 bool isKeyUp();
106
107 // additional key processing info
108 bool processDirectionalButtons();
109 int directionForKeycode();
110 void toggleHeldButtons();
111
113 void processJoystickHotplugging(SDL_Event *event);
114
115 CST_Keycode keyCode = -1;
116 CST_Keymod mod = -1;
117 SDL_Event event; // underlying SDL event
118
119 bool held_directions[4] = { false, false, false, false };
120 Uint32 held_type;
121
122 int rapidFireRate = 12; // fire duplicate events if curframe mod rapidFireRate is 0 (higher = slower)
123 int curFrame = 0;
124
125 static bool bypassKeyEvents;
126 static GamepadInfo& getLastGamepadInfo();
127 static std::string lastGamepadKey;
128
129 std::function<void()> quitaction = NULL; //Called for an SDL_Quit event, usually caused by a SIGINT
130
131 float wheelScroll = 0;
132
133 int yPos = 0;
134 int xPos = 0;
135 bool noop = false;
136
137 Uint32 type;
138};
139
140} // namespace Chesto
bool processSDLEvents()
update which buttons are pressed
Definition: InputEvents.cpp:79
bool touchIn(int x, int width, int y, int height)
whether or not a touch is detected within the specified rect in this cycle
void processJoystickHotplugging(SDL_Event *event)
joystick device events processing
bool held(int buttons)
whether or not a button is pressed during this cycle