1#include "InputEvents.hpp"
2#include "RootDisplay.hpp"
7#if defined(__WIIU__) && defined(USE_KEYBOARD)
8#include "../libs/wiiu_kbd/keybdwrapper.h"
14CST_Keycode key_buttons[] = { SDLK_a, SDLK_b, SDLK_x, SDLK_y, SDLK_UP, SDLK_DOWN, SDLK_LEFT, SDLK_RIGHT, SDLK_RETURN, SDLK_l, SDLK_r, SDLK_z, SDLK_BACKSPACE, SDLK_UP, SDLK_DOWN, SDLK_LEFT, SDLK_RIGHT, SDLK_q };
16SDL_GameControllerButton pad_buttons[] = { SDL_A, SDL_B, SDL_X, SDL_Y, SDL_UP, SDL_DOWN, SDL_LEFT, SDL_RIGHT, SDL_PLUS, SDL_L, SDL_R, SDL_ZL, SDL_MINUS, SDL_UP_STICK, SDL_DOWN_STICK, SDL_LEFT_STICK, SDL_RIGHT_STICK, SDL_ZR, };
19unsigned int nintendo_buttons[] = { A_BUTTON, B_BUTTON, X_BUTTON, Y_BUTTON, UP_BUTTON, DOWN_BUTTON, LEFT_BUTTON, RIGHT_BUTTON, START_BUTTON, L_BUTTON, R_BUTTON, ZL_BUTTON, SELECT_BUTTON, UP_BUTTON, DOWN_BUTTON, LEFT_BUTTON, RIGHT_BUTTON, ZR_BUTTON };
22std::string nintendoButtonNames[] = {
"a",
"b",
"x",
"y",
"up",
"down",
"left",
"right",
"plus",
"l",
"r",
"zl",
"minus",
"up",
"down",
"left",
"right",
"zr" };
25std::string keyButtonNames[] = {
"a",
"b",
"x",
"y",
"up",
"down",
"left",
"right",
"return",
"l",
"r",
"z",
"backspace",
"up",
"down",
"left",
"right",
"q" };
29unsigned int wii_buttons[] = { A_BUTTON, B_BUTTON, L_BUTTON, R_BUTTON, UP_BUTTON, DOWN_BUTTON, LEFT_BUTTON, RIGHT_BUTTON, START_BUTTON, 0, 0, 0, SELECT_BUTTON, 0, 0, 0, 0, 0 };
31std::string wiiButtonNames[] = {
"a",
"b",
"1",
"2",
"up",
"down",
"left",
"right",
"plus",
"",
"",
"",
"minus",
"",
"",
"",
"",
"" };
34unsigned int nunchuk_buttons[] = { A_BUTTON, B_BUTTON, L_BUTTON, R_BUTTON, UP_BUTTON, DOWN_BUTTON, LEFT_BUTTON, RIGHT_BUTTON, START_BUTTON, 0, 0, X_BUTTON, SELECT_BUTTON, UP_BUTTON, DOWN_BUTTON, LEFT_BUTTON, RIGHT_BUTTON, Y_BUTTON };
36std::string nunchukButtonNames[] = {
"a",
"b",
"1",
"2",
"up",
"down",
"left",
"right",
"plus",
"",
"",
"z",
"minus",
"up",
"down",
"left",
"right",
"c" };
39bool InputEvents::bypassKeyEvents =
false;
41auto defaultKeyName =
"WiiU Gamepad";
42std::string InputEvents::lastGamepadKey = defaultKeyName;
43unsigned int* currentButtons = nintendo_buttons;
44std::string* currentButtonNames = nintendoButtonNames;
47std::map<std::string, GamepadInfo> gamepadMap = {
49 {
"Keyboard", GamepadInfo(nintendo_buttons, keyButtonNames,
"keyboard",
"key") },
51 {
"WiiU Gamepad", GamepadInfo(nintendo_buttons, nintendoButtonNames,
"wiiu_button",
"gamepad") },
52 {
"WiiU Pro Controller", { nintendo_buttons, nintendoButtonNames,
"wiiu_button",
"pro" } },
53 {
"Wii Remote", { wii_buttons, wiiButtonNames,
"wii_button",
"remote" } },
54 {
"Wii Remote and Nunchuk", { nunchuk_buttons, nunchukButtonNames,
"wii_button",
"nunchuk" } },
55 {
"Wii Classic Controller", { nintendo_buttons, nintendoButtonNames,
"wii_button",
"classic"} },
57 {
"Switch Controller", { nintendo_buttons, nintendoButtonNames,
"switch_button",
"pro" } },
71InputEvents::InputEvents()
73#if defined(__WIIU__) && defined(USE_KEYBOARD)
75 KBWrapper* kbdwrapper =
new KBWrapper(
true,
true);
82 if (!SDL_PollEvent(&event))
86 this->type =
event.type;
92 std::string curControllerName= lastGamepadKey;
95 if (this->type == SDL_KEYDOWN || this->type == SDL_KEYUP) {
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;
103 if (!controllerName.empty() && gamepadMap.find(controllerName) != gamepadMap.end()){
104 lastGamepadKey = controllerName;
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;
118 this->isScrolling =
false;
121 this->allowTouch =
false;
122 if (event.type == SDL_MOUSEWHEEL) {
123 this->wheelScroll =
event.wheel.y;
124 this->isScrolling =
true;
128 if (this->type == SDL_QUIT)
130 RootDisplay::mainDisplay->requestQuit();
133 else if (event.key.repeat == 0 && (this->type == SDL_KEYDOWN || this->type == SDL_KEYUP))
135 this->keyCode =
event.key.keysym.sym;
136 this->mod =
event.key.keysym.mod;
138 else if (this->type == SDL_JOYBUTTONDOWN || this->type == SDL_JOYBUTTONUP)
140 this->keyCode =
event.jbutton.button;
142 else if (this->type == SDL_MOUSEMOTION || this->type == SDL_MOUSEBUTTONUP || this->type == SDL_MOUSEBUTTONDOWN)
144 bool isMotion = this->type == SDL_MOUSEMOTION;
146 this->yPos = isMotion ?
event.motion.y :
event.button.y;
147 this->xPos = isMotion ?
event.motion.x :
event.button.x;
149 else if (allowTouch && (this->type == SDL_FINGERMOTION || this->type == SDL_FINGERUP || this->type == SDL_FINGERDOWN))
151 this->yPos =
event.tfinger.y * SCREEN_HEIGHT;
152 this->xPos =
event.tfinger.x * SCREEN_WIDTH;
155 if (this->type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESIZED) {
157 RootDisplay::mainDisplay->setScreenResolution(
158 event.window.data1 * RootDisplay::dpiScale,
159 event.window.data2 * RootDisplay::dpiScale
163 if (RootDisplay::mainDisplay->windowResizeCallback)
164 RootDisplay::mainDisplay->windowResizeCallback();
166 RootDisplay::mainDisplay->needsRedraw =
true;
171 float scaledX = (float)this->xPos * RootDisplay::dpiScale;
172 float scaledY = (float)this->yPos * RootDisplay::dpiScale;
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());
178 this->xPos = (int)scaledX;
179 this->yPos = (int)scaledY;
186bool InputEvents::update()
196void InputEvents::toggleHeldButtons()
198 int directionCode = directionForKeycode();
200 if (directionCode >= 0)
205 if (!held_directions[directionCode])
208 held_directions[directionCode] =
true;
209 held_type = this->type;
220 held_directions[directionCode] =
false;
226bool InputEvents::processDirectionalButtons()
233 if (curFrame > 0 && curFrame % rapidFireRate == 0)
235 for (
int x = 0; x < 4; x++)
237 if (!held_directions[x])
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];
253int InputEvents::directionForKeycode()
256 if (this->type == SDL_KEYDOWN && this->keyCode == SDLK_RETURN)
260 switch (this->keyCode)
274 case SDL_RIGHT_STICK:
287 if ((this->type == SDL_KEYDOWN || this->type == SDL_KEYUP) && !InputEvents::bypassKeyEvents)
289 for (
int x = 0; x < TOTAL_BUTTONS; x++)
290 if (key_buttons[x] == keyCode && (buttons & currentButtons[x]))
295 else if (this->type == SDL_JOYBUTTONDOWN || this->type == SDL_JOYBUTTONUP)
297 for (
int x = 0; x < TOTAL_BUTTONS; x++)
298 if (pad_buttons[x] == keyCode && (buttons & currentButtons[x]))
305bool InputEvents::pressed(
int buttons)
307 return isKeyDown() &&
held(buttons);
310bool InputEvents::released(
int buttons)
312 return isKeyUp() &&
held(buttons);
317 return (this->xPos >= x && this->xPos <= x + width && this->yPos >= y && this->yPos <= y + height);
320bool InputEvents::isTouchDown()
322 return this->type == SDL_MOUSEBUTTONDOWN || (allowTouch && this->type == SDL_FINGERDOWN);
325bool InputEvents::isTouchDrag()
327 return this->type == SDL_MOUSEMOTION || (allowTouch && this->type == SDL_FINGERMOTION);
330bool InputEvents::isTouchUp()
332 return this->type == SDL_MOUSEBUTTONUP || (allowTouch && this->type == SDL_FINGERUP);
335bool InputEvents::isTouch()
337 return isTouchDown() || isTouchDrag() || isTouchUp();
340bool InputEvents::isScroll()
342 return this->isScrolling;
345bool InputEvents::isKeyDown()
347 return this->type == SDL_KEYDOWN || this->type == SDL_JOYBUTTONDOWN;
350bool InputEvents::isKeyUp()
352 return this->type == SDL_KEYUP || this->type == SDL_JOYBUTTONUP;
360 case SDL_JOYDEVICEADDED:
361 j = SDL_JoystickOpen(event->jdevice.which);
365 case SDL_JOYDEVICEREMOVED:
366 j = SDL_JoystickFromInstanceID(event->jdevice.which);
367 if (j && SDL_JoystickGetAttached(j))
370 SDL_JoystickClose(j);
380 return gamepadMap[lastGamepadKey];