The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
context_manager.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2017 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 
18 #include "preferences/editor.hpp"
20 
21 class map_generator;
22 
23 namespace editor
24 {
25 
27 {
28 public:
29  using context_ptr = std::unique_ptr<map_context>;
30 
33 
34  bool is_active_transitions_hotkey(const std::string& item);
35 
36  size_t modified_maps(std::string& modified);
37 
39  {
42  }
43 
45 
47  {
48  return clipboard_.empty();
49  }
50 
52  {
53  return clipboard_;
54  }
55 
56  /** Fill the selection with the foreground terrain */
57  void fill_selection();
58 
59  /** Index into the map_contexts_ array */
61  {
63  }
64 
65  size_t open_maps(void)
66  {
67  return map_contexts_.size();
68  }
69 
70  /**
71  * Perform an action on the current map_context, then refresh the display.
72  */
73  void perform_refresh(const editor_action& action, bool drag_part = false);
74 
75  /**
76  * Save all maps, open dialog if not named yet, except when using
77  * auto_save_windows which will name unnamed maps "windows_N".
78  * Also record all filenames for future reopening.
79  */
80  void save_all_maps(bool auto_save_windows = false);
81 
82  /** Save the map, open dialog if not named yet. */
83  void save_map();
84 
86  {
87  return gui_;
88  }
89 
90  /**
91  * Refresh everything, i.e. invalidate all hexes and redraw them. Does *not* reload the map.
92  */
93  void refresh_all();
94 
95  /** Display an apply mask dialog and process user input. */
96  void apply_mask_dialog();
97 
98  /** Display an apply mask dialog and process user input. */
99  void create_mask_to_dialog();
100 
101  /** Display an dialog to querry a new id for an [time_area] */
102  void rename_area_dialog();
103 
104  /** Menu expanding for open maps list */
105  void expand_open_maps_menu(std::vector<config>& items, int i);
106 
107  /** Menu expanding for most recent loaded list */
108  void expand_load_mru_menu(std::vector<config>& items, int i);
109 
110  /** Menu expanding for the map's player sides */
111  void expand_sides_menu(std::vector<config>& items, int i);
112 
113  /** Menu expanding for the map's defined areas */
114  void expand_areas_menu(std::vector<config>& items, int i);
115 
116  /** Menu expanding for the map's defined areas */
117  void expand_time_menu(std::vector<config>& items, int i);
118 
119  /** Menu expanding for the map's defined areas */
120  void expand_local_time_menu(std::vector<config>& items, int i);
121 
122  /** Display a load map dialog and process user input. */
123  void load_map_dialog(bool force_same_context = false);
124 
125  /** Open the specified entry from the recent files list. */
126  void load_mru_item(unsigned index, bool force_same_context = false);
127 
128  /** Display a scenario edit dialog and process user input. */
129  void edit_scenario_dialog();
130 
131  /** Display a side edit dialog and process user input. */
132  void edit_side_dialog(int side_index);
133 
134  /** Display a new map dialog and process user input. */
135  void new_map_dialog();
136 
137  /** Display a new map dialog and process user input. */
138  void new_scenario_dialog();
139 
140  /** Display a save map as dialog and process user input. */
141  void save_map_as_dialog();
142 
143  /** Display a save map as dialog and process user input. */
145 
146  /** Display a generate random map dialog and process user input. */
147  void generate_map_dialog();
148 
149  /** Display a load map dialog and process user input. */
150  void resize_map_dialog();
151 
152  size_t size()
153  {
154  return map_contexts_.size();
155  }
156 
157  /** Get the current map context object */
159  {
161  }
162 
163  /** Get the map from the current map context object - const version*/
164  const editor_map& get_map() const
165  {
166  return get_map_context().get_map();
167  }
168 
170  {
171  return get_map_context().get_labels();
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 private:
181  /** init available random map generators */
183 
184  /**
185  * Shows an are-you-sure dialog if the map was modified.
186  * @return true if the user confirmed or the map was not modified, false otherwise
187  */
188  bool confirm_discard();
189 
190  /** Get the current map context object - const version */
192  {
194  }
195 
196  /**
197  * Add a map context. The controller assumes ownership.
198  * @return the index of the added map context in the map_contexts_ array
199  */
200  template<typename... T>
201  int add_map_context(const T&... args);
202 
204 
205  /**
206  * Replace the current map context and refresh accordingly
207  */
208  template<typename... T>
209  void replace_map_context(const T&... args);
210 
212 
213  /**
214  * Creates a default map context object, used to ensure there is always at least one.
215  * Except when we saved windows, in which case reopen them
216  */
217  void create_default_context();
218 
219 public:
220  /**
221  * Refresh the display after an action has been performed.
222  * The map context contains details of what needs to be refreshed.
223  */
224  void refresh_after_action(bool drag_part = false);
225 
226  /** Get the map from the current map context object */
228  {
229  return get_map_context().get_map();
230  }
231 
232  /** Closes the active map context. Switches to a valid context afterward or creates a dummy one. */
233  void close_current_context();
234 
235  /** Switches the context to the one under the specified index. */
236  void switch_context(const int index, const bool force = false);
237 
238 private:
239  /**
240  * Save the map under a given filename.
241  * @return true on success
242  */
243  bool save_map_as(const std::string& filename);
244  //TODO
245  bool save_scenario_as(const std::string& filename);
246 
247  /**
248  * Save the map under a given filename. Displays an error message on failure.
249  * @return true on success
250  */
251  bool write_map(bool display_confirmation = false);
252  bool write_scenario(bool display_confirmation = false);
253 
254  /**
255  * Create a new map.
256  */
257  void new_map(int width, int height, const t_translation::terrain_code & fill, bool new_context);
258 
259  /**
260  * Create a new scenario.
261  */
262  void new_scenario(int width, int height, const t_translation::terrain_code & fill, bool new_context);
263 
264  /**
265  * Check if a map is already open.
266  * @return index of the map context containing the given filename,
267  * or map_contexts_.size() if not found.
268  */
269  size_t check_open_map(const std::string& fn) const;
270 
271  /**
272  * Check if a map is already open. If yes, switch to it
273  * and return true, return false otherwise.
274  */
275  bool check_switch_open_map(const std::string& fn);
276 
277  /**
278  * Displays the specified map name in the window titlebar
279  */
280  void set_window_title();
281 
282 public:
283  /**
284  * Load a map given the filename
285  */
286  void load_map(const std::string& filename, bool new_context);
287 
288  /**
289  * Revert the map by reloading it from disk
290  */
291  void revert_map();
292 
293  /**
294  * Reload the map after it has significantly changed (when e.g. the dimensions changed).
295  * This is necessary to avoid issues with parts of the map being cached in the display class.
296  */
297  void reload_map();
298 
299 private:
301 
303 
304  /** Default directory for map load/save as dialogs */
306 
307  /** Available random map generators */
308  std::vector<std::unique_ptr<map_generator>> map_generators_;
310 
312 
313  /** Flag to rebuild terrain on every terrain change */
315 
316  /** The currently opened map context object */
317  std::vector<context_ptr> map_contexts_;
318 
319  /** Clipboard map_fragment -- used for copy-paste. */
321 };
322 
323 }
bool write_scenario(bool display_confirmation=false)
void apply_mask_dialog()
Display an apply mask dialog and process user input.
A map fragment – a collection of locations and information abut them.
std::vector< char_t > string
bool check_switch_open_map(const std::string &fn)
Check if a map is already open.
std::unique_ptr< map_context > context_ptr
size_t index(const utf8::string &str, const size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
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.
context_manager(editor_display &gui, const config &game_config)
map_labels & get_labels()
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:55
void perform_refresh(const editor_action &action, bool drag_part=false)
Perform an action on the current map_context, then refresh the display.
size_t check_open_map(const std::string &fn) const
Check if a map is already open.
void set_update_transitions_mode(int mode)
const std::vector< std::string > items
editor_map & get_map()
Get the map from the current map context object.
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.
const editor_map & get_map() const
Get the map from the current map context object - const version.
void expand_areas_menu(std::vector< config > &items, int i)
Menu expanding for the map's defined areas.
int add_map_context(const T &...args)
Add a map context.
void close_current_context()
Closes the active map context.
void load_map_dialog(bool force_same_context=false)
Display a load map dialog and process user input.
void replace_map_context(const T &...args)
Replace the current map context and refresh accordingly.
editor_map & get_map()
Map accessor.
Definition: map_context.hpp:92
void set_auto_update_transitions(int value)
Definition: editor.cpp:29
void new_scenario_dialog()
Display a new map dialog and process user input.
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.
This class adds extra editor-specific functionality to a normal gamemap.
Definition: editor_map.hpp:69
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:53
void expand_local_time_menu(std::vector< config > &items, int i)
Menu expanding for the map's defined areas.
bool save_scenario_as(const std::string &filename)
editor_display & gui()
Base class for all editor actions.
Definition: action_base.hpp:40
size_t i
Definition: function.cpp:933
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...
size_t modified_maps(std::string &modified)
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
void expand_sides_menu(std::vector< config > &items, int i)
Menu expanding for the map's player sides.
map_generator * last_map_generator_
void edit_side_dialog(int side_index)
Display a side edit dialog and process user input.
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's defined areas.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
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.
void expand_open_maps_menu(std::vector< config > &items, int i)
Menu expanding for open maps list.
const map_context & get_map_context() const
Get the current map context object - const version.
void new_map_dialog()
Display a new map dialog and process user input.