1 /*************************************************************************/
2 /* tile_set.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /*************************************************************************/
30 #include "tile_set.h"
31
_set(const StringName & p_name,const Variant & p_value)32 bool TileSet::_set(const StringName &p_name, const Variant &p_value) {
33
34 String n = p_name;
35 int slash = n.find("/");
36 if (slash == -1)
37 return false;
38 int id = String::to_int(n.c_str(), slash);
39
40 if (!tile_map.has(id))
41 create_tile(id);
42 String what = n.substr(slash + 1, n.length());
43
44 if (what == "name")
45 tile_set_name(id, p_value);
46 else if (what == "texture")
47 tile_set_texture(id, p_value);
48 else if (what == "tex_offset")
49 tile_set_texture_offset(id, p_value);
50 else if (what == "material")
51 tile_set_material(id, p_value);
52 else if (what == "modulate")
53 tile_set_modulate(id, p_value);
54 else if (what == "shape_offset")
55 tile_set_shape_offset(id, p_value);
56 else if (what == "region")
57 tile_set_region(id, p_value);
58 else if (what == "shape")
59 tile_set_shape(id, p_value);
60 else if (what == "shapes")
61 _tile_set_shapes(id, p_value);
62 else if (what == "one_way_collision_direction")
63 tile_set_one_way_collision_direction(id, p_value);
64 else if (what == "one_way_collision_max_depth")
65 tile_set_one_way_collision_max_depth(id, p_value);
66 else if (what == "occluder")
67 tile_set_light_occluder(id, p_value);
68 else if (what == "occluder_offset")
69 tile_set_occluder_offset(id, p_value);
70 else if (what == "navigation")
71 tile_set_navigation_polygon(id, p_value);
72 else if (what == "navigation_offset")
73 tile_set_navigation_polygon_offset(id, p_value);
74 else
75 return false;
76
77 return true;
78 }
79
_get(const StringName & p_name,Variant & r_ret) const80 bool TileSet::_get(const StringName &p_name, Variant &r_ret) const {
81
82 String n = p_name;
83 int slash = n.find("/");
84 if (slash == -1)
85 return false;
86 int id = String::to_int(n.c_str(), slash);
87
88 ERR_FAIL_COND_V(!tile_map.has(id), false);
89
90 String what = n.substr(slash + 1, n.length());
91
92 if (what == "name")
93 r_ret = tile_get_name(id);
94 else if (what == "texture")
95 r_ret = tile_get_texture(id);
96 else if (what == "tex_offset")
97 r_ret = tile_get_texture_offset(id);
98 else if (what == "material")
99 r_ret = tile_get_material(id);
100 else if (what == "modulate")
101 r_ret = tile_get_modulate(id);
102 else if (what == "shape_offset")
103 r_ret = tile_get_shape_offset(id);
104 else if (what == "region")
105 r_ret = tile_get_region(id);
106 else if (what == "shape")
107 r_ret = tile_get_shape(id);
108 else if (what == "shapes")
109 r_ret = _tile_get_shapes(id);
110 else if (what == "one_way_collision_direction")
111 r_ret = tile_get_one_way_collision_direction(id);
112 else if (what == "one_way_collision_max_depth")
113 r_ret = tile_get_one_way_collision_max_depth(id);
114 else if (what == "occluder")
115 r_ret = tile_get_light_occluder(id);
116 else if (what == "occluder_offset")
117 r_ret = tile_get_occluder_offset(id);
118 else if (what == "navigation")
119 r_ret = tile_get_navigation_polygon(id);
120 else if (what == "navigation_offset")
121 r_ret = tile_get_navigation_polygon_offset(id);
122 else
123 return false;
124
125 return true;
126 }
127
_get_property_list(List<PropertyInfo> * p_list) const128 void TileSet::_get_property_list(List<PropertyInfo> *p_list) const {
129
130 for (Map<int, Data>::Element *E = tile_map.front(); E; E = E->next()) {
131
132 int id = E->key();
133 String pre = itos(id) + "/";
134 p_list->push_back(PropertyInfo(Variant::STRING, pre + "name"));
135 p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"));
136 p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "tex_offset"));
137 p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "material", PROPERTY_HINT_RESOURCE_TYPE, "CanvasItemMaterial"));
138 p_list->push_back(PropertyInfo(Variant::COLOR, pre + "modulate"));
139 p_list->push_back(PropertyInfo(Variant::RECT2, pre + "region"));
140 p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "occluder_offset"));
141 p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "occluder", PROPERTY_HINT_RESOURCE_TYPE, "OccluderPolygon2D"));
142 p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "navigation_offset"));
143 p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "navigation", PROPERTY_HINT_RESOURCE_TYPE, "NavigationPolygon"));
144 p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "shape_offset"));
145 p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D", PROPERTY_USAGE_EDITOR));
146 p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "shapes", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
147 p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "one_way_collision_direction"));
148 p_list->push_back(PropertyInfo(Variant::REAL, pre + "one_way_collision_max_depth"));
149 }
150 }
151
create_tile(int p_id)152 void TileSet::create_tile(int p_id) {
153
154 ERR_FAIL_COND(tile_map.has(p_id));
155 tile_map[p_id] = Data();
156 _change_notify("");
157 emit_changed();
158 }
159
tile_set_texture(int p_id,const Ref<Texture> & p_texture)160 void TileSet::tile_set_texture(int p_id, const Ref<Texture> &p_texture) {
161
162 ERR_FAIL_COND(!tile_map.has(p_id));
163 tile_map[p_id].texture = p_texture;
164 emit_changed();
165 }
166
tile_get_texture(int p_id) const167 Ref<Texture> TileSet::tile_get_texture(int p_id) const {
168
169 ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<Texture>());
170 return tile_map[p_id].texture;
171 }
172
tile_set_material(int p_id,const Ref<CanvasItemMaterial> & p_material)173 void TileSet::tile_set_material(int p_id, const Ref<CanvasItemMaterial> &p_material) {
174
175 ERR_FAIL_COND(!tile_map.has(p_id));
176 tile_map[p_id].material = p_material;
177 emit_changed();
178 }
179
tile_get_material(int p_id) const180 Ref<CanvasItemMaterial> TileSet::tile_get_material(int p_id) const {
181
182 ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<CanvasItemMaterial>());
183 return tile_map[p_id].material;
184 }
185
tile_set_modulate(int p_id,const Color & p_modulate)186 void TileSet::tile_set_modulate(int p_id, const Color &p_modulate) {
187
188 ERR_FAIL_COND(!tile_map.has(p_id));
189 tile_map[p_id].modulate = p_modulate;
190 emit_changed();
191 }
192
tile_get_modulate(int p_id) const193 Color TileSet::tile_get_modulate(int p_id) const {
194
195 ERR_FAIL_COND_V(!tile_map.has(p_id), Color(1, 1, 1));
196 return tile_map[p_id].modulate;
197 }
198
tile_set_texture_offset(int p_id,const Vector2 & p_offset)199 void TileSet::tile_set_texture_offset(int p_id, const Vector2 &p_offset) {
200
201 ERR_FAIL_COND(!tile_map.has(p_id));
202 tile_map[p_id].offset = p_offset;
203 emit_changed();
204 }
205
tile_get_texture_offset(int p_id) const206 Vector2 TileSet::tile_get_texture_offset(int p_id) const {
207
208 ERR_FAIL_COND_V(!tile_map.has(p_id), Vector2());
209 return tile_map[p_id].offset;
210 }
211
tile_set_shape_offset(int p_id,const Vector2 & p_offset)212 void TileSet::tile_set_shape_offset(int p_id, const Vector2 &p_offset) {
213
214 ERR_FAIL_COND(!tile_map.has(p_id));
215 tile_map[p_id].shape_offset = p_offset;
216 emit_changed();
217 }
218
tile_get_shape_offset(int p_id) const219 Vector2 TileSet::tile_get_shape_offset(int p_id) const {
220
221 ERR_FAIL_COND_V(!tile_map.has(p_id), Vector2());
222 return tile_map[p_id].shape_offset;
223 }
224
tile_set_region(int p_id,const Rect2 & p_region)225 void TileSet::tile_set_region(int p_id, const Rect2 &p_region) {
226
227 ERR_FAIL_COND(!tile_map.has(p_id));
228 tile_map[p_id].region = p_region;
229 emit_changed();
230 }
231
tile_get_region(int p_id) const232 Rect2 TileSet::tile_get_region(int p_id) const {
233
234 ERR_FAIL_COND_V(!tile_map.has(p_id), Rect2());
235 return tile_map[p_id].region;
236 }
237
tile_set_name(int p_id,const String & p_name)238 void TileSet::tile_set_name(int p_id, const String &p_name) {
239
240 ERR_FAIL_COND(!tile_map.has(p_id));
241 tile_map[p_id].name = p_name;
242 emit_changed();
243 }
244
tile_get_name(int p_id) const245 String TileSet::tile_get_name(int p_id) const {
246
247 ERR_FAIL_COND_V(!tile_map.has(p_id), String());
248 return tile_map[p_id].name;
249 }
250
tile_set_shape(int p_id,const Ref<Shape2D> & p_shape)251 void TileSet::tile_set_shape(int p_id, const Ref<Shape2D> &p_shape) {
252
253 ERR_FAIL_COND(!tile_map.has(p_id));
254 tile_map[p_id].shapes.resize(1);
255 tile_map[p_id].shapes[0] = p_shape;
256 emit_changed();
257 }
258
tile_get_shape(int p_id) const259 Ref<Shape2D> TileSet::tile_get_shape(int p_id) const {
260
261 ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<Shape2D>());
262 if (tile_map[p_id].shapes.size() > 0)
263 return tile_map[p_id].shapes[0];
264
265 return Ref<Shape2D>();
266 }
267
tile_set_light_occluder(int p_id,const Ref<OccluderPolygon2D> & p_light_occluder)268 void TileSet::tile_set_light_occluder(int p_id, const Ref<OccluderPolygon2D> &p_light_occluder) {
269
270 ERR_FAIL_COND(!tile_map.has(p_id));
271 tile_map[p_id].occluder = p_light_occluder;
272 }
273
tile_get_light_occluder(int p_id) const274 Ref<OccluderPolygon2D> TileSet::tile_get_light_occluder(int p_id) const {
275
276 ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<OccluderPolygon2D>());
277 return tile_map[p_id].occluder;
278 }
279
tile_set_navigation_polygon_offset(int p_id,const Vector2 & p_offset)280 void TileSet::tile_set_navigation_polygon_offset(int p_id, const Vector2 &p_offset) {
281
282 ERR_FAIL_COND(!tile_map.has(p_id));
283 tile_map[p_id].navigation_polygon_offset = p_offset;
284 }
285
tile_get_navigation_polygon_offset(int p_id) const286 Vector2 TileSet::tile_get_navigation_polygon_offset(int p_id) const {
287 ERR_FAIL_COND_V(!tile_map.has(p_id), Vector2());
288 return tile_map[p_id].navigation_polygon_offset;
289 }
290
tile_set_navigation_polygon(int p_id,const Ref<NavigationPolygon> & p_navigation_polygon)291 void TileSet::tile_set_navigation_polygon(int p_id, const Ref<NavigationPolygon> &p_navigation_polygon) {
292
293 ERR_FAIL_COND(!tile_map.has(p_id));
294 tile_map[p_id].navigation_polygon = p_navigation_polygon;
295 }
296
tile_get_navigation_polygon(int p_id) const297 Ref<NavigationPolygon> TileSet::tile_get_navigation_polygon(int p_id) const {
298
299 ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<NavigationPolygon>());
300 return tile_map[p_id].navigation_polygon;
301 }
302
tile_set_occluder_offset(int p_id,const Vector2 & p_offset)303 void TileSet::tile_set_occluder_offset(int p_id, const Vector2 &p_offset) {
304
305 ERR_FAIL_COND(!tile_map.has(p_id));
306 tile_map[p_id].occluder_offset = p_offset;
307 }
308
tile_get_occluder_offset(int p_id) const309 Vector2 TileSet::tile_get_occluder_offset(int p_id) const {
310 ERR_FAIL_COND_V(!tile_map.has(p_id), Vector2());
311 return tile_map[p_id].occluder_offset;
312 }
313
tile_set_shapes(int p_id,const Vector<Ref<Shape2D>> & p_shapes)314 void TileSet::tile_set_shapes(int p_id, const Vector<Ref<Shape2D> > &p_shapes) {
315
316 ERR_FAIL_COND(!tile_map.has(p_id));
317 tile_map[p_id].shapes = p_shapes;
318 emit_changed();
319 }
320
tile_get_shapes(int p_id) const321 Vector<Ref<Shape2D> > TileSet::tile_get_shapes(int p_id) const {
322
323 ERR_FAIL_COND_V(!tile_map.has(p_id), Vector<Ref<Shape2D> >());
324 return tile_map[p_id].shapes;
325 }
326
_tile_set_shapes(int p_id,const Array & p_shapes)327 void TileSet::_tile_set_shapes(int p_id, const Array &p_shapes) {
328
329 ERR_FAIL_COND(!tile_map.has(p_id));
330 Vector<Ref<Shape2D> > shapes;
331 for (int i = 0; i < p_shapes.size(); i++) {
332
333 Ref<Shape2D> s = p_shapes[i];
334 if (s.is_valid())
335 shapes.push_back(s);
336 }
337
338 tile_set_shapes(p_id, shapes);
339 }
340
_tile_get_shapes(int p_id) const341 Array TileSet::_tile_get_shapes(int p_id) const {
342
343 ERR_FAIL_COND_V(!tile_map.has(p_id), Array());
344 Array arr;
345
346 Vector<Ref<Shape2D> > shp = tile_map[p_id].shapes;
347 for (int i = 0; i < shp.size(); i++)
348 arr.push_back(shp[i]);
349
350 return arr;
351 }
352
tile_set_one_way_collision_direction(int p_id,Vector2 p_direction)353 void TileSet::tile_set_one_way_collision_direction(int p_id, Vector2 p_direction) {
354
355 ERR_FAIL_COND(!tile_map.has(p_id));
356 tile_map[p_id].one_way_collision_direction = p_direction;
357 }
358
tile_get_one_way_collision_direction(int p_id) const359 Vector2 TileSet::tile_get_one_way_collision_direction(int p_id) const {
360
361 ERR_FAIL_COND_V(!tile_map.has(p_id), Vector2());
362 return tile_map[p_id].one_way_collision_direction;
363 }
364
tile_set_one_way_collision_max_depth(int p_id,float p_max_depth)365 void TileSet::tile_set_one_way_collision_max_depth(int p_id, float p_max_depth) {
366
367 ERR_FAIL_COND(!tile_map.has(p_id));
368 tile_map[p_id].one_way_collision_max_depth = p_max_depth;
369 }
370
tile_get_one_way_collision_max_depth(int p_id) const371 float TileSet::tile_get_one_way_collision_max_depth(int p_id) const {
372
373 ERR_FAIL_COND_V(!tile_map.has(p_id), 0.0f);
374 return tile_map[p_id].one_way_collision_max_depth;
375 }
376
_get_tiles_ids() const377 Array TileSet::_get_tiles_ids() const {
378
379 Array arr;
380
381 for (Map<int, Data>::Element *E = tile_map.front(); E; E = E->next()) {
382 arr.push_back(E->key());
383 }
384
385 return arr;
386 }
387
get_tile_list(List<int> * p_tiles) const388 void TileSet::get_tile_list(List<int> *p_tiles) const {
389
390 for (Map<int, Data>::Element *E = tile_map.front(); E; E = E->next()) {
391
392 p_tiles->push_back(E->key());
393 }
394 }
395
has_tile(int p_id) const396 bool TileSet::has_tile(int p_id) const {
397
398 return tile_map.has(p_id);
399 }
400
remove_tile(int p_id)401 void TileSet::remove_tile(int p_id) {
402
403 ERR_FAIL_COND(!tile_map.has(p_id));
404 tile_map.erase(p_id);
405 _change_notify("");
406 emit_changed();
407 }
408
get_last_unused_tile_id() const409 int TileSet::get_last_unused_tile_id() const {
410
411 if (tile_map.size())
412 return tile_map.back()->key() + 1;
413 else
414 return 0;
415 }
416
find_tile_by_name(const String & p_name) const417 int TileSet::find_tile_by_name(const String &p_name) const {
418
419 for (Map<int, Data>::Element *E = tile_map.front(); E; E = E->next()) {
420
421 if (p_name == E->get().name)
422 return E->key();
423 }
424 return -1;
425 }
426
clear()427 void TileSet::clear() {
428
429 tile_map.clear();
430 _change_notify("");
431 emit_changed();
432 }
433
_bind_methods()434 void TileSet::_bind_methods() {
435
436 ObjectTypeDB::bind_method(_MD("create_tile", "id"), &TileSet::create_tile);
437 ObjectTypeDB::bind_method(_MD("tile_set_name", "id", "name"), &TileSet::tile_set_name);
438 ObjectTypeDB::bind_method(_MD("tile_get_name", "id"), &TileSet::tile_get_name);
439 ObjectTypeDB::bind_method(_MD("tile_set_texture", "id", "texture:Texture"), &TileSet::tile_set_texture);
440 ObjectTypeDB::bind_method(_MD("tile_get_texture:Texture", "id"), &TileSet::tile_get_texture);
441 ObjectTypeDB::bind_method(_MD("tile_set_material", "id", "material:CanvasItemMaterial"), &TileSet::tile_set_material);
442 ObjectTypeDB::bind_method(_MD("tile_get_material:CanvasItemMaterial", "id"), &TileSet::tile_get_material);
443 ObjectTypeDB::bind_method(_MD("tile_set_texture_offset", "id", "texture_offset"), &TileSet::tile_set_texture_offset);
444 ObjectTypeDB::bind_method(_MD("tile_get_texture_offset", "id"), &TileSet::tile_get_texture_offset);
445 ObjectTypeDB::bind_method(_MD("tile_set_shape_offset", "id", "shape_offset"), &TileSet::tile_set_shape_offset);
446 ObjectTypeDB::bind_method(_MD("tile_get_shape_offset", "id"), &TileSet::tile_get_shape_offset);
447 ObjectTypeDB::bind_method(_MD("tile_set_region", "id", "region"), &TileSet::tile_set_region);
448 ObjectTypeDB::bind_method(_MD("tile_get_region", "id"), &TileSet::tile_get_region);
449 ObjectTypeDB::bind_method(_MD("tile_set_shape", "id", "shape:Shape2D"), &TileSet::tile_set_shape);
450 ObjectTypeDB::bind_method(_MD("tile_get_shape:Shape2D", "id"), &TileSet::tile_get_shape);
451 ObjectTypeDB::bind_method(_MD("tile_set_shapes", "id", "shapes"), &TileSet::_tile_set_shapes);
452 ObjectTypeDB::bind_method(_MD("tile_get_shapes", "id"), &TileSet::_tile_get_shapes);
453 ObjectTypeDB::bind_method(_MD("tile_set_navigation_polygon", "id", "navigation_polygon:NavigationPolygon"), &TileSet::tile_set_navigation_polygon);
454 ObjectTypeDB::bind_method(_MD("tile_get_navigation_polygon:NavigationPolygon", "id"), &TileSet::tile_get_navigation_polygon);
455 ObjectTypeDB::bind_method(_MD("tile_set_navigation_polygon_offset", "id", "navigation_polygon_offset"), &TileSet::tile_set_navigation_polygon_offset);
456 ObjectTypeDB::bind_method(_MD("tile_get_navigation_polygon_offset", "id"), &TileSet::tile_get_navigation_polygon_offset);
457 ObjectTypeDB::bind_method(_MD("tile_set_light_occluder", "id", "light_occluder:OccluderPolygon2D"), &TileSet::tile_set_light_occluder);
458 ObjectTypeDB::bind_method(_MD("tile_get_light_occluder:OccluderPolygon2D", "id"), &TileSet::tile_get_light_occluder);
459 ObjectTypeDB::bind_method(_MD("tile_set_occluder_offset", "id", "occluder_offset"), &TileSet::tile_set_occluder_offset);
460 ObjectTypeDB::bind_method(_MD("tile_get_occluder_offset", "id"), &TileSet::tile_get_occluder_offset);
461
462 ObjectTypeDB::bind_method(_MD("remove_tile", "id"), &TileSet::remove_tile);
463 ObjectTypeDB::bind_method(_MD("clear"), &TileSet::clear);
464 ObjectTypeDB::bind_method(_MD("get_last_unused_tile_id"), &TileSet::get_last_unused_tile_id);
465 ObjectTypeDB::bind_method(_MD("find_tile_by_name", "name"), &TileSet::find_tile_by_name);
466 ObjectTypeDB::bind_method(_MD("get_tiles_ids", "name"), &TileSet::_get_tiles_ids);
467 }
468
TileSet()469 TileSet::TileSet() {
470 }
471