The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
menu_item.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2017 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Declarations for a class that implements WML-defined (right-click) menu items.
18  */
19 
20 #pragma once
21 
22 #include "config.hpp"
23 #include "variable.hpp"
24 
25 class filter_context;
26 class game_data;
27 struct map_location;
28 
29 namespace game_events
30 {
32 {
33 public:
34  /**
35  * Constructor for reading from a saved config.
36  * This is the reverse of to_config() and corresponds to reading [menu_item].
37  * Handlers are not initialized.
38  */
39  wml_menu_item(const std::string& id, const config& cfg);
40 
41  /**
42  * Constructor for items defined in an event.
43  * This is where default values are defined (the other constructors should have
44  * all values to work with).
45  * @param[in] id The id of the menu item.
46  * @param[in] definition The WML defining this menu item.
47  */
48  wml_menu_item(const std::string& id, const vconfig& definition);
49 
50  /**
51  * Constructor for items modified by an event.
52  * (To avoid problems with a menu item's command changing itself, we make a
53  * new menu item instead of modifying the existing one.)
54  * @param[in] id The id of the menu item.
55  * @param[in] definition The WML defining this menu item.
56  * @param[in] original The previous version of the menu item with this id.
57  */
58  wml_menu_item(const std::string& id, const vconfig& definition, const wml_menu_item& original);
59 
60  /** The id of this item. */
61  const std::string& id() const
62  {
63  return item_id_;
64  }
65 
66  /**
67  * The image associated with this menu item.
68  * The returned string will not be empty; a default will be supplied if needed.
69  */
70  const std::string& image() const;
71 
72  /** If true, allow using the menu to trigger this item. */
73  bool use_wml_menu() const
74  {
75  return use_wml_menu_;
76  }
77 
78  /**
79  * Returns whether or not *this is applicable given the context.
80  * Assumes game variables x1, y1, and unit have been set.
81  * @param[in] hex The hex where the menu will appear.
82  */
83  bool can_show(const map_location& hex, const game_data& data, filter_context& context) const;
84 
85  /**
86  * Causes the event associated with this item to fire.
87  * Also records the event.
88  * This includes recording the previous select event, if applicable.
89  * The undo stack will be cleared if the event mutated the gamestate.
90  *
91  * @param[in] event_hex The location of the event (where the menu was opened).
92  * @param[in] data The game data for the most recent "select" event.
93  */
94  void fire_event(const map_location& event_hex, const game_data& data) const;
95 
96  /** Removes the implicit event handler for an inlined [command]. */
97  void finish_handler() const;
98 
99  /** Initializes the implicit event handler for an inlined [command]. */
100  void init_handler() const;
101 
102  /**
103  * The text to put in a menu for this item.
104  * This will be either translated text or a hotkey identifier.
105  */
107  {
108  // The space is to prevent accidental hotkey binding.
109  return use_hotkey_ ? hotkey_id_ : description_.str() + ' ';
110  }
111 
112  /**
113  * Writes *this to the provided config.
114  * This is the reverse of the constructor from a config and corresponds to
115  * what will appear in [menu_item].
116  */
117  void to_config(config& cfg) const;
118 
119  bool is_synced() const
120  {
121  return is_synced_;
122  }
123 
124 private:
125  /**
126  * Updates *this based on @a vcfg.
127  * This corresponds to what can appear in [set_menu_item].
128  */
129  void update(const vconfig& vcfg);
130 
131  /** Updates our command to @a new_command. */
132  void update_command(const config& new_command);
133 
134 private:
135  /** The id of this menu item. */
137 
138  /** The name of this item's event(s); based on the item's id. */
140 
141  /** The id for this item's hotkey; based on the item's id. */
143 
144  /** The image to display in the menu next to this item's description. */
146 
147  /** The text to display in the menu for this item. */
149 
150  /** Whether or not this event says it makes use of the last selected unit. */
152 
153  /**
154  * A condition that must hold in order for this menu item to be visible.
155  * (An empty condition always holds.)
156  *
157  * When used, we need a vconfig instead of a config.
158  */
160 
161  /**
162  * A location filter to be applied to the hex where the menu is invoked.
163  * (An empty filter always passes.)
164  *
165  * When used, we need a vconfig instead of a config.
166  */
168 
169  /** Actions to take when this item is chosen. */
171 
172  /** Config object containing the default hotkey. */
174 
175  /** If true, allow using a hotkey to trigger this item. */
177 
178  /** If true, allow using the menu to trigger this item. */
180 
181  /**
182  * If true, this item will be sent ot ther clients.
183  * The command shall not change the gamestate if !is_synced_
184  */
186 };
187 
188 } // end namespace game_events
bool needs_select_
Whether or not this event says it makes use of the last selected unit.
Definition: menu_item.hpp:151
std::vector< char_t > string
vconfig filter_location_
A location filter to be applied to the hex where the menu is invoked.
Definition: menu_item.hpp:167
config command_
Actions to take when this item is chosen.
Definition: menu_item.hpp:170
t_string description_
The text to display in the menu for this item.
Definition: menu_item.hpp:148
void init_handler() const
Initializes the implicit event handler for an inlined [command].
Definition: menu_item.cpp:200
void to_config(config &cfg) const
Writes *this to the provided config.
Definition: menu_item.cpp:214
std::string image_
The image to display in the menu next to this item's description.
Definition: menu_item.hpp:145
Definitions for the interface to Wesnoth Markup Language (WML).
std::string menu_text() const
The text to put in a menu for this item.
Definition: menu_item.hpp:106
vconfig show_if_
A condition that must hold in order for this menu item to be visible.
Definition: menu_item.hpp:159
const std::string item_id_
The id of this menu item.
Definition: menu_item.hpp:136
bool can_show(const map_location &hex, const game_data &data, filter_context &context) const
Returns whether or not *this is applicable given the context.
Definition: menu_item.cpp:138
Encapsulates the map of the game.
Definition: location.hpp:40
Domain specific events.
Definition: action_wml.cpp:88
bool is_synced_
If true, this item will be sent ot ther clients.
Definition: menu_item.hpp:185
const std::string & image() const
The image associated with this menu item.
Definition: menu_item.cpp:131
void fire_event(const map_location &event_hex, const game_data &data) const
Causes the event associated with this item to fire.
Definition: menu_item.cpp:159
bool use_hotkey_
If true, allow using a hotkey to trigger this item.
Definition: menu_item.hpp:176
void finish_handler() const
Removes the implicit event handler for an inlined [command].
Definition: menu_item.cpp:187
A variable-expanding proxy for the config class.
Definition: variable.hpp:42
bool use_wml_menu_
If true, allow using the menu to trigger this item.
Definition: menu_item.hpp:179
wml_menu_item(const std::string &id, const config &cfg)
Constructor for reading from a saved config.
Definition: menu_item.cpp:67
void update_command(const config &new_command)
Updates our command to new_command.
Definition: menu_item.cpp:324
void update(const vconfig &vcfg)
Updates *this based on vcfg.
Definition: menu_item.cpp:257
const std::string & str() const
Definition: tstring.hpp:186
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
bool use_wml_menu() const
If true, allow using the menu to trigger this item.
Definition: menu_item.hpp:73
const std::string event_name_
The name of this item's event(s); based on the item's id.
Definition: menu_item.hpp:139
const std::string & id() const
The id of this item.
Definition: menu_item.hpp:61
const std::string hotkey_id_
The id for this item's hotkey; based on the item's id.
Definition: menu_item.hpp:142
config default_hotkey_
Config object containing the default hotkey.
Definition: menu_item.hpp:173