1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-12-23 01:59:43 +01:00

Refactor KeyPoll::Poll() to use case-switch statements

Reduces the amount of copy-pasted if-expressions.
This commit is contained in:
Misa 2020-06-26 18:17:01 -07:00 committed by Ethan Lee
parent a87ebd2945
commit 2662cd4d06

View file

@ -74,8 +74,10 @@ void KeyPoll::Poll()
SDL_Event evt; SDL_Event evt;
while (SDL_PollEvent(&evt)) while (SDL_PollEvent(&evt))
{ {
switch (evt.type)
{
/* Keyboard Input */ /* Keyboard Input */
if (evt.type == SDL_KEYDOWN) case SDL_KEYDOWN:
{ {
keymap[evt.key.keysym.sym] = true; keymap[evt.key.keysym.sym] = true;
@ -115,82 +117,76 @@ void KeyPoll::Poll()
keybuffer += SDL_GetClipboardText(); keybuffer += SDL_GetClipboardText();
} }
} }
break;
} }
else if (evt.type == SDL_KEYUP) case SDL_KEYUP:
{
keymap[evt.key.keysym.sym] = false; keymap[evt.key.keysym.sym] = false;
if (evt.key.keysym.sym == SDLK_BACKSPACE) if (evt.key.keysym.sym == SDLK_BACKSPACE)
{ {
pressedbackspace = false; pressedbackspace = false;
} }
} break;
else if (evt.type == SDL_TEXTINPUT) case SDL_TEXTINPUT:
{
keybuffer += evt.text.text; keybuffer += evt.text.text;
} break;
/* Mouse Input */ /* Mouse Input */
else if (evt.type == SDL_MOUSEMOTION) case SDL_MOUSEMOTION:
{
mx = evt.motion.x; mx = evt.motion.x;
my = evt.motion.y; my = evt.motion.y;
} break;
else if (evt.type == SDL_MOUSEBUTTONDOWN) case SDL_MOUSEBUTTONDOWN:
{ switch (evt.button.button)
if (evt.button.button == SDL_BUTTON_LEFT)
{ {
case SDL_BUTTON_LEFT:
mx = evt.button.x; mx = evt.button.x;
my = evt.button.y; my = evt.button.y;
leftbutton = 1; leftbutton = 1;
} break;
else if (evt.button.button == SDL_BUTTON_RIGHT) case SDL_BUTTON_RIGHT:
{
mx = evt.button.x; mx = evt.button.x;
my = evt.button.y; my = evt.button.y;
rightbutton = 1; rightbutton = 1;
} break;
else if (evt.button.button == SDL_BUTTON_MIDDLE) case SDL_BUTTON_MIDDLE:
{
mx = evt.button.x; mx = evt.button.x;
my = evt.button.y; my = evt.button.y;
middlebutton = 1; middlebutton = 1;
break;
} }
} break;
else if (evt.type == SDL_MOUSEBUTTONUP) case SDL_MOUSEBUTTONUP:
{ switch (evt.button.button)
if (evt.button.button == SDL_BUTTON_LEFT)
{ {
case SDL_BUTTON_LEFT:
mx = evt.button.x; mx = evt.button.x;
my = evt.button.y; my = evt.button.y;
leftbutton=0; leftbutton=0;
} break;
else if (evt.button.button == SDL_BUTTON_RIGHT) case SDL_BUTTON_RIGHT:
{
mx = evt.button.x; mx = evt.button.x;
my = evt.button.y; my = evt.button.y;
rightbutton=0; rightbutton=0;
} break;
else if (evt.button.button == SDL_BUTTON_MIDDLE) case SDL_BUTTON_MIDDLE:
{
mx = evt.button.x; mx = evt.button.x;
my = evt.button.y; my = evt.button.y;
middlebutton=0; middlebutton=0;
break;
} }
} break;
/* Controller Input */ /* Controller Input */
else if (evt.type == SDL_CONTROLLERBUTTONDOWN) case SDL_CONTROLLERBUTTONDOWN:
{
buttonmap[(SDL_GameControllerButton) evt.cbutton.button] = true; buttonmap[(SDL_GameControllerButton) evt.cbutton.button] = true;
} break;
else if (evt.type == SDL_CONTROLLERBUTTONUP) case SDL_CONTROLLERBUTTONUP:
{
buttonmap[(SDL_GameControllerButton) evt.cbutton.button] = false; buttonmap[(SDL_GameControllerButton) evt.cbutton.button] = false;
} break;
else if (evt.type == SDL_CONTROLLERAXISMOTION) case SDL_CONTROLLERAXISMOTION:
{ switch (evt.caxis.axis)
if (evt.caxis.axis == SDL_CONTROLLER_AXIS_LEFTX)
{ {
case SDL_CONTROLLER_AXIS_LEFTX:
if ( evt.caxis.value > -sensitivity && if ( evt.caxis.value > -sensitivity &&
evt.caxis.value < sensitivity ) evt.caxis.value < sensitivity )
{ {
@ -200,9 +196,8 @@ void KeyPoll::Poll()
{ {
xVel = (evt.caxis.value > 0) ? 1 : -1; xVel = (evt.caxis.value > 0) ? 1 : -1;
} }
} break;
if (evt.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY) case SDL_CONTROLLER_AXIS_LEFTY:
{
if ( evt.caxis.value > -sensitivity && if ( evt.caxis.value > -sensitivity &&
evt.caxis.value < sensitivity ) evt.caxis.value < sensitivity )
{ {
@ -212,9 +207,10 @@ void KeyPoll::Poll()
{ {
yVel = (evt.caxis.value > 0) ? 1 : -1; yVel = (evt.caxis.value > 0) ? 1 : -1;
} }
break;
} }
} break;
else if (evt.type == SDL_CONTROLLERDEVICEADDED) case SDL_CONTROLLERDEVICEADDED:
{ {
SDL_GameController *toOpen = SDL_GameControllerOpen(evt.cdevice.which); SDL_GameController *toOpen = SDL_GameControllerOpen(evt.cdevice.which);
printf( printf(
@ -223,27 +219,28 @@ void KeyPoll::Poll()
SDL_GameControllerName(toOpen) SDL_GameControllerName(toOpen)
); );
controllers[SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(toOpen))] = toOpen; controllers[SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(toOpen))] = toOpen;
break;
} }
else if (evt.type == SDL_CONTROLLERDEVICEREMOVED) case SDL_CONTROLLERDEVICEREMOVED:
{ {
SDL_GameController *toClose = controllers[evt.cdevice.which]; SDL_GameController *toClose = controllers[evt.cdevice.which];
controllers.erase(evt.cdevice.which); controllers.erase(evt.cdevice.which);
printf("Closing %s\n", SDL_GameControllerName(toClose)); printf("Closing %s\n", SDL_GameControllerName(toClose));
SDL_GameControllerClose(toClose); SDL_GameControllerClose(toClose);
break;
} }
/* Window Events */ /* Window Events */
else if (evt.type == SDL_WINDOWEVENT) case SDL_WINDOWEVENT:
{ switch (evt.window.event)
/* Window Resize */
if (evt.window.event == SDL_WINDOWEVENT_RESIZED)
{ {
/* Window Resize */
case SDL_WINDOWEVENT_RESIZED:
resetWindow = true; resetWindow = true;
} break;
/* Window Focus */ /* Window Focus */
else if (evt.window.event == SDL_WINDOWEVENT_FOCUS_GAINED) case SDL_WINDOWEVENT_FOCUS_GAINED:
{
isActive = true; isActive = true;
if (!useFullscreenSpaces) if (!useFullscreenSpaces)
{ {
@ -257,9 +254,8 @@ void KeyPoll::Poll()
} }
} }
SDL_DisableScreenSaver(); SDL_DisableScreenSaver();
} break;
else if (evt.window.event == SDL_WINDOWEVENT_FOCUS_LOST) case SDL_WINDOWEVENT_FOCUS_LOST:
{
isActive = false; isActive = false;
if (!useFullscreenSpaces) if (!useFullscreenSpaces)
{ {
@ -271,23 +267,22 @@ void KeyPoll::Poll()
); );
} }
SDL_EnableScreenSaver(); SDL_EnableScreenSaver();
} break;
/* Mouse Focus */ /* Mouse Focus */
else if (evt.window.event == SDL_WINDOWEVENT_ENTER) case SDL_WINDOWEVENT_ENTER:
{
SDL_DisableScreenSaver(); SDL_DisableScreenSaver();
} break;
else if (evt.window.event == SDL_WINDOWEVENT_LEAVE) case SDL_WINDOWEVENT_LEAVE:
{
SDL_EnableScreenSaver(); SDL_EnableScreenSaver();
break;
} }
} break;
/* Quit Event */ /* Quit Event */
else if (evt.type == SDL_QUIT) case SDL_QUIT:
{
quitProgram = true; quitProgram = true;
break;
} }
} }
} }