Chesto 0.9
A declarative and element-based library for creating GUIs on homebrew'd consoles
Loading...
Searching...
No Matches
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
 
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
 
bool processSDLEvents ()
 update which buttons are pressed
 
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
 

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 253 of file InputEvents.cpp.

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

◆ getLastGamepadInfo()

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

Definition at line 378 of file InputEvents.cpp.

379{
380 return gamepadMap[lastGamepadKey];
381}

◆ held()

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

whether or not a button is pressed during this cycle

Definition at line 284 of file InputEvents.cpp.

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

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

◆ isKeyDown()

bool Chesto::InputEvents::isKeyDown ( )

Definition at line 345 of file InputEvents.cpp.

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

◆ isKeyUp()

bool Chesto::InputEvents::isKeyUp ( )

Definition at line 350 of file InputEvents.cpp.

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

◆ isScroll()

bool Chesto::InputEvents::isScroll ( )

Definition at line 340 of file InputEvents.cpp.

341{
342 return this->isScrolling;
343}

◆ isTouch()

bool Chesto::InputEvents::isTouch ( )

Definition at line 335 of file InputEvents.cpp.

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

◆ isTouchDown()

bool Chesto::InputEvents::isTouchDown ( )

Definition at line 320 of file InputEvents.cpp.

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

◆ isTouchDrag()

bool Chesto::InputEvents::isTouchDrag ( )

Definition at line 325 of file InputEvents.cpp.

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

◆ isTouchUp()

bool Chesto::InputEvents::isTouchUp ( )

Definition at line 330 of file InputEvents.cpp.

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

◆ pressed()

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

Definition at line 305 of file InputEvents.cpp.

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

◆ processDirectionalButtons()

bool Chesto::InputEvents::processDirectionalButtons ( )

Definition at line 226 of file InputEvents.cpp.

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

◆ processJoystickHotplugging()

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

joystick device events processing

Definition at line 355 of file InputEvents.cpp.

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

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 RootDisplay::mainDisplay->requestQuit();
131 return false; //Quitting overrides all other events.
132 }
133 else if (event.key.repeat == 0 && (this->type == SDL_KEYDOWN || this->type == SDL_KEYUP))
134 {
135 this->keyCode = event.key.keysym.sym;
136 this->mod = event.key.keysym.mod;
137 }
138 else if (this->type == SDL_JOYBUTTONDOWN || this->type == SDL_JOYBUTTONUP)
139 {
140 this->keyCode = event.jbutton.button;
141 }
142 else if (this->type == SDL_MOUSEMOTION || this->type == SDL_MOUSEBUTTONUP || this->type == SDL_MOUSEBUTTONDOWN)
143 {
144 bool isMotion = this->type == SDL_MOUSEMOTION;
145
146 this->yPos = isMotion ? event.motion.y : event.button.y;
147 this->xPos = isMotion ? event.motion.x : event.button.x;
148 }
149 else if (allowTouch && (this->type == SDL_FINGERMOTION || this->type == SDL_FINGERUP || this->type == SDL_FINGERDOWN))
150 {
151 this->yPos = event.tfinger.y * SCREEN_HEIGHT;
152 this->xPos = event.tfinger.x * SCREEN_WIDTH;
153 }
154
155 if (this->type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESIZED) {
156 // printf("Window resized to %dx%d\n", event.window.data1, event.window.data2);
157 RootDisplay::mainDisplay->setScreenResolution(
158 event.window.data1 * RootDisplay::dpiScale,
159 event.window.data2 * RootDisplay::dpiScale
160 );
161
162 // callback to alert the app that the window changed resolution
163 if (RootDisplay::mainDisplay->windowResizeCallback)
164 RootDisplay::mainDisplay->windowResizeCallback();
165
166 RootDisplay::mainDisplay->needsRedraw = true;
167 }
168
169 // offset the x, y positions by the dpi scale
170 // clamp to prevent overflow when casting float to int
171 float scaledX = (float)this->xPos * RootDisplay::dpiScale;
172 float scaledY = (float)this->yPos * RootDisplay::dpiScale;
173
174 // clamp to int range to avoid undefined behavior
175 scaledX = std::max(std::min(scaledX, (float)std::numeric_limits<int>::max()), (float)std::numeric_limits<int>::min());
176 scaledY = std::max(std::min(scaledY, (float)std::numeric_limits<int>::max()), (float)std::numeric_limits<int>::min());
177
178 this->xPos = (int)scaledX;
179 this->yPos = (int)scaledY;
180
181 toggleHeldButtons();
182
183 return true;
184}
void processJoystickHotplugging(SDL_Event *event)
joystick device events processing

References processJoystickHotplugging().

◆ released()

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

Definition at line 310 of file InputEvents.cpp.

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

◆ toggleHeldButtons()

void Chesto::InputEvents::toggleHeldButtons ( )

Definition at line 196 of file InputEvents.cpp.

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

◆ 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 315 of file InputEvents.cpp.

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

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

◆ update()

bool Chesto::InputEvents::update ( )

Definition at line 186 of file InputEvents.cpp.

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

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.

119{ false, false, false, false };

◆ 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: