tile_set.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*************************************************************************/
  2. /* tile_set.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "tile_set.h"
  30. bool TileSet::_set(const StringName& p_name, const Variant& p_value) {
  31. String n = p_name;
  32. int slash = n.find("/");
  33. if (slash==-1)
  34. return false;
  35. int id = String::to_int(n.c_str(),slash);
  36. if (!tile_map.has(id))
  37. create_tile(id);
  38. String what = n.substr(slash+1,n.length());
  39. if (what=="name")
  40. tile_set_name(id,p_value);
  41. else if (what=="texture")
  42. tile_set_texture(id,p_value);
  43. else if (what=="tex_offset")
  44. tile_set_texture_offset(id,p_value);
  45. else if (what=="material")
  46. tile_set_material(id,p_value);
  47. else if (what=="shape_offset")
  48. tile_set_shape_offset(id,p_value);
  49. else if (what=="region")
  50. tile_set_region(id,p_value);
  51. else if (what=="shape")
  52. tile_set_shape(id,p_value);
  53. else if (what=="shapes")
  54. _tile_set_shapes(id,p_value);
  55. else if (what=="occluder")
  56. tile_set_light_occluder(id,p_value);
  57. else if (what=="occluder_offset")
  58. tile_set_occluder_offset(id,p_value);
  59. else if (what=="navigation")
  60. tile_set_navigation_polygon(id,p_value);
  61. else if (what=="navigation_offset")
  62. tile_set_navigation_polygon_offset(id,p_value);
  63. else
  64. return false;
  65. return true;
  66. }
  67. bool TileSet::_get(const StringName& p_name,Variant &r_ret) const{
  68. String n = p_name;
  69. int slash = n.find("/");
  70. if (slash==-1)
  71. return false;
  72. int id = String::to_int(n.c_str(),slash);
  73. ERR_FAIL_COND_V(!tile_map.has(id),false);
  74. String what = n.substr(slash+1,n.length());
  75. if (what=="name")
  76. r_ret=tile_get_name(id);
  77. else if (what=="texture")
  78. r_ret=tile_get_texture(id);
  79. else if (what=="tex_offset")
  80. r_ret=tile_get_texture_offset(id);
  81. else if (what=="material")
  82. r_ret=tile_get_material(id);
  83. else if (what=="shape_offset")
  84. r_ret=tile_get_shape_offset(id);
  85. else if (what=="region")
  86. r_ret=tile_get_region(id);
  87. else if (what=="shape")
  88. r_ret=tile_get_shape(id);
  89. else if (what=="shapes")
  90. r_ret=_tile_get_shapes(id);
  91. else if (what=="occluder")
  92. r_ret=tile_get_light_occluder(id);
  93. else if (what=="occluder_offset")
  94. r_ret=tile_get_occluder_offset(id);
  95. else if (what=="navigation")
  96. r_ret=tile_get_navigation_polygon(id);
  97. else if (what=="navigation_offset")
  98. r_ret=tile_get_navigation_polygon_offset(id);
  99. else
  100. return false;
  101. return true;
  102. }
  103. void TileSet::_get_property_list( List<PropertyInfo> *p_list) const{
  104. for(Map<int,Data>::Element *E=tile_map.front();E;E=E->next()) {
  105. int id = E->key();
  106. String pre = itos(id)+"/";
  107. p_list->push_back(PropertyInfo(Variant::STRING,pre+"name"));
  108. p_list->push_back(PropertyInfo(Variant::OBJECT,pre+"texture",PROPERTY_HINT_RESOURCE_TYPE,"Texture"));
  109. p_list->push_back(PropertyInfo(Variant::VECTOR2,pre+"tex_offset"));
  110. p_list->push_back(PropertyInfo(Variant::OBJECT,pre+"material",PROPERTY_HINT_RESOURCE_TYPE,"CanvasItemMaterial"));
  111. p_list->push_back(PropertyInfo(Variant::RECT2,pre+"region"));
  112. p_list->push_back(PropertyInfo(Variant::VECTOR2,pre+"occluder_offset"));
  113. p_list->push_back(PropertyInfo(Variant::OBJECT,pre+"occluder",PROPERTY_HINT_RESOURCE_TYPE,"OccluderPolygon2D"));
  114. p_list->push_back(PropertyInfo(Variant::VECTOR2,pre+"navigation_offset"));
  115. p_list->push_back(PropertyInfo(Variant::OBJECT,pre+"navigation",PROPERTY_HINT_RESOURCE_TYPE,"NavigationPolygon"));
  116. p_list->push_back(PropertyInfo(Variant::VECTOR2,pre+"shape_offset"));
  117. p_list->push_back(PropertyInfo(Variant::OBJECT,pre+"shape",PROPERTY_HINT_RESOURCE_TYPE,"Shape2D",PROPERTY_USAGE_EDITOR));
  118. p_list->push_back(PropertyInfo(Variant::ARRAY,pre+"shapes",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR));
  119. }
  120. }
  121. void TileSet::create_tile(int p_id) {
  122. ERR_FAIL_COND( tile_map.has(p_id) );
  123. tile_map[p_id]=Data();
  124. _change_notify("");
  125. emit_changed();
  126. }
  127. void TileSet::tile_set_texture(int p_id,const Ref<Texture> &p_texture) {
  128. ERR_FAIL_COND(!tile_map.has(p_id));
  129. tile_map[p_id].texture=p_texture;
  130. emit_changed();
  131. }
  132. Ref<Texture> TileSet::tile_get_texture(int p_id) const {
  133. ERR_FAIL_COND_V(!tile_map.has(p_id),Ref<Texture>());
  134. return tile_map[p_id].texture;
  135. }
  136. void TileSet::tile_set_material(int p_id,const Ref<CanvasItemMaterial> &p_material) {
  137. ERR_FAIL_COND(!tile_map.has(p_id));
  138. tile_map[p_id].material=p_material;
  139. emit_changed();
  140. }
  141. Ref<CanvasItemMaterial> TileSet::tile_get_material(int p_id) const{
  142. ERR_FAIL_COND_V(!tile_map.has(p_id),Ref<CanvasItemMaterial>());
  143. return tile_map[p_id].material;
  144. }
  145. void TileSet::tile_set_texture_offset(int p_id,const Vector2 &p_offset) {
  146. ERR_FAIL_COND(!tile_map.has(p_id));
  147. tile_map[p_id].offset=p_offset;
  148. emit_changed();
  149. }
  150. Vector2 TileSet::tile_get_texture_offset(int p_id) const {
  151. ERR_FAIL_COND_V(!tile_map.has(p_id),Vector2());
  152. return tile_map[p_id].offset;
  153. }
  154. void TileSet::tile_set_shape_offset(int p_id,const Vector2 &p_offset) {
  155. ERR_FAIL_COND(!tile_map.has(p_id));
  156. tile_map[p_id].shape_offset=p_offset;
  157. emit_changed();
  158. }
  159. Vector2 TileSet::tile_get_shape_offset(int p_id) const {
  160. ERR_FAIL_COND_V(!tile_map.has(p_id),Vector2());
  161. return tile_map[p_id].shape_offset;
  162. }
  163. void TileSet::tile_set_region(int p_id,const Rect2 &p_region) {
  164. ERR_FAIL_COND(!tile_map.has(p_id));
  165. tile_map[p_id].region=p_region;
  166. emit_changed();
  167. }
  168. Rect2 TileSet::tile_get_region(int p_id) const {
  169. ERR_FAIL_COND_V(!tile_map.has(p_id),Rect2());
  170. return tile_map[p_id].region;
  171. }
  172. void TileSet::tile_set_name(int p_id,const String &p_name) {
  173. ERR_FAIL_COND(!tile_map.has(p_id));
  174. tile_map[p_id].name=p_name;
  175. emit_changed();
  176. }
  177. String TileSet::tile_get_name(int p_id) const {
  178. ERR_FAIL_COND_V(!tile_map.has(p_id),String());
  179. return tile_map[p_id].name;
  180. }
  181. void TileSet::tile_set_shape(int p_id,const Ref<Shape2D> &p_shape) {
  182. ERR_FAIL_COND(!tile_map.has(p_id));
  183. tile_map[p_id].shapes.resize(1);
  184. tile_map[p_id].shapes[0]=p_shape;
  185. emit_changed();
  186. }
  187. Ref<Shape2D> TileSet::tile_get_shape(int p_id) const {
  188. ERR_FAIL_COND_V(!tile_map.has(p_id),Ref<Shape2D>());
  189. if (tile_map[p_id].shapes.size()>0)
  190. return tile_map[p_id].shapes[0];
  191. return Ref<Shape2D>();
  192. }
  193. void TileSet::tile_set_light_occluder(int p_id,const Ref<OccluderPolygon2D> &p_light_occluder) {
  194. ERR_FAIL_COND(!tile_map.has(p_id));
  195. tile_map[p_id].occluder=p_light_occluder;
  196. }
  197. Ref<OccluderPolygon2D> TileSet::tile_get_light_occluder(int p_id) const{
  198. ERR_FAIL_COND_V(!tile_map.has(p_id),Ref<OccluderPolygon2D>());
  199. return tile_map[p_id].occluder;
  200. }
  201. void TileSet::tile_set_navigation_polygon_offset(int p_id,const Vector2& p_offset) {
  202. ERR_FAIL_COND(!tile_map.has(p_id));
  203. tile_map[p_id].navigation_polygon_offset=p_offset;
  204. }
  205. Vector2 TileSet::tile_get_navigation_polygon_offset(int p_id) const{
  206. ERR_FAIL_COND_V(!tile_map.has(p_id),Vector2());
  207. return tile_map[p_id].navigation_polygon_offset;
  208. }
  209. void TileSet::tile_set_navigation_polygon(int p_id,const Ref<NavigationPolygon> &p_navigation_polygon) {
  210. ERR_FAIL_COND(!tile_map.has(p_id));
  211. tile_map[p_id].navigation_polygon=p_navigation_polygon;
  212. }
  213. Ref<NavigationPolygon> TileSet::tile_get_navigation_polygon(int p_id) const {
  214. ERR_FAIL_COND_V(!tile_map.has(p_id),Ref<NavigationPolygon>());
  215. return tile_map[p_id].navigation_polygon;
  216. }
  217. void TileSet::tile_set_occluder_offset(int p_id,const Vector2& p_offset) {
  218. ERR_FAIL_COND(!tile_map.has(p_id));
  219. tile_map[p_id].occluder_offset=p_offset;
  220. }
  221. Vector2 TileSet::tile_get_occluder_offset(int p_id) const{
  222. ERR_FAIL_COND_V(!tile_map.has(p_id),Vector2());
  223. return tile_map[p_id].occluder_offset;
  224. }
  225. void TileSet::tile_set_shapes(int p_id,const Vector<Ref<Shape2D> > &p_shapes) {
  226. ERR_FAIL_COND(!tile_map.has(p_id));
  227. tile_map[p_id].shapes=p_shapes;
  228. emit_changed();
  229. }
  230. Vector<Ref<Shape2D> > TileSet::tile_get_shapes(int p_id) const {
  231. ERR_FAIL_COND_V(!tile_map.has(p_id),Vector<Ref<Shape2D> >());
  232. return tile_map[p_id].shapes;
  233. }
  234. void TileSet::_tile_set_shapes(int p_id,const Array& p_shapes) {
  235. ERR_FAIL_COND(!tile_map.has(p_id));
  236. Vector<Ref<Shape2D> > shapes;
  237. for(int i=0;i<p_shapes.size();i++) {
  238. Ref<Shape2D> s = p_shapes[i];
  239. if (s.is_valid())
  240. shapes.push_back(s);
  241. }
  242. tile_set_shapes(p_id,shapes);
  243. }
  244. Array TileSet::_tile_get_shapes(int p_id) const{
  245. ERR_FAIL_COND_V(!tile_map.has(p_id),Array());
  246. Array arr;
  247. Vector<Ref<Shape2D> >shp = tile_map[p_id].shapes;
  248. for(int i=0;i<shp.size();i++)
  249. arr.push_back(shp[i]);
  250. return arr;
  251. }
  252. Array TileSet::_get_tiles_ids() const{
  253. Array arr;
  254. for (Map<int, Data>::Element *E = tile_map.front(); E; E = E->next()) {
  255. arr.push_back(E->key());
  256. }
  257. return arr;
  258. }
  259. void TileSet::get_tile_list(List<int> *p_tiles) const {
  260. for(Map<int,Data>::Element *E=tile_map.front();E;E=E->next()) {
  261. p_tiles->push_back(E->key());
  262. }
  263. }
  264. bool TileSet::has_tile(int p_id) const {
  265. return tile_map.has(p_id);
  266. }
  267. void TileSet::remove_tile(int p_id) {
  268. ERR_FAIL_COND(!tile_map.has(p_id));
  269. tile_map.erase(p_id);
  270. _change_notify("");
  271. emit_changed();
  272. }
  273. int TileSet::get_last_unused_tile_id() const {
  274. if (tile_map.size())
  275. return tile_map.back()->key()+1;
  276. else
  277. return 0;
  278. }
  279. int TileSet::find_tile_by_name(const String& p_name) const {
  280. for(Map<int,Data>::Element *E=tile_map.front();E;E=E->next()) {
  281. if (p_name==E->get().name)
  282. return E->key();
  283. }
  284. return -1;
  285. }
  286. void TileSet::clear() {
  287. tile_map.clear();
  288. _change_notify("");
  289. emit_changed();
  290. }
  291. void TileSet::_bind_methods() {
  292. ObjectTypeDB::bind_method(_MD("create_tile","id"),&TileSet::create_tile);
  293. ObjectTypeDB::bind_method(_MD("tile_set_name","id","name"),&TileSet::tile_set_name);
  294. ObjectTypeDB::bind_method(_MD("tile_get_name","id"),&TileSet::tile_get_name);
  295. ObjectTypeDB::bind_method(_MD("tile_set_texture","id","texture:Texture"),&TileSet::tile_set_texture);
  296. ObjectTypeDB::bind_method(_MD("tile_get_texture:Texture","id"),&TileSet::tile_get_texture);
  297. ObjectTypeDB::bind_method(_MD("tile_set_material","id","material:CanvasItemMaterial"),&TileSet::tile_set_material);
  298. ObjectTypeDB::bind_method(_MD("tile_get_material:CanvasItemMaterial","id"),&TileSet::tile_get_material);
  299. ObjectTypeDB::bind_method(_MD("tile_set_texture_offset","id","texture_offset"),&TileSet::tile_set_texture_offset);
  300. ObjectTypeDB::bind_method(_MD("tile_get_texture_offset","id"),&TileSet::tile_get_texture_offset);
  301. ObjectTypeDB::bind_method(_MD("tile_set_shape_offset","id","shape_offset"),&TileSet::tile_set_shape_offset);
  302. ObjectTypeDB::bind_method(_MD("tile_get_shape_offset","id"),&TileSet::tile_get_shape_offset);
  303. ObjectTypeDB::bind_method(_MD("tile_set_region","id","region"),&TileSet::tile_set_region);
  304. ObjectTypeDB::bind_method(_MD("tile_get_region","id"),&TileSet::tile_get_region);
  305. ObjectTypeDB::bind_method(_MD("tile_set_shape","id","shape:Shape2D"),&TileSet::tile_set_shape);
  306. ObjectTypeDB::bind_method(_MD("tile_get_shape:Shape2D","id"),&TileSet::tile_get_shape);
  307. ObjectTypeDB::bind_method(_MD("tile_set_shapes","id","shapes"),&TileSet::_tile_set_shapes);
  308. ObjectTypeDB::bind_method(_MD("tile_get_shapes","id"),&TileSet::_tile_get_shapes);
  309. ObjectTypeDB::bind_method(_MD("tile_set_navigation_polygon","id","navigation_polygon:NavigationPolygon"),&TileSet::tile_set_navigation_polygon);
  310. ObjectTypeDB::bind_method(_MD("tile_get_navigation_polygon:NavigationPolygon","id"),&TileSet::tile_get_navigation_polygon);
  311. ObjectTypeDB::bind_method(_MD("tile_set_navigation_polygon_offset","id","navigation_polygon_offset"),&TileSet::tile_set_navigation_polygon_offset);
  312. ObjectTypeDB::bind_method(_MD("tile_get_navigation_polygon_offset","id"),&TileSet::tile_get_navigation_polygon_offset);
  313. ObjectTypeDB::bind_method(_MD("tile_set_light_occluder","id","light_occluder:OccluderPolygon2D"),&TileSet::tile_set_light_occluder);
  314. ObjectTypeDB::bind_method(_MD("tile_get_light_occluder:OccluderPolygon2D","id"),&TileSet::tile_get_light_occluder);
  315. ObjectTypeDB::bind_method(_MD("tile_set_occluder_offset","id","occluder_offset"),&TileSet::tile_set_occluder_offset);
  316. ObjectTypeDB::bind_method(_MD("tile_get_occluder_offset","id"),&TileSet::tile_get_occluder_offset);
  317. ObjectTypeDB::bind_method(_MD("remove_tile","id"),&TileSet::remove_tile);
  318. ObjectTypeDB::bind_method(_MD("clear"),&TileSet::clear);
  319. ObjectTypeDB::bind_method(_MD("get_last_unused_tile_id"),&TileSet::get_last_unused_tile_id);
  320. ObjectTypeDB::bind_method(_MD("find_tile_by_name","name"),&TileSet::find_tile_by_name);
  321. ObjectTypeDB::bind_method(_MD("get_tiles_ids", "name"), &TileSet::_get_tiles_ids);
  322. }
  323. TileSet::TileSet() {
  324. }