Skip to content

Commit

Permalink
Add synced=yes(default)/no to [set_menu_item]
Browse files Browse the repository at this point in the history
Allows to add 'unsynced' wml menu hotkeys which are not sended to other
players and are also clickable off-turn.

This can for example used for help menus and similar.
  • Loading branch information
gfgtdf committed Jun 22, 2015
1 parent 5e49703 commit 4074851
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
19 changes: 18 additions & 1 deletion src/game_events/menu_item.cpp
Expand Up @@ -92,6 +92,7 @@ wml_menu_item::wml_menu_item(const std::string& id, const config & cfg) :
default_hotkey_(cfg.child_or_empty("default_hotkey")),
use_hotkey_(cfg["use_hotkey"].to_bool(true)),
use_wml_menu_(cfg["use_hotkey"].str() != "only"),
is_synced_(cfg["synced"].to_bool(true)),
my_manager_()
{
}
Expand All @@ -115,7 +116,9 @@ wml_menu_item::wml_menu_item(const std::string& id, const vconfig & definition)
command_(),
default_hotkey_(),
use_hotkey_(true),
use_wml_menu_(true)
use_wml_menu_(true),
is_synced_(true),
my_manager_()
{
// On the off-chance that update() doesn't do it, add the hotkey here.
// (Update can always modify it.)
Expand Down Expand Up @@ -197,8 +200,19 @@ bool wml_menu_item::can_show(const map_location & hex, const game_data & data, f
* @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.
*/
#define STR(X) #X
void wml_menu_item::fire_event(const map_location & event_hex, const game_data & data) const
{
if(!this->is_synced())
{
if (boost::shared_ptr<manager * const> man = my_manager_.lock()) {
(**man).pump().fire(command_["id"], event_hex);
} else {
ERR_NG << "?? File: " __FILE__ " Line:" STR(__LINE__) " my_manager_.lock() failed\n";
#undef STR
}
return;
}
const map_location & last_select = data.last_selected;

// No new player-issued commands allowed while this is firing.
Expand Down Expand Up @@ -270,6 +284,7 @@ void wml_menu_item::to_config(config & cfg) const
cfg["image"] = image_;
cfg["description"] = description_;
cfg["needs_select"] = needs_select_;
cfg["synced"] = is_synced_;

if ( use_hotkey_ && use_wml_menu_ )
cfg["use_hotkey"] = true;
Expand Down Expand Up @@ -312,6 +327,8 @@ void wml_menu_item::update(const vconfig & vcfg)

if ( vcfg.has_attribute("needs_select") )
needs_select_ = vcfg["needs_select"].to_bool();
if ( vcfg.has_attribute("synced") )
is_synced_ = vcfg["synced"].to_bool(true);

if ( const vconfig & child = vcfg.child("show_if") ) {
show_if_ = child;
Expand Down
5 changes: 4 additions & 1 deletion src/game_events/menu_item.hpp
Expand Up @@ -69,7 +69,7 @@ class wml_menu_item
{ return use_hotkey_ ? hotkey_id_ : description_.str() + ' '; } // The space is to prevent accidental hotkey binding.
/// Writes *this to the provided config.
void to_config(config & cfg) const;

bool is_synced() const { return is_synced_; }
private: // Functions
/// Updates *this based on @a vcfg.
void update(const vconfig & vcfg);
Expand Down Expand Up @@ -103,6 +103,9 @@ class wml_menu_item
bool use_hotkey_;
/// If true, allow using the menu to trigger this item.
bool use_wml_menu_;
/// If true, this item will be sended ot ther clients.
/// The command shall not change the gamestate if !is_synced_
bool is_synced_;

/// A link back to my manager
mutable boost::weak_ptr<manager * const> my_manager_;
Expand Down
3 changes: 1 addition & 2 deletions src/game_events/wmi_container.hpp
Expand Up @@ -95,10 +95,9 @@ class wmi_container{
private:
/// Returns an iterator to a menu item with the given @a id, if one exists.
iterator find(const std::string & id) { return iterator(wml_menu_items_.find(id)); }
public:
/// Returns an iterator to a menu item with the given @a id, if one exists.
const_iterator find(const std::string & id) const { return const_iterator(wml_menu_items_.find(id)); }

public:
// Iteration support:
iterator begin() { return iterator(wml_menu_items_.begin()); }
iterator end() { return iterator(wml_menu_items_.end()); }
Expand Down
5 changes: 5 additions & 0 deletions src/game_state.cpp
Expand Up @@ -350,3 +350,8 @@ game_events::wmi_container& game_state::get_wml_menu_items()
{
return this->events_manager_->wml_menu_items_;
}

const game_events::wmi_container& game_state::get_wml_menu_items() const
{
return this->events_manager_->wml_menu_items_;
}
1 change: 1 addition & 0 deletions src/game_state.hpp
Expand Up @@ -55,6 +55,7 @@ class game_state : public filter_context


game_events::wmi_container& get_wml_menu_items();
const game_events::wmi_container& get_wml_menu_items() const;
int first_human_team_; //needed to initialize the viewpoint during setup

game_state(const config & level, const tdata_cache & tdata);
Expand Down
17 changes: 15 additions & 2 deletions src/hotkey_handler_sp.cpp
Expand Up @@ -21,6 +21,8 @@
#include "play_controller.hpp"
#include "playsingle_controller.hpp"
#include "whiteboard/manager.hpp"
#include "game_events/menu_item.hpp"
#include "game_events/wmi_container.hpp"

#include "unit.hpp"

Expand Down Expand Up @@ -187,8 +189,19 @@ bool playsingle_controller::hotkey_handler::can_execute_command(const hotkey::ho
switch (command){

case hotkey::HOTKEY_WML:
//code mixed from play_controller::show_menu and code here
return viewing_team_is_playing() && !events::commands_disabled && viewing_team().is_local_human() && !linger() && !browse();
{

int prefixlen = wml_menu_hotkey_prefix.length();
if(cmd.command.compare(0, prefixlen, wml_menu_hotkey_prefix) != 0) {
return false;
}
game_events::wmi_container::const_iterator it = gamestate().get_wml_menu_items().find(cmd.command.substr(prefixlen));
if(it == gamestate().get_wml_menu_items().end()) {
return false;
}

return !(**it).is_synced() || (viewing_team_is_playing() && !events::commands_disabled && viewing_team().is_local_human() && !linger() && !browse());
}
case hotkey::HOTKEY_UNIT_HOLD_POSITION:
case hotkey::HOTKEY_END_UNIT_TURN:
return !browse() && !linger() && !events::commands_disabled;
Expand Down

0 comments on commit 4074851

Please sign in to comment.