The Battle for Wesnoth  1.15.11+dev
context_manager.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://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 #pragma once
16 
19 #include "filter_context.hpp"
20 #include "preferences/editor.hpp"
21 
22 class map_generator;
23 class game_config_view;
24 
25 namespace editor
26 {
27 
29 {
30 public:
31  using context_ptr = std::unique_ptr<map_context>;
32 
35 
36  bool is_active_transitions_hotkey(const std::string& item);
37 
38  std::size_t modified_maps(std::string& modified);
39 
41  {
44  }
45 
47 
49  {
50  return clipboard_.empty();
51  }
52 
54  {
55  return clipboard_;
56  }
57 
58  /** Fill the selection with the foreground terrain */
59  void fill_selection();
60 
61  /** Index into the map_contexts_ array */
63  {
65  }
66 
67  std::size_t open_maps(void)
68  {
69  return map_contexts_.size();
70  }
71 
72  /**
73  * Perform an action on the current map_context, then refresh the display.
74  */
75  void perform_refresh(const editor_action& action, bool drag_part = false);
76 
77  /**
78  * Save all maps, open dialog if not named yet, except when using
79  * auto_save_windows which will name unnamed maps "windows_N".
80  * Also record all filenames for future reopening.
81  */
82  void save_all_maps(bool auto_save_windows = false);
83 
84  /** Save the map, open dialog if not named yet. */
85  void save_map();
86 
88  {
89  return gui_;
90  }
91 
92  /**
93  * Refresh everything, i.e. invalidate all hexes and redraw them. Does *not* reload the map.
94  */
95  void refresh_all();
96 
97  /** Display an apply mask dialog and process user input. */
98  void apply_mask_dialog();
99 
100  /** Display an apply mask dialog and process user input. */
101  void create_mask_to_dialog();
102 
103  /** Display an dialog to querry a new id for an [time_area] */
104  void rename_area_dialog();
105 
106  /** Menu expanding for open maps list */
107  void expand_open_maps_menu(std::vector<config>& items, int i);
108 
109  /** Menu expanding for most recent loaded list */
110  void expand_load_mru_menu(std::vector<config>& items, int i);
111 
112  /** Menu expanding for the map's player sides */
113  void expand_sides_menu(std::vector<config>& items, int i);
114 
115  /** Menu expanding for the map's defined areas */
116  void expand_areas_menu(std::vector<config>& items, int i);
117 
118  /** Menu expanding for the map's defined areas */
119  void expand_time_menu(std::vector<config>& items, int i);
120 
121  /** Menu expanding for the map's defined areas */
122  void expand_local_time_menu(std::vector<config>& items, int i);
123 
124  /** Display a load map dialog and process user input. */
125  void load_map_dialog(bool force_same_context = false);
126 
127  /** Open the specified entry from the recent files list. */
128  void load_mru_item(unsigned index, bool force_same_context = false);
129 
130  /** Display a scenario edit dialog and process user input. */
131  void edit_scenario_dialog();
132 
133  /** Display a side edit dialog and process user input. */
134  void edit_side_dialog(int side_index);
135 
136  /** Display a new map dialog and process user input. */
137  void new_map_dialog();
138 
139  /** Display a new map dialog and process user input. */
140  void new_scenario_dialog();
141 
142  /** Display a save map as dialog and process user input. */
143  void save_map_as_dialog();
144 
145  /** Display a save map as dialog and process user input. */
147 
148  /** Display a generate random map dialog and process user input. */
149  void generate_map_dialog();
150 
151  /** Display a load map dialog and process user input. */
152  void resize_map_dialog();
153 
154  std::size_t size()
155  {
156  return map_contexts_.size();
157  }
158 
159  /** Get the current map context object */
161  {
163  }
164 
165  /** Set the default dir (where the filebrowser is pointing at when there is no map file opened) */
166  void set_default_dir(const std::string& str)
167  {
168  default_dir_ = str;
169  }
170 
171  /** Inherited from @ref filter_context. */
172  virtual const display_context& get_disp_context() const override
173  {
174  return get_map_context();
175  }
176 
177  /** Inherited from @ref filter_context. */
178  virtual const tod_manager& get_tod_man() const override
179  {
180  return *get_map_context().get_time_manager();
181  }
182 
183  /** Inherited from @ref filter_context. */
184  virtual const game_data* get_game_data() const override
185  {
186  return nullptr; // No game_data in the editor.
187  }
188 
189  /** Inherited from @ref filter_context. */
190  virtual game_lua_kernel* get_lua_kernel() const override
191  {
192  return nullptr; // No Lua kernel in the editor.
193  }
194 
195  // TODO: Make this private with an accessor or something
197 private:
198  /** init available random map generators */
200 
201  /**
202  * Shows an are-you-sure dialog if the map was modified.
203  * @return true if the user confirmed or the map was not modified, false otherwise
204  */
205  bool confirm_discard();
206 
207  /** Get the current map context object - const version */
209  {
211  }
212 
213  /**
214  * Add a map context. The controller assumes ownership.
215  * @return the index of the added map context in the map_contexts_ array
216  */
217  template<typename... T>
218  int add_map_context(const T&... args);
219 
221 
222  /**
223  * Replace the current map context and refresh accordingly
224  */
225  template<typename... T>
226  void replace_map_context(const T&... args);
227 
229 
230  /**
231  * Creates a default map context object, used to ensure there is always at least one.
232  * Except when we saved windows, in which case reopen them
233  */
234  void create_default_context();
235 
236  /** Performs the necessary housekeeping necessary when switching contexts. */
238 
239 public:
240  /**
241  * Refresh the display after an action has been performed.
242  * The map context contains details of what needs to be refreshed.
243  */
244  void refresh_after_action(bool drag_part = false);
245 
246  /** Closes the active map context. Switches to a valid context afterward or creates a dummy one. */
247  void close_current_context();
248 
249  /** Switches the context to the one under the specified index. */
250  void switch_context(const int index, const bool force = false);
251 
252 private:
253  /**
254  * Save the map under a given filename.
255  * @return true on success
256  */
257  bool save_map_as(const std::string& filename);
258  //TODO
259  bool save_scenario_as(const std::string& filename);
260 
261  /**
262  * Save the map under a given filename. Displays an error message on failure.
263  * @return true on success
264  */
265  bool write_map(bool display_confirmation = false);
266  bool write_scenario(bool display_confirmation = false);
267 
268  /**
269  * Create a new map.
270  */
271  void new_map(int width, int height, const t_translation::terrain_code & fill, bool new_context);
272 
273  /**
274  * Create a new scenario.
275  */
276  void new_scenario(int width, int height, const t_translation::terrain_code & fill, bool new_context);
277 
278  /**
279  * Check if a map is already open.
280  * @return index of the map context containing the given filename,
281  * or map_contexts_.size() if not found.
282  */
283  std::size_t check_open_map(const std::string& fn) const;
284 
285  /**
286  * Check if a map is already open. If yes, switch to it
287  * and return true, return false otherwise.
288  */
289  bool check_switch_open_map(const std::string& fn);
290 
291  /**
292  * Displays the specified map name in the window titlebar
293  */
294  void set_window_title();
295 
296 public:
297  /**
298  * Load a map given the filename
299  */
300  void load_map(const std::string& filename, bool new_context);
301 
302  /**
303  * Revert the map by reloading it from disk
304  */
305  void revert_map();
306 
307  /**
308  * Reload the map after it has significantly changed (when e.g. the dimensions changed).
309  * This is necessary to avoid issues with parts of the map being cached in the display class.
310  */
311  void reload_map();
312 
313 private:
315 
317 
318  /** Default directory for map load/save as dialogs */
319  std::string default_dir_;
320 
321  /** Available random map generators */
322  std::vector<std::unique_ptr<map_generator>> map_generators_;
324 
326 
327  /** Flag to rebuild terrain on every terrain change */
329 
330  /** The currently opened map context object */
331  std::vector<context_ptr> map_contexts_;
332 
333  /** Clipboard map_fragment -- used for copy-paste. */
335 };
336 
337 }
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:49
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:28
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:29
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:940
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:58
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:40
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:59
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:71
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:409
void new_map_dialog()
Display a new map dialog and process user input.