1 // Copyright (C) 2007, 2008, 2009, 2010, 2014, 2015 Ben Asselstine
2 //
3 //  This program is free software; you can redistribute it and/or modify
4 //  it under the terms of the GNU General Public License as published by
5 //  the Free Software Foundation; either version 3 of the License, or
6 //  (at your option) any later version.
7 //
8 //  This program is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 //  GNU Library General Public License for more details.
12 //
13 //  You should have received a copy of the GNU General Public License
14 //  along with this program; if not, write to the Free Software
15 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 //  02110-1301, USA.
17 
18 #pragma once
19 #ifndef TILESTYLE_H
20 #define TILESTYLE_H
21 
22 #include <gtkmm.h>
23 
24 #include "PixMask.h"
25 
26 class XML_Helper;
27 
28 //! Describes the look of a Tile.
29 /**
30   * A TileStyle describes a single look of a Tile.  It is responsible for
31   * keeping the id, the image, and the kind of style.  The TileStyles for a
32   * given tile are defined in the Tileset configuration file.
33   * If the kind of tile is `forest', then the TileStyles are defined in
34   * individual tileset.tile.tilestyleset.tilestyle XML entities.
35   *
36   * There are many different images that can represent a kind of Tile.
37   * Only certain tiles look right when positioned beside each other.  The
38   * correct styles are assigned by the GameMap::calculatePreferredStyle method.
39   * The id is important because it is referenced by the saved-game file
40   * (the lordsawar.map.styles XML entity).
41   *
42   * Every TileStyle belongs to a TileStyleSet, which belongs to a Tile.
43   * Every Maptile refers to a TileStyle.
44   */
45 class TileStyle
46 {
47     public:
48 	//! The xml tag of this object in a tileset configuration file.
49 	static Glib::ustring d_tag;
50 
51         //! Describe terrain tile styles.
52 	  /**
53 	   * All of the Tilestyle kinds can be described by using the
54 	   * following template:
55 	   * @verbatim
56 +-----+
57 |#####|
58 |#+-+#|
59 |#|o|#|
60 |#+-+#|
61 |#####|
62 +-----+
63 @endverbatim
64            * Picture a terrain feature transitioning to grass on the outside
65 	   * of the ring and on the inside of the ring also.
66 	   * In the very center of the template is a single feature that
67 	   * transitions to grass on all sides.
68            */
69 	enum Type {
70 	  /**
71 	   * @verbatim
72 ooooooo
73 ooooooo
74 ooooooo
75 oooxooo
76 ooooooo
77 ooooooo
78 ooooooo
79 @endverbatim
80            */
81 	  LONE = 0,
82 
83 	  /**
84 	   * @verbatim
85 xoooooo
86 ooooooo
87 ooooooo
88 ooooooo
89 ooooooo
90 ooooooo
91 ooooooo
92 @endverbatim
93            */
94 	  OUTERTOPLEFT = 1,
95 	  /**
96 	   * @verbatim
97 oxxxxxo
98 ooooooo
99 ooooooo
100 ooooooo
101 ooooooo
102 ooooooo
103 ooooooo
104 @endverbatim
105            */
106 	  OUTERTOPCENTER = 2,
107 	  /**
108 	   * @verbatim
109 oooooox
110 ooooooo
111 ooooooo
112 ooooooo
113 ooooooo
114 ooooooo
115 ooooooo
116 @endverbatim
117            */
118 	  OUTERTOPRIGHT = 3,
119 	  /**
120 	   * @verbatim
121 ooooooo
122 ooooooo
123 ooooooo
124 ooooooo
125 ooooooo
126 ooooooo
127 xoooooo
128 @endverbatim
129            */
130 	  OUTERBOTTOMLEFT = 4,
131 	  /**
132 	   * @verbatim
133 ooooooo
134 ooooooo
135 ooooooo
136 ooooooo
137 ooooooo
138 ooooooo
139 oxxxxxo
140 @endverbatim
141            */
142 	  OUTERBOTTOMCENTER = 5,
143 	  /**
144 	   * @verbatim
145 ooooooo
146 ooooooo
147 ooooooo
148 ooooooo
149 ooooooo
150 ooooooo
151 oooooox
152 @endverbatim
153            */
154 	  OUTERBOTTOMRIGHT = 6,
155 	  /**
156 	   * @verbatim
157 ooooooo
158 xoooooo
159 xoooooo
160 xoooooo
161 xoooooo
162 xoooooo
163 ooooooo
164 @endverbatim
165            */
166 	  OUTERMIDDLELEFT = 7,
167 	  /**
168 	   * @verbatim
169 ooooooo
170 oxxxxxo
171 oxoooxo
172 oxoooxo
173 oxoooxo
174 oxxxxxo
175 ooooooo
176 @endverbatim
177            */
178 	  INNERMIDDLECENTER = 8,
179 	  /**
180 	   * @verbatim
181 ooooooo
182 oooooox
183 oooooox
184 oooooox
185 oooooox
186 oooooox
187 ooooooo
188 @endverbatim
189            */
190 	  OUTERMIDDLERIGHT = 9,
191 	  /**
192 	   * @verbatim
193 ooooooo
194 ooooooo
195 ooxoooo
196 ooooooo
197 ooooooo
198 ooooooo
199 ooooooo
200 @endverbatim
201            */
202 	  INNERTOPLEFT = 10,
203 	  /**
204 	   * @verbatim
205 ooooooo
206 ooooooo
207 ooooxoo
208 ooooooo
209 ooooooo
210 ooooooo
211 ooooooo
212 @endverbatim
213            */
214 	  INNERTOPRIGHT = 11,
215 	  /**
216 	   * @verbatim
217 ooooooo
218 ooooooo
219 ooooooo
220 ooooooo
221 ooxoooo
222 ooooooo
223 ooooooo
224 @endverbatim
225            */
226 	  INNERBOTTOMLEFT = 12,
227 	  /**
228 	   * @verbatim
229 ooooooo
230 ooooooo
231 ooooooo
232 ooooooo
233 ooooxoo
234 ooooooo
235 ooooooo
236 @endverbatim
237            */
238 	  INNERBOTTOMRIGHT = 13,
239 	  /**
240 	   * Visually it's the merging of positions 1 and 2.
241 	   * @verbatim
242 1oooooo
243 ooooooo
244 ooooooo
245 ooooooo
246 ooooooo
247 ooooooo
248 oooooo2
249 @endverbatim
250            */
251 	  TOPLEFTTOBOTTOMRIGHTDIAGONAL = 14,
252 	  /**
253 	   * Visually it's the merging of positions 1 and 2.
254 	   * @verbatim
255 oooooo1
256 ooooooo
257 ooooooo
258 ooooooo
259 ooooooo
260 ooooooo
261 2oooooo
262 @endverbatim
263            */
264 	  BOTTOMLEFTTOTOPRIGHTDIAGONAL = 15,
265 	  OTHER = 16,
266           UNKNOWN = 17
267 	};
268 
269 	//! Default constructor.
270 	TileStyle();
271 
272         //! Copy constructor.
273         TileStyle(const TileStyle& t);
274 
275         //! Make a new tilestyle from an id, and a type.
276         TileStyle(guint32 id, TileStyle::Type type);
277 
278         //! Loading constructor.
279 	/**
280 	 * Load the tileset.tile.tilestyles.tilestyle XML entities from the
281 	 * tileset configuration files.
282 	 */
283         TileStyle(XML_Helper* helper);
284 
285 	//! Destructor.
286         ~TileStyle();
287 
288 	// Get Methods
289 
290         //! Get the id for this tilestyle.
291 	/*
292 	 * The id is unique among all other tilestyles in the Tileset.
293 	 */
getId()294 	guint32 getId() const {return d_id;}
295 
296         //! Get the style type of this tile style.
getType()297         Type getType() const {return d_type;}
298 
299         //! Get the picture for tile style.
getImage()300 	PixMask* getImage() const {return d_image;}
301 
302 	//! Get the name of the current style.
303 	Glib::ustring getTypeName() const;
304 
305 
306 	// Set Methods
307 
308 	//! Set the style type of this tile style.
309 	/**
310 	 * @note This method is only used in the tileset editor.
311 	 */
setType(Type type)312 	void setType(Type type) {d_type = type;}
313 
314 	//! Set the id for this tilestyle.
setId(guint32 id)315 	void setId(guint32 id) {d_id = id;}
316 
317 	//! Set the image for the tilestyle.
setImage(PixMask * image)318 	void setImage(PixMask* image) {d_image = image;};
319 
320         //! Free up the image associated with this tilestyle.
321         void uninstantiateImage();
322 
323 	// Methods that operate on the class data but do not modify the class.
324 
325 	//! Save a TileStyle to an opened tile configuration file.
326 	/**
327 	 * @param  The opened XML tile configuration file.
328 	 */
329 	bool save(XML_Helper *helper);
330 
331 	// Static Methods
332 
333 	//! Get the name of the TileStyle::Type in string form.
334 	static Glib::ustring getTypeName(Type type);
335 
336 	//! Return the style type enumeration given the type name.
337 	static TileStyle::Type typeNameToType(Glib::ustring name);
338 
339         //! Return how many digits the hex number should be for an id this big.
340         /**
341          * It returns 2, 3, 4 or 5.  e.g. 0x12345
342          */
343         static guint32 calculateHexDigits(guint32 id);
344 
345         //! Return the string representation of a tile style id.
346         /**
347          * This is a bit trickier than expected because the GameMap object
348          * wants to save a series of tile style ids with the same width (in
349          * characters).
350          */
351         static Glib::ustring idToString(guint32 id, guint32 digits = 0);
352 
353     private:
354         // DATA
355 
356 	//! The image of this tilestyle.
357 	PixMask* d_image;
358 
359 	//! The type of the tilestyle.
360         Type d_type;
361 
362 	//! The unique id for this tilestyle.
363 	/**
364 	 * It must be unique among all other TileStyle objects in the
365 	 * Tileset.
366 	 * This id shows up in the saved-game file in the lordsawar.map.styles
367 	 * XML entity as a hexidecimal number.
368 	 */
369 	guint32 d_id;
370 };
371 
372 #endif // TILESTYLE_H
373 
374 // End of file
375