Chesto 0.9
A declarative and element-based library for creating GUIs on homebrew'd consoles
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
InputEvents Class Reference
Collaboration diagram for InputEvents:
Collaboration graph
[legend]

Public Member Functions

bool held (int buttons)
 whether or not a button is pressed during this cycle More...
 
bool pressed (int buttons)
 
bool released (int buttons)
 
bool touchIn (int x, int width, int y, int height)
 whether or not a touch is detected within the specified rect in this cycle More...
 
bool processSDLEvents ()
 update which buttons are pressed More...
 
bool update ()
 
bool isTouchDown ()
 
bool isTouchUp ()
 
bool isTouchDrag ()
 
bool isTouch ()
 
bool isScroll ()
 
bool isKeyDown ()
 
bool isKeyUp ()
 
bool processDirectionalButtons ()
 
int directionForKeycode ()
 
void toggleHeldButtons ()
 
void processJoystickHotplugging (SDL_Event *event)
 joystick device events processing More...
 

Static Public Member Functions

static GamepadInfogetLastGamepadInfo ()
 

Public Attributes

bool allowTouch = true
 
bool isScrolling = false
 
CST_Keycode keyCode = -1
 
CST_Keymod mod = -1
 
SDL_Event event
 
bool held_directions [4] = { false, false, false, false }
 
Uint32 held_type
 
int rapidFireRate = 12
 
int curFrame = 0
 
std::function< void()> quitaction = NULL
 
float wheelScroll = 0
 
int yPos = 0
 
int xPos = 0
 
bool noop = false
 
Uint32 type
 

Static Public Attributes

static bool bypassKeyEvents = false
 
static std::string lastGamepadKey = defaultKeyName
 

Detailed Description

Definition at line 79 of file InputEvents.hpp.

Constructor & Destructor Documentation

◆ InputEvents()

InputEvents::InputEvents ( )

Definition at line 67 of file InputEvents.cpp.

68{
69#if defined(__WIIU__) && defined(USE_KEYBOARD)
70 // hook up keyboard events for wiiu and SDL (TODO: have these fired by SDL2 port itself)
71 KBWrapper* kbdwrapper = new KBWrapper(true, true);
72#endif
73}

Member Function Documentation

◆ directionForKeycode()

int InputEvents::directionForKeycode ( )

Definition at line 241 of file InputEvents.cpp.

242{
243 // this keycode overlaps with some other constants, so just return asap
244 if (this->type == SDL_KEYDOWN && this->keyCode == SDLK_RETURN)
245 return -1;
246
247 // returns 0 1 2 or 3 for up down left or right
248 switch (this->keyCode)
249 {
250 case SDL_UP_STICK:
251 case SDL_UP:
252 case SDLK_UP:
253 return 0;
254 case SDL_DOWN_STICK:
255 case SDL_DOWN:
256 case SDLK_DOWN:
257 return 1;
258 case SDL_LEFT_STICK:
259 case SDL_LEFT:
260 case SDLK_LEFT:
261 return 2;
262 case SDL_RIGHT_STICK:
263 case SDL_RIGHT:
264 case SDLK_RIGHT:
265 return 3;
266 default:
267 return -1;
268 }
269 return -1;
270}

◆ getLastGamepadInfo()

GamepadInfo & InputEvents::getLastGamepadInfo ( )
static

Definition at line 366 of file InputEvents.cpp.

367{
368 return gamepadMap[lastGamepadKey];
369}

◆ held()

bool InputEvents::held ( int  buttons)

whether or not a button is pressed during this cycle

Definition at line 272 of file InputEvents.cpp.

273{
274 // if it's a key event
275 if ((this->type == SDL_KEYDOWN || this->type == SDL_KEYUP) && !InputEvents::bypassKeyEvents)
276 {
277 for (int x = 0; x < TOTAL_BUTTONS; x++)
278 if (key_buttons[x] == keyCode && (buttons & currentButtons[x]))
279 return true;
280 }
281
282 // if it's a controller event
283 else if (this->type == SDL_JOYBUTTONDOWN || this->type == SDL_JOYBUTTONUP)
284 {
285 for (int x = 0; x < TOTAL_BUTTONS; x++)
286 if (pad_buttons[x] == keyCode && (buttons & currentButtons[x]))
287 return true;
288 }
289
290 return false;
291}

