The Battle for Wesnoth  1.19.18+dev
context_manager.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2025
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 namespace editor
24 {
25 
27 {
28 public:
29  context_manager(editor_display& gui, const std::string& addon_id);
31 
32  bool is_active_transitions_hotkey(const std::string& item);
33 
34  std::size_t modified_maps(std::string& modified);
35 
37  {
39  prefs::get().set_editor_auto_update_transitions(mode);
40  }
41 
43 
45  {
46  return clipboard_.empty();
47  }
48 
50  {
51  return clipboard_;
52  }
53 
54  /** Fill the selection with the foreground terrain */
55  void fill_selection();
56 
57  /** Index into the map_contexts_ array */
59  {
61  }
62 
63  std::size_t open_maps(void)
64  {
65  return map_contexts_.size();
66  }
67 
68  /**
69  * Perform an action on the current map_context, then refresh the display.
70  */
71  void perform_refresh(const editor_action& action, bool drag_part = false);
72 
73 
74  /** Save all open map_contexts to memory */
75  void save_contexts();
76 
77  /** Save all maps, show save dialogs for unsaved ones */
78  void save_all_maps();
79 
80  /** Save the map, open dialog if not named yet. */
81  void save_map(bool show_confirmation = true);
82 
84  {
85  return gui_;
86  }
87 
88  /**
89  * Refresh everything, i.e. invalidate all hexes and redraw them. Does *not* reload the map.
90  */
91  void refresh_all();
92 
93  /** Display an apply mask dialog and process user input. */
94  void apply_mask_dialog();
95 
96  /** Display an apply mask dialog and process user input. */
97  void create_mask_to_dialog();
98 
99  /** Display an dialog to querry a new id for an [time_area] */
100  void rename_area_dialog();
101 
102  /** Menu expanding for open maps list */
103  void expand_open_maps_menu(std::vector<config>& items) const;
104 
105  /** Menu expanding for most recent loaded list */
106  void expand_load_mru_menu(std::vector<config>& items) const;
107 
108  /** Menu expanding for the map's player sides */
109  void expand_sides_menu(std::vector<config>& items) const;
110 
111  /** Menu expanding for the map's defined areas */
112  void expand_areas_menu(std::vector<config>& items) const;
113 
114  /** Menu expanding for the map's defined areas */
115  void expand_time_menu(std::vector<config>& items) const;
116 
117  /** Menu expanding for the map's defined areas */
118  void expand_local_time_menu(std::vector<config>& items) const;
119 
120  /** Display a load map dialog and process user input. */
121  void load_map_dialog(bool force_same_context = false);
122 
123  /** Open the specified entry from the recent files list. */
124  void load_mru_item(unsigned index, bool force_same_context = false);
125 
126  /** Display a scenario edit dialog and process user input. */
127  void edit_scenario_dialog();
128 
129  /** Display a side edit dialog and process user input. */
130  void edit_side_dialog(const team& t);
131 
132  /** Display a new map dialog and process user input. */
133  void new_map_dialog();
134 
135  /** Display a new map dialog and process user input. */
136  void new_scenario_dialog();
137 
138  /** Display a save map as dialog and process user input. */
139  void save_map_as_dialog();
140 
141  /** Display a save map as dialog and process user input. */
143 
144  /** Display a generate random map dialog and process user input. */
145  void generate_map_dialog(const std::vector<std::unique_ptr<map_generator>>& map_generators);
146 
147  /** Display a load map dialog and process user input. */
148  void resize_map_dialog();
149 
150  std::size_t size()
151  {
152  return map_contexts_.size();
153  }
154 
155  /** Get the current map context object */
157  {
159  }
160 
161  void set_addon_id(const std::string& id)
162  {
163  current_addon_ = id;
164  for(auto& map : map_contexts_) {
165  map->set_addon_id(id);
166  }
167  }
168 
169  /** Set the default dir (where the filebrowser is pointing at when there is no map file opened) */
170  void set_default_dir(const std::string& str)
171  {
172  default_dir_ = str;
173  }
174 
175  void edit_pbl();
176  void change_addon_id();
177 
178  /** Inherited from @ref filter_context. */
179  virtual const display_context& get_disp_context() const override
180  {
181  return get_map_context();
182  }
183 
184  /** Inherited from @ref filter_context. */
185  virtual const tod_manager& get_tod_man() const override
186  {
187  return *get_map_context().get_time_manager();
188  }
189 
190  /** Inherited from @ref filter_context. */
191  virtual const game_data* get_game_data() const override
192  {
193  return nullptr; // No game_data in the editor.
194  }
195 
196  /** Inherited from @ref filter_context. */
197  virtual game_lua_kernel* get_lua_kernel() const override
198  {
199  return nullptr; // No Lua kernel in the editor.
200  }
201 
202  // TODO: Make this private with an accessor or something
204 private:
205 
206  /**
207  * Shows an are-you-sure dialog if the map was modified.
208  * @return true if the user confirmed or the map was not modified, false otherwise
209  */
210  bool confirm_discard();
211 
212  /** Get the current map context object - const version */
214  {
216  }
217 
218  /**
219  * Add a map context. The controller assumes ownership.
220  * @return the index of the added map context in the map_contexts_ array
221  */
222  template<typename... T>
223  int add_map_context(T&&... args);
224 
225  int add_map_context_of(std::unique_ptr<map_context>&& mc);
226 
227  /**
228  * Replace the current map context and refresh accordingly
229  */
230  template<typename... T>
231  void replace_map_context(T&&... args);
232 
233  void replace_map_context_with(std::unique_ptr<map_context>&& mc);
234 
235  /**
236  * Creates a default map context object, used to ensure there is always at least one.
237  * When we have saved contexts, reopen them instead.
238  */
239  void create_default_context();
240 
241  void create_blank_context();
242 
243  /** Performs the necessary housekeeping necessary when switching contexts. */
245 
246 public:
247  /**
248  * Refresh the display after an action has been performed.
249  * The map context contains details of what needs to be refreshed.
250  */
251  void refresh_after_action(bool drag_part = false);
252 
253  /** Closes the active map context. Switches to a valid context afterward or creates a dummy one. */
254  void close_current_context();
255 
256  /** Switches the context to the one under the specified index. */
257  void switch_context(const int index, const bool force = false);
258 
259  /**
260  * Convert existing map to scenario.
261  */
262  void map_to_scenario();
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  void init_context(int width, int height, const t_translation::terrain_code& fill, bool new_context, bool is_pure_map = true);
273 
274  /**
275  * Create a new map.
276  */
277  void new_map(int width, int height, const t_translation::terrain_code & fill, bool new_context);
278 
279  /**
280  * Create a new scenario.
281  */
282  void new_scenario(int width, int height, const t_translation::terrain_code & fill, bool new_context);
283 
284  /**
285  * Check if a map is already open.
286  * @return index of the map context containing the given filename,
287  * or map_contexts_.size() if not found.
288  */
289  std::size_t check_open_map(const std::string& fn) const;
290 
291  /**
292  * Check if a map is already open. If yes, switch to it
293  * and return true, return false otherwise.
294  */
295  bool check_switch_open_map(const std::string& fn);
296 
297  /**
298  * Displays the specified map name in the window titlebar
299  */
300  void set_window_title();
301 
302 public:
303  /**
304  * Load a map given the filename
305  */
306  void load_map(const std::string& filename, bool new_context);
307 
308  /**
309  * Revert the map by reloading it from disk
310  */
311  void revert_map();
312 
313  /**
314  * Reload the map after it has significantly changed (when e.g. the dimensions changed).
315  * This is necessary to avoid issues with parts of the map being cached in the display class.
316  */
317  void reload_map();
318 
319 private:
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 
329 
330  /** Flag to rebuild terrain on every terrain change */
332 
333  /** The currently opened map context object */
334  std::vector<std::unique_ptr<map_context>> map_contexts_;
335 
336  /** Clipboard map_fragment -- used for copy-paste. */
338 };
339 
340 }
double t
Definition: astarsearch.cpp:63
Abstract class for exposing game data that doesn't depend on the GUI, however which for historical re...
std::vector< std::unique_ptr< map_context > > map_contexts_
The currently opened map context object.
bool write_scenario(bool display_confirmation=true)
void replace_map_context(T &&... args)
Replace the current map context and refresh accordingly.
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.
int add_map_context(T &&... args)
Add a map context.
void expand_local_time_menu(std::vector< config > &items) const
Menu expanding for the map's defined areas.
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.
bool confirm_discard()
Shows an are-you-sure dialog if the map was modified.
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.
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.
void expand_sides_menu(std::vector< config > &items) const
Menu expanding for the map's player sides.
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.
context_manager(editor_display &gui, const std::string &addon_id)
int current_context_index()
Index into the map_contexts_ array.
void refresh_all()
Refresh everything, i.e.
void generate_map_dialog(const std::vector< std::unique_ptr< map_generator >> &map_generators)
Display a generate random map dialog and process user input.
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 expand_time_menu(std::vector< config > &items) const
Menu expanding for the map's defined areas.
void map_to_scenario()
Convert existing map to scenario.
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 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.
const map_context & get_map_context() const
Get the current map context object - const version.
void init_context(int width, int height, const t_translation::terrain_code &fill, bool new_context, bool is_pure_map=true)
std::string default_dir_
Default directory for map load/save as dialogs.
void expand_load_mru_menu(std::vector< config > &items) const
Menu expanding for most recent loaded list.
void save_map(bool show_confirmation=true)
Save the map, open dialog if not named yet.
void edit_side_dialog(const team &t)
Display a side edit dialog and process user input.
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.
void expand_areas_menu(std::vector< config > &items) const
Menu expanding for the map's defined areas.
void expand_open_maps_menu(std::vector< config > &items) const
Menu expanding for open maps list.
map_fragment & get_clipboard()
virtual const tod_manager & get_tod_man() const override
Inherited from filter_context.
class location_palette * locs_
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:64
const tod_manager * get_time_manager() const
A map fragment – a collection of locations and information abut them.
static prefs & get()
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:74
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:199
void fill(const ::rect &rect, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Fill an area with the given colour.
Definition: draw.cpp:52
Manage the empty-palette in the editor.
Definition: action.cpp:31
General purpose widgets.
std::size_t index(std::string_view str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:70
std::string filename
Filename.
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