From 2687090ac275673fca71db5842ee7c804819460c Mon Sep 17 00:00:00 2001 From: Info Teddy Date: Sat, 18 Jan 2020 18:17:46 -0800 Subject: [PATCH] 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. --- desktop_version/src/KeyPoll.cpp | 7 +++++++ desktop_version/src/KeyPoll.h | 2 ++ desktop_version/src/editor.cpp | 8 +++++++- desktop_version/src/main.cpp | 3 +++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/desktop_version/src/KeyPoll.cpp b/desktop_version/src/KeyPoll.cpp index e199df13..1c3e6c6e 100644 --- a/desktop_version/src/KeyPoll.cpp +++ b/desktop_version/src/KeyPoll.cpp @@ -50,6 +50,8 @@ KeyPoll::KeyPoll() useFullscreenSpaces = (strcmp(hint, "1") == 0); } } + + linealreadyemptykludge = false; } void KeyPoll::enabletextentry() @@ -94,7 +96,12 @@ void KeyPoll::Poll() { if (evt.key.keysym.sym == SDLK_BACKSPACE) { + bool kbemptybefore = keybuffer.empty(); keybuffer = keybuffer.substr(0, keybuffer.length() - 1); + if (!kbemptybefore && keybuffer.empty()) + { + linealreadyemptykludge = true; + } } else if ( evt.key.keysym.sym == SDLK_v && keymap[SDLK_LCTRL] ) diff --git a/desktop_version/src/KeyPoll.h b/desktop_version/src/KeyPoll.h index 0d803e96..2a21405c 100644 --- a/desktop_version/src/KeyPoll.h +++ b/desktop_version/src/KeyPoll.h @@ -71,6 +71,8 @@ public: bool pressedbackspace; std::string keybuffer; + bool linealreadyemptykludge; + private: std::map controllers; std::map buttonmap; diff --git a/desktop_version/src/editor.cpp b/desktop_version/src/editor.cpp index 1375889a..6ec05ba4 100644 --- a/desktop_version/src/editor.cpp +++ b/desktop_version/src/editor.cpp @@ -3846,7 +3846,13 @@ void editorinput( KeyPoll& key, Graphics& dwgfx, Game& game, mapclass& map, enti key.keybuffer=ed.sb[ed.pagey+ed.sby]; } - if(key.pressedbackspace && ed.sb[ed.pagey+ed.sby]=="") + if(key.linealreadyemptykludge) + { + ed.keydelay=6; + key.linealreadyemptykludge=false; + } + + if(key.pressedbackspace && ed.sb[ed.pagey+ed.sby]=="" && ed.keydelay<=0) { //Remove this line completely ed.removeline(ed.pagey+ed.sby); diff --git a/desktop_version/src/main.cpp b/desktop_version/src/main.cpp index 1ad67575..f50be9b3 100644 --- a/desktop_version/src/main.cpp +++ b/desktop_version/src/main.cpp @@ -473,6 +473,9 @@ int main(int argc, char *argv[]) } + //We did editorinput, now it's safe to turn this off + key.linealreadyemptykludge = false; + if (game.savemystats) { game.savemystats = false;