The Battle for Wesnoth  1.17.0-dev
context_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 #pragma once
17 
20 #include "filter_context.hpp"
21 #include "preferences/editor.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 
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  /** Set the default dir (where the filebrowser is pointing at when there is no map file opened) */
167  void set_default_dir(const std::string& str)
168  {
169  default_dir_ = str;
170  }
171 
172  /** Inherited from @ref filter_context. */
173  virtual const display_context& get_disp_context() const override
174  {
175  return get_map_context();
176  }
177 
178  /** Inherited from @ref filter_context. */
179  virtual const tod_manager& get_tod_man() const override
180  {
181  return *get_map_context().get_time_manager();
182  }
183 
184  /** Inherited from @ref filter_context. */
185  virtual const game_data* get_game_data() const override
186  {
187  return nullptr; // No game_data in the editor.
188  }
189 
190  /** Inherited from @ref filter_context. */
191  virtual game_lua_kernel* get_lua_kernel() const override
192  {
193  return nullptr; // No Lua kernel in the editor.
194  }
195 
196  // TODO: Make this private with an accessor or something
198 private:
199  /** init available random map generators */
201 
202  /**
203  * Shows an are-you-sure dialog if the map was modified.
204  * @return true if the user confirmed or the map was not modified, false otherwise
205  */
206  bool confirm_discard();
207 
208  /** Get the current map context object - const version */
210  {
212  }
213 
214  /**
215  * Add a map context. The controller assumes ownership.
216  * @return the index of the added map context in the map_contexts_ array
217  */
218  template<typename... T>
219  int add_map_context(const T&... args);
220 
222 
223  /**
224  * Replace the current map context and refresh accordingly
225  */
226  template<typename... T>
227  void replace_map_context(const T&... args);
228 
230 
231  /**
232  * Creates a default map context object, used to ensure there is always at least one.
233  * Except when we saved windows, in which case reopen them
234  */
235  void create_default_context();
236 
237  /** Performs the necessary housekeeping necessary when switching contexts. */
239 
240 public:
241  /**
242  * Refresh the display after an action has been performed.
243  * The map context contains details of what needs to be refreshed.
244  */
245  void refresh_after_action(bool drag_part = false);
246 
247  /** Closes the active map context. Switches to a valid context afterward or creates a dummy one. */
248  void close_current_context();
249 
250  /** Switches the context to the one under the specified index. */
251  void switch_context(const int index, const bool force = false);
252 
253 private:
254  /**
255  * Save the map under a given filename.
256  * @return true on success
257  */
258  bool save_map_as(const std::string& filename);
259  //TODO
260  bool save_scenario_as(const std::string& filename);
261 
262  /**
263  * Save the map under a given filename. Displays an error message on failure.
264  * @return true on success
265  */
266  bool write_map(bool display_confirmation = false);
267  bool write_scenario(bool display_confirmation = false);
268 
269  /**
270  * Create a new map.
271  */
272  void new_map(int width, int height, const t_translation::terrain_code & fill, bool new_context);
273 
274  /**
275  * Create a new scenario.
276  */
277  void new_scenario(int width, int height, const t_translation::terrain_code & fill, bool new_context);
278 
279  /**
280  * Check if a map is already open.
281  * @return index of the map context containing the given filename,
282  * or map_contexts_.size() if not found.
283  */
284  std::size_t check_open_map(const std::string& fn) const;
285 
286  /**
287  * Check if a map is already open. If yes, switch to it
288  * and return true, return false otherwise.
289  */
290  bool check_switch_open_map(const std::string& fn);
291 
292  /**
293  * Displays the specified map name in the window titlebar
294  */
295  void set_window_title();
296 
297 public:
298  /**
299  * Load a map given the filename
300  */
301  void load_map(const std::string& filename, bool new_context);
302 
303  /**
304  * Revert the map by reloading it from disk
305  */
306  void revert_map();
307 
308  /**
309  * Reload the map after it has significantly changed (when e.g. the dimensions changed).
310  * This is necessary to avoid issues with parts of the map being cached in the display class.
311  */
312  void reload_map();
313 
314 private:
316 
318 
319  /** Default directory for map load/save as dialogs */
320  std::string default_dir_;
321 
322  /** Available random map generators */
323  std::vector<std::unique_ptr<map_generator>> map_generators_;
325 
327 
328  /** Flag to rebuild terrain on every terrain change */
330 
331  /** The currently opened map context object */
332  std::vector<context_ptr> map_contexts_;
333 
334  /** Clipboard map_fragment -- used for copy-paste. */
336 };
337 
338 }
bool write_scenario(bool display_confirmation=false)
void apply_mask_dialog()
Display an apply mask dialog and process user input.
std::size_t check_open_map(const std::string &fn) const
Check if a map is already open.
A map fragment – a collection of locations and information abut them.
bool check_switch_open_map(const std::string &fn)
Check if a map is already open.
std::unique_ptr< map_context > context_ptr
std::size_t modified_maps(std::string &modified)
int auto_update_transitions_
Flag to rebuild terrain on every terrain change.
int current_context_index()
Index into the map_contexts_ array.
void edit_scenario_dialog()
Display a scenario edit dialog and process user input.
void replace_map_context_with(context_ptr &&mc)
std::vector< context_ptr > map_contexts_
The currently opened map context object.
void reload_map()
Reload the map after it has significantly changed (when e.g.
void resize_map_dialog()
Display a load map dialog and process user input.
void save_scenario_as_dialog()
Display a save map as dialog and process user input.
map_fragment clipboard_
Clipboard map_fragment – used for copy-paste.
void rename_area_dialog()
Display an dialog to querry a new id for an [time_area].
void refresh_after_action(bool drag_part=false)
Refresh the display after an action has been performed.
virtual const game_data * get_game_data() const override
Inherited from filter_context.
void save_map()
Save the map, open dialog if not named yet.
General purpose widgets.
std::string default_dir_
Default directory for map load/save as dialogs.
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:50
void perform_refresh(const editor_action &action, bool drag_part=false)
Perform an action on the current map_context, then refresh the display.
void set_update_transitions_mode(int mode)
const std::vector< std::string > items
int add_map_context(const T &... args)
Add a map context.
bool confirm_discard()
Shows an are-you-sure dialog if the map was modified.
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) ...
map_context & get_map_context()
Get the current map context object.
void refresh_all()
Refresh everything, i.e.
int add_map_context_of(context_ptr &&mc)
void revert_map()
Revert the map by reloading it from disk.
void expand_areas_menu(std::vector< config > &items, int i)
Menu expanding for the map&#39;s defined areas.
void close_current_context()
Closes the active map context.
virtual game_lua_kernel * get_lua_kernel() const override
Inherited from filter_context.
void load_map_dialog(bool force_same_context=false)
Display a load map dialog and process user input.
const game_config_view & game_config_
void set_auto_update_transitions(int value)
Definition: editor.cpp:29
void new_scenario_dialog()
Display a new map dialog and process user input.
std::size_t open_maps(void)
void refresh_on_context_change()
Performs the necessary housekeeping necessary when switching contexts.
Manage the empty-palette in the editor.
Definition: action.cpp:30
map_fragment & get_clipboard()
bool is_active_transitions_hotkey(const std::string &item)
void switch_context(const int index, const bool force=false)
Switches the context to the one under the specified index.
void new_scenario(int width, int height, const t_translation::terrain_code &fill, bool new_context)
Create a new scenario.
void new_map(int width, int height, const t_translation::terrain_code &fill, bool new_context)
Create a new map.
void load_map(const std::string &filename, bool new_context)
Load a map given the filename.
class location_palette * locs_
std::size_t i
Definition: function.cpp:967
void fill_selection()
Fill the selection with the foreground terrain.
void save_map_as_dialog()
Display a save map as dialog and process user input.
bool write_map(bool display_confirmation=false)
Save the map under a given filename.
Game configuration data as global variables.
Definition: build_info.cpp:59
void expand_local_time_menu(std::vector< config > &items, int i)
Menu expanding for the map&#39;s defined areas.
bool save_scenario_as(const std::string &filename)
const tod_manager * get_time_manager() const
editor_display & gui()
Base class for all editor actions.
Definition: action_base.hpp:41
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 create_mask_to_dialog()
Display an apply mask dialog and process user input.
This class wraps around a map to provide a concise interface for the editor to work with...
Definition: map_context.hpp:60
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:72
void replace_map_context(const T &... args)
Replace the current map context and refresh accordingly.
context_manager(editor_display &gui, const game_config_view &game_config)
void expand_sides_menu(std::vector< config > &items, int i)
Menu expanding for the map&#39;s player sides.
map_generator * last_map_generator_
void edit_side_dialog(int side_index)
Display a side edit dialog and process user input.
const map_context & get_map_context() const
Get the current map context object - const version.
std::vector< std::unique_ptr< map_generator > > map_generators_
Available random map generators.
void expand_load_mru_menu(std::vector< config > &items, int i)
Menu expanding for most recent loaded list.
void init_map_generators(const game_config_view &game_config)
init available random map generators
void generate_map_dialog()
Display a generate random map dialog and process user input.
void set_window_title()
Displays the specified map name in the window titlebar.
void create_default_context()
Creates a default map context object, used to ensure there is always at least one.
bool save_map_as(const std::string &filename)
Save the map under a given filename.
void expand_time_menu(std::vector< config > &items, int i)
Menu expanding for the map&#39;s defined areas.
virtual const tod_manager & get_tod_man() const override
Inherited from filter_context.
void load_mru_item(unsigned index, bool force_same_context=false)
Open the specified entry from the recent files list.
virtual const display_context & get_disp_context() const override
Inherited from filter_context.
void expand_open_maps_menu(std::vector< config > &items, int i)
Menu expanding for open maps list.
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:410
void new_map_dialog()
Display a new map dialog and process user input.