diff --git a/dialogs.cpp b/dialogs.cpp index 7692b636..716373d9 100644 --- a/dialogs.cpp +++ b/dialogs.cpp @@ -838,4 +838,161 @@ namespace dialog { editNumber(x, vmin, vmax, step, dft, title, help); } + //-- choose file dialog-- + + bool filecmp(const pair &f1, const pair &f2) { + if(f1.first == "../") return true; + if(f2.first == "../") return false; + if(f1.second != f2.second) + return f1.second == CDIR; + return f1.first < f2.first; + } + + string filecaption, cfileext; + string *cfileptr; + bool editext = false; + + bool handleKeyFile(int sym, int uni); + + void drawFileDialog() { + displayfr(vid.xres/2, 30 + vid.fsize, 2, vid.fsize, + filecaption, forecolor, 8); + + string& cfile = *cfileptr; + + displayfr(vid.xres/2, 34 + vid.fsize * 2, 2, vid.fsize, + cfile, editext ? 0xFF00FF : 0xFFFF00, 8); + + displayButton(vid.xres*1/5, 38+vid.fsize * 3, + "F2 = save", 2000+SDLK_F2, 8); + displayButton(vid.xres*2/5, 38+vid.fsize * 3, + "F3 = load", 2000+SDLK_F3, 8); + displayButton(vid.xres*3/5, 38+vid.fsize * 3, + "F4 = extension", 2000+SDLK_F4, 8); + displayButton(vid.xres*4/5, 38+vid.fsize * 3, + "Enter = back", 2000+SDLK_RETURN, 8); + + v.clear(); + + DIR *d; + struct dirent *dir; + + string where = "."; + for(int i=0; id_name; + if(s != ".." && s[0] == '.') ; + else if(size(s) > 4 && s.substr(size(s)-4) == cfileext) + v.push_back(make_pair(s, CFILE)); + else if(dir->d_type & DT_DIR) + v.push_back(make_pair(s+"/", CDIR)); + } + closedir(d); + } + sort(v.begin(), v.end(), filecmp); + + int q = v.size(); + int percolumn = (vid.yres-38) / (vid.fsize+5) - 4; + int columns = 1 + (q-1) / percolumn; + + for(int i=0; i 2000) sym = uni - 2000; + if(sym == SDLK_RETURN || sym == SDLK_KP_ENTER || sym == SDLK_ESCAPE) { + popScreen(); + return true; + } +/* else if(sym == SDLK_F2 || sym == SDLK_F3) { + popScreen(); + return false; + } */ + else if(sym == SDLK_F4) { + editext = !editext; + } + else if(sym == SDLK_BACKSPACE && i) { + s.erase(i-1, 1); + } + else if(uni >= 32 && uni < 127) { + s.insert(i, s0 + char(uni)); + } + else if(uni >= 1000 && uni <= 1000+size(v)) { + string where = "", what = s, whereparent = "../"; + for(int i=0; i= 2 && s.substr(i-2,3) == "../") + whereparent = s.substr(0, i+1) + "../"; + else + whereparent = where; + where = s.substr(0, i+1), what = s.substr(i+1); + } + int i = uni - 1000; + if(v[i].first == "../") { + s = whereparent + what; + } + else if(v[i].second == CDIR) + s = where + v[i].first + what; + else + s = where + v[i].first; + } + return true; + } + + void openFileDialog(string& filename, string fcap, string ext) { + cfileptr = &filename; + filecaption = fcap; + cfileext = ext; + pushScreen(dialog::drawFileDialog); + } + + // infix/v/vpush + + string infix; + + bool hasInfix(const string &s) { + if(infix == "") return true; + string t = ""; + for(int i=0; i= 'a' && c <= 'z') tt += c - 32; + else if(c >= 'A' && c <= 'Z') tt += c; + else if(c == '@') tt += c; + if(tt) t += tt; + } + return t.find(infix) != string::npos; + } + + bool editInfix(int uni) { + if(uni >= 'A' && uni <= 'Z') infix += uni; + else if(uni >= 'a' && uni <= 'z') infix += uni-32; + else if(infix != "" && uni == 8) infix = infix.substr(0, size(infix)-1); + else if(infix != "" && uni != 0) infix = ""; + else return false; + return true; + } + + vector > v; + + void vpush(int i, const char *name) { + string s = XLATN(name); + if(!hasInfix(s)) return; + dialog::v.push_back(make_pair(s, i)); + } + }; diff --git a/hyper.h b/hyper.h index 01ac13ef..4398ee56 100644 --- a/hyper.h +++ b/hyper.h @@ -1133,6 +1133,15 @@ namespace dialog { extern bool sidedialog; extern int dialogflags; extern int dcenter; + + void openFileDialog(string& filename, string fcap, string ext); + + extern string infix; + bool hasInfix(const string &s); + bool editInfix(int uni); + + void vpush(int i, const char *name); + extern vector > v; } void checkStunKill(cell *dest); diff --git a/mapeditor.cpp b/mapeditor.cpp index ae50d16b..381bc277 100644 --- a/mapeditor.cpp +++ b/mapeditor.cpp @@ -25,7 +25,6 @@ namespace mapeditor { map modelcell; void handleKeyMap(int sym, int uni); - bool handleKeyFile(int sym, int uni); void applyModelcell(cell *c) { if(patterns::whichPattern == 'H') return; @@ -309,31 +308,6 @@ namespace mapeditor { bool drawplayer = true; - string infix; - - bool hasInfix(const string &s) { - if(infix == "") return true; - string t = ""; - for(int i=0; i= 'a' && c <= 'z') tt += c - 32; - else if(c >= 'A' && c <= 'Z') tt += c; - else if(c == '@') tt += c; - if(tt) t += tt; - } - return t.find(infix) != string::npos; - } - - bool editInfix(int uni) { - if(uni >= 'A' && uni <= 'Z') infix += uni; - else if(uni >= 'a' && uni <= 'z') infix += uni-32; - else if(infix != "" && uni == 8) infix = infix.substr(0, size(infix)-1); - else if(infix != "" && uni != 0) infix = ""; - else return false; - return true; - } - cell *drawcell; #if CAP_EDIT @@ -387,8 +361,6 @@ namespace mapeditor { return XLAT(mapeditorhelp) + XLAT(patthelp); } - vector > v; - struct undo_info { cell *c; eWall w; @@ -451,78 +423,10 @@ namespace mapeditor { } bool choosefile = false; - bool editext = false; #define CDIR 0xC0C0C0 #define CFILE forecolor - bool filecmp(const pair &f1, const pair &f2) { - if(f1.first == "../") return true; - if(f2.first == "../") return false; - if(f1.second != f2.second) - return f1.second == CDIR; - return f1.first < f2.first; - } - - string filecaption, cfileext; - string *cfileptr; - - void drawFileDialog() { - displayfr(vid.xres/2, 30 + vid.fsize, 2, vid.fsize, - filecaption, forecolor, 8); - - string& cfile = *cfileptr; - - displayfr(vid.xres/2, 34 + vid.fsize * 2, 2, vid.fsize, - cfile, editext ? 0xFF00FF : 0xFFFF00, 8); - - displayButton(vid.xres*1/5, 38+vid.fsize * 3, - "F2 = save", 2000+SDLK_F2, 8); - displayButton(vid.xres*2/5, 38+vid.fsize * 3, - "F3 = load", 2000+SDLK_F3, 8); - displayButton(vid.xres*3/5, 38+vid.fsize * 3, - "F4 = extension", 2000+SDLK_F4, 8); - displayButton(vid.xres*4/5, 38+vid.fsize * 3, - "Enter = back", 2000+SDLK_RETURN, 8); - - v.clear(); - - DIR *d; - struct dirent *dir; - - string where = "."; - for(int i=0; id_name; - if(s != ".." && s[0] == '.') ; - else if(size(s) > 4 && s.substr(size(s)-4) == cfileext) - v.push_back(make_pair(s, CFILE)); - else if(dir->d_type & DT_DIR) - v.push_back(make_pair(s+"/", CDIR)); - } - closedir(d); - } - sort(v.begin(), v.end(), filecmp); - - int q = v.size(); - int percolumn = (vid.yres-38) / (vid.fsize+5) - 4; - int columns = 1 + (q-1) / percolumn; - - for(int i=0; i 2000) sym = uni - 2000; - if(sym == SDLK_RETURN || sym == SDLK_KP_ENTER || sym == SDLK_ESCAPE) { - popScreen(); - return true; - } - else if(sym == SDLK_F2 || sym == SDLK_F3) { - popScreen(); - return false; - } - else if(sym == SDLK_F4) { - editext = !editext; - } - else if(sym == SDLK_BACKSPACE && i) { - s.erase(i-1, 1); - } - else if(uni >= 32 && uni < 127) { - s.insert(i, s0 + char(uni)); - } - else if(uni >= 1000 && uni <= 1000+size(v)) { - string where = "", what = s, whereparent = "../"; - for(int i=0; i= 2 && s.substr(i-2,3) == "../") - whereparent = s.substr(0, i+1) + "../"; - else - whereparent = where; - where = s.substr(0, i+1), what = s.substr(i+1); - } - int i = uni - 1000; - if(v[i].first == "../") { - s = whereparent + what; - } - else if(v[i].second == CDIR) - s = where + v[i].first + what; - else - s = where + v[i].first; - } - return true; - } - cellwalker mouseover_cw(bool fix) { int d = neighborId(mouseover, mouseover2); if(d == -1 && fix) d = hrand(mouseover->type); @@ -813,7 +668,7 @@ namespace mapeditor { } void showList() { - v.clear(); + dialog::v.clear(); if(painttype == 4) painttype = 0; switch(painttype) { case 0: @@ -826,25 +681,25 @@ namespace mapeditor { m == moTameBomberbirdMoved || m == moKnightMoved || m == moDeadBug || m == moLightningBolt || m == moDeadBird || m == moMouseMoved || m == moPrincessMoved || m == moPrincessArmedMoved) ; - else if(m == moDragonHead) vpush(i, "Dragon Head"); - else vpush(i, minf[i].name); + else if(m == moDragonHead) dialog::vpush(i, "Dragon Head"); + else dialog::vpush(i, minf[i].name); } break; case 1: - for(int i=0; i= '1' && uni <= '9') uni = 1000 + uni - '1'; if(sym == SDLK_RETURN || sym == SDLK_KP_ENTER || sym == '-' || sym == SDLK_KP_MINUS) uni = 1000; - for(int z=0; z= '0' && uni <= '9') radius = uni - '0'; - else if(uni == 'm') pushScreen(showList), painttype = 0, infix = ""; - else if(uni == 'i') pushScreen(showList), painttype = 1, infix = ""; - else if(uni == 'l') pushScreen(showList), painttype = 2, infix = ""; - else if(uni == 'w') pushScreen(showList), painttype = 3, infix = ""; + else if(uni == 'm') pushScreen(showList), painttype = 0, dialog::infix = ""; + else if(uni == 'i') pushScreen(showList), painttype = 1, dialog::infix = ""; + else if(uni == 'l') pushScreen(showList), painttype = 2, dialog::infix = ""; + else if(uni == 'w') pushScreen(showList), painttype = 3, dialog::infix = ""; else if(uni == 'r') pushScreen(patterns::showPattern); else if(uni == 't' && mouseover) { playermoved = true; @@ -917,13 +772,9 @@ namespace mapeditor { else addMessage(XLAT("Failed to load map from %1", levelfile)); } - else if(sym == SDLK_F4) { - cfileptr = &levelfile; - filecaption = XLAT("level to save/load:"); - cfileext = ".lev"; - pushScreen(drawFileDialog); - } -#if CAP_SDL + else if(sym == SDLK_F4) + dialog::openFileDialog(levelfile, XLAT("level to save/load:"), ".lev"); +#if CAP_SDL else if(sym == SDLK_F6) { saveHighQualityShot(); } @@ -1446,13 +1297,8 @@ namespace mapeditor { colorkey = true; } - if(sym == SDLK_F4) { - filecaption = XLAT("pics to save/load:"); - cfileptr = &picfile; - cfileext = ".pic"; - pushScreen(drawFileDialog); - return; - } + if(sym == SDLK_F4) + dialog::openFileDialog(picfile, XLAT("pics to save/load:"), ".pic"); if(sym == SDLK_F2) savePicFile(picfile); diff --git a/menus.cpp b/menus.cpp index c7da477e..13f8c128 100644 --- a/menus.cpp +++ b/menus.cpp @@ -20,8 +20,8 @@ void showOverview() { cmode = sm::ZOOMABLE | sm::OVERVIEW; DEBB(DF_GRAPH, (debugfile,"show overview\n")); - if(mapeditor::infix != "") - mouseovers = mapeditor::infix; + if(dialog::infix != "") + mouseovers = dialog::infix; else { mouseovers = XLAT("world overview"); mouseovers += " "; @@ -39,7 +39,7 @@ void showOverview() { generateLandList(isLandValid); - if(mapeditor::infix != "") { + if(dialog::infix != "") { vector filtered; for(eLand l: landlist) { string s = dnameof(l); @@ -47,7 +47,7 @@ void showOverview() { s += dnameof(treasureType(l)); s += "@"; s += dnameof(nativeOrbType(l)); - if(mapeditor::hasInfix(s)) + if(dialog::hasInfix(s)) filtered.push_back(l); } if(filtered.size()) @@ -183,7 +183,7 @@ void showOverview() { "mousewheel to gain or lose treasures and orbs quickly (Ctrl = precise, Shift = reverse)." ); else if(dialog::handlePageButtons(uni)) ; - else if(mapeditor::editInfix(uni)) ; + else if(dialog::editInfix(uni)) ; else if(doexiton(sym, uni)) popScreen(); }; } @@ -709,7 +709,7 @@ void setAppropriateOverview() { runGeometryExperiments(); } else { - mapeditor::infix = ""; + dialog::infix = ""; pushScreen(showOverview); } } diff --git a/scores.cpp b/scores.cpp index 594b6124..383e3396 100644 --- a/scores.cpp +++ b/scores.cpp @@ -133,7 +133,7 @@ void showPickScores() { scoredisplay = i; if(!fakescore()) { string s = displayfor(NULL); - if(mapeditor::hasInfix(s)) + if(dialog::hasInfix(s)) if(monsbox[scoredisplay] == monsterpage) pickscore_options.push_back(make_pair(s, i)); } @@ -159,9 +159,9 @@ void showPickScores() { scoredisplay = d; - mouseovers = mapeditor::infix; + mouseovers = dialog::infix; keyhandler = [] (int sym, int uni) { - if(uni == '/' && mapeditor::infix == "") monsterpage = !monsterpage; else + if(uni == '/' && dialog::infix == "") monsterpage = !monsterpage; else if(uni >= '1' && uni <= '9') uni = uni + 1000 - '1'; else if(uni >= 1000 && uni < 1000 + size(pickscore_options)) { scoredisplay = pickscore_options[uni - 1000].second; @@ -169,7 +169,7 @@ void showPickScores() { if(columns[i] == scoredisplay) swap(columns[i], columns[curcol]); popScreen(); } - else if(mapeditor::editInfix(uni)) ; + else if(dialog::editInfix(uni)) ; else if(doexiton(sym, uni)) popScreen(); }; } @@ -249,7 +249,7 @@ void show() { } else if(sym >= 1000 && sym <= 1000+POSSCORE) curcol = sym - 1000; - else if(sym == 't') { mapeditor::infix = ""; pushScreen(showPickScores); } + else if(sym == 't') { dialog::infix = ""; pushScreen(showPickScores); } else if(sym == SDLK_UP || sym == 'k' || sym == 'w') scorefrom -= 5; else if(sym == SDLK_DOWN || sym == 'j' || sym == 'x') diff --git a/textures.cpp b/textures.cpp index bad3fd5b..a63f70fd 100644 --- a/textures.cpp +++ b/textures.cpp @@ -252,12 +252,8 @@ void showTextureMenu() { dialog::handleNavigation(sym, uni); if(uni == 'r') pushScreen(patterns::showPattern); - else if(uni == 'f') { - mapeditor::cfileptr = &texturename; - mapeditor::filecaption = XLAT("texture to load:"); - mapeditor::cfileext = ".png"; - pushScreen(mapeditor::drawFileDialog); - } + else if(uni == 'f') + dialog::openFileDialog(texturename, XLAT("texture to load:"), ".png"); else if(uni == 'm') { texture_on = !texture_on; if(texture_on) perform_mapping();