Editing town entrance, allow Create/Edit

This commit is contained in:
2025-05-28 11:28:21 -05:00
parent b99e49da45
commit 75944686b3
5 changed files with 45 additions and 13 deletions

View File

@@ -2456,6 +2456,7 @@ void town_entry(location spot_hit) {
}
}
auto iter = std::find(current_terrain->city_locs.begin(), current_terrain->city_locs.end(), spot_hit);
// Edit existing town entrance
if(iter != current_terrain->city_locs.end()) {
int town = pick_town_num("select-town-enter",iter->spec,scenario);
if(town >= 0) iter->spec = town;
@@ -2463,13 +2464,16 @@ void town_entry(location spot_hit) {
iter = std::find_if(current_terrain->city_locs.begin(), current_terrain->city_locs.end(), [](const spec_loc_t& loc) {
return loc.spec < 0;
});
// Find unused town entrance and fill it
if(iter != current_terrain->city_locs.end()) {
int town = pick_town_num("select-town-enter",0,scenario);
if(town >= 0) {
*iter = spot_hit;
iter->spec = town;
}
} else {
}
// Add new town entrance at the back of list
else {
int town = pick_town_num("select-town-enter",0,scenario);
if(town >= 0) {
current_terrain->city_locs.emplace_back(spot_hit);

View File

@@ -453,17 +453,43 @@ void edit_sign(sign_loc_t& which_sign,short num,short picture) {
}
static bool save_town_num(cDialog& me, std::string, eKeyMod) {
using namespace std::placeholders;
me["town"].attachFocusHandler(std::bind(check_range, _1, _2, _3, 0, scenario.towns.size() - 1, "Town number"));
if(me.toast(true)) me.setResult<short>(me["town"].getTextAsNum());
return true;
}
short pick_town_num(std::string which_dlog,short def,cScenario& scenario) {
static bool create_town_num(cDialog& me, std::string, eKeyMod) {
using namespace std::placeholders;
me["town"].attachFocusHandler(std::bind(check_range, _1, _2, _3, 0, scenario.towns.size(), "Town number"));
if(me.toast(true)){
int i = me["town"].getTextAsNum();
if(i == scenario.towns.size()){
// create town
if(scenario.towns.size() >= 200) {
showError("You have reached the limit of 200 towns you can have in one scenario.");
return true;
}
if(!new_town()){
// Create town canceled -- don't store the number because it is out of range
return true;
}
}
cur_town = i;
town = scenario.towns[cur_town];
start_town_edit();
me.setResult<short>(i);
}
return true;
}
short pick_town_num(std::string which_dlog,short def,cScenario& scenario) {
cDialog town_dlg(*ResMgr::dialogs.get(which_dlog));
town_dlg["prompt"].replaceText("{{max-num}}", std::to_string(scenario.towns.size() - 1));
town_dlg["prompt"].replaceText("{{next-num}}", std::to_string(scenario.towns.size()));
town_dlg["cancel"].attachClickHandler(std::bind(&cDialog::toast, &town_dlg, false));
town_dlg["okay"].attachClickHandler(save_town_num);
town_dlg["town"].attachFocusHandler(std::bind(check_range, _1, _2, _3, 0, scenario.towns.size() - 1, "Town number"));
town_dlg["choose"].attachClickHandler([&scenario](cDialog& me, std::string, eKeyMod) -> bool {
int i = me["town"].getTextAsNum();
if(&scenario != &::scenario)
@@ -474,11 +500,11 @@ short pick_town_num(std::string which_dlog,short def,cScenario& scenario) {
me["town"].setTextToNum(i);
return true;
});
if(town_dlg.hasControl("edit")){
town_dlg["edit"].attachClickHandler(create_town_num);
}
town_dlg["town"].setTextToNum(def);
std::string prompt = town_dlg["prompt"].getText();
prompt += " (0 - " + std::to_string(scenario.towns.size() - 1) + ')';
town_dlg["prompt"].setText(prompt);
town_dlg.setResult<short>(-1);
town_dlg.run();