The Battle for Wesnoth  1.13.4+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 - 2016 by David White <[email protected]>
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 #ifndef GAME_EVENTS_MENU_ITEM_HPP_INCLUDED
21 #define GAME_EVENTS_MENU_ITEM_HPP_INCLUDED
22 
23 #include "config.hpp"
24 #include "tstring.hpp"
25 #include "variable.hpp"
26 
27 class filter_context;
28 class game_data;
29 struct map_location;
30 class unit_map;
31 
32 namespace game_events
33 {
34 
36 {
37 public:
38  /// Constructor for when read from a saved config.
39  wml_menu_item(const std::string& id, const config & cfg);
40  /// Constructor for when defined in an event.
41  wml_menu_item(const std::string& id, const vconfig & definition);
42  /// Constructor for when modified by an event.
43  wml_menu_item(const std::string& id, const vconfig & definition,
44  const wml_menu_item & original);
45 
46  /// The id of this item.
47  const std::string & id() const { return item_id_; }
48  /// The image associated with this menu item.
49  const std::string & image() const;
50  /// If true, allow using the menu to trigger this item.
51  bool use_wml_menu() const { return use_wml_menu_; }
52 
53  /// Returns whether or not *this is applicable given the context.
54  bool can_show(const map_location & hex, const game_data & data, filter_context & context) const;
55  /// Causes the event associated with this item to fire.
56  void fire_event(const map_location & event_hex, const game_data & data) const;
57  /// Removes the implicit event handler for an inlined [command].
58  void finish_handler() const;
59  /// Initializes the implicit event handler for an inlined [command].
60  void init_handler() const;
61  /// The text to put in a menu for this item.
62  /// This will be either translated text or a hotkey identifier.
64  { return use_hotkey_ ? hotkey_id_ : description_.str() + ' '; } // The space is to prevent accidental hotkey binding.
65  /// Writes *this to the provided config.
66  void to_config(config & cfg) const;
67  bool is_synced() const { return is_synced_; }
68 private: // Functions
69  /// Updates *this based on @a vcfg.
70  void update(const vconfig & vcfg);
71  /// Updates our command to @a new_command.
72  void update_command(const config & new_command);
73 
74 private: // Data
75  /// The id of this menu item.
77  /// The name of this item's event(s); based on the item's id.
79  /// The id for this item's hotkey; based on the item's id.
81  /// The image to display in the menu next to this item's description.
83  /// The text to display in the menu for this item.
85  /// Whether or not this event says it makes use of the last selected unit.
87  /// A condition that must hold in order for this menu item to be visible.
88  /// (An empty condition always holds.)
89  vconfig show_if_; // When used, we need a vconfig instead of a config.
90  /// A location filter to be applied to the hex where the menu is invoked.
91  /// (An empty filter always passes.)
92  vconfig filter_location_; // When used, we need a vconfig instead of a config.
93  /// Actions to take when this item is chosen.
95  /// Config object containing the default hotkey.
97  /// If true, allow using a hotkey to trigger this item.
99  /// If true, allow using the menu to trigger this item.
101  /// If true, this item will be sended ot ther clients.
102  /// The command shall not change the gamestate if !is_synced_
104 };
105 
106 } // end namespace game_events
107 
108 #endif // GAME_EVENTS_MENU_ITEM_HPP_INCLUDED
109 
bool needs_select_
Whether or not this event says it makes use of the last selected unit.
Definition: menu_item.hpp:86
vconfig filter_location_
A location filter to be applied to the hex where the menu is invoked.
Definition: menu_item.hpp:92
config command_
Actions to take when this item is chosen.
Definition: menu_item.hpp:94
t_string description_
The text to display in the menu for this item.
Definition: menu_item.hpp:84
void init_handler() const
Initializes the implicit event handler for an inlined [command].
Definition: menu_item.cpp:248
void to_config(config &cfg) const
Writes *this to the provided config.
Definition: menu_item.cpp:267
std::string image_
The image to display in the menu next to this item's description.
Definition: menu_item.hpp:82
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
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:63
vconfig show_if_
A condition that must hold in order for this menu item to be visible.
Definition: menu_item.hpp:89
const std::string item_id_
The id of this menu item.
Definition: menu_item.hpp:76
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:174
Encapsulates the map of the game.
Definition: location.hpp:38
Domain specific events.
Definition: action_wml.cpp:93
bool is_synced_
If true, this item will be sended ot ther clients.
Definition: menu_item.hpp:103
const std::string & image() const
The image associated with this menu item.
Definition: menu_item.cpp:163
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:203
bool use_hotkey_
If true, allow using a hotkey to trigger this item.
Definition: menu_item.hpp:98
void finish_handler() const
Removes the implicit event handler for an inlined [command].
Definition: menu_item.cpp:233
A variable-expanding proxy for the config class.
Definition: variable.hpp:36
bool use_wml_menu_
If true, allow using the menu to trigger this item.
Definition: menu_item.hpp:100
Container associating units to locations.
Definition: map.hpp:90
wml_menu_item(const std::string &id, const config &cfg)
Constructor for when read from a saved config.
Definition: menu_item.cpp:83
void update_command(const config &new_command)
Updates our command to new_command.
Definition: menu_item.cpp:365
void update(const vconfig &vcfg)
Updates *this based on vcfg.
Definition: menu_item.cpp:300
const std::string & str() const
Definition: tstring.hpp:170
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
GLsizei const GLcharARB ** string
Definition: glew.h:4503
bool use_wml_menu() const
If true, allow using the menu to trigger this item.
Definition: menu_item.hpp:51
const std::string event_name_
The name of this item's event(s); based on the item's id.
Definition: menu_item.hpp:78
const std::string & id() const
The id of this item.
Definition: menu_item.hpp:47
const std::string hotkey_id_
The id for this item's hotkey; based on the item's id.
Definition: menu_item.hpp:80
config default_hotkey_
Config object containing the default hotkey.
Definition: menu_item.hpp:96