1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2024-11-23 21:07:17 +00:00

dialog:: abstracted string editing

This commit is contained in:
Zeno Rogue 2018-11-07 00:50:03 +01:00
parent ade8bda5f9
commit 7e80be5a87
4 changed files with 89 additions and 43 deletions

View File

@ -929,8 +929,6 @@ vector<archimedean_tiling> tilings;
int spos = 0; int spos = 0;
int editpos = 0;
archimedean_tiling edited; archimedean_tiling edited;
bool symbol_editing; bool symbol_editing;
@ -1008,11 +1006,7 @@ void show() {
dialog::init(XLAT("Archimedean tilings")); dialog::init(XLAT("Archimedean tilings"));
if(symbol_editing) { if(symbol_editing) {
string cs = edited.symbol; dialog::addSelItem("edit", dialog::view_edited_string(), '/');
if(editpos < 0) editpos = 0;
if(editpos > isize(cs)) editpos = isize(cs);
cs.insert(editpos, "°");
dialog::addSelItem("edit", cs, '/');
dialog::add_action([] () { dialog::add_action([] () {
symbol_editing = false; symbol_editing = false;
if(!edited.errors) enable(edited); if(!edited.errors) enable(edited);
@ -1038,7 +1032,7 @@ void show() {
dialog::add_action([] () { dialog::add_action([] () {
symbol_editing = true; symbol_editing = true;
edited = current; edited = current;
editpos = isize(current.symbol); dialog::start_editing(edited.symbol);
edited.parse(); edited.parse();
}); });
dialog::addBreak(100); dialog::addBreak(100);
@ -1128,19 +1122,8 @@ void show() {
keyhandler = [] (int sym, int uni) { keyhandler = [] (int sym, int uni) {
if(symbol_editing && sym == SDLK_RETURN) sym = uni = '/'; if(symbol_editing && sym == SDLK_RETURN) sym = uni = '/';
if(sym == SDLK_LEFT) editpos--;
if(sym == SDLK_RIGHT) editpos++;
dialog::handleNavigation(sym, uni); dialog::handleNavigation(sym, uni);
if(symbol_editing && uni == 8) { if(symbol_editing && dialog::handle_edit_string(sym, uni)) {
if(editpos == 0) return;
edited.symbol.replace(editpos-1, 1, "");
editpos--;
edited.parse(edited.symbol);
return;
}
if(symbol_editing && uni >= 32 && uni < 128) {
edited.symbol.insert(editpos, 1, uni);
editpos++;
edited.parse(edited.symbol); edited.parse(edited.symbol);
return; return;
} }

View File

@ -596,6 +596,7 @@ namespace dialog {
string disp(ld x) { if(ne.intval) return its(ldtoint(x)); else if(ne.vmax-ne.vmin < 1) return fts4(x); else return fts(x); } string disp(ld x) { if(ne.intval) return its(ldtoint(x)); else if(ne.vmax-ne.vmin < 1) return fts4(x); else return fts(x); }
reaction_t reaction; reaction_t reaction;
reaction_t reaction_final;
reaction_t extra_options; reaction_t extra_options;
@ -770,7 +771,7 @@ namespace dialog {
ne.sc.inverse(d * (ne.sc.direct(ne.vmax) - ne.sc.direct(ne.vmin)) + ne.sc.direct(ne.vmin)); ne.sc.inverse(d * (ne.sc.direct(ne.vmax) - ne.sc.direct(ne.vmin)) + ne.sc.direct(ne.vmin));
affect('v'); affect('v');
} }
else if(doexiton(sym, uni)) popScreen(); else if(doexiton(sym, uni)) { popScreen(); if(reaction_final) reaction_final(); }
}; };
} }
@ -839,6 +840,7 @@ namespace dialog {
cmode |= sm::NUMBER; cmode |= sm::NUMBER;
pushScreen(drawNumberDialog); pushScreen(drawNumberDialog);
reaction = reaction_t(); reaction = reaction_t();
reaction_final = reaction_t();
extra_options = reaction_t(); extra_options = reaction_t();
numberdark = 0; numberdark = 0;
} }
@ -1021,6 +1023,79 @@ namespace dialog {
dialog::v.push_back(make_pair(s, color)); dialog::v.push_back(make_pair(s, color));
} }
int editpos = 0;
string *edited_string;
string view_edited_string() {
string cs = *edited_string;
if(editpos < 0) editpos = 0;
if(editpos > isize(cs)) editpos = isize(cs);
cs.insert(editpos, "°");
return cs;
}
void start_editing(string& s) {
edited_string = &s;
editpos = isize(s);
}
bool handle_edit_string(int sym, int uni) {
auto& es = *edited_string;
if(sym == SDLK_LEFT) editpos--;
else if(sym == SDLK_RIGHT) editpos++;
else if(uni == 8) {
if(editpos == 0) return true;
es.replace(editpos-1, 1, "");
editpos--;
}
else if(uni >= 32 && uni < 128) {
es.insert(editpos, 1, uni);
editpos++;
}
else return false;
return true;
}
void string_edit_dialog() {
cmode = sm::NUMBER | dialogflags;
gamescreen(numberdark);
init(ne.title);
addInfo(view_edited_string());
addBreak(100);
dialog::addBack();
addBreak(100);
if(ne.help != "") {
addHelp(ne.help);
}
if(extra_options) extra_options();
display();
keyhandler = [] (int sym, int uni) {
handleNavigation(sym, uni);
if(handle_edit_string(sym, uni)) ;
else if(doexiton(sym, uni)) {
popScreen();
if(reaction_final) reaction_final();
}
};
}
void edit_string(string& s, string title, string help) {
start_editing(s);
ne.title = title;
ne.help = help;
dialogflags = (cmode & sm::A3);
if(cmode & sm::SIDE) dialogflags |= sm::MAYDARK | sm::SIDE;
cmode |= sm::NUMBER;
pushScreen(string_edit_dialog);
reaction = reaction_t();
extra_options = reaction_t();
numberdark = 0;
}
}; };
} }

View File

@ -1741,7 +1741,7 @@ namespace dialog {
extern vector<item> items; extern vector<item> items;
extern reaction_t reaction, extra_options; extern reaction_t reaction, extra_options, reaction_final;
item& lastItem(); item& lastItem();
extern color_t *palette; extern color_t *palette;
@ -1797,6 +1797,11 @@ namespace dialog {
void addHelp(); void addHelp();
void addBack(); void addBack();
void add_action(reaction_t action); void add_action(reaction_t action);
string view_edited_string();
void start_editing(string& s);
bool handle_edit_string(int sym, int uni);
void edit_string(string& s, string title, string help);
} }
void checkStunKill(cell *dest); void checkStunKill(cell *dest);

View File

@ -350,7 +350,7 @@ void draw_to(ld t0, hyperpoint h0, ld t1, hyperpoint h1, int small = 0, int big
draw_to(t2, h2, t1, h1, small, big+1); draw_to(t2, h2, t1, h1, small, big+1);
} }
int editpos = -1, editwhich = -1; int editwhich = -1;
void show_graph() { void show_graph() {
cmode = sm::SIDE | sm::MAYDARK; cmode = sm::SIDE | sm::MAYDARK;
@ -358,15 +358,11 @@ void show_graph() {
dialog::init(XLAT("graph")); dialog::init(XLAT("graph"));
for(int i=0; i<isize(formula); i++) { for(int i=0; i<isize(formula); i++) {
if(editwhich == i) { if(editwhich == i) {
string cs = formula[i]; dialog::addItem(dialog::view_edited_string(), '1'+i);
if(editpos < 0) editpos = 0;
if(editpos > isize(cs)) editpos = isize(cs);
cs.insert(editpos, "°");
dialog::addItem(cs, '1'+i);
} }
else { else {
dialog::addItem(formula[i], editwhich == -1 ? '1'+i : 0); dialog::addItem(formula[i], editwhich == -1 ? '1'+i : 0);
dialog::add_action([i] () { editwhich = i; editpos = isize(formula[i]); }); dialog::add_action([i] () { editwhich = i; dialog::start_editing(formula[i]); });
} }
} }
@ -375,20 +371,7 @@ void show_graph() {
keyhandler = [] (int sym, int uni) { keyhandler = [] (int sym, int uni) {
if(editwhich >= 0) { if(editwhich >= 0) {
string& edited = formula[editwhich]; if(dialog::handle_edit_string(sym, uni)) ;
if(sym == SDLK_LEFT) editpos--;
else if(sym == SDLK_RIGHT) editpos++;
else if(uni == 8) {
if(editpos == 0) return;
edited.replace(editpos-1, 1, "");
editpos--;
return;
}
else if(uni >= 32 && uni < 128) {
edited.insert(editpos, 1, uni);
editpos++;
return;
}
else if(doexiton(sym, uni)) else if(doexiton(sym, uni))
editwhich = -1; editwhich = -1;
} }