From 43f7fced78c2f6daf777c0f06542ad30df6e8c6d Mon Sep 17 00:00:00 2001 From: gfgtdf Date: Sun, 28 Feb 2016 21:24:36 +0100 Subject: [PATCH] split out show_binding_dialog function from gui1 hotkey_preferences_dialog so that it can also be used by a gui2 hotkey preferences dialog --- src/hotkey/hotkey_item.hpp | 4 +- src/hotkey/hotkey_preferences_display.cpp | 140 ++++++++++++---------- 2 files changed, 77 insertions(+), 67 deletions(-) diff --git a/src/hotkey/hotkey_item.hpp b/src/hotkey/hotkey_item.hpp index 724950821465..830f8fd569ab 100644 --- a/src/hotkey/hotkey_item.hpp +++ b/src/hotkey/hotkey_item.hpp @@ -26,7 +26,7 @@ #endif class config; - +class CVideo; namespace hotkey { /* forward declarations */ @@ -433,6 +433,8 @@ std::string get_names(std::string id); */ void save_hotkeys(config& cfg); +hotkey_ptr show_binding_dialog(CVideo& video, const std::string& id); + } #endif diff --git a/src/hotkey/hotkey_preferences_display.cpp b/src/hotkey/hotkey_preferences_display.cpp index 44999779fcfb..c2155bbacf3b 100644 --- a/src/hotkey/hotkey_preferences_display.cpp +++ b/src/hotkey/hotkey_preferences_display.cpp @@ -559,35 +559,98 @@ void hotkey_preferences_dialog::update_location(SDL_Rect const &rect) { #pragma warning (push) #pragma warning (disable:4701) #endif -void hotkey_preferences_dialog::show_binding_dialog( - const std::string& id) { + +void hotkey_preferences_dialog::show_binding_dialog(const std::string& id) +{ + hotkey::hotkey_ptr newhk = hotkey::show_binding_dialog(video_, id); + hotkey::hotkey_ptr oldhk; + // only if not cancelled. + if (newhk.get() != NULL) { + BOOST_FOREACH(const hotkey::hotkey_ptr& hk, hotkey::get_hotkeys()) { + if(newhk->bindings_equal(hk)) { + oldhk == hk; + } + } + hotkey::scope_changer scope_restorer; + hotkey::set_active_scopes(hotkey::get_hotkey_command(id).scope); + +#if 0 + //TODO +// if ( (hotkey::get_id(newhk.get_command()) == hotkey::HOTKEY_SCREENSHOT +// || hotkey::get_id(newhk.get_command()) == hotkey::HOTKEY_MAP_SCREENSHOT) +// && (mod & any_mod) == 0 ) { +// gui2::show_transient_message(video, _("Warning"), +/* _("Screenshot hotkeys should be combined with the \ +Control, Alt or Meta modifiers to avoid problems.")); */ +// } + break; + } +#endif + + if (oldhk && oldhk->active()) { + if (oldhk->get_command() != id) { + + utils::string_map symbols; + symbols["hotkey_sequence"] = oldhk->get_name(); + symbols["old_hotkey_action"] = hotkey::get_description(oldhk->get_command()); + symbols["new_hotkey_action"] = hotkey::get_description(newhk->get_command()); + + std::string text = + vgettext("\"$hotkey_sequence|\" is in use by \ +\"$old_hotkey_action|\". Do you wish to reassign it to \"$new_hotkey_action|\"?" + , symbols); + + const int res = gui2::show_message(video_, + _("Reassign Hotkey"), text, + gui2::tmessage::yes_no_buttons); + if (res == gui2::twindow::OK) { + hotkey::add_hotkey(newhk); + set_hotkey_menu(true); + } + } + } else { + hotkey::add_hotkey(newhk); + set_hotkey_menu(true); + } + } + +} +#ifdef _MSC_VER +#pragma warning (pop) +#endif + +} // end namespace preferences +namespace hotkey { + +hotkey::hotkey_ptr show_binding_dialog(CVideo& video, const std::string& id) +{ // Lets bind a hotkey... const std::string text = _("Press desired hotkey (Esc cancels)"); - SDL_Rect clip_rect = sdl::create_rect(0, 0, video_.getx(), video_.gety()); + SDL_Rect clip_rect = sdl::create_rect(0, 0, video.getx(), video.gety()); SDL_Rect text_size = font::draw_text(NULL, clip_rect, font::SIZE_LARGE, font::NORMAL_COLOR, text, 0, 0); - const int centerx = video_.getx() / 2; - const int centery = video_.gety() / 2; + const int centerx = video.getx() / 2; + const int centery = video.gety() / 2; SDL_Rect dlgr = sdl::create_rect(centerx - text_size.w / 2 - 30, centery - text_size.h / 2 - 16, text_size.w + 60, text_size.h + 32); - surface_restorer restorer(&video_, dlgr); - gui::dialog_frame mini_frame(video_); + surface_restorer restorer(&video, dlgr); + gui::dialog_frame mini_frame(video); mini_frame.layout(centerx - text_size.w / 2 - 20, centery - text_size.h / 2 - 6, text_size.w + 40, text_size.h + 12); mini_frame.draw_background(); mini_frame.draw_border(); - font::draw_text(&video_, clip_rect, font::SIZE_LARGE, + font::draw_text(&video, clip_rect, font::SIZE_LARGE, font::NORMAL_COLOR, text, centerx - text_size.w / 2, centery - text_size.h / 2); - video_.flip(); + video.flip(); SDL_Event event; event.type = 0; int keycode = -1, mod = -1; @@ -622,62 +685,7 @@ void hotkey_preferences_dialog::show_binding_dialog( && event.type != SDL_MOUSEBUTTONUP); restorer.restore(); - - // only if not cancelled. - if (!(keycode == SDLK_ESCAPE && (mod & any_mod) == 0)) { - hotkey::scope_changer scope_restorer; - hotkey::set_active_scopes(hotkey::get_hotkey_command(id).scope); - - hotkey::hotkey_ptr newhk; - hotkey::hotkey_ptr oldhk; - - oldhk = hotkey::get_hotkey(event); - newhk = hotkey::create_hotkey(id, event); - -#if 0 - //TODO -// if ( (hotkey::get_id(newhk.get_command()) == hotkey::HOTKEY_SCREENSHOT -// || hotkey::get_id(newhk.get_command()) == hotkey::HOTKEY_MAP_SCREENSHOT) -// && (mod & any_mod) == 0 ) { -// gui2::show_transient_message(video_, _("Warning"), -/* _("Screenshot hotkeys should be combined with the \ -Control, Alt or Meta modifiers to avoid problems.")); */ -// } - break; - } -#endif - - if (oldhk && oldhk->active()) { - if (oldhk->get_command() != id) { - - utils::string_map symbols; - symbols["hotkey_sequence"] = oldhk->get_name(); - symbols["old_hotkey_action"] = hotkey::get_description(oldhk->get_command()); - symbols["new_hotkey_action"] = hotkey::get_description(newhk->get_command()); - - std::string text = - vgettext("\"$hotkey_sequence|\" is in use by \ -\"$old_hotkey_action|\". Do you wish to reassign it to \"$new_hotkey_action|\"?" - , symbols); - - const int res = gui2::show_message(video_, - _("Reassign Hotkey"), text, - gui2::tmessage::yes_no_buttons); - if (res == gui2::twindow::OK) { - hotkey::add_hotkey(newhk); - set_hotkey_menu(true); - } - } - } else { - hotkey::add_hotkey(newhk); - set_hotkey_menu(true); - } - } - + return keycode == SDLK_ESCAPE ? hotkey::hotkey_ptr() : hotkey::create_hotkey(id, event); } -#ifdef _MSC_VER -#pragma warning (pop) -#endif - -} // end namespace preferences +} //end namespace hotkey