The Battle for Wesnoth  1.19.2+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  context_manager(editor_display& gui, const game_config_view& game_config, const std::string& addon_id);
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 open map_contexts to memory */
78  void save_contexts();
79 
80  /** Save all maps, show save dialogs for unsaved ones */
81  void save_all_maps();
82 
83  /** Save the map, open dialog if not named yet. */
84  void save_map(bool show_confirmation = true);
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  void set_addon_id(const std::string& id)
165  {
166  current_addon_ = id;
167  for(auto& map : map_contexts_) {
168  map->set_addon_id(id);
169  }
170  }
171 
172  /** Set the default dir (where the filebrowser is pointing at when there is no map file opened) */
173  void set_default_dir(const std::string& str)
174  {
175  default_dir_ = str;
176  }
177 
178  void edit_pbl();
179  void change_addon_id();
180 
181  /** Inherited from @ref filter_context. */
182  virtual const display_context& get_disp_context() const override
183  {
184  return get_map_context();
185  }
186 
187  /** Inherited from @ref filter_context. */
188  virtual const tod_manager& get_tod_man() const override
189  {
190  return *get_map_context().get_time_manager();
191  }
192 
193  /** Inherited from @ref filter_context. */
194  virtual const game_data* get_game_data() const override
195  {
196  return nullptr; // No game_data in the editor.
197  }
198 
199  /** Inherited from @ref filter_context. */
200  virtual game_lua_kernel* get_lua_kernel() const override
201  {
202  return nullptr; // No Lua kernel in the editor.
203  }
204 
205  // TODO: Make this private with an accessor or something
207 private:
208  /** init available random map generators */
210 
211  /**
212  * Shows an are-you-sure dialog if the map was modified.
213  * @return true if the user confirmed or the map was not modified, false otherwise
214  */
215  bool confirm_discard();
216 
217  /** Get the current map context object - const version */
219  {
221  }
222 
223  /**
224  * Add a map context. The controller assumes ownership.
225  * @return the index of the added map context in the map_contexts_ array
226  */
227  template<typename... T>
228  int add_map_context(const T&... args);
229 
230  int add_map_context_of(std::unique_ptr<map_context>&& mc);
231 
232  /**
233  * Replace the current map context and refresh accordingly
234  */
235  template<typename... T>
236  void replace_map_context(const T&... args);
237 
238  void replace_map_context_with(std::unique_ptr<map_context>&& mc);
239 
240  /**
241  * Creates a default map context object, used to ensure there is always at least one.
242  * When we have saved contexts, reopen them instead.
243  */
244  void create_default_context();
245 
246  void create_blank_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 = true);
270  bool write_scenario(bool display_confirmation = true);
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<std::unique_ptr<map_context>> 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_
std::vector< std::unique_ptr< map_context > > map_contexts_
The currently opened map context object.
bool write_scenario(bool display_confirmation=true)
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.
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.
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.
void set_window_title()
Displays the specified map name in the window titlebar.
std::size_t open_maps(void)
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_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
void save_all_maps()
Save all maps, show save dialogs for unsaved ones.
int add_map_context_of(std::unique_ptr< map_context > &&mc)
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 reload_map()
Reload the map after it has significantly changed (when e.g.
void save_contexts()
Save all open map_contexts to memory.
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 replace_map_context_with(std::unique_ptr< map_context > &&mc)
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 save_map(bool show_confirmation=true)
Save the map, open dialog if not named yet.
void expand_local_time_menu(std::vector< config > &items, int i)
Menu expanding for the map's defined areas.
bool write_map(bool display_confirmation=true)
Save the map under a given filename.
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.
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:411
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