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
Chesto::InputEvents Class Reference
Collaboration diagram for Chesto::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 77 of file InputEvents.hpp.

Constructor & Destructor Documentation

◆ InputEvents()

Chesto::InputEvents::InputEvents ( )

Definition at line 71 of file InputEvents.cpp.

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

Member Function Documentation

◆ directionForKeycode()

int Chesto::InputEvents::directionForKeycode ( )

Definition at line 252 of file InputEvents.cpp.

253{
254 // this keycode overlaps with some other constants, so just return asap
255 if (this->type == SDL_KEYDOWN && this->keyCode == SDLK_RETURN)
256 return -1;
257
258 // returns 0 1 2 or 3 for up down left or right
259 switch (this->keyCode)
260 {
261 case SDL_UP_STICK:
262 case SDL_UP:
263 case SDLK_UP:
264 return 0;
265 case SDL_DOWN_STICK:
266 case SDL_DOWN:
267 case SDLK_DOWN:
268 return 1;
269 case SDL_LEFT_STICK:
270 case SDL_LEFT:
271 case SDLK_LEFT:
272 return 2;
273 case SDL_RIGHT_STICK:
274 case SDL_RIGHT:
275 case SDLK_RIGHT:
276 return 3;
277 default:
278 return -1;
279 }
280 return -1;
281}

◆ getLastGamepadInfo()

GamepadInfo & Chesto::InputEvents::getLastGamepadInfo ( )
static

Definition at line 377 of file InputEvents.cpp.

378{
379 return gamepadMap[lastGamepadKey];
380}

◆ held()

bool Chesto::InputEvents::held ( int  buttons)

whether or not a button is pressed during this cycle

Definition at line 283 of file InputEvents.cpp.

284{
285 // if it's a key event
286 if ((this->type == SDL_KEYDOWN || this->type == SDL_KEYUP) && !InputEvents::bypassKeyEvents)
287 {
288 for (int x = 0; x < TOTAL_BUTTONS; x++)
289 if (key_buttons[x] == keyCode && (buttons & currentButtons[x]))
290 return true;
291 }
292
293 // if it's a controller event
294 else if (this->type == SDL_JOYBUTTONDOWN || this->type == SDL_JOYBUTTONUP)
295 {
296 for (int x = 0; x < TOTAL_BUTTONS; x++)
297 if (pad_buttons[x] == keyCode && (buttons & currentButtons[x]))
298 return true;
299 }
300
301 return false;
302}

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

◆ isKeyDown()

bool Chesto::InputEvents::isKeyDown ( )

Definition at line 344 of file InputEvents.cpp.

345{
346 return this->type == SDL_KEYDOWN || this->type == SDL_JOYBUTTONDOWN;
347}

◆ isKeyUp()

bool Chesto::InputEvents::isKeyUp ( )

Definition at line 349 of file InputEvents.cpp.

350{
351 return this->type == SDL_KEYUP || this->type == SDL_JOYBUTTONUP;
352}

◆ isScroll()

bool Chesto::InputEvents::isScroll ( )

Definition at line 339 of file InputEvents.cpp.

340{
341 return this->isScrolling;
342}

◆ isTouch()

bool Chesto::InputEvents::isTouch ( )

Definition at line 334 of file InputEvents.cpp.

335{
336 return isTouchDown() || isTouchDrag() || isTouchUp();
337}

◆ isTouchDown()

bool Chesto::InputEvents::isTouchDown ( )

Definition at line 319 of file InputEvents.cpp.

320{
321 return this->type == SDL_MOUSEBUTTONDOWN || (allowTouch && this->type == SDL_FINGERDOWN);
322}

◆ isTouchDrag()

bool Chesto::InputEvents::isTouchDrag ( )

Definition at line 324 of file InputEvents.cpp.

325{
326 return this->type == SDL_MOUSEMOTION || (allowTouch && this->type == SDL_FINGERMOTION);
327}

◆ isTouchUp()

bool Chesto::InputEvents::isTouchUp ( )

Definition at line 329 of file InputEvents.cpp.

330{
331 return this->type == SDL_MOUSEBUTTONUP || (allowTouch && this->type == SDL_FINGERUP);
332}

◆ pressed()

bool Chesto::InputEvents::pressed ( int  buttons)

Definition at line 304 of file InputEvents.cpp.

305{
306 return isKeyDown() && held(buttons);
307}
bool held(int buttons)
whether or not a button is pressed during this cycle

◆ processDirectionalButtons()

bool Chesto::InputEvents::processDirectionalButtons ( )

Definition at line 225 of file InputEvents.cpp.

226{
227 // up the counter
228 curFrame++;
229
230 // if one of the four direction keys is true, fire off repeat events for it
231 // (when rapidFire lines up only)
232 if (curFrame > 0 && curFrame % rapidFireRate == 0)
233 {
234 for (int x = 0; x < 4; x++)
235 {
236 if (!held_directions[x])
237 continue;
238
239 // send a corresponding directional event
240 this->type = held_type;
241 bool isGamepad = (this->type == SDL_JOYBUTTONDOWN || this->type == SDL_JOYBUTTONUP);
242 this->keyCode = isGamepad ? pad_buttons[4 + x] : key_buttons[4 + x]; // send up through right directions
243 this->noop = false;
244
245 return true;
246 }
247 }
248
249 return false;
250}

