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