Skip to content

Commit

Permalink
GUI2: changed hotkey callback return value to void
Browse files Browse the repository at this point in the history
The bool retval was presumably meant to allow stopping execution of the main hotkey callbacks
if a local one was meant to take precedence. However, that wasn't used anywhere (all callbacks
were returning true) and the return value just meant we had to create lambdas or wrapper functions
everywhere. If necessary, we can re-add halt functionality later.

I did leave sdl_event_handler::hotkey_pressed returning a bool value, though, except now it signals
whether there was a valid dispatcher with which to execute the hotkey callback. This seemed reasonable
since we probably wouldn't want to continue if there were no dispatcher anyway, though this only has
any effect in the key_down handler.
  • Loading branch information
Vultraz authored and GregoryLundberg committed Nov 30, 2017
1 parent 6e401eb commit 033b69f
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 46 deletions.
6 changes: 3 additions & 3 deletions src/gui/core/event/dispatcher.cpp
Expand Up @@ -190,15 +190,15 @@ void dispatcher::register_hotkey(const hotkey::HOTKEY_COMMAND id, const hotkey_f
hotkeys_[id] = function;
}

bool dispatcher::execute_hotkey(const hotkey::HOTKEY_COMMAND id)
void dispatcher::execute_hotkey(const hotkey::HOTKEY_COMMAND id)
{
std::map<hotkey::HOTKEY_COMMAND, hotkey_function>::iterator itor = hotkeys_.find(id);

if(itor == hotkeys_.end()) {
return false;
return;
}

return itor->second(dynamic_cast<widget&>(*this), id);
itor->second(dynamic_cast<widget&>(*this), id);
}

void connect_signal_pre_key_press(dispatcher& dispatcher, const signal_keyboard_function& signal)
Expand Down
8 changes: 2 additions & 6 deletions src/gui/core/event/dispatcher.hpp
Expand Up @@ -140,8 +140,7 @@ typedef std::function<void(widget& dispatcher,
int32_t select_len)> signal_text_input_function;

/** Hotkey function handler signature. */
typedef std::function<bool(widget& dispatcher,
hotkey::HOTKEY_COMMAND id)> hotkey_function;
typedef std::function<void(widget& dispatcher, hotkey::HOTKEY_COMMAND id)> hotkey_function;

/**
* Base class for event handling.
Expand Down Expand Up @@ -795,11 +794,8 @@ class dispatcher
* Executes a hotkey.
*
* @param id The hotkey to execute.
*
* @returns true if the hotkey is handled, false
* otherwise.
*/
bool execute_hotkey(const hotkey::HOTKEY_COMMAND id);
void execute_hotkey(const hotkey::HOTKEY_COMMAND id);

private:
/** The mouse behavior for the dispatcher. */
Expand Down
10 changes: 6 additions & 4 deletions src/gui/core/event/handler.cpp
Expand Up @@ -257,7 +257,9 @@ class sdl_event_handler : public events::sdl_handler
*
* @param key The hotkey item pressed.
*
* @returns True if the hotkey is handled false otherwise.
* @returns True if there was a valid dispatcher with
* which to execute the hotkey callback, false
* otherwise.
*/
bool hotkey_pressed(const hotkey::hotkey_ptr key);

Expand Down Expand Up @@ -741,11 +743,11 @@ bool sdl_event_handler::hotkey_pressed(const hotkey::hotkey_ptr key)
{
dispatcher* dispatcher = keyboard_dispatcher();

if(!dispatcher) {
return false;
if(dispatcher) {
dispatcher->execute_hotkey(hotkey::get_id(key->get_command()));
}

return dispatcher->execute_hotkey(hotkey::get_id(key->get_command()));
return dispatcher != nullptr;
}

void sdl_event_handler::key_down(const SDL_Keycode key,
Expand Down
15 changes: 4 additions & 11 deletions src/gui/dialogs/multiplayer/lobby.cpp
Expand Up @@ -175,22 +175,15 @@ mp_lobby::~mp_lobby()
}
}

static bool fullscreen(CVideo& video)
{
video.set_fullscreen(!preferences::fullscreen());
return true;
}