Referenced by Button::process(), and EKeyboard::process().

◆ isKeyDown()

bool InputEvents::isKeyDown ( )

Definition at line 333 of file InputEvents.cpp.

334{
335 return this->type == SDL_KEYDOWN || this->type == SDL_JOYBUTTONDOWN;
336}

◆ isKeyUp()

bool InputEvents::isKeyUp ( )

Definition at line 338 of file InputEvents.cpp.

339{
340 return this->type == SDL_KEYUP || this->type == SDL_JOYBUTTONUP;
341}

◆ isScroll()

bool InputEvents::isScroll ( )

Definition at line 328 of file InputEvents.cpp.

329{
330 return this->isScrolling;
331}

◆ isTouch()

bool InputEvents::isTouch ( )

Definition at line 323 of file InputEvents.cpp.

324{
325 return isTouchDown() || isTouchDrag() || isTouchUp();
326}

◆ isTouchDown()

bool InputEvents::isTouchDown ( )

Definition at line 308 of file InputEvents.cpp.

309{
310 return this->type == SDL_MOUSEBUTTONDOWN || (allowTouch && this->type == SDL_FINGERDOWN);
311}

◆ isTouchDrag()

bool InputEvents::isTouchDrag ( )

Definition at line 313 of file InputEvents.cpp.

314{
315 return this->type == SDL_MOUSEMOTION || (allowTouch && this->type == SDL_FINGERMOTION);
316}

◆ isTouchUp()

bool InputEvents::isTouchUp ( )

Definition at line 318 of file InputEvents.cpp.

319{
320 return this->type == SDL_MOUSEBUTTONUP || (allowTouch && this->type == SDL_FINGERUP);
321}

◆ pressed()

bool InputEvents::pressed ( int  buttons)

Definition at line 293 of file InputEvents.cpp.

294{
295 return isKeyDown() && held(buttons);
296}
bool held(int buttons)
whether or not a button is pressed during this cycle

◆ processDirectionalButtons()

bool InputEvents::processDirectionalButtons ( )

Definition at line 214 of file InputEvents.cpp.

215{
216 // up the counter
217 curFrame++;
218
219 // if one of the four direction keys is true, fire off repeat events for it
220 // (when rapidFire lines up only)
221 if (curFrame > 0 && curFrame % rapidFireRate == 0)
222 {
223 for (int x = 0; x < 4; x++)
224 {
225 if (!held_directions[x])
226 continue;
227
228 // send a corresponding directional event
229 this->type = held_type;
230 bool isGamepad = (this->type == SDL_JOYBUTTONDOWN || this->type == SDL_JOYBUTTONUP);
231 this->keyCode = isGamepad ? pad_buttons[4 + x] : key_buttons[4 + x]; // send up through right directions
232 this->noop = false;
233
234 return true;
235 }
236 }
237
238 return false;
239}

◆ processJoystickHotplugging()

void InputEvents::processJoystickHotplugging ( SDL_Event *  event)

joystick device events processing

Definition at line 343 of file InputEvents.cpp.

344{
345 SDL_Joystick *j;
346 switch(event->type)
347 {
348 case SDL_JOYDEVICEADDED:
349 j = SDL_JoystickOpen(event->jdevice.which);
350 if (j)
351 printf("Added joystick device: %s, with ID %d\n", SDL_JoystickName(j), SDL_JoystickInstanceID(j));
352 break;
353 case SDL_JOYDEVICEREMOVED:
354 j = SDL_JoystickFromInstanceID(event->jdevice.which);
355 if (j && SDL_JoystickGetAttached(j))
356 {
357 printf("Removed joystick device: %s\n", SDL_JoystickName(j));
358 SDL_JoystickClose(j);
359 }
360 break;
361 default:
362 break;
363 }
364}

Referenced by processSDLEvents().

◆ processSDLEvents()

