2020-01-01 21:29:24 +01:00
|
|
|
#ifndef KEYPOLL_H
|
|
|
|
#define KEYPOLL_H
|
|
|
|
|
|
|
|
#include <map> // FIXME: I should feel very bad for using C++ -flibit
|
2020-07-19 06:21:27 +02:00
|
|
|
#include <SDL.h>
|
2020-07-19 21:43:29 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2020-01-01 21:29:24 +01:00
|
|
|
|
|
|
|
enum Kybrd
|
|
|
|
{
|
2021-09-07 03:56:39 +02:00
|
|
|
KEYBOARD_UP = SDLK_UP,
|
|
|
|
KEYBOARD_DOWN = SDLK_DOWN,
|
|
|
|
KEYBOARD_LEFT = SDLK_LEFT,
|
|
|
|
KEYBOARD_RIGHT = SDLK_RIGHT,
|
|
|
|
KEYBOARD_ENTER = SDLK_RETURN,
|
|
|
|
KEYBOARD_SPACE = SDLK_SPACE,
|
|
|
|
|
|
|
|
KEYBOARD_w = SDLK_w,
|
|
|
|
KEYBOARD_s = SDLK_s,
|
|
|
|
KEYBOARD_a = SDLK_a,
|
|
|
|
KEYBOARD_d = SDLK_d,
|
|
|
|
KEYBOARD_e = SDLK_e,
|
|
|
|
KEYBOARD_m = SDLK_m,
|
|
|
|
KEYBOARD_n = SDLK_n,
|
|
|
|
|
|
|
|
KEYBOARD_v = SDLK_v,
|
|
|
|
KEYBOARD_z = SDLK_z,
|
|
|
|
|
|
|
|
KEYBOARD_BACKSPACE = SDLK_BACKSPACE
|
2020-01-01 21:29:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class KeyPoll
|
|
|
|
{
|
|
|
|
public:
|
2021-09-07 03:56:39 +02:00
|
|
|
std::map<SDL_Keycode, bool> keymap;
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2021-09-07 03:56:39 +02:00
|
|
|
bool isActive;
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2021-09-07 03:56:39 +02:00
|
|
|
bool resetWindow;
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2021-09-07 03:56:39 +02:00
|
|
|
void toggleFullscreen(void);
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2021-09-07 03:56:39 +02:00
|
|
|
int sensitivity;
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2021-09-07 03:56:39 +02:00
|
|
|
int inline getThreshold(void);
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2021-09-07 03:56:39 +02:00
|
|
|
KeyPoll(void);
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2021-09-07 03:56:39 +02:00
|
|
|
void enabletextentry(void);
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2021-09-07 03:56:39 +02:00
|
|
|
void disabletextentry(void);
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2021-09-07 03:56:39 +02:00
|
|
|
void Poll(void);
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2021-09-07 03:56:39 +02:00
|
|
|
bool isDown(SDL_Keycode key);
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2021-09-07 03:56:39 +02:00
|
|
|
bool isDown(std::vector<SDL_GameControllerButton> buttons);
|
|
|
|
bool isDown(SDL_GameControllerButton button);
|
|
|
|
bool controllerButtonDown(void);
|
|
|
|
bool controllerWantsLeft(bool includeVert);
|
|
|
|
bool controllerWantsRight(bool includeVert);
|
2022-12-30 22:57:24 +01:00
|
|
|
bool controllerWantsUp(void);
|
|
|
|
bool controllerWantsDown(void);
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2021-09-07 03:56:39 +02:00
|
|
|
int leftbutton, rightbutton, middlebutton;
|
2023-08-18 04:57:54 +02:00
|
|
|
int mousex;
|
|
|
|
int mousey;
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2021-09-07 03:56:39 +02:00
|
|
|
bool textentry(void);
|
|
|
|
bool pressedbackspace;
|
|
|
|
std::string keybuffer;
|
2024-08-01 19:18:20 +02:00
|
|
|
std::string imebuffer;
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2021-09-07 03:56:39 +02:00
|
|
|
bool linealreadyemptykludge;
|
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
|
|
|
|
2020-01-01 21:29:24 +01:00
|
|
|
private:
|
2021-09-07 03:56:39 +02:00
|
|
|
std::map<SDL_JoystickID, SDL_GameController*> controllers;
|
|
|
|
std::map<SDL_GameControllerButton, bool> buttonmap;
|
|
|
|
int xVel, yVel;
|
|
|
|
Uint32 wasFullscreen;
|
2020-01-01 21:29:24 +01:00
|
|
|
};
|
|
|
|
|
2020-09-28 04:15:06 +02:00
|
|
|
#ifndef KEY_DEFINITION
|
Allow using help/graphics/music/game/key/map/obj everywhere
This commit makes `help`, `graphics`, `music`, `game`, `key`, `map`, and
`obj` essentially static global objects that can be used everywhere.
This is useful in case we ever need to add a new function in the future,
so we don't have to bother with passing a new argument in which means we
have to pass a new argument in to the function that calls that function
which means having to pass a new argument into the function that calls
THAT function, etc. which is a real headache when working on fan mods of
the source code.
Note that this changes NONE of the existing function signatures, it
merely just makes those variables accessible everywhere in the same way
`script` and `ed` are.
Also note that some classes had to be initialized after the filesystem
was initialized, but C++ would keep initializing them before the
filesystem got initialized, because I *had* to put them at the top of
`main.cpp`, or else they wouldn't be global variables.
The only way to work around this was to use entityclass's initialization
style (which I'm pretty sure entityclass of all things doesn't need to
be initialized this way), where you actually initialize the class in an
`init()` function, and so then you do `graphics.init()` after the
filesystem initialization, AFTER doing `Graphics graphics` up at the
top.
I've had to do this for `graphics` (but only because its child
GraphicsResources `grphx` needs to be initialized this way), `music`,
and `game`. I don't think this will affect anything. Other than that,
`help`, `key`, and `map` are still using the C++-intended method of
having ClassName::ClassName() functions.
2020-01-29 08:35:03 +01:00
|
|
|
extern KeyPoll key;
|
2020-09-28 04:15:06 +02:00
|
|
|
#endif
|
2020-01-01 21:29:24 +01:00
|
|
|
|
|
|
|
#endif /* KEYPOLL_H */
|