1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2025-09-09 22:06:01 +00:00

correct text handling in SDL2

This commit is contained in:
Zeno Rogue
2024-06-28 12:20:26 +02:00
parent 920ed4bb86
commit 8a45b7e1e7
5 changed files with 74 additions and 11 deletions

View File

@@ -1443,16 +1443,26 @@ EX namespace dialog {
}
display();
#if CAP_SDL2
texthandler = [&ne] (const SDL_TextInputEvent& ev) {
if(key_actions.count(ev.text[0])) return;
ne.s += ev.text;
ne.apply_edit();
};
#endif
keyhandler = [this, &ne] (int sym, int uni) {
handleNavigation(sym, uni);
if((uni >= '0' && uni <= '9') || among(uni, '.', '+', '-', '*', '/', '^', '(', ')', ',', '|', 3) || (uni >= 'a' && uni <= 'z')) {
#if !CAP_SDL2
if(uni == 3) ne.s += "pi";
else ne.s += uni;
apply_edit();
#endif
}
else if(uni == '\b' || uni == '\t') {
ne.s = ne.s. substr(0, isize(ne.s)-1);
ne.s = ne.s. substr(0, isize(ne.s)-utfsize_before(ne.s, isize(ne.s)));
sscanf(ne.s.c_str(), LDF, ne.editwhat);
apply_edit();
}
@@ -1770,6 +1780,13 @@ EX namespace dialog {
dialog::addItem("cancel", SDLK_ESCAPE);
dialog::display();
#if CAP_SDL2
texthandler = [this] (const SDL_TextInputEvent& ev) {
int i = isize(*cfileptr) - (editext?0:4);
cfileptr->insert(i, ev.text);
};
#endif
keyhandler = handleKeyFile;
}
@@ -1786,15 +1803,18 @@ EX namespace dialog {
if(ac) popScreen();
}
else if(sym == SDLK_BACKSPACE && i) {
s.erase(i-1, 1);
int len = utfsize_before(s, i);
s.erase(i-len, len);
highlight_text = "//missing";
list_skip = 0;
}
#if !CAP_SDL2
else if(uni >= 32 && uni < 127) {
s.insert(i, s0 + char(uni));
highlight_text = "//missing";
list_skip = 0;
}
#endif
return;
}
@@ -1880,6 +1900,7 @@ EX namespace dialog {
void draw() override;
void start_editing(string& s);
bool handle_edit_string(int sym, int uni, function<string(int, int)> checker = editchecker);
void handle_textinput();
};
#endif
@@ -1899,25 +1920,40 @@ EX namespace dialog {
bool string_dialog::handle_edit_string(int sym, int uni, function<string(int, int)> checker) {
auto& es = *edited_string;
string u2;
if(DKEY == SDLK_LEFT) editpos--;
else if(DKEY == SDLK_RIGHT) editpos++;
if(DKEY == SDLK_LEFT) editpos -= utfsize_before(es, editpos);
else if(DKEY == SDLK_RIGHT) editpos += utfsize(es[editpos]);
else if(uni == 8) {
if(editpos == 0) return true;
es.replace(editpos-1, 1, "");
editpos--;
int len = utfsize_before(es, editpos);
es.replace(editpos-len, len, "");
editpos -= len;
if(reaction) reaction();
}
else if((u2 = checker(sym, uni)) != "") {
#if !CAP_SDL2
for(char c: u2) {
es.insert(editpos, 1, c);
editpos ++;
}
#endif
if(reaction) reaction();
}
else return false;
return true;
}
void string_dialog::handle_textinput() {
#if CAP_SDL2
texthandler = [this] (const SDL_TextInputEvent& ev) {
auto& es = *edited_string;
string txt = ev.text;
es.insert(editpos, txt);
editpos += isize(txt);
if(reaction) reaction();
};
#endif
}
void string_dialog::draw() {
cmode = sm::NUMBER | dialogflags;
gamescreen();
@@ -1942,6 +1978,8 @@ EX namespace dialog {
if(handle_edit_string(sym, uni)) ;
else if(doexiton(sym, uni)) popfinal();
};
handle_textinput();
}
EX void edit_string(string& s, string title, string help) {