Skip to content

Commit c4acce0

Browse files
committed
Simplify text input and improve backspace handling
1 parent a35130e commit c4acce0

3 files changed

Lines changed: 8 additions & 10 deletions

File tree

src/Input.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,15 @@ void Input::Update()
9999
|| (e.key.keysym.sym == SDLK_SPACE)) {
100100
char ch = (char)e.key.keysym.sym;
101101
ch = shift ? toupper(ch) : ch;
102-
text.write(&ch, 1);
102+
text += ch;
103103
}
104104
else if (e.key.keysym.sym == SDLK_LSHIFT
105105
|| e.key.keysym.sym == SDLK_RSHIFT) {
106106
shift = true;
107107
}
108-
else if (e.key.keysym.sym == SDLK_BACKSPACE && text.tellp() > 0) {
109-
long pos = text.tellp();
110-
if (pos > 0)
111-
text.seekp(pos - 1);
108+
else if (e.key.keysym.sym == SDLK_BACKSPACE) {
109+
if (!text.empty())
110+
text.erase(text.end() - 1);
112111
}
113112
}
114113
break;
@@ -248,7 +247,7 @@ void Input::OpenCharBuffer(int max)
248247

249248
shift = false;
250249
maxchar = max;
251-
text.str("");
250+
text = "";
252251
textinput = true;
253252
}
254253

@@ -268,5 +267,5 @@ void Input::CloseCharBuffer()
268267
//
269268
string Input::GetInput() const
270269
{
271-
return text.str();
270+
return text;
272271
}

src/Input.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222

2323
#include "Platform.hpp"
2424

25-
#include <sstream>
26-
2725
//
2826
// A singleton class to manage SDL input.
2927
//
@@ -60,7 +58,7 @@ class Input {
6058

6159
bool shift;
6260
bool textinput; // Is a character buffer open?
63-
ostringstream text; // Text read so far
61+
string text; // Text read so far
6462
int maxchar; // Maximum number of characters to read
6563

6664
// Record joystick state

src/Options.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "InterfaceSounds.hpp"
2525

2626
#include <cassert>
27+
#include <sstream>
2728

2829
const double Options::FADE_SPEED = 0.1;
2930

0 commit comments

Comments
 (0)