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#ifdef __WIIU__
51#define SDL_DOWN SDL_CONTROLLER_BUTTON_MAX
52#else
53#define SDL_DOWN SDL_CONTROLLER_BUTTON_MISC1
54#endif
55#define SDL_LEFT SDL_CONTROLLER_BUTTON_DPAD_DOWN
56#define SDL_RIGHT SDL_CONTROLLER_BUTTON_DPAD_RIGHT
57
58#define SDL_LEFT_STICK (SDL_GameControllerButton)16
59#define SDL_UP_STICK (SDL_GameControllerButton)17
60#define SDL_RIGHT_STICK (SDL_GameControllerButton)18
61#define SDL_DOWN_STICK (SDL_GameControllerButton)19
62// clang-format on
63
65 unsigned int* buttons;
66 std::string* names;
67 std::string prefix;
68 std::string controller_type;
69
70public:
71 GamepadInfo(unsigned int* buttons, std::string* names, std::string prefix, std::string controller_type)
72 : buttons(buttons), names(names), prefix(prefix), controller_type(controller_type) {}
74 : buttons(nullptr), names(nullptr), prefix(""), controller_type("")
75 {
76 }
77};
78
80{
81public:
83
85 bool held(int buttons);
86 bool pressed(int buttons);
87 bool released(int buttons);
88
90 bool touchIn(int x, int width, int y, int height);
91
93 bool processSDLEvents();
94 bool update();
95
96 bool allowTouch = true;
97 bool isScrolling = false;
98
99 // whether or not the current event is one of a few known ones
100 bool isTouchDown();
101 bool isTouchUp();
102 bool isTouchDrag();
103 bool isTouch();
104
105 bool isScroll();
106 bool isKeyDown();
107 bool isKeyUp();
108
109 // additional key processing info
110 bool processDirectionalButtons();
111 int directionForKeycode();
112 void toggleHeldButtons();
113
115 void processJoystickHotplugging(SDL_Event *event);
116
117 CST_Keycode keyCode = -1;
118 CST_Keymod mod = -1;
119 SDL_Event event; // underlying SDL event
120
121 bool held_directions[4] = { false, false, false, false };
122 Uint32 held_type;
123
124 int rapidFireRate = 12; // fire duplicate events if curframe mod rapidFireRate is 0 (higher = slower)
125 int curFrame = 0;
126
127 static bool bypassKeyEvents;
128 static GamepadInfo& getLastGamepadInfo();
129 static std::string lastGamepadKey;
130
131 std::function<void()> quitaction = NULL; //Called for an SDL_Quit event, usually caused by a SIGINT
132
133 float wheelScroll = 0;
134
135 int yPos = 0;
136 int xPos = 0;
137 bool noop = false;
138
139 Uint32 type;
140};
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