The Battle for Wesnoth  1.15.12+dev
editor_map.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Tomasz Sniatowski <kailoran@gmail.com>
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 
17 #include "editor/editor_common.hpp"
18 
19 #include "map/map.hpp"
20 
21 #include <deque>
22 
23 namespace editor {
24 
26 {
28  : editor_exception("Map operation error. Check debug log for details.")
29  {
30  }
31 };
32 
34 {
36  : editor_exception("Map integrity error. Check debug log for details.")
37  {
38  }
39 };
40 
42 {
43  editor_map_load_exception(const std::string& fn, const std::string& msg)
44  : editor_exception(msg), filename(fn)
45  {
46  }
48  std::string filename;
49 };
50 
52 {
53  editor_map_save_exception(const std::string& msg)
54  : editor_exception(msg)
55  {
56  }
58 };
59 
60 
61 /**
62  * Exception wrapping utility
63  */
64 editor_map_load_exception wrap_exc(const char* type, const std::string& e_msg, const std::string& filename);
65 
66 /**
67  * This class adds extra editor-specific functionality to a normal gamemap.
68  */
69 class editor_map : public gamemap
70 {
71 public:
72  /**
73  * Empty map constructor
74  */
75  editor_map();
76 
77  /**
78  * Create an editor map from a map data string
79  */
80  editor_map(const std::string& data);
81 
82  /**
83  * Wrapper around editor_map(cfg, data) that catches possible exceptions
84  * and wraps them in a editor_map_load_exception
85  */
86  static editor_map from_string(const std::string& data);
87 
88  /**
89  * Create an editor map with the given dimensions and filler terrain
90  */
91  editor_map(std::size_t width, std::size_t height, const t_translation::terrain_code & filler);
92 
93  /**
94  * Create an editor_map by upgrading an existing gamemap. The map data is
95  * copied. Marked "explicit" to avoid potentially harmful automatic conversions.
96  */
97  explicit editor_map(const gamemap& map);
98 
99  /**
100  * editor_map destructor
101  */
102  ~editor_map();
103 
104  /**
105  * Debugging aid. Check if the widths and heights correspond to the actual map data sizes.
106  */
107  void sanity_check();
108 
109  /**
110  * Get a contiguous set of tiles having the same terrain as the starting location.
111  * Useful for flood fill or magic wand selection
112  * @return a contiguous set of locations that will always contain at least the starting element
113  */
114  std::set<map_location> get_contiguous_terrain_tiles(const map_location& start) const;
115 
116  /**
117  * Set labels for staring positions in the given display object.
118  * @return the locations where the labels were added
119  */
120  std::set<map_location> set_starting_position_labels(display& disp);
121 
122  /**
123  * @return true when the location is part of the selection, false otherwise
124  */
125  bool in_selection(const map_location& loc) const;
126 
127  /**
128  * Add a location to the selection. The location should be valid (i.e. on the map)
129  * @return true if the selected hexes set was modified
130  */
131  bool add_to_selection(const map_location& loc);
132 
133  /**
134  * Remove a location to the selection. The location does not actually have to be selected
135  * @return true if the selected hexes set was modified
136  */
137  bool remove_from_selection(const map_location& loc);
138 
139  /**
140  * Select the given area.
141  * @param area to select.
142  */
143  bool set_selection(const std::set<map_location>& area);
144 
145  /**
146  * Return the selection set.
147  */
148  const std::set<map_location>& selection() const { return selection_; }
149 
150  /**
151  * Clear the selection
152  */
153  void clear_selection();
154 
155  /**
156  * Invert the selection, i.e. select all the map hexes that were not selected.
157  */
158  void invert_selection();
159 
160  /**
161  * Select all map hexes
162  */
163  void select_all();
164 
165  /**
166  * @return true if the entire map is selected, false otherwise
167  */
168  bool everything_selected() const;
169 
170  /**
171  * Resize the map. If the filler is NONE, the border terrain will be copied
172  * when expanding, otherwise the filler terrain will be inserted there
173  */
174  void resize(int width, int height, int x_offset, int y_offset,
176 
177  /**
178  * A sort-of diff operation returning a mask that, when applied to the current editor_map,
179  * will transform it into the target map.
180  */
181  gamemap mask_to(const gamemap& target) const;
182 
183  /**
184  * A precondition to several map operations
185  * @return true if this map has the same dimensions as the other map
186  */
187  bool same_size_as(const gamemap& other) const;
188 
189 protected:
190  //helper functions for resizing
191  void expand_right(int count, const t_translation::terrain_code & filler);
192  void expand_left(int count, const t_translation::terrain_code & filler);
193  void expand_top(int count, const t_translation::terrain_code & filler);
194  void expand_bottom(int count, const t_translation::terrain_code & filler);
195  void shrink_right(int count);
196  void shrink_left(int count);
197  void shrink_top(int count);
198  void shrink_bottom(int count);
199 
200  /**
201  * The selected hexes
202  */
203  std::set<map_location> selection_;
204 };
205 
206 
207 } //end namespace editor
editor_map_save_exception(const std::string &msg)
Definition: editor_map.hpp:53
editor_map_load_exception(const std::string &fn, const std::string &msg)
Definition: editor_map.hpp:43
const terrain_code NONE_TERRAIN
Definition: translation.hpp:58
std::set< map_location > selection_
The selected hexes.
Definition: editor_map.hpp:203
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
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
editor_map_load_exception wrap_exc(const char *type, const std::string &e_msg, const std::string &filename)
Exception wrapping utility.
Definition: editor_map.cpp:28
const std::set< map_location > & selection() const
Return the selection set.
Definition: editor_map.hpp:148
Encapsulates the map of the game.
Definition: map.hpp:170
Manage the empty-palette in the editor.
Definition: action.cpp:29
Encapsulates the map of the game.
Definition: location.hpp:37
This class adds extra editor-specific functionality to a normal gamemap.
Definition: editor_map.hpp:69
Main (common) editor header.
EXIT_STATUS start(const std::string &filename, bool take_screenshot, const std::string &screenshot_filename)
Main interface for launching the editor from the title screen.
Definition: editor_main.cpp:28