1
0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-06-26 14:38:30 +02:00
VVVVVV/desktop_version/src/KeyPoll.cpp
Misa d7040b23d0 Axe manual state trackers and use SDL_IsTextInputActive()
After looking at pull request #446, I got a bit annoyed that we have TWO
variables, key.textentrymode and ed.textentry, that we rolled ourselves
to track the state of something SDL already provides us a function to
easily query: SDL_IsTextInputActive(). We don't need to have either of
these two variables, and we shouldn't.

So that's what I do in this patch. Both variables have been axed in
favor of using this function, and I just made a wrapper out of it, named
key.textentry().

For bonus points, this gets rid of the ugly NO_CUSTOM_LEVELS and
NO_EDITOR ifdef in main.cpp, since text entry is enabled when entering
the script list and disabled when exiting it. This makes the code there
easier to read, too.

Furthermore, apparently key.textentrymode was initialized to *true*
instead of false... for whatever reason. But that's gone now, too.

Now, you'd think there wouldn't be any downside to using
SDL_IsTextInputActive(). After all, it's a function that SDL itself
provides, right?

Wrong. For whatever reason, it seems like text input is active *from the
start of the program*, meaning that what would happen is I would go into
the editor, and find that I can't move around nor place tiles nor
anything else. Then I would press Esc, and then suddenly become able to
do those things I wanted to do before.

I have no idea why the above happens, but all I can do is to just insert
an SDL_StopTextInput() immediately after the SDL_Init() in main.cpp. Of
course, I have to surround it with an SDL_IsTextInputActive() check to
make sure I don't do anything extraneous by stopping input when it's
already stopped.
2020-08-13 17:51:38 -04:00

378 lines
7.4 KiB
C++

