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