The Battle for Wesnoth  1.19.0+dev
context_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 #pragma once
17 
20 #include "filter_context.hpp"
22 
23 class map_generator;
24 class game_config_view;
25 
26 namespace editor
27 {
28 
30 {
31 public:
32  using context_ptr = std::unique_ptr<map_context>;
33 
34  context_manager(editor_display& gui, const game_config_view& game_config, const std::string& addon_id);
36 
37  bool is_active_transitions_hotkey(const std::string& item);
38 
39  std::size_t modified_maps(std::string& modified);
40 
42  {
45  }
46 
48 
50  {
51  return clipboard_.empty();
52  }
53 
55  {
56  return clipboard_;
57  }
58 
59  /** Fill the selection with the foreground terrain */
60  void fill_selection();
61 
62  /** Index into the map_contexts_ array */
64  {
66  }
67 
68  std::size_t open_maps(void)
69  {
70  return map_contexts_.size();
71  }
72 
73  /**
74  * Perform an action on the current map_context, then refresh the display.
75  */
76  void perform_refresh(const editor_action& action, bool drag_part = false);
77 
78  /**
79  * Save all maps, open dialog if not named yet, except when using
80  * auto_save_windows which will name unnamed maps "windows_N".
81  * Also record all filenames for future reopening.
82  */
83  void save_all_maps(bool auto_save_windows = false);
84 
85  /** Save the map, open dialog if not named yet. */
86  void save_map();
87 
89  {
90  return gui_;
91  }
92 
93  /**
94  * Refresh everything, i.e. invalidate all hexes and redraw them. Does *not* reload the map.
95  */
96  void refresh_all();
97 
98  /** Display an apply mask dialog and process user input. */
99  void apply_mask_dialog();
100 
101  /** Display an apply mask dialog and process user input. */
102  void create_mask_to_dialog();
103 
104  /** Display an dialog to querry a new id for an [time_area] */
105  void rename_area_dialog();
106 
107  /** Menu expanding for open maps list */
108  void expand_open_maps_menu(std::vector<config>& items, int i);
109 
110  /** Menu expanding for most recent loaded list */
111  void expand_load_mru_menu(std::vector<config>& items, int i);
112 
113  /** Menu expanding for the map's player sides */
114  void expand_sides_menu(std::vector<config>& items, int i);
115 
116  /** Menu expanding for the map's defined areas */
117  void expand_areas_menu(std::vector<config>& items, int i);
118 
119  /** Menu expanding for the map's defined areas */
120  void expand_time_menu(std::vector<config>& items, int i);
121 
122  /** Menu expanding for the map's defined areas */
123  void expand_local_time_menu(std::vector<config>& items, int i);
124 
125  /** Display a load map dialog and process user input. */
126  void load_map_dialog(bool force_same_context = false);
127 
128  /** Open the specified entry from the recent files list. */
129  void load_mru_item(unsigned index, bool force_same_context = false);
130 
131  /** Display a scenario edit dialog and process user input. */
132  void edit_scenario_dialog();
133 
134  /** Display a side edit dialog and process user input. */
135  void edit_side_dialog(int side_index);
136 
137  /** Display a new map dialog and process user input. */
138  void new_map_dialog();
139 
140  /** Display a new map dialog and process user input. */
141  void new_scenario_dialog();
142 
143  /** Display a save map as dialog and process user input. */
144  void save_map_as_dialog();
145 
146  /** Display a save map as dialog and process user input. */
148 
149  /** Display a generate random map dialog and process user input. */
150  void generate_map_dialog();
151 
152  /** Display a load map dialog and process user input. */
153  void resize_map_dialog();
154 
155  std::size_t size()
156  {
157  return map_contexts_.size();
158  }
159 
160  /** Get the current map context object */
162  {
164  }
165 
166  void set_addon_id(const std::string& id)
167  {
168  current_addon_ = id;
169  for(auto& map : map_contexts_) {
170  map->set_addon_id(id);
171  }
172  }
173 
174  /** Set the default dir (where the filebrowser is pointing at when there is no map file opened) */
175  void set_default_dir(const std::string& str)
176  {
177  default_dir_ = str;
178  }
179 
180  void edit_pbl();
181  void change_addon_id();
182 
183  /** Inherited from @ref filter_context. */
184  virtual const display_context& get_disp_context() const override
185  {
186  return get_map_context();
187  }
188 
189  /** Inherited from @ref filter_context. */
190  virtual const tod_manager& get_tod_man() const override
191  {
192  return *get_map_context().get_time_manager();
193  }
194 
195  /** Inherited from @ref filter_context. */
196  virtual const game_data* get_game_data() const override
197  {
198  return nullptr; // No game_data in the editor.
199  }
200 
201  /** Inherited from @ref filter_context. */
202  virtual game_lua_kernel* get_lua_kernel() const override
203  {
204  return nullptr; // No Lua kernel in the editor.
205  }
206 
207  // TODO: Make this private with an accessor or something
209 private:
210  /** init available random map generators */
212 
213  /**
214  * Shows an are-you-sure dialog if the map was modified.
215  * @return true if the user confirmed or the map was not modified, false otherwise
216  */
217  bool confirm_discard();
218 
219  /** Get the current map context object - const version */
221  {
223  }
224 
225  /**
226  * Add a map context. The controller assumes ownership.
227  * @return the index of the added map context in the map_contexts_ array
228  */
229  template<typename... T>
230  int add_map_context(const T&... args);
231 
233 
234  /**
235  * Replace the current map context and refresh accordingly
236  */
237  template<typename... T>
238  void replace_map_context(const T&... args);
239 
241 
242  /**
243  * Creates a default map context object, used to ensure there is always at least one.
244  * Except when we saved windows, in which case reopen them
245  */
246  void create_default_context();
247 
248  /** Performs the necessary housekeeping necessary when switching contexts. */
250 
251 public:
252  /**
253  * Refresh the display after an action has been performed.
254  * The map context contains details of what needs to be refreshed.
255  */
256  void refresh_after_action(bool drag_part = false);
257 
258  /** Closes the active map context. Switches to a valid context afterward or creates a dummy one. */
259  void close_current_context();
260 
261  /** Switches the context to the one under the specified index. */
262  void switch_context(const int index, const bool force = false);
263 
264 private:
265  /**
266  * Save the map under a given filename. Displays an error message on failure.
267  * @return true on success
268  */
269  bool write_map(bool display_confirmation = false);
270  bool write_scenario(bool display_confirmation = false);
271 
272  /**
273  * Create a new map.
274  */
275  void new_map(int width, int height, const t_translation::terrain_code & fill, bool new_context);
276 
277  /**
278  * Create a new scenario.
279  */
280  void new_scenario(int width, int height, const t_translation::terrain_code & fill, bool new_context);
281 
282  /**
283  * Check if a map is already open.
284  * @return index of the map context containing the given filename,
285  * or map_contexts_.size() if not found.
286  */
287  std::size_t check_open_map(const std::string& fn) const;
288 
289  /**
290  * Check if a map is already open. If yes, switch to it
291  * and return true, return false otherwise.
292  */
293  bool check_switch_open_map(const std::string& fn);
294 
295  /**
296  * Displays the specified map name in the window titlebar
297  */
298  void set_window_title();
299 
300 public:
301  /**
302  * Load a map given the filename
303  */
304  void load_map(const std::string& filename, bool new_context);
305 
306  /**
307  * Revert the map by reloading it from disk
308  */
309  void revert_map();
310 
311  /**
312  * Reload the map after it has significantly changed (when e.g. the dimensions changed).
313  * This is necessary to avoid issues with parts of the map being cached in the display class.
314  */
315  void reload_map();
316 
317 private:
319 
321 
322  /** Default directory for map load/save as dialogs */
323  std::string default_dir_;
324 
325  /** The currently selected add-on */
326  std::string current_addon_;
327 
328  /** Available random map generators */
329  std::vector<std::unique_ptr<map_generator>> map_generators_;
331 
333 
334  /** Flag to rebuild terrain on every terrain change */
336 
337  /** The currently opened map context object */
338  std::vector<context_ptr> map_contexts_;
339 
340  /** Clipboard map_fragment -- used for copy-paste. */
342 };
343 
344 }
Abstract class for exposing game data that doesn't depend on the GUI, however which for historical re...
const game_config_view & game_config_
context_manager(editor_display &gui, const game_config_view &game_config, const std::string &addon_id)
bool is_active_transitions_hotkey(const std::string &item)
void new_scenario(int width, int height, const t_translation::terrain_code &fill, bool new_context)
Create a new scenario.
void set_update_transitions_mode(int mode)
void new_map(int width, int height, const t_translation::terrain_code &fill, bool new_context)
Create a new map.
virtual const game_data * get_game_data() const override
Inherited from filter_context.
void fill_selection()
Fill the selection with the foreground terrain.
bool write_map(bool display_confirmation=false)
Save the map under a given filename.
void new_map_dialog()
Display a new map dialog and process user input.
void expand_time_menu(std::vector< config > &items, int i)
Menu expanding for the map's defined areas.
bool confirm_discard()
Shows an are-you-sure dialog if the map was modified.
void expand_sides_menu(std::vector< config > &items, int i)
Menu expanding for the map's player sides.
void generate_map_dialog()
Display a generate random map dialog and process user input.
bool write_scenario(bool display_confirmation=false)
int auto_update_transitions_
Flag to rebuild terrain on every terrain change.
void rename_area_dialog()
Display an dialog to querry a new id for an [time_area].
void revert_map()
Revert the map by reloading it from disk.
map_fragment clipboard_
Clipboard map_fragment – used for copy-paste.
void load_map_dialog(bool force_same_context=false)
Display a load map dialog and process user input.
bool check_switch_open_map(const std::string &fn)
Check if a map is already open.
std::vector< std::unique_ptr< map_generator > > map_generators_
Available random map generators.
int add_map_context_of(context_ptr &&mc)
void set_window_title()
Displays the specified map name in the window titlebar.
std::size_t open_maps(void)
void replace_map_context_with(context_ptr &&mc)
virtual game_lua_kernel * get_lua_kernel() const override
Inherited from filter_context.
void set_default_dir(const std::string &str)
Set the default dir (where the filebrowser is pointing at when there is no map file opened)
void switch_context(const int index, const bool force=false)
Switches the context to the one under the specified index.
map_generator * last_map_generator_
std::size_t check_open_map(const std::string &fn) const
Check if a map is already open.
void save_all_maps(bool auto_save_windows=false)
Save all maps, open dialog if not named yet, except when using auto_save_windows which will name unna...
void save_scenario_as_dialog()
Display a save map as dialog and process user input.
int current_context_index()
Index into the map_contexts_ array.
void refresh_all()
Refresh everything, i.e.
void init_map_generators(const game_config_view &game_config)
init available random map generators
virtual const display_context & get_disp_context() const override
Inherited from filter_context.
void save_map_as_dialog()
Display a save map as dialog and process user input.
void create_default_context()
Creates a default map context object, used to ensure there is always at least one.
void new_scenario_dialog()
Display a new map dialog and process user input.
void replace_map_context(const T &... args)
Replace the current map context and refresh accordingly.
void apply_mask_dialog()
Display an apply mask dialog and process user input.
void load_map(const std::string &filename, bool new_context)
Load a map given the filename.
void save_map()
Save the map, open dialog if not named yet.
void reload_map()
Reload the map after it has significantly changed (when e.g.
void set_addon_id(const std::string &id)
void create_mask_to_dialog()
Display an apply mask dialog and process user input.
void close_current_context()
Closes the active map context.
editor_display & gui()
void resize_map_dialog()
Display a load map dialog and process user input.
void edit_scenario_dialog()
Display a scenario edit dialog and process user input.
std::string current_addon_
The currently selected add-on.
void perform_refresh(const editor_action &action, bool drag_part=false)
Perform an action on the current map_context, then refresh the display.
int add_map_context(const T &... args)
Add a map context.
const map_context & get_map_context() const
Get the current map context object - const version.
std::string default_dir_
Default directory for map load/save as dialogs.
void expand_areas_menu(std::vector< config > &items, int i)
Menu expanding for the map's defined areas.
void expand_local_time_menu(std::vector< config > &items, int i)
Menu expanding for the map's defined areas.
std::unique_ptr< map_context > context_ptr
void load_mru_item(unsigned index, bool force_same_context=false)
Open the specified entry from the recent files list.
map_context & get_map_context()
Get the current map context object.
map_fragment & get_clipboard()
virtual const tod_manager & get_tod_man() const override
Inherited from filter_context.
class location_palette * locs_
void expand_load_mru_menu(std::vector< config > &items, int i)
Menu expanding for most recent loaded list.
void expand_open_maps_menu(std::vector< config > &items, int i)
Menu expanding for open maps list.
std::vector< context_ptr > map_contexts_
The currently opened map context object.
void edit_side_dialog(int side_index)
Display a side edit dialog and process user input.
void refresh_after_action(bool drag_part=false)
Refresh the display after an action has been performed.
void refresh_on_context_change()
Performs the necessary housekeeping necessary when switching contexts.
std::size_t modified_maps(std::string &modified)
Base class for all editor actions.
Definition: action_base.hpp:42
List of starting locations and location ids.
This class wraps around a map to provide a concise interface for the editor to work with.
Definition: map_context.hpp:63
const tod_manager * get_time_manager() const
A map fragment – a collection of locations and information abut them.
A class grating read only view to a vector of config objects, viewed as one config with all children ...
static prefs & get()
void set_editor_auto_update_transitions(int value)
std::size_t i
Definition: function.cpp:968
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:205
void fill(const SDL_Rect &rect, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Fill an area with the given colour.
Definition: draw.cpp:50
Manage the empty-palette in the editor.
Definition: action.cpp:31
Game configuration data as global variables.
Definition: build_info.cpp:61
General purpose widgets.
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:410
const std::vector< std::string > items
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:70
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:49