◆ processJoystickHotplugging()

void Chesto::InputEvents::processJoystickHotplugging ( SDL_Event *  event)

joystick device events processing

Definition at line 354 of file InputEvents.cpp.

355{
356 SDL_Joystick *j;
357 switch(event->type)
358 {
359 case SDL_JOYDEVICEADDED:
360 j = SDL_JoystickOpen(event->jdevice.which);
361 // if (j)
362 // printf("Added joystick device: %s, with ID %d\n", SDL_JoystickName(j), SDL_JoystickInstanceID(j));
363 break;
364 case SDL_JOYDEVICEREMOVED:
365 j = SDL_JoystickFromInstanceID(event->jdevice.which);
366 if (j && SDL_JoystickGetAttached(j))
367 {
368 // printf("Removed joystick device: %s\n", SDL_JoystickName(j));
369 SDL_JoystickClose(j);
370 }
371 break;
372 default:
373 break;
374 }
375}

Referenced by processSDLEvents().

◆ processSDLEvents()

bool Chesto::InputEvents::processSDLEvents ( )

update which buttons are pressed

Definition at line 79 of file InputEvents.cpp.

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

References processJoystickHotplugging().

◆ released()

bool Chesto::InputEvents::released ( int  buttons)

Definition at line 309 of file InputEvents.cpp.

310{
311 return isKeyUp() && held(buttons);
312}

◆ toggleHeldButtons()

void Chesto::InputEvents::toggleHeldButtons ( )

Definition at line 195 of file InputEvents.cpp.

196{
197 int directionCode = directionForKeycode();
198
199 if (directionCode >= 0)
200 {
201 if (isKeyDown())
202 {
203 // make sure it's not already down
204 if (!held_directions[directionCode])
205 {
206 // on key down, set the corresponding held boolean to true
207 held_directions[directionCode] = true;
208 held_type = this->type;
209
210 // reset the frame counter so we don't fire on this frame
211 // (initial reset is lower to add a slight delay when they first start holding)
212 curFrame = -25;
213 }
214 }
215
216 if (isKeyUp())
217 {
218 // release the corresponding key too
219 held_directions[directionCode] = false;
220 }
221 }
222}

◆ touchIn()

bool Chesto::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 314 of file InputEvents.cpp.

315{
316 return (this->xPos >= x && this->xPos <= x + width && this->yPos >= y && this->yPos <= y + height);
317}

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

◆ update()

bool Chesto::InputEvents::update ( )

Definition at line 185 of file InputEvents.cpp.

186{
187 this->type = 0;
188 this->keyCode = -1;
189 this->noop = true;
190
191 // process SDL or directional events
192 return processSDLEvents() || processDirectionalButtons();
193}
bool processSDLEvents()
update which buttons are pressed
Definition: InputEvents.cpp:79

Member Data Documentation

◆ allowTouch

bool Chesto::InputEvents::allowTouch = true

Definition at line 94 of file InputEvents.hpp.

◆ bypassKeyEvents

bool Chesto::InputEvents::bypassKeyEvents = false
static

Definition at line 125 of file InputEvents.hpp.

◆ curFrame

int Chesto::InputEvents::curFrame = 0

Definition at line 123 of file InputEvents.hpp.

◆ event

SDL_Event Chesto::InputEvents::event

Definition at line 117 of file InputEvents.hpp.

◆ held_directions

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

Definition at line 119 of file InputEvents.hpp.

◆ held_type

Uint32 Chesto::InputEvents::held_type

Definition at line 120 of file InputEvents.hpp.

◆ isScrolling

bool Chesto::InputEvents::isScrolling = false

Definition at line 95 of file InputEvents.hpp.

◆ keyCode

CST_Keycode Chesto::InputEvents::keyCode = -1

Definition at line 115 of file InputEvents.hpp.

◆ lastGamepadKey

std::string Chesto::InputEvents::lastGamepadKey = defaultKeyName
static

Definition at line 127 of file InputEvents.hpp.

◆ mod

CST_Keymod Chesto::InputEvents::mod = -1

Definition at line 116 of file InputEvents.hpp.

◆ noop

bool Chesto::InputEvents::noop = false

Definition at line 135 of file InputEvents.hpp.

◆ quitaction

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

Definition at line 129 of file InputEvents.hpp.

◆ rapidFireRate

int Chesto::InputEvents::rapidFireRate = 12

Definition at line 122 of file InputEvents.hpp.

◆ type

Uint32 Chesto::InputEvents::type

Definition at line 137 of file InputEvents.hpp.

◆ wheelScroll

float Chesto::InputEvents::wheelScroll = 0

Definition at line 131 of file InputEvents.hpp.

◆ xPos

int Chesto::InputEvents::xPos = 0

Definition at line 134 of file InputEvents.hpp.

◆ yPos

int Chesto::InputEvents::yPos = 0

Definition at line 133 of file InputEvents.hpp.


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