The Battle for Wesnoth  1.19.0-dev
wmi_manager.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
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 container for wml_menu_item.
19  */
20 
21 #pragma once
22 
23 #include <map>
24 #include <memory>
25 #include <string>
26 #include <vector>
27 
28 class config;
29 class filter_context;
30 class game_data;
31 class game_lua_kernel;
32 struct map_location;
33 class unit_map;
34 class vconfig;
35 
36 namespace game_events
37 {
38 class wml_menu_item;
39 
41 {
42 public:
43  /** wml_menu_item pointers */
44  using item_ptr = std::shared_ptr<wml_menu_item>;
45 
46  wmi_manager();
47  ~wmi_manager();
48 
49  /** Returns true if no menu items are being managed. */
50  bool empty() const
51  {
52  return wml_menu_items_.empty();
53  }
54 
55  /** Erases the item with the provided @a id. */
56  bool erase(const std::string& id);
57 
58  /** Fires the menu item with the given @a id. */
59  bool fire_item(const std::string& id,
60  const map_location& hex,
62  filter_context& fc,
63  unit_map& units,
64  bool is_key_hold_repeat = false) const;
65 
66  /**
67  * Gets the menu item with the specified ID.
68  *
69  * @param id Item id.
70  * @returns Pointer to the relevant item, or nullptr if not found.
71  */
72  item_ptr get_item(const std::string& id) const;
73 
74  /** Returns the menu items that can be shown for the given location. */
75  void get_items(const map_location& hex,
76  std::vector<config>& items,
77  filter_context& fc,
79  unit_map& units) const;
80 
81  /** Initializes the implicit event handlers for inlined [command]s. */
82  void init_handlers(game_lua_kernel& lk) const;
83 
84  void to_config(config& cfg) const;
85 
86  /** Updates or creates (as appropriate) the menu item with the given @a id. */
87  void set_item(const std::string& id, const vconfig& menu_item);
88 
89  /** Sets the current menu items to the "menu_item" children of @a cfg. */
90  void set_menu_items(const config& cfg);
91 
92  /** Gets the number of menu items owned. */
93  std::size_t size() const
94  {
95  return wml_menu_items_.size();
96  }
97 
98 private:
99  std::map<std::string, item_ptr> wml_menu_items_;
100 };
101 
102 } // end namespace game_events
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
void get_items(const map_location &hex, std::vector< config > &items, filter_context &fc, game_data &gamedata, unit_map &units) const
Returns the menu items that can be shown for the given location.
void init_handlers(game_lua_kernel &lk) const
Initializes the implicit event handlers for inlined [command]s.
bool empty() const
Returns true if no menu items are being managed.
Definition: wmi_manager.hpp:50
std::shared_ptr< wml_menu_item > item_ptr
wml_menu_item pointers
Definition: wmi_manager.hpp:44
bool fire_item(const std::string &id, const map_location &hex, game_data &gamedata, filter_context &fc, unit_map &units, bool is_key_hold_repeat=false) const
Fires the menu item with the given id.
Definition: wmi_manager.cpp:79
std::map< std::string, item_ptr > wml_menu_items_
Definition: wmi_manager.hpp:99
bool erase(const std::string &id)
Erases the item with the provided id.
Definition: wmi_manager.cpp:53
void to_config(config &cfg) const
std::size_t size() const
Gets the number of menu items owned.
Definition: wmi_manager.hpp:93
item_ptr get_item(const std::string &id) const
Gets the menu item with the specified ID.
void set_item(const std::string &id, const vconfig &menu_item)
Updates or creates (as appropriate) the menu item with the given id.
void set_menu_items(const config &cfg)
Sets the current menu items to the "menu_item" children of cfg.
Container associating units to locations.
Definition: map.hpp:98
A variable-expanding proxy for the config class.
Definition: variable.hpp:45
Domain specific events.
const std::vector< std::string > items
Encapsulates the map of the game.
Definition: location.hpp:38
const std::string & gamedata