#include "KeyPoll.h"
#include <stdio.h>
#include <string.h>
#include <utf8/unchecked.h>
#include "Game.h"
#include "Graphics.h"
#include "Music.h"
void KeyPoll::setSensitivity(int _value)
{
switch (_value)
{
case 0:
sensitivity = 28000;
break;
case 1:
sensitivity = 16000;
break;
case 2:
sensitivity = 8000;
break;
case 3:
sensitivity = 4000;
break;
case 4:
sensitivity = 2000;
break;
}
}
KeyPoll::KeyPoll()
{
xVel = 0;
yVel = 0;
setSensitivity(2);
quitProgram = 0;
keybuffer="";
leftbutton=0; rightbutton=0; middlebutton=0;
mx=0; my=0;
resetWindow = 0;
toggleFullscreen = false;
pressedbackspace=false;
useFullscreenSpaces = false;
if (strcmp(SDL_GetPlatform(), "Mac OS X") == 0)
{
useFullscreenSpaces = true;
const char *hint = SDL_GetHint(SDL_HINT_VIDEO_MAC_FULLSCREEN_SPACES);
if (hint != NULL)
{
useFullscreenSpaces = (strcmp(hint, "1") == 0);
}
}
linealreadyemptykludge = false;
pauseStart = 0;
isActive = true;
}
void KeyPoll::enabletextentry()
{
keybuffer="";
SDL_StartTextInput();
}
void KeyPoll::disabletextentry()
{
SDL_StopTextInput();
}
bool KeyPoll::textentry()
{
return SDL_IsTextInputActive() == SDL_TRUE;
}
void KeyPoll::Poll()
{
bool altpressed = false;
SDL_Event evt;
while (SDL_PollEvent(&evt))
{
switch (evt.type)
{
/* Keyboard Input */
case SDL_KEYDOWN:
{
keymap[evt.key.keysym.sym] = true;
if (evt.key.keysym.sym == SDLK_BACKSPACE)
{
pressedbackspace = true;
}
#ifdef __APPLE__ /* OSX prefers the command keys over the alt keys. -flibit */
altpressed = keymap[SDLK_LGUI] || keymap[SDLK_RGUI];
#else
altpressed = keymap[SDLK_LALT] || keymap[SDLK_RALT];
#endif
bool returnpressed = evt.key.keysym.sym == SDLK_RETURN;
bool fpressed = evt.key.keysym.sym == SDLK_f;
bool f11pressed = evt.key.keysym.sym == SDLK_F11;
if ((altpressed && (returnpressed || fpressed)) || f11pressed)
{
toggleFullscreen = true;
}
if (textentry())
{
if (evt.key.keysym.sym == SDLK_BACKSPACE && !keybuffer.empty())
{
std::string::iterator iter = keybuffer.end();
utf8::unchecked::prior(iter);
keybuffer = keybuffer.substr(0, iter - keybuffer.begin());
if (keybuffer.empty())
{
linealreadyemptykludge = true;
}
}
else if ( evt.key.keysym.sym == SDLK_v &&
keymap[SDLK_LCTRL] )
{
keybuffer += SDL_GetClipboardText();
}
}
break;
}
case SDL_KEYUP:
keymap[evt.key.keysym.sym] = false;
if (evt.key.keysym.sym == SDLK_BACKSPACE)
{
pressedbackspace = false;
}
break;
case SDL_TEXTINPUT:
if (!altpressed)
{
keybuffer += evt.text.text;
}
break;
/* Mouse Input */
case SDL_MOUSEMOTION:
mx = evt.motion.x;
my = evt.motion.y;
break;
case SDL_MOUSEBUTTONDOWN:
switch (evt.button.button)
{
case SDL_BUTTON_LEFT:
mx = evt.button.x;
my = evt.button.y;
leftbutton = 1;
break;
case SDL_BUTTON_RIGHT:
mx = evt.button.x;
my = evt.button.y;
rightbutton = 1;
break;
case SDL_BUTTON_MIDDLE:
mx = evt.button.x;
my = evt.button.y;
middlebutton = 1;
break;
}
break;
case SDL_MOUSEBUTTONUP:
switch (evt.button.button)
{
case SDL_BUTTON_LEFT:
mx = evt.button.x;
my = evt.button.y;
leftbutton=0;
break;
case SDL_BUTTON_RIGHT:
mx = evt.button.x;
my = evt.button.y;
rightbutton=0;
break;
case SDL_BUTTON_MIDDLE:
mx = evt.button.x;
my = evt.button.y;
middlebutton=0;
break;
}
break;
/* Controller Input */
case SDL_CONTROLLERBUTTONDOWN:
buttonmap[(SDL_GameControllerButton) evt.cbutton.button] = true;
break;
case SDL_CONTROLLERBUTTONUP:
buttonmap[(SDL_GameControllerButton) evt.cbutton.button] = false;
break;
case SDL_CONTROLLERAXISMOTION:
switch (evt.caxis.axis)
{
case SDL_CONTROLLER_AXIS_LEFTX:
if ( evt.caxis.value > -sensitivity &&
evt.caxis.value < sensitivity )
{
xVel = 0;
}
else
{
xVel = (evt.caxis.value > 0) ? 1 : -1;
}
break;
case SDL_CONTROLLER_AXIS_LEFTY:
if ( evt.caxis.value > -sensitivity &&
evt.caxis.value < sensitivity )
{
yVel = 0;
}
else
{
yVel = (evt.caxis.value > 0) ? 1 : -1;
}
break;
}
break;
case SDL_CONTROLLERDEVICEADDED:
{
SDL_GameController *toOpen = SDL_GameControllerOpen(evt.cdevice.which);
printf(
"Opened SDL_GameController ID #%i, %s\n",
evt.cdevice.which,
SDL_GameControllerName(toOpen)
);
controllers[SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(toOpen))] = toOpen;
break;
}
case SDL_CONTROLLERDEVICEREMOVED:
{
SDL_GameController *toClose = controllers[evt.cdevice.which];
controllers.erase(evt.cdevice.which);
printf("Closing %s\n", SDL_GameControllerName(toClose));
SDL_GameControllerClose(toClose);
break;
}
/* Window Events */
case SDL_WINDOWEVENT:
switch (evt.window.event)
{
/* Window Resize */
case SDL_WINDOWEVENT_RESIZED:
resetWindow = true;
break;
/* Window Focus */
case SDL_WINDOWEVENT_FOCUS_GAINED:
if (!game.disablepause)
{
isActive = true;
}
if (!useFullscreenSpaces)
{
if (wasFullscreen)
{
graphics.screenbuffer->isWindowed = false;
SDL_SetWindowFullscreen(
SDL_GetWindowFromID(evt.window.windowID),
SDL_WINDOW_FULLSCREEN_DESKTOP
);
}
}
SDL_DisableScreenSaver();
if (!game.disablepause && Mix_PlayingMusic())
{
// Correct songStart for how long we were paused
music.songStart += SDL_GetPerformanceCounter() - pauseStart;
}
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
if (!game.disablepause)
{
isActive = false;
}
if (!useFullscreenSpaces)
{
wasFullscreen = !graphics.screenbuffer->isWindowed;
graphics.screenbuffer->isWindowed = true;
SDL_SetWindowFullscreen(
SDL_GetWindowFromID(evt.window.windowID),
0
);
}
SDL_EnableScreenSaver();
if (!game.disablepause)
{
pauseStart = SDL_GetPerformanceCounter();
}
break;
/* Mouse Focus */
case SDL_WINDOWEVENT_ENTER:
SDL_DisableScreenSaver();
break;
case SDL_WINDOWEVENT_LEAVE:
SDL_EnableScreenSaver();
break;
}
break;
/* Quit Event */
case SDL_QUIT:
quitProgram = true;
break;
}
}
}
bool KeyPoll::isDown(SDL_Keycode key)
{
return keymap[key];
}
bool KeyPoll::isUp(SDL_Keycode key)
{
return !keymap[key];
}
bool KeyPoll::isDown(std::vector<SDL_GameControllerButton> buttons)
{
for (size_t i = 0; i < buttons.size(); i += 1)
{
if (buttonmap[buttons[i]])
{
return true;
}
}
return false;
}
bool KeyPoll::isDown(SDL_GameControllerButton button)
{
return buttonmap[button];
}
bool KeyPoll::controllerButtonDown()
{
for (
SDL_GameControllerButton button = SDL_CONTROLLER_BUTTON_A;
button < SDL_CONTROLLER_BUTTON_DPAD_UP;
button = (SDL_GameControllerButton) (button + 1)
) {
if (isDown(button))
{
return true;
}
}
return false;
}
bool KeyPoll::controllerWantsLeft(bool includeVert)
{
return ( buttonmap[SDL_CONTROLLER_BUTTON_DPAD_LEFT] ||
xVel < 0 ||
( includeVert &&
( buttonmap[SDL_CONTROLLER_BUTTON_DPAD_UP] ||
yVel < 0 ) ) );
}
bool KeyPoll::controllerWantsRight(bool includeVert)
{
return ( buttonmap[SDL_CONTROLLER_BUTTON_DPAD_RIGHT] ||
xVel > 0 ||
( includeVert &&
( buttonmap[SDL_CONTROLLER_BUTTON_DPAD_DOWN] ||
yVel > 0 ) ) );
}