bool InputEvents::processSDLEvents ( )

update which buttons are pressed

Definition at line 75 of file InputEvents.cpp.

76{
77 // get an event from SDL
78 if (!SDL_PollEvent(&event))
79 return false;
80
81 // update our variables
82 this->type = event.type;
83 this->noop = false;
84
85 // process joystick hotplugging events
87
88 std::string curControllerName= lastGamepadKey;
89
90 // get the controller name
91 if (this->type == SDL_KEYDOWN || this->type == SDL_KEYUP) {
92 // keyboard event
93 lastGamepadKey = "Keyboard";
94 } else if (this->type == SDL_JOYBUTTONDOWN || this->type == SDL_JOYBUTTONUP) {
95 SDL_Joystick* joystickId = SDL_JoystickFromInstanceID(event.jbutton.which);
96 if (joystickId != NULL) {
97 std::string controllerName = SDL_JoystickName(joystickId);
98 lastGamepadKey = defaultKeyName; // default in case no match is found
99 if (!controllerName.empty() && gamepadMap.find(controllerName) != gamepadMap.end()){
100 lastGamepadKey = controllerName;
101 }
102 }
103 }
104 if (curControllerName != lastGamepadKey) {
105 printf("Switched to controller profile: %s\n", lastGamepadKey.c_str());
106 GamepadInfo& gamepadInfo = gamepadMap[lastGamepadKey];
107 if (gamepadInfo.buttons != nullptr) {
108 currentButtons = gamepadInfo.buttons;
109 }
110 // keyButtonNames = gamepadInfo.names;
111 // TODO: callback to update all buttons on the UI
112 }
113
114 this->isScrolling = false;
115
116#ifdef PC
117 this->allowTouch = false;
118 if (event.type == SDL_MOUSEWHEEL) {
119 this->wheelScroll = event.wheel.y;
120 this->isScrolling = true;
121 }
122#endif
123
124 if (this->type == SDL_QUIT)
125 {
126 if (this->quitaction != NULL) this->quitaction();
127 return false; //Quitting overrides all other events.
128 }
129 else if (event.key.repeat == 0 && (this->type == SDL_KEYDOWN || this->type == SDL_KEYUP))
130 {
131 this->keyCode = event.key.keysym.sym;
132 this->mod = event.key.keysym.mod;
133 }
134 else if (this->type == SDL_JOYBUTTONDOWN || this->type == SDL_JOYBUTTONUP)
135 {
136 this->keyCode = event.jbutton.button;
137 }
138 else if (this->type == SDL_MOUSEMOTION || this->type == SDL_MOUSEBUTTONUP || this->type == SDL_MOUSEBUTTONDOWN)
139 {
140 bool isMotion = this->type == SDL_MOUSEMOTION;
141
142 this->yPos = isMotion ? event.motion.y : event.button.y;
143 this->xPos = isMotion ? event.motion.x : event.button.x;
144 }
145 else if (allowTouch && (this->type == SDL_FINGERMOTION || this->type == SDL_FINGERUP || this->type == SDL_FINGERDOWN))
146 {
147 this->yPos = event.tfinger.y * SCREEN_HEIGHT;
148 this->xPos = event.tfinger.x * SCREEN_WIDTH;
149 }
150
151 if (this->type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESIZED) {
152 printf("Window resized to %dx%d\n", event.window.data1, event.window.data2);
153 RootDisplay::mainDisplay->setScreenResolution(
154 event.window.data1 * RootDisplay::dpiScale,
155 event.window.data2 * RootDisplay::dpiScale
156 );
157
158 // callback to alert the app that the window changed resolution
159 if (RootDisplay::mainDisplay->windowResizeCallback)
160 RootDisplay::mainDisplay->windowResizeCallback();
161
162 RootDisplay::mainDisplay->needsRedraw = true;
163 }
164
165 // offset the x, y positions by the dpi scale
166 this->xPos = (int)(this->xPos * RootDisplay::dpiScale);
167 this->yPos = (int)(this->yPos * RootDisplay::dpiScale);
168
169 toggleHeldButtons();
170
171 return true;
172}
bool needsRedraw
whether or not this element needs the screen redrawn next time it's processed
Definition: Element.hpp:84
void processJoystickHotplugging(SDL_Event *event)
joystick device events processing

