Move fire_wml_menu_item_event into wml_menu_item.

This allows better encapsulation, as the play_controller no longer
needs to know details about the menu item, like the event's name.
This commit is contained in:
JaMiT 2013-10-27 02:33:43 -05:00
parent 8e1a1dd5f6
commit 6013a7293c
4 changed files with 38 additions and 21 deletions

View file

@ -21,10 +21,14 @@
#include "menu_item.hpp"
#include "conditional_wml.hpp"
#include "handlers.hpp"
#include "pump.hpp"
#include "../actions/undo.hpp"
#include "../game_config.hpp"
#include "../gamestatus.hpp"
#include "../log.hpp"
#include "../mouse_handler_base.hpp"
#include "../replay.hpp"
#include "../resources.hpp"
#include "../terrain_filter.hpp"
@ -106,6 +110,32 @@ bool wml_menu_item::can_show(const map_location & hex) const
return true;
}
/**
* Causes the event associated with this item to fire.
* Also records the event.
* This includes recording the previous select event, if applicable.
* The undo stack will be cleared if the event mutated the gamestate.
*
* @param[in] event_hex The location of the event (where the menu was opened).
* @param[in] last_select The location of the most recent "select" event.
*/
void wml_menu_item::fire_event(const map_location & event_hex,
const map_location & last_select) const
{
// No new player-issued commands allowed while this is firing.
const events::command_disabler disable_commands;
// Should we record the preceding select event?
if ( needs_select_ && last_select.valid() )
recorder.add_event("select", last_select);
// Record and fire this item's event.
recorder.add_event(event_name_, event_hex);
if ( fire(event_name_, event_hex) )
// The event has mutated the gamestate
resources::undo_stack->clear();
}
/**
* Writes *this to the provided config.
*/

View file

@ -56,6 +56,9 @@ public:
/// Returns whether or not *this is applicable given the context.
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 map_location & last_select) 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; }

View file

@ -802,24 +802,6 @@ bool play_controller::enemies_visible() const
return false;
}
//used in play_controller::execute_command
void play_controller::fire_wml_menu_item_event(const game_events::wml_menu_item &menu_item)
{
const events::command_disabler disable_commands;
if(gamedata_.last_selected.valid() && menu_item.needs_select())
{
recorder.add_event("select", gamedata_.last_selected);
}
map_location const& menu_hex = mouse_handler_.get_last_hex();
std::string const & event_name = menu_item.event_name();
recorder.add_event(event_name, menu_hex);
if(game_events::fire(event_name, menu_hex))
{
// The event has mutated the gamestate
undo_stack_->clear();
}
}
bool play_controller::execute_command(const hotkey::hotkey_command& cmd, int index)
{
@ -831,7 +813,8 @@ bool play_controller::execute_command(const hotkey::hotkey_command& cmd, int ind
throw game::load_game_exception(savenames_[i],false,false,false,"");
} else if (i < wml_commands_.size() && wml_commands_[i] != NULL) {
fire_wml_menu_item_event(*wml_commands_[i]);
wml_commands_[i]->fire_event(mouse_handler_.get_last_hex(),
gamedata_.last_selected);
return true;
}
}
@ -857,7 +840,8 @@ bool play_controller::execute_command(const hotkey::hotkey_command& cmd, int ind
if((!iter->needs_select()
|| gamedata_.last_selected.valid()))
{
fire_wml_menu_item_event(*iter);
iter->fire_event(mouse_handler_.get_last_hex(),
gamedata_.last_selected);
}
}
}

View file

@ -262,8 +262,8 @@ private:
void init(CVideo &video);
// Expand AUTOSAVES in the menu items, setting the real savenames.
void expand_autosaves(std::vector<std::string>& items);
std::vector<std::string> savenames_;
void fire_wml_menu_item_event(const game_events::wml_menu_item &menu_item);
void expand_wml_commands(std::vector<std::string>& items);
std::vector<const game_events::wml_menu_item *> wml_commands_;