1#include "InputEvents.hpp"
2#include "RootDisplay.hpp"
10CST_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 };
12SDL_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, };
14#if defined(__WIIU__) && defined(USE_KEYBOARD)
15#include "../libs/wiiu_kbd/keybdwrapper.h"
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)
132 else if (event.key.repeat == 0 && (this->type == SDL_KEYDOWN || this->type == SDL_KEYUP))
134 this->keyCode =
event.key.keysym.sym;
135 this->mod =
event.key.keysym.mod;
137 else if (this->type == SDL_JOYBUTTONDOWN || this->type == SDL_JOYBUTTONUP)
139 this->keyCode =
event.jbutton.button;
141 else if (this->type == SDL_MOUSEMOTION || this->type == SDL_MOUSEBUTTONUP || this->type == SDL_MOUSEBUTTONDOWN)
143 bool isMotion = this->type == SDL_MOUSEMOTION;
145 this->yPos = isMotion ?
event.motion.y :
event.button.y;
146 this->xPos = isMotion ?
event.motion.x :
event.button.x;
148 else if (allowTouch && (this->type == SDL_FINGERMOTION || this->type == SDL_FINGERUP || this->type == SDL_FINGERDOWN))
150 this->yPos =
event.tfinger.y * SCREEN_HEIGHT;
151 this->xPos =
event.tfinger.x * SCREEN_WIDTH;
154 if (this->type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESIZED) {
156 RootDisplay::mainDisplay->setScreenResolution(
157 event.window.data1 * RootDisplay::dpiScale,
158 event.window.data2 * RootDisplay::dpiScale
162 if (RootDisplay::mainDisplay->windowResizeCallback)
163 RootDisplay::mainDisplay->windowResizeCallback();
165 RootDisplay::mainDisplay->needsRedraw =
true;
170 float scaledX = (float)this->xPos * RootDisplay::dpiScale;
171 float scaledY = (float)this->yPos * RootDisplay::dpiScale;
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());
177 this->xPos = (int)scaledX;
178 this->yPos = (int)scaledY;
185bool InputEvents::update()
195void InputEvents::toggleHeldButtons()
197 int directionCode = directionForKeycode();
199 if (directionCode >= 0)
204 if (!held_directions[directionCode])
207 held_directions[directionCode] =
true;
208 held_type = this->type;
219 held_directions[directionCode] =
false;
225bool InputEvents::processDirectionalButtons()
232 if (curFrame > 0 && curFrame % rapidFireRate == 0)
234 for (
int x = 0; x < 4; x++)
236 if (!held_directions[x])
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];
252int InputEvents::directionForKeycode()
255 if (this->type == SDL_KEYDOWN && this->keyCode == SDLK_RETURN)
259 switch (this->keyCode)
273 case SDL_RIGHT_STICK:
286 if ((this->type == SDL_KEYDOWN || this->type == SDL_KEYUP) && !InputEvents::bypassKeyEvents)
288 for (
int x = 0; x < TOTAL_BUTTONS; x++)
289 if (key_buttons[x] == keyCode && (buttons & currentButtons[x]))
294 else if (this->type == SDL_JOYBUTTONDOWN || this->type == SDL_JOYBUTTONUP)
296 for (
int x = 0; x < TOTAL_BUTTONS; x++)
297 if (pad_buttons[x] == keyCode && (buttons & currentButtons[x]))
304bool InputEvents::pressed(
int buttons)
306 return isKeyDown() &&
held(buttons);
309bool InputEvents::released(
int buttons)
311 return isKeyUp() &&
held(buttons);
316 return (this->xPos >= x && this->xPos <= x + width && this->yPos >= y && this->yPos <= y + height);
319bool InputEvents::isTouchDown()
321 return this->type == SDL_MOUSEBUTTONDOWN || (allowTouch && this->type == SDL_FINGERDOWN);
324bool InputEvents::isTouchDrag()
326 return this->type == SDL_MOUSEMOTION || (allowTouch && this->type == SDL_FINGERMOTION);
329bool InputEvents::isTouchUp()
331 return this->type == SDL_MOUSEBUTTONUP || (allowTouch && this->type == SDL_FINGERUP);
334bool InputEvents::isTouch()
336 return isTouchDown() || isTouchDrag() || isTouchUp();
339bool InputEvents::isScroll()
341 return this->isScrolling;
344bool InputEvents::isKeyDown()
346 return this->type == SDL_KEYDOWN || this->type == SDL_JOYBUTTONDOWN;
349bool InputEvents::isKeyUp()
351 return this->type == SDL_KEYUP || this->type == SDL_JOYBUTTONUP;
359 case SDL_JOYDEVICEADDED:
360 j = SDL_JoystickOpen(event->jdevice.which);
364 case SDL_JOYDEVICEREMOVED:
365 j = SDL_JoystickFromInstanceID(event->jdevice.which);
366 if (j && SDL_JoystickGetAttached(j))
369 SDL_JoystickClose(j);
379 return gamepadMap[lastGamepadKey];