1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-11-10 13:09:43 +01:00
VVVVVV/desktop_version/src/KeyPoll.cpp

353 lines
7.1 KiB
C++
Raw Normal View History

2020-01-01 21:29:24 +01:00
#include "KeyPoll.h"
#include <stdio.h>
#include <string.h>
#include <utf8/unchecked.h>
2020-01-01 21:29:24 +01:00
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;
textentrymode=true;
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);
}
}
Fix frame-ordering backspacing empty line bug in script editor There is a long-standing bug with the script editor where if you delete the last character of a line, it IMMEDIATELY deletes the line you're on, and then moves your cursor back to the previous line. This is annoying, to say the least. The reason for this is that, in the sequence of events that happens in one frame (known as frame ordering), the code that backspaces one character from the line when you press Backspace is ran BEFORE the code to remove an empty line if you backspace it is ran. The former is located in key.Poll(), and the latter is located in editorinput(). Thus, when you press Backspace, the game first runs key.Poll(), sees that you've pressed Backspace, and dutifully removes the last character from a line. The line is now empty. Then, when the game gets around to the "Are you pressing Backspace on an empty line?" check in editorinput(), it thinks that you're pressing Backspace on an empty line, and then does the usual line-removing stuff. And actually, when it does the check in editorinput(), it ACTUALLY asks "Are you pressing Backspace on THIS frame and was the line empty LAST frame?" because it's checking against its own copy of the input buffer, before copying the input buffer to its own local copy. So the problem only happens if you press and hold Backspace for more than 1 frame. It's a small consolation prize for this annoyance, getting to tap-tap-tap Backspace in the hopes that you only press it for 1 frame, while in the middle of something more important to do like, oh I don't know, writing a script. So there are two potential solutions here: (1) Just change the frame ordering around. This is risky to say the least, because I'm not sure what behavior depends on exactly which frame order. It's not like it's key.Poll() and then IMMEDIATELY afterwards editorinput() is run, it's more like key.Poll(), some things that obviously depend on key.Poll() running before them, and THEN editorinput(). Also, editorinput() is only one possible thing that could be ran afterwards, on the next frame we could be running something else entirely instead. (2) Add a kludge variable to signal when the line is ALREADY empty so the game doesn't re-check the already-empty line and conclude that you're already immediately backspacing an empty line. I went with (2) for this commit, and I've added the kludge variable key.linealreadyemptykludge. However, that by itself isn't enough to fix it. It only adds about a frame or so of delay before the game goes right back to saying "Oh, you're ALREADY somehow pressing backspace again? I'll just delete this line real quick" and the behavior is basically the same as before, except now you have to hit Backspace for TWO frames or less instead of one in order to not have it happen. What we need is to have a delay set as well, when the game deletes the last line of a char. So I set ed.keydelay to 6 as well if editorinput() sses that key.linealreadyemptykludge is on.
2020-01-19 03:17:46 +01:00
linealreadyemptykludge = false;
2020-01-01 21:29:24 +01:00
}
void KeyPoll::enabletextentry()
{
keybuffer="";
textentrymode = true;
SDL_StartTextInput();
}
void KeyPoll::disabletextentry()
{
textentrymode = false;
SDL_StopTextInput();
}
void KeyPoll::Poll()
2020-01-01 21:29:24 +01:00
{
SDL_Event evt;
while (SDL_PollEvent(&evt))
{
/* Keyboard Input */
if (evt.type == SDL_KEYDOWN)
{
keymap[evt.key.keysym.sym] = true;
2020-01-01 21:29:24 +01:00
if (evt.key.keysym.sym == SDLK_BACKSPACE)
{
pressedbackspace = true;
}
#ifdef __APPLE__ /* OSX prefers the command keys over the alt keys. -flibit */
bool altpressed = keymap[SDLK_LGUI] || keymap[SDLK_RGUI];
2020-01-01 21:29:24 +01:00
#else
bool altpressed = keymap[SDLK_LALT] || keymap[SDLK_RALT];
2020-01-01 21:29:24 +01:00
#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)
2020-01-01 21:29:24 +01:00
{
toggleFullscreen = true;
}
if (textentrymode)
{
if (evt.key.keysym.sym == SDLK_BACKSPACE && !keybuffer.empty())
2020-01-01 21:29:24 +01:00
{
std::string::iterator iter = keybuffer.end();
utf8::unchecked::prior(iter);
keybuffer = keybuffer.substr(0, iter - keybuffer.begin());
if (keybuffer.empty())
Fix frame-ordering backspacing empty line bug in script editor There is a long-standing bug with the script editor where if you delete the last character of a line, it IMMEDIATELY deletes the line you're on, and then moves your cursor back to the previous line. This is annoying, to say the least. The reason for this is that, in the sequence of events that happens in one frame (known as frame ordering), the code that backspaces one character from the line when you press Backspace is ran BEFORE the code to remove an empty line if you backspace it is ran. The former is located in key.Poll(), and the latter is located in editorinput(). Thus, when you press Backspace, the game first runs key.Poll(), sees that you've pressed Backspace, and dutifully removes the last character from a line. The line is now empty. Then, when the game gets around to the "Are you pressing Backspace on an empty line?" check in editorinput(), it thinks that you're pressing Backspace on an empty line, and then does the usual line-removing stuff. And actually, when it does the check in editorinput(), it ACTUALLY asks "Are you pressing Backspace on THIS frame and was the line empty LAST frame?" because it's checking against its own copy of the input buffer, before copying the input buffer to its own local copy. So the problem only happens if you press and hold Backspace for more than 1 frame. It's a small consolation prize for this annoyance, getting to tap-tap-tap Backspace in the hopes that you only press it for 1 frame, while in the middle of something more important to do like, oh I don't know, writing a script. So there are two potential solutions here: (1) Just change the frame ordering around. This is risky to say the least, because I'm not sure what behavior depends on exactly which frame order. It's not like it's key.Poll() and then IMMEDIATELY afterwards editorinput() is run, it's more like key.Poll(), some things that obviously depend on key.Poll() running before them, and THEN editorinput(). Also, editorinput() is only one possible thing that could be ran afterwards, on the next frame we could be running something else entirely instead. (2) Add a kludge variable to signal when the line is ALREADY empty so the game doesn't re-check the already-empty line and conclude that you're already immediately backspacing an empty line. I went with (2) for this commit, and I've added the kludge variable key.linealreadyemptykludge. However, that by itself isn't enough to fix it. It only adds about a frame or so of delay before the game goes right back to saying "Oh, you're ALREADY somehow pressing backspace again? I'll just delete this line real quick" and the behavior is basically the same as before, except now you have to hit Backspace for TWO frames or less instead of one in order to not have it happen. What we need is to have a delay set as well, when the game deletes the last line of a char. So I set ed.keydelay to 6 as well if editorinput() sses that key.linealreadyemptykludge is on.
2020-01-19 03:17:46 +01:00
{
linealreadyemptykludge = true;
}
2020-01-01 21:29:24 +01:00
}
else if ( evt.key.keysym.sym == SDLK_v &&
keymap[SDLK_LCTRL] )
{
keybuffer += SDL_GetClipboardText();
}
}
}
else if (evt.type == SDL_KEYUP)
{
keymap[evt.key.keysym.sym] = false;
if (evt.key.keysym.sym == SDLK_BACKSPACE)
{
pressedbackspace = false;
}
}
else if (evt.type == SDL_TEXTINPUT)
{
keybuffer += evt.text.text;
}
/* Mouse Input */
else if (evt.type == SDL_MOUSEMOTION)
{
mx = evt.motion.x;
my = evt.motion.y;
}
else if (evt.type == SDL_MOUSEBUTTONDOWN)
{
if (evt.button.button == SDL_BUTTON_LEFT)
{
mx = evt.button.x;
my = evt.button.y;
leftbutton = 1;
}
else if (evt.button.button == SDL_BUTTON_RIGHT)
{
mx = evt.button.x;
my = evt.button.y;
rightbutton = 1;
}
else if (evt.button.button == SDL_BUTTON_MIDDLE)
{
mx = evt.button.x;
my = evt.button.y;
middlebutton = 1;
}
}
else if (evt.type == SDL_MOUSEBUTTONUP)
{
if (evt.button.button == SDL_BUTTON_LEFT)
{
mx = evt.button.x;
my = evt.button.y;
leftbutton=0;
}
else if (evt.button.button == SDL_BUTTON_RIGHT)
{
mx = evt.button.x;
my = evt.button.y;
rightbutton=0;
}
else if (evt.button.button == SDL_BUTTON_MIDDLE)
{
mx = evt.button.x;
my = evt.button.y;
middlebutton=0;
}
}
/* Controller Input */
else if (evt.type == SDL_CONTROLLERBUTTONDOWN)
{
buttonmap[(SDL_GameControllerButton) evt.cbutton.button] = true;
}
else if (evt.type == SDL_CONTROLLERBUTTONUP)
{
buttonmap[(SDL_GameControllerButton) evt.cbutton.button] = false;
}
else if (evt.type == SDL_CONTROLLERAXISMOTION)
{
if (evt.caxis.axis == SDL_CONTROLLER_AXIS_LEFTX)
{
if ( evt.caxis.value > -sensitivity &&
evt.caxis.value < sensitivity )
{
xVel = 0;
}
else
{
xVel = (evt.caxis.value > 0) ? 1 : -1;
}
}
if (evt.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY)
{
if ( evt.caxis.value > -sensitivity &&
evt.caxis.value < sensitivity )
{
yVel = 0;
}
else
{
yVel = (evt.caxis.value > 0) ? 1 : -1;
}
}
}
else if (evt.type == 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;
}
else if (evt.type == SDL_CONTROLLERDEVICEREMOVED)
{
SDL_GameController *toClose = controllers[evt.cdevice.which];
controllers.erase(evt.cdevice.which);
printf("Closing %s\n", SDL_GameControllerName(toClose));
SDL_GameControllerClose(toClose);
}
/* Window Events */
else if (evt.type == SDL_WINDOWEVENT)
{
/* Window Resize */
if (evt.window.event == SDL_WINDOWEVENT_RESIZED)
{
resetWindow = true;
}
/* Window Focus */
else if (evt.window.event == SDL_WINDOWEVENT_FOCUS_GAINED)
{
isActive = true;
if (!useFullscreenSpaces)
{
2020-06-13 16:35:05 +02:00
if (wasFullscreen)
{
gameScreen.isWindowed = false;
2020-06-13 16:35:05 +02:00
SDL_SetWindowFullscreen(
SDL_GetWindowFromID(evt.window.windowID),
SDL_WINDOW_FULLSCREEN_DESKTOP
);
}
2020-01-01 21:29:24 +01:00
}
SDL_DisableScreenSaver();
}
else if (evt.window.event == SDL_WINDOWEVENT_FOCUS_LOST)
{
isActive = false;
if (!useFullscreenSpaces)
{
wasFullscreen = !gameScreen.isWindowed;
gameScreen.isWindowed = true;
2020-01-01 21:29:24 +01:00
SDL_SetWindowFullscreen(
SDL_GetWindowFromID(evt.window.windowID),
2020-06-13 16:35:05 +02:00
0
2020-01-01 21:29:24 +01:00
);
}
SDL_EnableScreenSaver();
}
/* Mouse Focus */
else if (evt.window.event == SDL_WINDOWEVENT_ENTER)
{
SDL_DisableScreenSaver();
}
else if (evt.window.event == SDL_WINDOWEVENT_LEAVE)
{
SDL_EnableScreenSaver();
}
}
/* Quit Event */
else if (evt.type == SDL_QUIT)
{
quitProgram = true;
}
}
}
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 ) ) );
}