void mp_lobby::post_build(window& win)
{
/** @todo Should become a global hotkey after 1.8, then remove it here. */
win.register_hotkey(hotkey::HOTKEY_FULLSCREEN, std::bind(fullscreen, std::ref(win.video())));
win.register_hotkey(hotkey::HOTKEY_FULLSCREEN,
std::bind(&CVideo::set_fullscreen, std::ref(win.video()), !preferences::fullscreen()));

/*** Local hotkeys. ***/
win.register_hotkey(hotkey::HOTKEY_PREFERENCES, [this](event::dispatcher& w, hotkey::HOTKEY_COMMAND)->bool {
show_preferences_button_callback(dynamic_cast<window&>(w));
return true;
});
win.register_hotkey(hotkey::HOTKEY_PREFERENCES,
std::bind(&mp_lobby::show_preferences_button_callback, this, std::ref(win)));
}

namespace
Expand Down
27 changes: 8 additions & 19 deletions src/gui/dialogs/title_screen.cpp
Expand Up @@ -155,26 +155,16 @@ using btn_callback = std::function<void(window&)>;
static void register_button(window& win, const std::string& id, hotkey::HOTKEY_COMMAND hk, btn_callback callback)
{
if(hk != hotkey::HOTKEY_NULL) {
win.register_hotkey(hk, [callback](event::dispatcher& w, hotkey::HOTKEY_COMMAND) {
callback(dynamic_cast<window&>(w));
return true;
});
win.register_hotkey(hk, std::bind(callback, std::ref(win)));
}

event::connect_signal_mouse_left_click(find_widget<button>(&win, id, false),
[callback](event::dispatcher& w, event::ui_event, bool&, bool&) { callback(dynamic_cast<window&>(w)); });
}

static bool fullscreen(CVideo& video)
{
video.set_fullscreen(!preferences::fullscreen());
return true;
}

static bool launch_lua_console()
static void launch_lua_console()
{
gui2::dialogs::lua_interpreter::display(gui2::dialogs::lua_interpreter::APP);
return true;
}

#ifdef DEBUG_TOOLTIP
Expand Down Expand Up @@ -222,10 +212,8 @@ void title_screen::pre_show(window& win)
//
// General hotkeys
//
win.register_hotkey(hotkey::TITLE_SCREEN__RELOAD_WML, [](event::dispatcher& w, hotkey::HOTKEY_COMMAND) {
dynamic_cast<window&>(w).set_retval(RELOAD_GAME_DATA);
return true;
});
win.register_hotkey(hotkey::TITLE_SCREEN__RELOAD_WML,
std::bind(&gui2::window::set_retval, std::ref(win), RELOAD_GAME_DATA, true));

win.register_hotkey(hotkey::TITLE_SCREEN__TEST, [this](event::dispatcher& w, hotkey::HOTKEY_COMMAND) {
game_config_manager::get()->load_game_config_for_create(false, true);
Expand All @@ -243,12 +231,13 @@ void title_screen::pre_show(window& win)
game_.set_test(options[choice]);
dynamic_cast<window&>(w).set_retval(LAUNCH_GAME);
}
return true;
});

win.register_hotkey(hotkey::HOTKEY_FULLSCREEN, std::bind(fullscreen, std::ref(win.video())));
win.register_hotkey(hotkey::HOTKEY_FULLSCREEN,
std::bind(&CVideo::set_fullscreen, std::ref(win.video()), !preferences::fullscreen()));

// std::bind is needed here since the bare function signature isn't what register_hotkey expects.
// A wrapper is needed here since the relevant display function is overloaded, and
// since the wrapper's signature doesn't exactly match what register_hotkey expects.
win.register_hotkey(hotkey::LUA_CONSOLE, std::bind(&launch_lua_console));

//
Expand Down
4 changes: 1 addition & 3 deletions src/gui/widgets/window.cpp
Expand Up @@ -183,7 +183,7 @@ static void delay_event(const SDL_Event& event, const Uint32 delay)
*
* The event is used to show the helptip for the currently focused widget.
*/
static bool helptip()
static void helptip()
{
DBG_GUI_E << "Pushing SHOW_HELPTIP_EVENT event in queue.\n";

Expand All @@ -194,8 +194,6 @@ static bool helptip()
event.user = data;

SDL_PushEvent(&event);

return true;
}

/**
Expand Down

0 comments on commit 033b69f

Please sign in to comment.