tile_set.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  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. #include "core/array.h"
  32. #include "core/engine.h"
  33. bool TileSet::_set(const StringName &p_name, const Variant &p_value) {
  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. if (!tile_map.has(id))
  40. create_tile(id);
  41. String what = n.substr(slash + 1, n.length());
  42. if (what == "name")
  43. tile_set_name(id, p_value);
  44. else if (what == "texture")
  45. tile_set_texture(id, p_value);
  46. else if (what == "normal_map")
  47. tile_set_normal_map(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 == "region")
  55. tile_set_region(id, p_value);
  56. else if (what == "tile_mode")
  57. tile_set_tile_mode(id, (TileMode)((int)p_value));
  58. else if (what == "is_autotile") {
  59. // backward compatibility for Godot 3.0.x
  60. // autotile used to be a bool, it's now an enum
  61. bool is_autotile = p_value;
  62. if (is_autotile)
  63. tile_set_tile_mode(id, AUTO_TILE);
  64. } else if (what.left(9) == "autotile/") {
  65. what = what.right(9);
  66. if (what == "bitmask_mode")
  67. autotile_set_bitmask_mode(id, (BitmaskMode)((int)p_value));
  68. else if (what == "icon_coordinate")
  69. autotile_set_icon_coordinate(id, p_value);
  70. else if (what == "tile_size")
  71. autotile_set_size(id, p_value);
  72. else if (what == "spacing")
  73. autotile_set_spacing(id, p_value);
  74. else if (what == "bitmask_flags") {
  75. tile_map[id].autotile_data.flags.clear();
  76. if (p_value.is_array()) {
  77. Array p = p_value;
  78. Vector2 last_coord;
  79. while (p.size() > 0) {
  80. if (p[0].get_type() == Variant::VECTOR2) {
  81. last_coord = p[0];
  82. } else if (p[0].get_type() == Variant::INT) {
  83. autotile_set_bitmask(id, last_coord, p[0]);
  84. }
  85. p.pop_front();
  86. }
  87. }
  88. } else if (what == "occluder_map") {
  89. tile_map[id].autotile_data.occluder_map.clear();
  90. Array p = p_value;
  91. Vector2 last_coord;
  92. while (p.size() > 0) {
  93. if (p[0].get_type() == Variant::VECTOR2) {
  94. last_coord = p[0];
  95. } else if (p[0].get_type() == Variant::OBJECT) {
  96. autotile_set_light_occluder(id, p[0], last_coord);
  97. }
  98. p.pop_front();
  99. }
  100. } else if (what == "navpoly_map") {
  101. tile_map[id].autotile_data.navpoly_map.clear();
  102. Array p = p_value;
  103. Vector2 last_coord;
  104. while (p.size() > 0) {
  105. if (p[0].get_type() == Variant::VECTOR2) {
  106. last_coord = p[0];
  107. } else if (p[0].get_type() == Variant::OBJECT) {
  108. autotile_set_navigation_polygon(id, p[0], last_coord);
  109. }
  110. p.pop_front();
  111. }
  112. } else if (what == "priority_map") {
  113. tile_map[id].autotile_data.priority_map.clear();
  114. Array p = p_value;
  115. Vector3 val;
  116. Vector2 v;
  117. int priority;
  118. while (p.size() > 0) {
  119. val = p[0];
  120. if (val.z > 1) {
  121. v.x = val.x;
  122. v.y = val.y;
  123. priority = (int)val.z;
  124. tile_map[id].autotile_data.priority_map[v] = priority;
  125. }
  126. p.pop_front();
  127. }
  128. } else if (what == "z_index_map") {
  129. tile_map[id].autotile_data.z_index_map.clear();
  130. Array p = p_value;
  131. Vector3 val;
  132. Vector2 v;
  133. int z_index;
  134. while (p.size() > 0) {
  135. val = p[0];
  136. if (val.z != 0) {
  137. v.x = val.x;
  138. v.y = val.y;
  139. z_index = (int)val.z;
  140. tile_map[id].autotile_data.z_index_map[v] = z_index;
  141. }
  142. p.pop_front();
  143. }
  144. }
  145. } else if (what == "shape")
  146. tile_set_shape(id, 0, p_value);
  147. else if (what == "shape_offset")
  148. tile_set_shape_offset(id, 0, p_value);
  149. else if (what == "shape_transform")
  150. tile_set_shape_transform(id, 0, p_value);
  151. else if (what == "shape_one_way")
  152. tile_set_shape_one_way(id, 0, p_value);
  153. else if (what == "shape_one_way_margin")
  154. tile_set_shape_one_way_margin(id, 0, p_value);
  155. else if (what == "shapes")
  156. _tile_set_shapes(id, p_value);
  157. else if (what == "occluder")
  158. tile_set_light_occluder(id, p_value);
  159. else if (what == "occluder_offset")
  160. tile_set_occluder_offset(id, p_value);
  161. else if (what == "navigation")
  162. tile_set_navigation_polygon(id, p_value);
  163. else if (what == "navigation_offset")
  164. tile_set_navigation_polygon_offset(id, p_value);
  165. else if (what == "z_index")
  166. tile_set_z_index(id, p_value);
  167. else
  168. return false;
  169. return true;
  170. }
  171. bool TileSet::_get(const StringName &p_name, Variant &r_ret) const {
  172. String n = p_name;
  173. int slash = n.find("/");
  174. if (slash == -1)
  175. return false;
  176. int id = String::to_int(n.c_str(), slash);
  177. ERR_FAIL_COND_V(!tile_map.has(id), false);
  178. String what = n.substr(slash + 1, n.length());
  179. if (what == "name")
  180. r_ret = tile_get_name(id);
  181. else if (what == "texture")
  182. r_ret = tile_get_texture(id);
  183. else if (what == "normal_map")
  184. r_ret = tile_get_normal_map(id);
  185. else if (what == "tex_offset")
  186. r_ret = tile_get_texture_offset(id);
  187. else if (what == "material")
  188. r_ret = tile_get_material(id);
  189. else if (what == "modulate")
  190. r_ret = tile_get_modulate(id);
  191. else if (what == "region")
  192. r_ret = tile_get_region(id);
  193. else if (what == "tile_mode")
  194. r_ret = tile_get_tile_mode(id);
  195. else if (what.left(9) == "autotile/") {
  196. what = what.right(9);
  197. if (what == "bitmask_mode")
  198. r_ret = autotile_get_bitmask_mode(id);
  199. else if (what == "icon_coordinate")
  200. r_ret = autotile_get_icon_coordinate(id);
  201. else if (what == "tile_size")
  202. r_ret = autotile_get_size(id);
  203. else if (what == "spacing")
  204. r_ret = autotile_get_spacing(id);
  205. else if (what == "bitmask_flags") {
  206. Array p;
  207. for (Map<Vector2, uint16_t>::Element *E = tile_map[id].autotile_data.flags.front(); E; E = E->next()) {
  208. p.push_back(E->key());
  209. p.push_back(E->value());
  210. }
  211. r_ret = p;
  212. } else if (what == "occluder_map") {
  213. Array p;
  214. for (Map<Vector2, Ref<OccluderPolygon2D> >::Element *E = tile_map[id].autotile_data.occluder_map.front(); E; E = E->next()) {
  215. p.push_back(E->key());
  216. p.push_back(E->value());
  217. }
  218. r_ret = p;
  219. } else if (what == "navpoly_map") {
  220. Array p;
  221. for (Map<Vector2, Ref<NavigationPolygon> >::Element *E = tile_map[id].autotile_data.navpoly_map.front(); E; E = E->next()) {
  222. p.push_back(E->key());
  223. p.push_back(E->value());
  224. }
  225. r_ret = p;
  226. } else if (what == "priority_map") {
  227. Array p;
  228. Vector3 v;
  229. for (Map<Vector2, int>::Element *E = tile_map[id].autotile_data.priority_map.front(); E; E = E->next()) {
  230. if (E->value() > 1) {
  231. //Don't save default value
  232. v.x = E->key().x;
  233. v.y = E->key().y;
  234. v.z = E->value();
  235. p.push_back(v);
  236. }
  237. }
  238. r_ret = p;
  239. } else if (what == "z_index_map") {
  240. Array p;
  241. Vector3 v;
  242. for (Map<Vector2, int>::Element *E = tile_map[id].autotile_data.z_index_map.front(); E; E = E->next()) {
  243. if (E->value() != 0) {
  244. //Don't save default value
  245. v.x = E->key().x;
  246. v.y = E->key().y;
  247. v.z = E->value();
  248. p.push_back(v);
  249. }
  250. }
  251. r_ret = p;
  252. }
  253. } else if (what == "shape")
  254. r_ret = tile_get_shape(id, 0);
  255. else if (what == "shape_offset")
  256. r_ret = tile_get_shape_offset(id, 0);
  257. else if (what == "shape_transform")
  258. r_ret = tile_get_shape_transform(id, 0);
  259. else if (what == "shape_one_way")
  260. r_ret = tile_get_shape_one_way(id, 0);
  261. else if (what == "shape_one_way_margin")
  262. r_ret = tile_get_shape_one_way_margin(id, 0);
  263. else if (what == "shapes")
  264. r_ret = _tile_get_shapes(id);
  265. else if (what == "occluder")
  266. r_ret = tile_get_light_occluder(id);
  267. else if (what == "occluder_offset")
  268. r_ret = tile_get_occluder_offset(id);
  269. else if (what == "navigation")
  270. r_ret = tile_get_navigation_polygon(id);
  271. else if (what == "navigation_offset")
  272. r_ret = tile_get_navigation_polygon_offset(id);
  273. else if (what == "z_index")
  274. r_ret = tile_get_z_index(id);
  275. else
  276. return false;
  277. return true;
  278. }
  279. void TileSet::_get_property_list(List<PropertyInfo> *p_list) const {
  280. for (Map<int, TileData>::Element *E = tile_map.front(); E; E = E->next()) {
  281. int id = E->key();
  282. String pre = itos(id) + "/";
  283. p_list->push_back(PropertyInfo(Variant::STRING, pre + "name"));
  284. p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"));
  285. p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "normal_map", PROPERTY_HINT_RESOURCE_TYPE, "Texture"));
  286. p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "tex_offset"));
  287. p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "material", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial"));
  288. p_list->push_back(PropertyInfo(Variant::COLOR, pre + "modulate"));
  289. p_list->push_back(PropertyInfo(Variant::RECT2, pre + "region"));
  290. p_list->push_back(PropertyInfo(Variant::INT, pre + "tile_mode", PROPERTY_HINT_ENUM, "SINGLE_TILE,AUTO_TILE,ATLAS_TILE"));
  291. if (tile_get_tile_mode(id) == AUTO_TILE) {
  292. p_list->push_back(PropertyInfo(Variant::INT, pre + "autotile/bitmask_mode", PROPERTY_HINT_ENUM, "2X2,3X3 (minimal),3X3", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  293. p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "autotile/bitmask_flags", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  294. p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "autotile/icon_coordinate", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  295. p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "autotile/tile_size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  296. p_list->push_back(PropertyInfo(Variant::INT, pre + "autotile/spacing", PROPERTY_HINT_RANGE, "0,256,1", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  297. p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "autotile/occluder_map", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  298. p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "autotile/navpoly_map", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  299. p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "autotile/priority_map", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  300. p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "autotile/z_index_map", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  301. } else if (tile_get_tile_mode(id) == ATLAS_TILE) {
  302. p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "autotile/icon_coordinate", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  303. p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "autotile/tile_size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  304. p_list->push_back(PropertyInfo(Variant::INT, pre + "autotile/spacing", PROPERTY_HINT_RANGE, "0,256,1", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  305. p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "autotile/occluder_map", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  306. p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "autotile/navpoly_map", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  307. p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "autotile/z_index_map", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  308. }
  309. p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "occluder_offset"));
  310. p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "occluder", PROPERTY_HINT_RESOURCE_TYPE, "OccluderPolygon2D"));
  311. p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "navigation_offset"));
  312. p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "navigation", PROPERTY_HINT_RESOURCE_TYPE, "NavigationPolygon"));
  313. p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "shape_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  314. p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "shape_transform", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  315. p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D", PROPERTY_USAGE_EDITOR));
  316. p_list->push_back(PropertyInfo(Variant::BOOL, pre + "shape_one_way", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  317. p_list->push_back(PropertyInfo(Variant::REAL, pre + "shape_one_way_margin", PROPERTY_HINT_RANGE, "0,128,0.01", PROPERTY_USAGE_EDITOR));
  318. p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "shapes", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  319. p_list->push_back(PropertyInfo(Variant::INT, pre + "z_index", PROPERTY_HINT_RANGE, itos(VS::CANVAS_ITEM_Z_MIN) + "," + itos(VS::CANVAS_ITEM_Z_MAX) + ",1"));
  320. }
  321. }
  322. void TileSet::create_tile(int p_id) {
  323. ERR_FAIL_COND(tile_map.has(p_id));
  324. tile_map[p_id] = TileData();
  325. tile_map[p_id].autotile_data = AutotileData();
  326. _change_notify("");
  327. emit_changed();
  328. }
  329. void TileSet::autotile_set_bitmask_mode(int p_id, BitmaskMode p_mode) {
  330. ERR_FAIL_COND(!tile_map.has(p_id));
  331. tile_map[p_id].autotile_data.bitmask_mode = p_mode;
  332. _change_notify("");
  333. emit_changed();
  334. }
  335. TileSet::BitmaskMode TileSet::autotile_get_bitmask_mode(int p_id) const {
  336. ERR_FAIL_COND_V(!tile_map.has(p_id), BITMASK_2X2);
  337. return tile_map[p_id].autotile_data.bitmask_mode;
  338. }
  339. void TileSet::tile_set_texture(int p_id, const Ref<Texture> &p_texture) {
  340. ERR_FAIL_COND(!tile_map.has(p_id));
  341. tile_map[p_id].texture = p_texture;
  342. emit_changed();
  343. _change_notify("texture");
  344. }
  345. Ref<Texture> TileSet::tile_get_texture(int p_id) const {
  346. ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<Texture>());
  347. return tile_map[p_id].texture;
  348. }
  349. void TileSet::tile_set_normal_map(int p_id, const Ref<Texture> &p_normal_map) {
  350. ERR_FAIL_COND(!tile_map.has(p_id));
  351. tile_map[p_id].normal_map = p_normal_map;
  352. emit_changed();
  353. }
  354. Ref<Texture> TileSet::tile_get_normal_map(int p_id) const {
  355. ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<Texture>());
  356. return tile_map[p_id].normal_map;
  357. }
  358. void TileSet::tile_set_material(int p_id, const Ref<ShaderMaterial> &p_material) {
  359. ERR_FAIL_COND(!tile_map.has(p_id));
  360. tile_map[p_id].material = p_material;
  361. emit_changed();
  362. }
  363. Ref<ShaderMaterial> TileSet::tile_get_material(int p_id) const {
  364. ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<ShaderMaterial>());
  365. return tile_map[p_id].material;
  366. }
  367. void TileSet::tile_set_modulate(int p_id, const Color &p_modulate) {
  368. ERR_FAIL_COND(!tile_map.has(p_id));
  369. tile_map[p_id].modulate = p_modulate;
  370. emit_changed();
  371. _change_notify("modulate");
  372. }
  373. Color TileSet::tile_get_modulate(int p_id) const {
  374. ERR_FAIL_COND_V(!tile_map.has(p_id), Color(1, 1, 1));
  375. return tile_map[p_id].modulate;
  376. }
  377. void TileSet::tile_set_texture_offset(int p_id, const Vector2 &p_offset) {
  378. ERR_FAIL_COND(!tile_map.has(p_id));
  379. tile_map[p_id].offset = p_offset;
  380. emit_changed();
  381. }
  382. Vector2 TileSet::tile_get_texture_offset(int p_id) const {
  383. ERR_FAIL_COND_V(!tile_map.has(p_id), Vector2());
  384. return tile_map[p_id].offset;
  385. }
  386. void TileSet::tile_set_region(int p_id, const Rect2 &p_region) {
  387. ERR_FAIL_COND(!tile_map.has(p_id));
  388. tile_map[p_id].region = p_region;
  389. emit_changed();
  390. _change_notify("region");
  391. }
  392. Rect2 TileSet::tile_get_region(int p_id) const {
  393. ERR_FAIL_COND_V(!tile_map.has(p_id), Rect2());
  394. return tile_map[p_id].region;
  395. }
  396. void TileSet::tile_set_tile_mode(int p_id, TileMode p_tile_mode) {
  397. ERR_FAIL_COND(!tile_map.has(p_id));
  398. tile_map[p_id].tile_mode = p_tile_mode;
  399. emit_changed();
  400. _change_notify("tile_mode");
  401. }
  402. TileSet::TileMode TileSet::tile_get_tile_mode(int p_id) const {
  403. ERR_FAIL_COND_V(!tile_map.has(p_id), SINGLE_TILE);
  404. return tile_map[p_id].tile_mode;
  405. }
  406. void TileSet::autotile_set_icon_coordinate(int p_id, Vector2 coord) {
  407. ERR_FAIL_COND(!tile_map.has(p_id));
  408. tile_map[p_id].autotile_data.icon_coord = coord;
  409. emit_changed();
  410. }
  411. Vector2 TileSet::autotile_get_icon_coordinate(int p_id) const {
  412. ERR_FAIL_COND_V(!tile_map.has(p_id), Vector2());
  413. return tile_map[p_id].autotile_data.icon_coord;
  414. }
  415. void TileSet::autotile_set_spacing(int p_id, int p_spacing) {
  416. ERR_FAIL_COND(!tile_map.has(p_id));
  417. ERR_FAIL_COND(p_spacing < 0);
  418. tile_map[p_id].autotile_data.spacing = p_spacing;
  419. emit_changed();
  420. }
  421. int TileSet::autotile_get_spacing(int p_id) const {
  422. ERR_FAIL_COND_V(!tile_map.has(p_id), 0);
  423. return tile_map[p_id].autotile_data.spacing;
  424. }
  425. void TileSet::autotile_set_size(int p_id, Size2 p_size) {
  426. ERR_FAIL_COND(!tile_map.has(p_id));
  427. ERR_FAIL_COND(p_size.x <= 0 || p_size.y <= 0);
  428. tile_map[p_id].autotile_data.size = p_size;
  429. }
  430. Size2 TileSet::autotile_get_size(int p_id) const {
  431. ERR_FAIL_COND_V(!tile_map.has(p_id), Size2());
  432. return tile_map[p_id].autotile_data.size;
  433. }
  434. void TileSet::autotile_clear_bitmask_map(int p_id) {
  435. ERR_FAIL_COND(!tile_map.has(p_id));
  436. tile_map[p_id].autotile_data.flags.clear();
  437. }
  438. void TileSet::autotile_set_subtile_priority(int p_id, const Vector2 &p_coord, int p_priority) {
  439. ERR_FAIL_COND(!tile_map.has(p_id));
  440. ERR_FAIL_COND(p_priority <= 0);
  441. tile_map[p_id].autotile_data.priority_map[p_coord] = p_priority;
  442. }
  443. int TileSet::autotile_get_subtile_priority(int p_id, const Vector2 &p_coord) {
  444. ERR_FAIL_COND_V(!tile_map.has(p_id), 1);
  445. if (tile_map[p_id].autotile_data.priority_map.has(p_coord)) {
  446. return tile_map[p_id].autotile_data.priority_map[p_coord];
  447. }
  448. //When not custom priority set return the default value
  449. return 1;
  450. }
  451. const Map<Vector2, int> &TileSet::autotile_get_priority_map(int p_id) const {
  452. static Map<Vector2, int> dummy;
  453. ERR_FAIL_COND_V(!tile_map.has(p_id), dummy);
  454. return tile_map[p_id].autotile_data.priority_map;
  455. }
  456. void TileSet::autotile_set_z_index(int p_id, const Vector2 &p_coord, int p_z_index) {
  457. ERR_FAIL_COND(!tile_map.has(p_id));
  458. tile_map[p_id].autotile_data.z_index_map[p_coord] = p_z_index;
  459. emit_changed();
  460. }
  461. int TileSet::autotile_get_z_index(int p_id, const Vector2 &p_coord) {
  462. ERR_FAIL_COND_V(!tile_map.has(p_id), 1);
  463. if (tile_map[p_id].autotile_data.z_index_map.has(p_coord)) {
  464. return tile_map[p_id].autotile_data.z_index_map[p_coord];
  465. }
  466. //When not custom z index set return the default value
  467. return 0;
  468. }
  469. const Map<Vector2, int> &TileSet::autotile_get_z_index_map(int p_id) const {
  470. static Map<Vector2, int> dummy;
  471. ERR_FAIL_COND_V(!tile_map.has(p_id), dummy);
  472. return tile_map[p_id].autotile_data.z_index_map;
  473. }
  474. void TileSet::autotile_set_bitmask(int p_id, Vector2 p_coord, uint16_t p_flag) {
  475. ERR_FAIL_COND(!tile_map.has(p_id));
  476. if (p_flag == 0) {
  477. if (tile_map[p_id].autotile_data.flags.has(p_coord))
  478. tile_map[p_id].autotile_data.flags.erase(p_coord);
  479. } else {
  480. tile_map[p_id].autotile_data.flags[p_coord] = p_flag;
  481. }
  482. }
  483. uint16_t TileSet::autotile_get_bitmask(int p_id, Vector2 p_coord) {
  484. ERR_FAIL_COND_V(!tile_map.has(p_id), 0);
  485. if (!tile_map[p_id].autotile_data.flags.has(p_coord)) {
  486. return 0;
  487. }
  488. return tile_map[p_id].autotile_data.flags[p_coord];
  489. }
  490. const Map<Vector2, uint16_t> &TileSet::autotile_get_bitmask_map(int p_id) {
  491. static Map<Vector2, uint16_t> dummy;
  492. static Map<Vector2, uint16_t> dummy_atlas;
  493. ERR_FAIL_COND_V(!tile_map.has(p_id), dummy);
  494. if (tile_get_tile_mode(p_id) == ATLAS_TILE) {
  495. dummy_atlas = Map<Vector2, uint16_t>();
  496. Rect2 region = tile_get_region(p_id);
  497. Size2 size = autotile_get_size(p_id);
  498. float spacing = autotile_get_spacing(p_id);
  499. for (int x = 0; x < (region.size.x / (size.x + spacing)); x++) {
  500. for (int y = 0; y < (region.size.y / (size.y + spacing)); y++) {
  501. dummy_atlas.insert(Vector2(x, y), 0);
  502. }
  503. }
  504. return dummy_atlas;
  505. } else
  506. return tile_map[p_id].autotile_data.flags;
  507. }
  508. Vector2 TileSet::autotile_get_subtile_for_bitmask(int p_id, uint16_t p_bitmask, const Node *p_tilemap_node, const Vector2 &p_tile_location) {
  509. ERR_FAIL_COND_V(!tile_map.has(p_id), Vector2());
  510. //First try to forward selection to script
  511. if (p_tilemap_node->get_class_name() == "TileMap") {
  512. if (get_script_instance() != NULL) {
  513. if (get_script_instance()->has_method("_forward_subtile_selection")) {
  514. Variant ret = get_script_instance()->call("_forward_subtile_selection", p_id, p_bitmask, p_tilemap_node, p_tile_location);
  515. if (ret.get_type() == Variant::VECTOR2) {
  516. return ret;
  517. }
  518. }
  519. }
  520. }
  521. List<Vector2> coords;
  522. uint16_t mask;
  523. for (Map<Vector2, uint16_t>::Element *E = tile_map[p_id].autotile_data.flags.front(); E; E = E->next()) {
  524. mask = E->get();
  525. if (tile_map[p_id].autotile_data.bitmask_mode == BITMASK_2X2) {
  526. mask &= (BIND_BOTTOMLEFT | BIND_BOTTOMRIGHT | BIND_TOPLEFT | BIND_TOPRIGHT);
  527. }
  528. if (mask == p_bitmask) {
  529. for (int i = 0; i < autotile_get_subtile_priority(p_id, E->key()); i++) {
  530. coords.push_back(E->key());
  531. }
  532. }
  533. }
  534. if (coords.size() == 0) {
  535. return autotile_get_icon_coordinate(p_id);
  536. } else {
  537. return coords[Math::rand() % coords.size()];
  538. }
  539. }
  540. void TileSet::tile_set_name(int p_id, const String &p_name) {
  541. ERR_FAIL_COND(!tile_map.has(p_id));
  542. tile_map[p_id].name = p_name;
  543. emit_changed();
  544. _change_notify("name");
  545. }
  546. String TileSet::tile_get_name(int p_id) const {
  547. ERR_FAIL_COND_V(!tile_map.has(p_id), String());
  548. return tile_map[p_id].name;
  549. }
  550. void TileSet::tile_clear_shapes(int p_id) {
  551. tile_map[p_id].shapes_data.clear();
  552. }
  553. void TileSet::tile_add_shape(int p_id, const Ref<Shape2D> &p_shape, const Transform2D &p_transform, bool p_one_way, const Vector2 &p_autotile_coord) {
  554. ERR_FAIL_COND(!tile_map.has(p_id));
  555. ShapeData new_data = ShapeData();
  556. new_data.shape = p_shape;
  557. new_data.shape_transform = p_transform;
  558. new_data.one_way_collision = p_one_way;
  559. new_data.autotile_coord = p_autotile_coord;
  560. tile_map[p_id].shapes_data.push_back(new_data);
  561. }
  562. int TileSet::tile_get_shape_count(int p_id) const {
  563. ERR_FAIL_COND_V(!tile_map.has(p_id), 0);
  564. return tile_map[p_id].shapes_data.size();
  565. }
  566. void TileSet::tile_set_shape(int p_id, int p_shape_id, const Ref<Shape2D> &p_shape) {
  567. ERR_FAIL_COND(!tile_map.has(p_id));
  568. if (tile_map[p_id].shapes_data.size() <= p_shape_id)
  569. tile_map[p_id].shapes_data.resize(p_shape_id + 1);
  570. tile_map[p_id].shapes_data.write[p_shape_id].shape = p_shape;
  571. _decompose_convex_shape(p_shape);
  572. emit_changed();
  573. }
  574. Ref<Shape2D> TileSet::tile_get_shape(int p_id, int p_shape_id) const {
  575. ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<Shape2D>());
  576. if (tile_map[p_id].shapes_data.size() > p_shape_id)
  577. return tile_map[p_id].shapes_data[p_shape_id].shape;
  578. return Ref<Shape2D>();
  579. }
  580. void TileSet::tile_set_shape_transform(int p_id, int p_shape_id, const Transform2D &p_offset) {
  581. ERR_FAIL_COND(!tile_map.has(p_id));
  582. if (tile_map[p_id].shapes_data.size() <= p_shape_id)
  583. tile_map[p_id].shapes_data.resize(p_shape_id + 1);
  584. tile_map[p_id].shapes_data.write[p_shape_id].shape_transform = p_offset;
  585. emit_changed();
  586. }
  587. Transform2D TileSet::tile_get_shape_transform(int p_id, int p_shape_id) const {
  588. ERR_FAIL_COND_V(!tile_map.has(p_id), Transform2D());
  589. if (tile_map[p_id].shapes_data.size() > p_shape_id)
  590. return tile_map[p_id].shapes_data[p_shape_id].shape_transform;
  591. return Transform2D();
  592. }
  593. void TileSet::tile_set_shape_offset(int p_id, int p_shape_id, const Vector2 &p_offset) {
  594. Transform2D transform = tile_get_shape_transform(p_id, p_shape_id);
  595. transform.set_origin(p_offset);
  596. tile_set_shape_transform(p_id, p_shape_id, transform);
  597. }
  598. Vector2 TileSet::tile_get_shape_offset(int p_id, int p_shape_id) const {
  599. return tile_get_shape_transform(p_id, p_shape_id).get_origin();
  600. }
  601. void TileSet::tile_set_shape_one_way(int p_id, int p_shape_id, const bool p_one_way) {
  602. ERR_FAIL_COND(!tile_map.has(p_id));
  603. if (tile_map[p_id].shapes_data.size() <= p_shape_id)
  604. tile_map[p_id].shapes_data.resize(p_shape_id + 1);
  605. tile_map[p_id].shapes_data.write[p_shape_id].one_way_collision = p_one_way;
  606. emit_changed();
  607. }
  608. bool TileSet::tile_get_shape_one_way(int p_id, int p_shape_id) const {
  609. ERR_FAIL_COND_V(!tile_map.has(p_id), false);
  610. if (tile_map[p_id].shapes_data.size() > p_shape_id)
  611. return tile_map[p_id].shapes_data[p_shape_id].one_way_collision;
  612. return false;
  613. }
  614. void TileSet::tile_set_shape_one_way_margin(int p_id, int p_shape_id, float p_margin) {
  615. ERR_FAIL_COND(!tile_map.has(p_id));
  616. if (tile_map[p_id].shapes_data.size() <= p_shape_id)
  617. tile_map[p_id].shapes_data.resize(p_shape_id + 1);
  618. tile_map[p_id].shapes_data.write[p_shape_id].one_way_collision_margin = p_margin;
  619. emit_changed();
  620. }
  621. float TileSet::tile_get_shape_one_way_margin(int p_id, int p_shape_id) const {
  622. ERR_FAIL_COND_V(!tile_map.has(p_id), 0);
  623. if (tile_map[p_id].shapes_data.size() > p_shape_id)
  624. return tile_map[p_id].shapes_data[p_shape_id].one_way_collision_margin;
  625. return 0;
  626. }
  627. void TileSet::tile_set_light_occluder(int p_id, const Ref<OccluderPolygon2D> &p_light_occluder) {
  628. ERR_FAIL_COND(!tile_map.has(p_id));
  629. tile_map[p_id].occluder = p_light_occluder;
  630. }
  631. Ref<OccluderPolygon2D> TileSet::tile_get_light_occluder(int p_id) const {
  632. ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<OccluderPolygon2D>());
  633. return tile_map[p_id].occluder;
  634. }
  635. void TileSet::autotile_set_light_occluder(int p_id, const Ref<OccluderPolygon2D> &p_light_occluder, const Vector2 &p_coord) {
  636. ERR_FAIL_COND(!tile_map.has(p_id));
  637. if (p_light_occluder.is_null()) {
  638. if (tile_map[p_id].autotile_data.occluder_map.has(p_coord)) {
  639. tile_map[p_id].autotile_data.occluder_map.erase(p_coord);
  640. }
  641. } else {
  642. tile_map[p_id].autotile_data.occluder_map[p_coord] = p_light_occluder;
  643. }
  644. }
  645. Ref<OccluderPolygon2D> TileSet::autotile_get_light_occluder(int p_id, const Vector2 &p_coord) const {
  646. ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<OccluderPolygon2D>());
  647. if (!tile_map[p_id].autotile_data.occluder_map.has(p_coord)) {
  648. return Ref<OccluderPolygon2D>();
  649. } else {
  650. return tile_map[p_id].autotile_data.occluder_map[p_coord];
  651. }
  652. }
  653. void TileSet::tile_set_navigation_polygon_offset(int p_id, const Vector2 &p_offset) {
  654. ERR_FAIL_COND(!tile_map.has(p_id));
  655. tile_map[p_id].navigation_polygon_offset = p_offset;
  656. }
  657. Vector2 TileSet::tile_get_navigation_polygon_offset(int p_id) const {
  658. ERR_FAIL_COND_V(!tile_map.has(p_id), Vector2());
  659. return tile_map[p_id].navigation_polygon_offset;
  660. }
  661. void TileSet::tile_set_navigation_polygon(int p_id, const Ref<NavigationPolygon> &p_navigation_polygon) {
  662. ERR_FAIL_COND(!tile_map.has(p_id));
  663. tile_map[p_id].navigation_polygon = p_navigation_polygon;
  664. }
  665. Ref<NavigationPolygon> TileSet::tile_get_navigation_polygon(int p_id) const {
  666. ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<NavigationPolygon>());
  667. return tile_map[p_id].navigation_polygon;
  668. }
  669. const Map<Vector2, Ref<OccluderPolygon2D> > &TileSet::autotile_get_light_oclusion_map(int p_id) const {
  670. static Map<Vector2, Ref<OccluderPolygon2D> > dummy;
  671. ERR_FAIL_COND_V(!tile_map.has(p_id), dummy);
  672. return tile_map[p_id].autotile_data.occluder_map;
  673. }
  674. void TileSet::autotile_set_navigation_polygon(int p_id, const Ref<NavigationPolygon> &p_navigation_polygon, const Vector2 &p_coord) {
  675. ERR_FAIL_COND(!tile_map.has(p_id));
  676. if (p_navigation_polygon.is_null()) {
  677. if (tile_map[p_id].autotile_data.navpoly_map.has(p_coord)) {
  678. tile_map[p_id].autotile_data.navpoly_map.erase(p_coord);
  679. }
  680. } else {
  681. tile_map[p_id].autotile_data.navpoly_map[p_coord] = p_navigation_polygon;
  682. }
  683. }
  684. Ref<NavigationPolygon> TileSet::autotile_get_navigation_polygon(int p_id, const Vector2 &p_coord) const {
  685. ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<NavigationPolygon>());
  686. if (!tile_map[p_id].autotile_data.navpoly_map.has(p_coord)) {
  687. return Ref<NavigationPolygon>();
  688. } else {
  689. return tile_map[p_id].autotile_data.navpoly_map[p_coord];
  690. }
  691. }
  692. const Map<Vector2, Ref<NavigationPolygon> > &TileSet::autotile_get_navigation_map(int p_id) const {
  693. static Map<Vector2, Ref<NavigationPolygon> > dummy;
  694. ERR_FAIL_COND_V(!tile_map.has(p_id), dummy);
  695. return tile_map[p_id].autotile_data.navpoly_map;
  696. }
  697. void TileSet::tile_set_occluder_offset(int p_id, const Vector2 &p_offset) {
  698. ERR_FAIL_COND(!tile_map.has(p_id));
  699. tile_map[p_id].occluder_offset = p_offset;
  700. }
  701. Vector2 TileSet::tile_get_occluder_offset(int p_id) const {
  702. ERR_FAIL_COND_V(!tile_map.has(p_id), Vector2());
  703. return tile_map[p_id].occluder_offset;
  704. }
  705. void TileSet::tile_set_shapes(int p_id, const Vector<ShapeData> &p_shapes) {
  706. ERR_FAIL_COND(!tile_map.has(p_id));
  707. tile_map[p_id].shapes_data = p_shapes;
  708. for (int i = 0; i < p_shapes.size(); i++) {
  709. _decompose_convex_shape(p_shapes[i].shape);
  710. }
  711. emit_changed();
  712. }
  713. Vector<TileSet::ShapeData> TileSet::tile_get_shapes(int p_id) const {
  714. ERR_FAIL_COND_V(!tile_map.has(p_id), Vector<ShapeData>());
  715. return tile_map[p_id].shapes_data;
  716. }
  717. int TileSet::tile_get_z_index(int p_id) const {
  718. ERR_FAIL_COND_V(!tile_map.has(p_id), 0);
  719. return tile_map[p_id].z_index;
  720. }
  721. void TileSet::tile_set_z_index(int p_id, int p_z_index) {
  722. ERR_FAIL_COND(!tile_map.has(p_id));
  723. tile_map[p_id].z_index = p_z_index;
  724. emit_changed();
  725. }
  726. void TileSet::_tile_set_shapes(int p_id, const Array &p_shapes) {
  727. ERR_FAIL_COND(!tile_map.has(p_id));
  728. Vector<ShapeData> shapes_data;
  729. Transform2D default_transform = tile_get_shape_transform(p_id, 0);
  730. bool default_one_way = tile_get_shape_one_way(p_id, 0);
  731. Vector2 default_autotile_coord = Vector2();
  732. for (int i = 0; i < p_shapes.size(); i++) {
  733. ShapeData s = ShapeData();
  734. if (p_shapes[i].get_type() == Variant::OBJECT) {
  735. Ref<Shape2D> shape = p_shapes[i];
  736. if (shape.is_null()) continue;
  737. s.shape = shape;
  738. s.shape_transform = default_transform;
  739. s.one_way_collision = default_one_way;
  740. s.autotile_coord = default_autotile_coord;
  741. } else if (p_shapes[i].get_type() == Variant::DICTIONARY) {
  742. Dictionary d = p_shapes[i];
  743. if (d.has("shape") && d["shape"].get_type() == Variant::OBJECT) {
  744. s.shape = d["shape"];
  745. _decompose_convex_shape(s.shape);
  746. } else
  747. continue;
  748. if (d.has("shape_transform") && d["shape_transform"].get_type() == Variant::TRANSFORM2D)
  749. s.shape_transform = d["shape_transform"];
  750. else if (d.has("shape_offset") && d["shape_offset"].get_type() == Variant::VECTOR2)
  751. s.shape_transform = Transform2D(0, (Vector2)d["shape_offset"]);
  752. else
  753. s.shape_transform = default_transform;
  754. if (d.has("one_way") && d["one_way"].get_type() == Variant::BOOL)
  755. s.one_way_collision = d["one_way"];
  756. else
  757. s.one_way_collision = default_one_way;
  758. if (d.has("one_way_margin") && d["one_way_margin"].is_num())
  759. s.one_way_collision_margin = d["one_way_margin"];
  760. else
  761. s.one_way_collision_margin = 1.0;
  762. if (d.has("autotile_coord") && d["autotile_coord"].get_type() == Variant::VECTOR2)
  763. s.autotile_coord = d["autotile_coord"];
  764. else
  765. s.autotile_coord = default_autotile_coord;
  766. } else {
  767. ERR_EXPLAIN("Expected an array of objects or dictionaries for tile_set_shapes");
  768. ERR_CONTINUE(true);
  769. }
  770. shapes_data.push_back(s);
  771. }
  772. tile_map[p_id].shapes_data = shapes_data;
  773. }
  774. Array TileSet::_tile_get_shapes(int p_id) const {
  775. ERR_FAIL_COND_V(!tile_map.has(p_id), Array());
  776. Array arr;
  777. Vector<ShapeData> data = tile_map[p_id].shapes_data;
  778. for (int i = 0; i < data.size(); i++) {
  779. Dictionary shape_data;
  780. shape_data["shape"] = data[i].shape;
  781. shape_data["shape_transform"] = data[i].shape_transform;
  782. shape_data["one_way"] = data[i].one_way_collision;
  783. shape_data["one_way_margin"] = data[i].one_way_collision_margin;
  784. shape_data["autotile_coord"] = data[i].autotile_coord;
  785. arr.push_back(shape_data);
  786. }
  787. return arr;
  788. }
  789. Array TileSet::_get_tiles_ids() const {
  790. Array arr;
  791. for (Map<int, TileData>::Element *E = tile_map.front(); E; E = E->next()) {
  792. arr.push_back(E->key());
  793. }
  794. return arr;
  795. }
  796. void TileSet::_decompose_convex_shape(Ref<Shape2D> p_shape) {
  797. if (Engine::get_singleton()->is_editor_hint())
  798. return;
  799. Ref<ConvexPolygonShape2D> convex = p_shape;
  800. if (!convex.is_valid())
  801. return;
  802. Vector<Vector<Vector2> > decomp = Geometry::decompose_polygon_in_convex(convex->get_points());
  803. if (decomp.size() > 1) {
  804. Array sub_shapes;
  805. for (int i = 0; i < decomp.size(); i++) {
  806. Ref<ConvexPolygonShape2D> _convex = memnew(ConvexPolygonShape2D);
  807. _convex->set_points(decomp[i]);
  808. sub_shapes.append(_convex);
  809. }
  810. convex->set_meta("decomposed", sub_shapes);
  811. } else {
  812. convex->set_meta("decomposed", Variant());
  813. }
  814. }
  815. void TileSet::get_tile_list(List<int> *p_tiles) const {
  816. for (Map<int, TileData>::Element *E = tile_map.front(); E; E = E->next()) {
  817. p_tiles->push_back(E->key());
  818. }
  819. }
  820. bool TileSet::has_tile(int p_id) const {
  821. return tile_map.has(p_id);
  822. }
  823. bool TileSet::is_tile_bound(int p_drawn_id, int p_neighbor_id) {
  824. if (p_drawn_id == p_neighbor_id) {
  825. return true;
  826. } else if (get_script_instance() != NULL) {
  827. if (get_script_instance()->has_method("_is_tile_bound")) {
  828. Variant ret = get_script_instance()->call("_is_tile_bound", p_drawn_id, p_neighbor_id);
  829. if (ret.get_type() == Variant::BOOL) {
  830. return ret;
  831. }
  832. }
  833. }
  834. return false;
  835. }
  836. void TileSet::remove_tile(int p_id) {
  837. ERR_FAIL_COND(!tile_map.has(p_id));
  838. tile_map.erase(p_id);
  839. _change_notify("");
  840. emit_changed();
  841. }
  842. int TileSet::get_last_unused_tile_id() const {
  843. if (tile_map.size())
  844. return tile_map.back()->key() + 1;
  845. else
  846. return 0;
  847. }
  848. int TileSet::find_tile_by_name(const String &p_name) const {
  849. for (Map<int, TileData>::Element *E = tile_map.front(); E; E = E->next()) {
  850. if (p_name == E->get().name)
  851. return E->key();
  852. }
  853. return -1;
  854. }
  855. void TileSet::clear() {
  856. tile_map.clear();
  857. _change_notify("");
  858. emit_changed();
  859. }
  860. void TileSet::_bind_methods() {
  861. ClassDB::bind_method(D_METHOD("create_tile", "id"), &TileSet::create_tile);
  862. ClassDB::bind_method(D_METHOD("autotile_clear_bitmask_map", "id"), &TileSet::autotile_clear_bitmask_map);
  863. ClassDB::bind_method(D_METHOD("autotile_set_icon_coordinate", "id", "coord"), &TileSet::autotile_set_icon_coordinate);
  864. ClassDB::bind_method(D_METHOD("autotile_get_icon_coordinate", "id"), &TileSet::autotile_get_icon_coordinate);
  865. ClassDB::bind_method(D_METHOD("autotile_set_subtile_priority", "id", "coord", "priority"), &TileSet::autotile_set_subtile_priority);
  866. ClassDB::bind_method(D_METHOD("autotile_get_subtile_priority", "id", "coord"), &TileSet::autotile_get_subtile_priority);
  867. ClassDB::bind_method(D_METHOD("autotile_set_z_index", "id", "coord", "z_index"), &TileSet::autotile_set_z_index);
  868. ClassDB::bind_method(D_METHOD("autotile_get_z_index", "id", "coord"), &TileSet::autotile_get_z_index);
  869. ClassDB::bind_method(D_METHOD("autotile_set_light_occluder", "id", "light_occluder", "coord"), &TileSet::autotile_set_light_occluder);
  870. ClassDB::bind_method(D_METHOD("autotile_get_light_occluder", "id", "coord"), &TileSet::autotile_get_light_occluder);
  871. ClassDB::bind_method(D_METHOD("autotile_set_navigation_polygon", "id", "navigation_polygon", "coord"), &TileSet::autotile_set_navigation_polygon);
  872. ClassDB::bind_method(D_METHOD("autotile_get_navigation_polygon", "id", "coord"), &TileSet::autotile_get_navigation_polygon);
  873. ClassDB::bind_method(D_METHOD("autotile_set_bitmask", "id", "bitmask", "flag"), &TileSet::autotile_set_bitmask);
  874. ClassDB::bind_method(D_METHOD("autotile_get_bitmask", "id", "coord"), &TileSet::autotile_get_bitmask);
  875. ClassDB::bind_method(D_METHOD("autotile_set_bitmask_mode", "id", "mode"), &TileSet::autotile_set_bitmask_mode);
  876. ClassDB::bind_method(D_METHOD("autotile_get_bitmask_mode", "id"), &TileSet::autotile_get_bitmask_mode);
  877. ClassDB::bind_method(D_METHOD("autotile_set_spacing", "id", "spacing"), &TileSet::autotile_set_spacing);
  878. ClassDB::bind_method(D_METHOD("autotile_get_spacing", "id"), &TileSet::autotile_get_spacing);
  879. ClassDB::bind_method(D_METHOD("autotile_set_size", "id", "size"), &TileSet::autotile_set_size);
  880. ClassDB::bind_method(D_METHOD("autotile_get_size", "id"), &TileSet::autotile_get_size);
  881. ClassDB::bind_method(D_METHOD("tile_set_name", "id", "name"), &TileSet::tile_set_name);
  882. ClassDB::bind_method(D_METHOD("tile_get_name", "id"), &TileSet::tile_get_name);
  883. ClassDB::bind_method(D_METHOD("tile_set_texture", "id", "texture"), &TileSet::tile_set_texture);
  884. ClassDB::bind_method(D_METHOD("tile_get_texture", "id"), &TileSet::tile_get_texture);
  885. ClassDB::bind_method(D_METHOD("tile_set_normal_map", "id", "normal_map"), &TileSet::tile_set_normal_map);
  886. ClassDB::bind_method(D_METHOD("tile_get_normal_map", "id"), &TileSet::tile_get_normal_map);
  887. ClassDB::bind_method(D_METHOD("tile_set_material", "id", "material"), &TileSet::tile_set_material);
  888. ClassDB::bind_method(D_METHOD("tile_get_material", "id"), &TileSet::tile_get_material);
  889. ClassDB::bind_method(D_METHOD("tile_set_modulate", "id", "color"), &TileSet::tile_set_modulate);
  890. ClassDB::bind_method(D_METHOD("tile_get_modulate", "id"), &TileSet::tile_get_modulate);
  891. ClassDB::bind_method(D_METHOD("tile_set_texture_offset", "id", "texture_offset"), &TileSet::tile_set_texture_offset);
  892. ClassDB::bind_method(D_METHOD("tile_get_texture_offset", "id"), &TileSet::tile_get_texture_offset);
  893. ClassDB::bind_method(D_METHOD("tile_set_region", "id", "region"), &TileSet::tile_set_region);
  894. ClassDB::bind_method(D_METHOD("tile_get_region", "id"), &TileSet::tile_get_region);
  895. ClassDB::bind_method(D_METHOD("tile_set_shape", "id", "shape_id", "shape"), &TileSet::tile_set_shape);
  896. ClassDB::bind_method(D_METHOD("tile_get_shape", "id", "shape_id"), &TileSet::tile_get_shape);
  897. ClassDB::bind_method(D_METHOD("tile_set_shape_offset", "id", "shape_id", "shape_offset"), &TileSet::tile_set_shape_offset);
  898. ClassDB::bind_method(D_METHOD("tile_get_shape_offset", "id", "shape_id"), &TileSet::tile_get_shape_offset);
  899. ClassDB::bind_method(D_METHOD("tile_set_shape_transform", "id", "shape_id", "shape_transform"), &TileSet::tile_set_shape_transform);
  900. ClassDB::bind_method(D_METHOD("tile_get_shape_transform", "id", "shape_id"), &TileSet::tile_get_shape_transform);
  901. ClassDB::bind_method(D_METHOD("tile_set_shape_one_way", "id", "shape_id", "one_way"), &TileSet::tile_set_shape_one_way);
  902. ClassDB::bind_method(D_METHOD("tile_get_shape_one_way", "id", "shape_id"), &TileSet::tile_get_shape_one_way);
  903. ClassDB::bind_method(D_METHOD("tile_set_shape_one_way_margin", "id", "shape_id", "one_way"), &TileSet::tile_set_shape_one_way_margin);
  904. ClassDB::bind_method(D_METHOD("tile_get_shape_one_way_margin", "id", "shape_id"), &TileSet::tile_get_shape_one_way_margin);
  905. ClassDB::bind_method(D_METHOD("tile_add_shape", "id", "shape", "shape_transform", "one_way", "autotile_coord"), &TileSet::tile_add_shape, DEFVAL(false), DEFVAL(Vector2()));
  906. ClassDB::bind_method(D_METHOD("tile_get_shape_count", "id"), &TileSet::tile_get_shape_count);
  907. ClassDB::bind_method(D_METHOD("tile_set_shapes", "id", "shapes"), &TileSet::_tile_set_shapes);
  908. ClassDB::bind_method(D_METHOD("tile_get_shapes", "id"), &TileSet::_tile_get_shapes);
  909. ClassDB::bind_method(D_METHOD("tile_set_tile_mode", "id", "tilemode"), &TileSet::tile_set_tile_mode);
  910. ClassDB::bind_method(D_METHOD("tile_get_tile_mode", "id"), &TileSet::tile_get_tile_mode);
  911. ClassDB::bind_method(D_METHOD("tile_set_navigation_polygon", "id", "navigation_polygon"), &TileSet::tile_set_navigation_polygon);
  912. ClassDB::bind_method(D_METHOD("tile_get_navigation_polygon", "id"), &TileSet::tile_get_navigation_polygon);
  913. ClassDB::bind_method(D_METHOD("tile_set_navigation_polygon_offset", "id", "navigation_polygon_offset"), &TileSet::tile_set_navigation_polygon_offset);
  914. ClassDB::bind_method(D_METHOD("tile_get_navigation_polygon_offset", "id"), &TileSet::tile_get_navigation_polygon_offset);
  915. ClassDB::bind_method(D_METHOD("tile_set_light_occluder", "id", "light_occluder"), &TileSet::tile_set_light_occluder);
  916. ClassDB::bind_method(D_METHOD("tile_get_light_occluder", "id"), &TileSet::tile_get_light_occluder);
  917. ClassDB::bind_method(D_METHOD("tile_set_occluder_offset", "id", "occluder_offset"), &TileSet::tile_set_occluder_offset);
  918. ClassDB::bind_method(D_METHOD("tile_get_occluder_offset", "id"), &TileSet::tile_get_occluder_offset);
  919. ClassDB::bind_method(D_METHOD("tile_set_z_index", "id", "z_index"), &TileSet::tile_set_z_index);
  920. ClassDB::bind_method(D_METHOD("tile_get_z_index", "id"), &TileSet::tile_get_z_index);
  921. ClassDB::bind_method(D_METHOD("remove_tile", "id"), &TileSet::remove_tile);
  922. ClassDB::bind_method(D_METHOD("clear"), &TileSet::clear);
  923. ClassDB::bind_method(D_METHOD("get_last_unused_tile_id"), &TileSet::get_last_unused_tile_id);
  924. ClassDB::bind_method(D_METHOD("find_tile_by_name", "name"), &TileSet::find_tile_by_name);
  925. ClassDB::bind_method(D_METHOD("get_tiles_ids"), &TileSet::_get_tiles_ids);
  926. BIND_VMETHOD(MethodInfo(Variant::BOOL, "_is_tile_bound", PropertyInfo(Variant::INT, "drawn_id"), PropertyInfo(Variant::INT, "neighbor_id")));
  927. BIND_VMETHOD(MethodInfo(Variant::VECTOR2, "_forward_subtile_selection", PropertyInfo(Variant::INT, "autotile_id"), PropertyInfo(Variant::INT, "bitmask"), PropertyInfo(Variant::OBJECT, "tilemap", PROPERTY_HINT_NONE, "TileMap"), PropertyInfo(Variant::VECTOR2, "tile_location")));
  928. BIND_ENUM_CONSTANT(BITMASK_2X2);
  929. BIND_ENUM_CONSTANT(BITMASK_3X3_MINIMAL);
  930. BIND_ENUM_CONSTANT(BITMASK_3X3);
  931. BIND_ENUM_CONSTANT(BIND_TOPLEFT);
  932. BIND_ENUM_CONSTANT(BIND_TOP);
  933. BIND_ENUM_CONSTANT(BIND_TOPRIGHT);
  934. BIND_ENUM_CONSTANT(BIND_LEFT);
  935. BIND_ENUM_CONSTANT(BIND_RIGHT);
  936. BIND_ENUM_CONSTANT(BIND_BOTTOMLEFT);
  937. BIND_ENUM_CONSTANT(BIND_BOTTOM);
  938. BIND_ENUM_CONSTANT(BIND_BOTTOMRIGHT);
  939. BIND_ENUM_CONSTANT(SINGLE_TILE);
  940. BIND_ENUM_CONSTANT(AUTO_TILE);
  941. BIND_ENUM_CONSTANT(ATLAS_TILE);
  942. }
  943. TileSet::TileSet() {
  944. }