undo/redo for edit personality

This commit is contained in:
2025-06-20 09:24:20 -05:00
parent a59dc176bf
commit 8f83427d3f
5 changed files with 83 additions and 8 deletions

View File

@@ -0,0 +1,12 @@
<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<!-- NOTE: This file should be updated to use relative positioning the next time it changes. -->
<?xml-stylesheet href="dialog.xsl" type="text/xsl"?>
<dialog defbtn='keep' escbtn='cancel'>
<pict type='dlog' num='7' top='6' left='6'/>
<text name='keep-msg' top='6' left='49' width='256' height='32'>
Keep changes to {{name}} before editing another personality?
</text>
<button name='cancel' type='regular' top='43' left='109'>Cancel</button>
<button name='revert' type='regular' top='43' left='175'>Discard</button>
<button name='keep' type='regular' top='43' left='240'>Keep</button>
</dialog>

View File

@@ -20,8 +20,19 @@ namespace legacy {
class cPersonality {
public:
// This is the character's name.
std::string title;
// name is how the character responds when asked about it
std::string look, name, job, dunno;
bool operator==(const cPersonality& other) const {
CHECK_EQ(other, title);
CHECK_EQ(other, look);
CHECK_EQ(other, name);
CHECK_EQ(other, job);
CHECK_EQ(other, dunno);
return true;
}
bool operator!=(const cPersonality& other) const { return !(*this == other); }
};
// This is used solely for porting old shops

View File

@@ -1163,13 +1163,37 @@ void edit_town_wand() {
wand_dlg.run();
}
static void save_basic_dlog(cDialog& me, short& which) {
// return true if the left/right buttons can continue their shift
bool save_basic_dlog(cDialog& me, short& which, bool need_confirm) {
auto& the_node = town->talking.people[which];
the_node.title = me["title"].getText().substr(0,30);
the_node.dunno = me["dunno"].getText();
the_node.look = me["look"].getText();
the_node.name = me["name"].getText();
the_node.job = me["job"].getText();
auto new_node = the_node;
new_node.title = me["title"].getText().substr(0,30);
new_node.dunno = me["dunno"].getText();
new_node.look = me["look"].getText();
new_node.name = me["name"].getText();
new_node.job = me["job"].getText();
if(new_node != the_node){
if(need_confirm){
cChoiceDlog dlog("confirm-edit-personality", {"keep","revert","cancel"}, &me);
dlog->getControl("keep-msg").replaceText("{{name}}", new_node.title);
std::string choice = dlog.show();
if(choice == "cancel"){
// Can't shift left/right
return false;
}else if(choice == "revert"){
// Shift left/right without saving
return true;
}
}
// We update the edit menu after all editing is done
undo_list.add(action_ptr(new aEditPersonality(cur_town, which, the_node, new_node)));
the_node = new_node;
}
return true;
}
static void put_basic_dlog_in_dlog(cDialog& me, const short which) {
@@ -1183,14 +1207,19 @@ static void put_basic_dlog_in_dlog(cDialog& me, const short which) {
}
static bool edit_basic_dlog_event_filter(cDialog& me, std::string hit, short& which) {
save_basic_dlog(me, which);
// There are no focus handlers right now, but there could be.
if(!me.toast(true)) return true;
if(hit != "okay") me.untoast();
if(hit == "okay")
me.toast(true);
save_basic_dlog(me, which, false);
else if(hit == "left") {
if(!save_basic_dlog(me, which, true)) return true;
which--;
if(which < 0) which = 9;
put_basic_dlog_in_dlog(me, which);
} else if(hit == "right") {
if(!save_basic_dlog(me, which, true)) return true;
which++;
if(which > 9) which = 0;
put_basic_dlog_in_dlog(me, which);
@@ -1208,6 +1237,7 @@ void edit_basic_dlog(short personality) {
put_basic_dlog_in_dlog(person_dlg, personality);
person_dlg.run();
update_edit_menu();
}
static bool check_talk_personality(cDialog& me, std::string item_hit, bool losing) {

View File

@@ -996,4 +996,14 @@ bool aEditOutEncounter::redo_me() {
encounters[which] = new_enc;
if(mode == 0) outdoors.wandering_locs[which] = new_loc;
return true;
}
bool aEditPersonality::undo_me() {
scenario.towns[town_num]->talking.people[which] = old_pers;
return true;
}
bool aEditPersonality::redo_me() {
scenario.towns[town_num]->talking.people[which] = new_pers;
return true;
}

View File

@@ -662,4 +662,16 @@ public:
out_sec(out_sec), mode(1), which(which), old_enc(old_enc), new_enc(new_enc) {}
};
class aEditPersonality : public cAction {
size_t town_num;
size_t which;
cPersonality old_pers;
cPersonality new_pers;
bool undo_me() override;
bool redo_me() override;
public:
aEditPersonality(size_t town_num, size_t which, cPersonality old_pers, cPersonality new_pers) :
cAction("Edit Personality"), town_num(town_num), which(which), old_pers(old_pers), new_pers(new_pers) {}
};
#endif