Skip to content

Commit

Permalink
split out show_binding_dialog function from gui1 hotkey_preferences_d…
Browse files Browse the repository at this point in the history
…ialog

so that it can also be used by a gui2 hotkey preferences dialog
  • Loading branch information
gfgtdf committed Feb 29, 2016
1 parent 56d8b9e commit 43f7fce
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 67 deletions.
4 changes: 3 additions & 1 deletion src/hotkey/hotkey_item.hpp
Expand Up @@ -26,7 +26,7 @@
#endif

class config;

class CVideo;
namespace hotkey {

/* forward declarations */
Expand Down Expand Up @@ -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
140 changes: 74 additions & 66 deletions src/hotkey/hotkey_preferences_display.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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

0 comments on commit 43f7fce

Please sign in to comment.