Shift most of wmi_container::init_handlers() to the wmi class.
This leaves wmi_container as a wrapper that does not need to know implementation details about wml_menu_item.
This commit is contained in:
parent
badec4e112
commit
f3d777360c
3 changed files with 31 additions and 20 deletions
|
@ -26,8 +26,10 @@
|
|||
#include "../actions/undo.hpp"
|
||||
#include "../game_config.hpp"
|
||||
#include "../gamestatus.hpp"
|
||||
#include "../hotkeys.hpp"
|
||||
#include "../log.hpp"
|
||||
#include "../mouse_handler_base.hpp"
|
||||
#include "../play_controller.hpp"
|
||||
#include "../replay.hpp"
|
||||
#include "../resources.hpp"
|
||||
#include "../terrain_filter.hpp"
|
||||
|
@ -137,6 +139,32 @@ void wml_menu_item::fire_event(const map_location & event_hex) const
|
|||
resources::undo_stack->clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the implicit event handler for an inlined [command].
|
||||
*/
|
||||
void wml_menu_item::init_handler() const
|
||||
{
|
||||
// If this menu item has a [command], add a handler for it.
|
||||
if ( !command_.empty() )
|
||||
{
|
||||
add_event_handler(command_, true);
|
||||
|
||||
// Hotkey support
|
||||
if ( use_hotkey_ ) {
|
||||
// Applying default hotkeys here currently does not work because
|
||||
// the hotkeys are reset by play_controler::init_managers() ->
|
||||
// display_manager::display_manager, which is called after this.
|
||||
// The result is that default wml hotkeys will be ignored if wml
|
||||
// hotkeys are set to default in the preferences menu. (They are
|
||||
// still reapplied if set_menu_item is called again, for example
|
||||
// by starting a new campaign.) Since it isn't that important
|
||||
// I'll just leave it for now.
|
||||
hotkey::add_wml_hotkey(play_controller::wml_menu_hotkey_prefix + item_id_,
|
||||
description_, default_hotkey_);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes *this to the provided config.
|
||||
*/
|
||||
|
|
|
@ -35,8 +35,6 @@ class wml_menu_item
|
|||
public:
|
||||
explicit wml_menu_item(const std::string& id, const config* cfg=NULL);
|
||||
|
||||
/// The identification of this item.
|
||||
const std::string & id() const { return item_id_; }
|
||||
/// The WML actions specified within this item.
|
||||
const config & command() const { return command_; }
|
||||
/// The text to display in the menu for this item.
|
||||
|
@ -56,6 +54,8 @@ public:
|
|||
bool can_show(const map_location & hex) const;
|
||||
/// Causes the event associated with this item to fire.
|
||||
void fire_event(const map_location & event_hex) const;
|
||||
/// Initializes the implicit event handler for an inlined [command].
|
||||
void init_handler() const;
|
||||
/// Change the actions associated with this item.
|
||||
/// (Internal bookkeeping only; the caller must still update the event handlers.)
|
||||
void set_command(const config & cfg) { command_ = cfg; }
|
||||
|
|
|
@ -19,13 +19,10 @@
|
|||
|
||||
#include "../global.hpp"
|
||||
#include "wmi_container.hpp"
|
||||
#include "handlers.hpp"
|
||||
#include "menu_item.hpp"
|
||||
|
||||
#include "../config.hpp"
|
||||
#include "../hotkeys.hpp"
|
||||
#include "../log.hpp"
|
||||
#include "../play_controller.hpp"
|
||||
|
||||
#include <boost/foreach.hpp>
|
||||
|
||||
|
@ -137,21 +134,7 @@ void wmi_container::init_handlers() const
|
|||
// Loop through each menu item.
|
||||
BOOST_FOREACH( const wml_menu_item & wmi, *this ) {
|
||||
// If this menu item has a [command], add a handler for it.
|
||||
const config & wmi_command = wmi.command();
|
||||
if ( !wmi_command.empty() ) {
|
||||
add_event_handler(wmi_command, true);
|
||||
if ( wmi.use_hotkey() ) {
|
||||
// Applying default hotkeys here currently does not work because
|
||||
// the hotkeys are reset by play_controler::init_managers() ->
|
||||
// display_manager::display_manager, which is called after this.
|
||||
// The result is that default wml hotkeys will be ignored if wml
|
||||
// hotkeys are set to default in the preferences menu. (They are
|
||||
// still reapplied if set_menu_item is called again, for example
|
||||
// by starting a new campaign.) Since it isn't that important
|
||||
// I'll just leave it for now.
|
||||
hotkey::add_wml_hotkey(play_controller::wml_menu_hotkey_prefix + wmi.id(), wmi.description(), wmi.default_hotkey());
|
||||
}
|
||||
}
|
||||
wmi.init_handler();
|
||||
// Count the menu items (for the diagnostic message).
|
||||
++wmi_count;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue