From 28e567b64110bed6b4a68cb2e2250356136975ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20=C5=9Aniatowski?= Date: Wed, 16 Jul 2008 17:25:07 +0100 Subject: [PATCH] Turn hotkey scoping back on and fix issues... ...by making the editor have separate bindings from the main game. The keys are separate, but some hotkey commands are shared. --- data/core/editor2-hotkeys.cfg | 4 +-- src/editor2/editor_controller.cpp | 3 -- src/editor2/editor_main.cpp | 7 +++- src/game.cpp | 7 ++-- src/hotkeys.cpp | 60 +++++++++++++++++++++++++------ src/hotkeys.hpp | 13 +++++++ 6 files changed, 74 insertions(+), 20 deletions(-) diff --git a/data/core/editor2-hotkeys.cfg b/data/core/editor2-hotkeys.cfg index 846f47a8c1d..7590f813fda 100644 --- a/data/core/editor2-hotkeys.cfg +++ b/data/core/editor2-hotkeys.cfg @@ -79,8 +79,8 @@ [/hotkey] [hotkey] - command="editor-tool-draw" - description="Select the draw tool" + command="editor-tool-paint" + description="Select the paint (draw) tool" key="d" [/hotkey] diff --git a/src/editor2/editor_controller.cpp b/src/editor2/editor_controller.cpp index 0a1451952e6..313991fd47c 100644 --- a/src/editor2/editor_controller.cpp +++ b/src/editor2/editor_controller.cpp @@ -41,9 +41,6 @@ editor_controller::editor_controller(const config &game_config, CVideo& video) , gui_(NULL), actions_since_save_(0), do_quit_(false), quit_mode_(EXIT_ERROR) , current_brush_index_(0) { - hotkey::deactivate_all_scopes(); - hotkey::set_scope_active(hotkey::SCOPE_GENERAL); - hotkey::set_scope_active(hotkey::SCOPE_EDITOR); init(video); cursor::set(cursor::NORMAL); diff --git a/src/editor2/editor_main.cpp b/src/editor2/editor_main.cpp index d50b5da9d13..08c93332be0 100644 --- a/src/editor2/editor_main.cpp +++ b/src/editor2/editor_main.cpp @@ -20,8 +20,13 @@ namespace editor2 { EXIT_STATUS start(config& game_conf, CVideo& video) { + hotkey::scope_changer h_(game_conf, "hotkey_editor"); + hotkey::deactivate_all_scopes(); + hotkey::set_scope_active(hotkey::SCOPE_GENERAL); + hotkey::set_scope_active(hotkey::SCOPE_EDITOR); editor_controller editor(game_conf, video); - return editor.main_loop(); + EXIT_STATUS e = editor.main_loop(); + return e; } } //end namespace editor2 diff --git a/src/game.cpp b/src/game.cpp index fe95234e08d..699c43d8983 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -529,6 +529,9 @@ bool game_controller::init_config() refresh_game_cfg(); game_config::load_config(game_config_.child("game_config")); + hotkey::deactivate_all_scopes(); + hotkey::set_scope_active(hotkey::SCOPE_GENERAL); + hotkey::set_scope_active(hotkey::SCOPE_GAME); hotkey::load_hotkeys(game_config_); paths_manager_.set_paths(game_config_); @@ -2003,9 +2006,6 @@ bool game_controller::change_language() void game_controller::show_preferences() { - hotkey::deactivate_all_scopes(); - hotkey::set_scope_active(hotkey::SCOPE_GENERAL); - hotkey::set_scope_active(hotkey::SCOPE_GAME); const preferences::display_manager disp_manager(&disp()); preferences::show_preferences_dialog(disp(),game_config_); @@ -2380,7 +2380,6 @@ editor2::EXIT_STATUS game_controller::start_editor() reset_game_cfg(); defines_map_["EDITOR2"] = preproc_define(); refresh_game_cfg(); - hotkey::load_hotkeys(game_config_); return editor2::start(game_config_, video_); } #endif diff --git a/src/hotkeys.cpp b/src/hotkeys.cpp index dd6a5ff0180..d991799c4b6 100644 --- a/src/hotkeys.cpp +++ b/src/hotkeys.cpp @@ -197,9 +197,11 @@ const struct { std::vector hotkeys_; hotkey::hotkey_item null_hotkey_; +std::string hotkey_tag_name = "hotkey"; + const std::string scope_strings_[] = {"general", "game", "editor"}; const std::string scope_labels_[] = {"Common", "Game", "Editor"}; -bool scope_active_[hotkey::SCOPE_COUNT] = {true, false}; +std::vector scope_active_(hotkey::SCOPE_COUNT, false); } namespace hotkey { @@ -207,8 +209,8 @@ namespace hotkey { void deactivate_all_scopes() { - foreach (bool& b, scope_active_) { - b = false; + for (int i = 0; i < hotkey::SCOPE_COUNT; ++i) { + scope_active_[i] = false; } } @@ -217,9 +219,9 @@ void set_scope_active(scope s, bool set) scope_active_[s] = set; } -bool is_scope_active(scope /*s*/) +bool is_scope_active(scope s) { - return true; //scope_active_[s]; + return scope_active_[s]; } const std::string& get_scope_string(scope s) @@ -389,6 +391,11 @@ void hotkey_item::set_key(int character, int keycode, bool shift, bool ctrl, boo } manager::manager() +{ + init(); +} + +void manager::init() { for (int i = 0; hotkey_list_[i].command; ++i) { hotkeys_.push_back(hotkey_item(hotkey_list_[i].id, hotkey_list_[i].command, @@ -396,10 +403,36 @@ manager::manager() } } +void manager::wipe() +{ + hotkeys_.clear(); +} manager::~manager() { - hotkeys_.clear(); + wipe(); +} + +scope_changer::scope_changer(const config& cfg, const std::string& hotkey_tag) +: cfg_(cfg) +, prev_tag_name_(hotkey_tag_name) +, prev_scope_active_(scope_active_) +{ + manager::wipe(); + manager::init(); + hotkey::load_descriptions(); + load_hotkeys(cfg_); + set_hotkey_tag_name(hotkey_tag); +} + +scope_changer::~scope_changer() +{ + scope_active_.swap(prev_scope_active_); + manager::wipe(); + manager::init(); + hotkey::load_descriptions(); + set_hotkey_tag_name(prev_tag_name_); + load_hotkeys(cfg_); } void load_descriptions() @@ -412,9 +445,15 @@ void load_descriptions() } } +void set_hotkey_tag_name(const std::string& name) +{ + hotkey_tag_name = name; +} + void load_hotkeys(const config& cfg) { - const config::child_list& children = cfg.get_children("hotkey"); + std::cerr << "load_hotkeys " << hotkey_tag_name << "\n"; + const config::child_list& children = cfg.get_children(hotkey_tag_name); for(config::child_list::const_iterator i = children.begin(); i != children.end(); ++i) { hotkey_item& h = get_hotkey((**i)["command"]); if(h.get_id() != HOTKEY_NULL) { @@ -425,13 +464,14 @@ void load_hotkeys(const config& cfg) void save_hotkeys(config& cfg) { - cfg.clear_children("hotkey"); + std::cerr << "save_hotkeys " << hotkey_tag_name << "\n"; + cfg.clear_children(hotkey_tag_name); for(std::vector::iterator i = hotkeys_.begin(); i != hotkeys_.end(); ++i) { - if (i->hidden() || i->get_type() == hotkey_item::UNBOUND) + if (i->hidden() || i->get_type() == hotkey_item::UNBOUND || !i->is_in_active_scope()) continue; - config& item = cfg.add_child("hotkey"); + config& item = cfg.add_child(hotkey_tag_name); item["command"] = i->get_command(); if (i->get_type() == hotkey_item::CLEARED) { diff --git a/src/hotkeys.hpp b/src/hotkeys.hpp index f2e7c9fc4ed..e604ce148f1 100644 --- a/src/hotkeys.hpp +++ b/src/hotkeys.hpp @@ -195,11 +195,24 @@ private: class manager { public: manager(); + static void init(); + static void wipe(); ~manager(); }; +class scope_changer { +public: + scope_changer(const config& cfg, const std::string& hotkey_tag); + ~scope_changer(); +private: + const config& cfg_; + std::string prev_tag_name_; + std::vector prev_scope_active_; +}; + void load_descriptions(); +void set_hotkey_tag_name(const std::string& name); void load_hotkeys(const config& cfg); void save_hotkeys(config& cfg);