References Element::needsRedraw, and processJoystickHotplugging().

◆ released()

bool InputEvents::released ( int  buttons)

Definition at line 298 of file InputEvents.cpp.

299{
300 return isKeyUp() && held(buttons);
301}

◆ toggleHeldButtons()

void InputEvents::toggleHeldButtons ( )

Definition at line 184 of file InputEvents.cpp.

185{
186 int directionCode = directionForKeycode();
187
188 if (directionCode >= 0)
189 {
190 if (isKeyDown())
191 {
192 // make sure it's not already down
193 if (!held_directions[directionCode])
194 {
195 // on key down, set the corresponding held boolean to true
196 held_directions[directionCode] = true;
197 held_type = this->type;
198
199 // reset the frame counter so we don't fire on this frame
200 // (initial reset is lower to add a slight delay when they first start holding)
201 curFrame = -25;
202 }
203 }
204
205 if (isKeyUp())
206 {
207 // release the corresponding key too
208 held_directions[directionCode] = false;
209 }
210 }
211}

◆ touchIn()

bool InputEvents::touchIn ( int  x,
int  width,
int  y,
int  height 
)

whether or not a touch is detected within the specified rect in this cycle

Definition at line 303 of file InputEvents.cpp.

304{
305 return (this->xPos >= x && this->xPos <= x + width && this->yPos >= y && this->yPos <= y + height);
306}

Referenced by EKeyboard::process(), and Element::process().

◆ update()

bool InputEvents::update ( )

Definition at line 174 of file InputEvents.cpp.

175{
176 this->type = 0;
177 this->keyCode = -1;
178 this->noop = true;
179
180 // process SDL or directional events
181 return processSDLEvents() || processDirectionalButtons();
182}
bool processSDLEvents()
update which buttons are pressed
Definition: InputEvents.cpp:75

Member Data Documentation

◆ allowTouch

bool InputEvents::allowTouch = true

Definition at line 96 of file InputEvents.hpp.

◆ bypassKeyEvents

bool InputEvents::bypassKeyEvents = false
static

Definition at line 127 of file InputEvents.hpp.

◆ curFrame

int InputEvents::curFrame = 0

Definition at line 125 of file InputEvents.hpp.

◆ event

SDL_Event InputEvents::event

Definition at line 119 of file InputEvents.hpp.

◆ held_directions

bool InputEvents::held_directions[4] = { false, false, false, false }

Definition at line 121 of file InputEvents.hpp.

◆ held_type

Uint32 InputEvents::held_type

Definition at line 122 of file InputEvents.hpp.

◆ isScrolling

bool InputEvents::isScrolling = false

Definition at line 97 of file InputEvents.hpp.

◆ keyCode

CST_Keycode InputEvents::keyCode = -1

Definition at line 117 of file InputEvents.hpp.

◆ lastGamepadKey

std::string InputEvents::lastGamepadKey = defaultKeyName
static

Definition at line 129 of file InputEvents.hpp.

◆ mod

CST_Keymod InputEvents::mod = -1

Definition at line 118 of file InputEvents.hpp.

◆ noop

bool InputEvents::noop = false

Definition at line 137 of file InputEvents.hpp.

◆ quitaction

std::function<void()> InputEvents::quitaction = NULL

Definition at line 131 of file InputEvents.hpp.

◆ rapidFireRate

int InputEvents::rapidFireRate = 12

Definition at line 124 of file InputEvents.hpp.

◆ type

Uint32 InputEvents::type

Definition at line 139 of file InputEvents.hpp.

◆ wheelScroll

float InputEvents::wheelScroll = 0

Definition at line 133 of file InputEvents.hpp.

◆ xPos

int InputEvents::xPos = 0

Definition at line 136 of file InputEvents.hpp.

◆ yPos

int InputEvents::yPos = 0

Definition at line 135 of file InputEvents.hpp.


The documentation for this class was generated from the following files: