The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
editor_map.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2017 by Tomasz Sniatowski <kailoran@gmail.com>
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 
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 {
44  : editor_exception(msg), filename(fn)
45  {
46  }
49 };
50 
52 {
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  /**
74  * Empty map constructor
75  */
76  explicit editor_map(const config& terrain_cfg);
77 
78  /**
79  * Create an editor map from a map data string
80  */
81  editor_map(const config& terrain_cfg, const std::string& data);
82 
83  /**
84  * Wrapper around editor_map(cfg, data) that catches possible exceptions
85  * and wraps them in a editor_map_load_exception
86  */
87  static editor_map from_string(const config& terrain_cfg, const std::string& data);
88 
89  /**
90  * Create an editor map with the given dimensions and filler terrain
91  */
92  editor_map(const config& terrain_cfg, size_t width, size_t height, const t_translation::terrain_code & filler);
93 
94  /**
95  * Create an editor_map by upgrading an existing gamemap. The map data is
96  * copied. Marked "explicit" to avoid potentially harmful automatic conversions.
97  */
98  explicit editor_map(const gamemap& map);
99 
100  /**
101  * editor_map destructor
102  */
103  ~editor_map();
104 
105  /**
106  * Debugging aid. Check if the widths and heights correspond to the actual map data sizes.
107  */
108  void sanity_check();
109 
110  /**
111  * Get a contiguous set of tiles having the same terrain as the starting location.
112  * Useful for flood fill or magic wand selection
113  * @return a contiguous set of locations that will always contain at least the starting element
114  */
115  std::set<map_location> get_contiguous_terrain_tiles(const map_location& start) const;
116 
117  /**
118  * Set labels for staring positions in the given display object.
119  * @return the locations where the labels were added
120  */
121  std::set<map_location> set_starting_position_labels(display& disp);
122 
123  /**
124  * @return true when the location is part of the selection, false otherwise
125  */
126  bool in_selection(const map_location& loc) const;
127 
128  /**
129  * Add a location to the selection. The location should be valid (i.e. on the map)
130  * @return true if the selected hexes set was modified
131  */
132  bool add_to_selection(const map_location& loc);
133 
134  /**
135  * Remove a location to the selection. The location does not actually have to be selected
136  * @return true if the selected hexes set was modified
137  */
138  bool remove_from_selection(const map_location& loc);
139 
140  /**
141  * Select the given area.
142  * @param area to select.
143  */
144  bool set_selection(const std::set<map_location>& area);
145 
146  /**
147  * Return the selection set.
148  */
149  const std::set<map_location>& selection() const { return selection_; }
150 
151  /**
152  * Clear the selection
153  */
154  void clear_selection();
155 
156  /**
157  * Invert the selection, i.e. select all the map hexes that were not selected.
158  */
159  void invert_selection();
160 
161  /**
162  * Select all map hexes
163  */
164  void select_all();
165 
166  /**
167  * @return true if the entire map is selected, false otherwise
168  */
169  bool everything_selected() const;
170 
171  /**
172  * Resize the map. If the filler is NONE, the border terrain will be copied
173  * when expanding, otherwise the filler terrain will be inserted there
174  */
175  void resize(int width, int height, int x_offset, int y_offset,
177 
178  /**
179  * A sort-of diff operation returning a mask that, when applied to the current editor_map,
180  * will transform it into the target map.
181  */
182  gamemap mask_to(const gamemap& target) const;
183 
184  /**
185  * A precondition to several map operations
186  * @return true if this map has the same dimensions as the other map
187  */
188  bool same_size_as(const gamemap& other) const;
189 
190 protected:
191  //helper functions for resizing
192  void expand_right(int count, const t_translation::terrain_code & filler);
193  void expand_left(int count, const t_translation::terrain_code & filler);
194  void expand_top(int count, const t_translation::terrain_code & filler);
195  void expand_bottom(int count, const t_translation::terrain_code & filler);
196  void shrink_right(int count);
197  void shrink_left(int count);
198  void shrink_top(int count);
199  void shrink_bottom(int count);
200 
201  /**
202  * The selected hexes
203  */
204  std::set<map_location> selection_;
205 };
206 
207 
208 } //end namespace editor
void expand_bottom(int count, const t_translation::terrain_code &filler)
Definition: editor_map.cpp:335
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
std::vector< char_t > string
const terrain_code NONE_TERRAIN
Definition: translation.hpp:64
std::set< map_location > selection_
The selected hexes.
Definition: editor_map.hpp:204
std::set< map_location > set_starting_position_labels(display &disp)
Set labels for staring positions in the given display object.
Definition: editor_map.cpp:139
void shrink_top(int count)
Definition: editor_map.cpp:382
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 sanity_check()
Debugging aid.
Definition: editor_map.cpp:87
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:110
void resize(int width, int height, int x_offset, int y_offset, const t_translation::terrain_code &filler=t_translation::NONE_TERRAIN)
Resize the map.
Definition: editor_map.cpp:216
std::set< map_location > get_contiguous_terrain_tiles(const map_location &start) const
Get a contiguous set of tiles having the same terrain as the starting location.
Definition: editor_map.cpp:116
~editor_map()
editor_map destructor
Definition: editor_map.cpp:83
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:31
bool everything_selected() const
Definition: editor_map.cpp:210
gamemap mask_to(const gamemap &target) const
A sort-of diff operation returning a mask that, when applied to the current editor_map, will transform it into the target map.
Definition: editor_map.cpp:261
void expand_left(int count, const t_translation::terrain_code &filler)
Definition: editor_map.cpp:301
bool in_selection(const map_location &loc) const
Definition: editor_map.cpp:161
void invert_selection()
Invert the selection, i.e.
Definition: editor_map.cpp:191
bool same_size_as(const gamemap &other) const
A precondition to several map operations.
Definition: editor_map.cpp:278
Encapsulates the map of the game.
Definition: map.hpp:34
Manage the empty-palette in the editor.
Definition: action.cpp:29
void shrink_left(int count)
Definition: editor_map.cpp:367
bool add_to_selection(const map_location &loc)
Add a location to the selection.
Definition: editor_map.cpp:166
Encapsulates the map of the game.
Definition: location.hpp:40
static editor_map from_string(const config &terrain_cfg, const std::string &data)
Wrapper around editor_map(cfg, data) that catches possible exceptions and wraps them in a editor_map_...
Definition: editor_map.cpp:56
This class adds extra editor-specific functionality to a normal gamemap.
Definition: editor_map.hpp:69
Main (common) editor header.
EXIT_STATUS start(const config &game_conf, 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
editor_map(const config &terrain_cfg)
Empty map constructor.
Definition: editor_map.cpp:43
void expand_right(int count, const t_translation::terrain_code &filler)
Definition: editor_map.cpp:284
void clear_selection()
Clear the selection.
Definition: editor_map.cpp:186
const std::set< map_location > & selection() const
Return the selection set.
Definition: editor_map.hpp:149
void select_all()
Select all map hexes.
Definition: editor_map.cpp:204
void expand_top(int count, const t_translation::terrain_code &filler)
Definition: editor_map.cpp:318
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
void shrink_right(int count)
Definition: editor_map.cpp:352
bool set_selection(const std::set< map_location > &area)
Select the given area.
Definition: editor_map.cpp:171
bool remove_from_selection(const map_location &loc)
Remove a location to the selection.
Definition: editor_map.cpp:181
void shrink_bottom(int count)
Definition: editor_map.cpp:397