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