From 34aeef509ddb85f9819c96bb9d824b62a6b706a5 Mon Sep 17 00:00:00 2001 From: Dave White Date: Tue, 23 Mar 2004 15:23:36 +0000 Subject: [PATCH] added 'undo', 'redo', and 'cycle' to the context menu made it so shortcut keys are displayed next to menu items --- data/themes/default.cfg | 2 +- data/translations/english.cfg | 2 +- src/config.cpp | 86 +++++++++++++++++++++++++++++++++++ src/config.hpp | 10 ++++ src/playturn.cpp | 20 +++++++- 5 files changed, 117 insertions(+), 3 deletions(-) diff --git a/data/themes/default.cfg b/data/themes/default.cfg index be1ba0d5e98..044af42a3bb 100644 --- a/data/themes/default.cfg +++ b/data/themes/default.cfg @@ -39,7 +39,7 @@ height=600 [menu] is_context_menu=true - items=describeunit,speak,recruit,recall,createunit,endturn + items=undo,redo,cycle,describeunit,speak,recruit,recall,createunit,endturn [/menu] # top panel diff --git a/data/translations/english.cfg b/data/translations/english.cfg index 2ae30ada20b..478983204a7 100644 --- a/data/translations/english.cfg +++ b/data/translations/english.cfg @@ -376,7 +376,7 @@ action_zoomout="Zoom Out" action_zoomdefault="Default Zoom" action_fullscreen="Fullscreen" action_accelerated="Accelerated" -action_cycle="Cycle units" +action_cycle="Next unit" action_endturn="End Turn" action_endunitturn="End Unit Turn" action_leader="Leader" diff --git a/src/config.cpp b/src/config.cpp index fba229c246e..3eb48c09fbd 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -1176,6 +1176,92 @@ config::all_children_iterator config::ordered_end() const return all_children_iterator(ordered_children.end()); } +config config::get_diff(const config& c) const +{ + config res; + + config* inserts = NULL; + + string_map::const_iterator i; + for(i = values.begin(); i != values.end(); ++i) { + const string_map::const_iterator j = c.values.find(i->first); + if(j == c.values.end() || i->second != j->second) { + if(inserts == NULL) { + inserts = &res.add_child("insert"); + } + + (*inserts)[i->first] = i->second; + } + } + + config* deletes = NULL; + + for(i = c.values.begin(); i != c.values.end(); ++i) { + if(values.count(i->first) == 0) { + if(deletes == NULL) { + deletes = &res.add_child("delete"); + } + + (*deletes)[i->first] = "x"; + } + } + + + + return res; +} + +void config::apply_diff(const config& diff) +{ + const config* const inserts = diff.child("insert"); + if(inserts != NULL) { + for(string_map::const_iterator i = inserts->values.begin(); i != inserts->values.end(); ++i) { + values[i->first] = i->second; + } + } + + const config* const deletes = diff.child("delete"); + if(deletes != NULL) { + for(string_map::const_iterator i = deletes->values.begin(); i != deletes->values.end(); ++i) { + values.erase(i->first); + } + } +} + +bool operator==(const config& a, const config& b) +{ + if(a.values.size() != b.values.size()) { + return false; + } + + for(string_map::const_iterator i = a.values.begin(); i != a.values.end(); ++i) { + const string_map::const_iterator j = b.values.find(i->first); + if(j == b.values.end() || i->second != j->second) { + return false; + } + } + + config::all_children_iterator x = a.ordered_begin(), y = b.ordered_begin(); + while(x != a.ordered_end() && y != b.ordered_end()) { + const std::pair val1 = *x; + const std::pair val2 = *y; + + if(*val1.first != *val2.first || *val1.second != *val2.second) { + return false; + } + + ++x; + ++y; + } + + return x == a.ordered_end() && y == b.ordered_end(); +} + +bool operator!=(const config& a, const config& b) +{ + return !operator==(a,b); +} + //#define TEST_CONFIG #ifdef TEST_CONFIG diff --git a/src/config.hpp b/src/config.hpp index 0e85e461420..100d39c55b5 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -195,6 +195,13 @@ struct config all_children_iterator ordered_begin() const; all_children_iterator ordered_end() const; + //a function to get the differences between this object, and 'c', as another config + //object. i.e. calling cfg2.apply_diff(cfg1.get_diff(cfg2)) will make cfg1 identical + //to cfg2. + config get_diff(const config& c) const; + + void apply_diff(const config& diff); + //all the attributes of this node. string_map values; @@ -210,6 +217,9 @@ private: std::vector ordered_children; }; +bool operator==(const config& a, const config& b); +bool operator!=(const config& a, const config& b); + struct config_has_value { config_has_value(const std::string& name, const std::string& value) : name_(name), value_(value) diff --git a/src/playturn.cpp b/src/playturn.cpp index 3728bbe1f0c..48fc0fcd230 100644 --- a/src/playturn.cpp +++ b/src/playturn.cpp @@ -851,7 +851,25 @@ void turn_info::show_menu(const std::vector& items_arg, int xloc, i std::vector menu; for(std::vector::const_iterator i = items.begin(); i != items.end(); ++i) { - menu.push_back(translate_string("action_" + *i)); + std::stringstream str; + str << translate_string("action_" + *i); + + //see if this menu item has an associated hotkey + const hotkey::HOTKEY_COMMAND cmd = hotkey::string_to_command(*i); + + const std::vector& hotkeys = hotkey::get_hotkeys(); + std::vector::const_iterator hk; + for(hk = hotkeys.begin(); hk != hotkeys.end(); ++hk) { + if(hk->action == cmd) { + break; + } + } + + if(hk != hotkeys.end()) { + str << "," << hotkey::get_hotkey_name(*hk); + } + + menu.push_back(str.str()); } static const std::string style = "menu2";