GUI2: changed hotkey callback return value to void

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.
This commit is contained in:
Charles Dang 2017-11-25 02:09:49 +11:00
parent b7367b8b07
commit 8bf95ce69a
6 changed files with 24 additions and 46 deletions

View file

@ -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)

View file

@ -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.
@ -795,11 +794,8 @@ public:
* 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. */

View file

@ -257,7 +257,9 @@ private:
*
* @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);
@ -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,

View file

@ -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

View file

@ -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
@ -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);
@ -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));
//

View file

@ -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";
@ -194,8 +194,6 @@ static bool helptip()
event.user = data;
SDL_PushEvent(&event);
return true;
}
/**