tile_set.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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-2014 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=="shape_offset")
  46. tile_set_shape_offset(id,p_value);
  47. else if (what=="region")
  48. tile_set_region(id,p_value);
  49. else if (what=="shape")
  50. tile_set_shape(id,p_value);
  51. else if (what=="shapes")
  52. _tile_set_shapes(id,p_value);
  53. else
  54. return false;
  55. return true;
  56. }
  57. bool TileSet::_get(const StringName& p_name,Variant &r_ret) const{
  58. String n = p_name;
  59. int slash = n.find("/");
  60. if (slash==-1)
  61. return false;
  62. int id = String::to_int(n.c_str(),slash);
  63. ERR_FAIL_COND_V(!tile_map.has(id),false);
  64. String what = n.substr(slash+1,n.length());
  65. if (what=="name")
  66. r_ret=tile_get_name(id);
  67. else if (what=="texture")
  68. r_ret=tile_get_texture(id);
  69. else if (what=="tex_offset")
  70. r_ret=tile_get_texture_offset(id);
  71. else if (what=="shape_offset")
  72. r_ret=tile_get_shape_offset(id);
  73. else if (what=="region")
  74. r_ret=tile_get_region(id);
  75. else if (what=="shape")
  76. r_ret=tile_get_shape(id);
  77. else if (what=="shapes")
  78. r_ret=_tile_get_shapes(id);
  79. else
  80. return false;
  81. return true;
  82. }
  83. void TileSet::_get_property_list( List<PropertyInfo> *p_list) const{
  84. for(Map<int,Data>::Element *E=tile_map.front();E;E=E->next()) {
  85. int id = E->key();
  86. String pre = itos(id)+"/";
  87. p_list->push_back(PropertyInfo(Variant::STRING,pre+"name"));
  88. p_list->push_back(PropertyInfo(Variant::OBJECT,pre+"texture",PROPERTY_HINT_RESOURCE_TYPE,"Texture"));
  89. p_list->push_back(PropertyInfo(Variant::VECTOR2,pre+"tex_offset"));
  90. p_list->push_back(PropertyInfo(Variant::VECTOR2,pre+"shape_offset"));
  91. p_list->push_back(PropertyInfo(Variant::RECT2,pre+"region"));
  92. p_list->push_back(PropertyInfo(Variant::OBJECT,pre+"shape",PROPERTY_HINT_RESOURCE_TYPE,"Shape2D",PROPERTY_USAGE_EDITOR));
  93. p_list->push_back(PropertyInfo(Variant::ARRAY,pre+"shapes",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR));
  94. }
  95. }
  96. void TileSet::create_tile(int p_id) {
  97. ERR_FAIL_COND( tile_map.has(p_id) );
  98. tile_map[p_id]=Data();
  99. _change_notify("");
  100. emit_changed();
  101. }
  102. void TileSet::tile_set_texture(int p_id,const Ref<Texture> &p_texture) {
  103. ERR_FAIL_COND(!tile_map.has(p_id));
  104. tile_map[p_id].texture=p_texture;
  105. emit_changed();
  106. }
  107. Ref<Texture> TileSet::tile_get_texture(int p_id) const {
  108. ERR_FAIL_COND_V(!tile_map.has(p_id),Ref<Texture>());
  109. return tile_map[p_id].texture;
  110. }
  111. void TileSet::tile_set_texture_offset(int p_id,const Vector2 &p_offset) {
  112. ERR_FAIL_COND(!tile_map.has(p_id));
  113. tile_map[p_id].offset=p_offset;
  114. emit_changed();
  115. }
  116. Vector2 TileSet::tile_get_texture_offset(int p_id) const {
  117. ERR_FAIL_COND_V(!tile_map.has(p_id),Vector2());
  118. return tile_map[p_id].offset;
  119. }
  120. void TileSet::tile_set_shape_offset(int p_id,const Vector2 &p_offset) {
  121. ERR_FAIL_COND(!tile_map.has(p_id));
  122. tile_map[p_id].shape_offset=p_offset;
  123. emit_changed();
  124. }
  125. Vector2 TileSet::tile_get_shape_offset(int p_id) const {
  126. ERR_FAIL_COND_V(!tile_map.has(p_id),Vector2());
  127. return tile_map[p_id].shape_offset;
  128. }
  129. void TileSet::tile_set_region(int p_id,const Rect2 &p_region) {
  130. ERR_FAIL_COND(!tile_map.has(p_id));
  131. tile_map[p_id].region=p_region;
  132. emit_changed();
  133. }
  134. Rect2 TileSet::tile_get_region(int p_id) const {
  135. ERR_FAIL_COND_V(!tile_map.has(p_id),Rect2());
  136. return tile_map[p_id].region;
  137. }
  138. void TileSet::tile_set_name(int p_id,const String &p_name) {
  139. ERR_FAIL_COND(!tile_map.has(p_id));
  140. tile_map[p_id].name=p_name;
  141. emit_changed();
  142. }
  143. String TileSet::tile_get_name(int p_id) const {
  144. ERR_FAIL_COND_V(!tile_map.has(p_id),String());
  145. return tile_map[p_id].name;
  146. }
  147. void TileSet::tile_set_shape(int p_id,const Ref<Shape2D> &p_shape) {
  148. ERR_FAIL_COND(!tile_map.has(p_id));
  149. tile_map[p_id].shapes.resize(1);
  150. tile_map[p_id].shapes[0]=p_shape;
  151. emit_changed();
  152. }
  153. Ref<Shape2D> TileSet::tile_get_shape(int p_id) const {
  154. ERR_FAIL_COND_V(!tile_map.has(p_id),Ref<Shape2D>());
  155. if (tile_map[p_id].shapes.size()>0)
  156. return tile_map[p_id].shapes[0];
  157. return Ref<Shape2D>();
  158. }
  159. void TileSet::tile_set_shapes(int p_id,const Vector<Ref<Shape2D> > &p_shapes) {
  160. ERR_FAIL_COND(!tile_map.has(p_id));
  161. tile_map[p_id].shapes=p_shapes;
  162. emit_changed();
  163. }
  164. Vector<Ref<Shape2D> > TileSet::tile_get_shapes(int p_id) const {
  165. ERR_FAIL_COND_V(!tile_map.has(p_id),Vector<Ref<Shape2D> >());
  166. return tile_map[p_id].shapes;
  167. }
  168. void TileSet::_tile_set_shapes(int p_id,const Array& p_shapes) {
  169. ERR_FAIL_COND(!tile_map.has(p_id));
  170. Vector<Ref<Shape2D> > shapes;
  171. for(int i=0;i<p_shapes.size();i++) {
  172. Ref<Shape2D> s = p_shapes[i];
  173. if (s.is_valid())
  174. shapes.push_back(s);
  175. }
  176. tile_set_shapes(p_id,shapes);
  177. }
  178. Array TileSet::_tile_get_shapes(int p_id) const{
  179. ERR_FAIL_COND_V(!tile_map.has(p_id),Array());
  180. Array arr;
  181. Vector<Ref<Shape2D> >shp = tile_map[p_id].shapes;
  182. for(int i=0;i<shp.size();i++)
  183. arr.push_back(shp[i]);
  184. return arr;
  185. }
  186. void TileSet::get_tile_list(List<int> *p_tiles) const {
  187. for(Map<int,Data>::Element *E=tile_map.front();E;E=E->next()) {
  188. p_tiles->push_back(E->key());
  189. }
  190. }
  191. bool TileSet::has_tile(int p_id) const {
  192. return tile_map.has(p_id);
  193. }
  194. void TileSet::remove_tile(int p_id) {
  195. ERR_FAIL_COND(!tile_map.has(p_id));
  196. tile_map.erase(p_id);
  197. _change_notify("");
  198. emit_changed();
  199. }
  200. int TileSet::get_last_unused_tile_id() const {
  201. if (tile_map.size())
  202. return tile_map.back()->key()+1;
  203. else
  204. return 0;
  205. }
  206. int TileSet::find_tile_by_name(const String& p_name) const {
  207. for(Map<int,Data>::Element *E=tile_map.front();E;E=E->next()) {
  208. if (p_name==E->get().name)
  209. return E->key();
  210. }
  211. return -1;
  212. }
  213. void TileSet::clear() {
  214. tile_map.clear();
  215. _change_notify("");
  216. emit_changed();
  217. }
  218. void TileSet::_bind_methods() {
  219. ObjectTypeDB::bind_method(_MD("create_tile","id"),&TileSet::create_tile);
  220. ObjectTypeDB::bind_method(_MD("tile_set_name","id","name"),&TileSet::tile_set_name);
  221. ObjectTypeDB::bind_method(_MD("tile_get_name","id"),&TileSet::tile_get_name);
  222. ObjectTypeDB::bind_method(_MD("tile_set_texture","id","texture:Texture"),&TileSet::tile_set_texture);
  223. ObjectTypeDB::bind_method(_MD("tile_get_texture:Texture","id"),&TileSet::tile_get_texture);
  224. ObjectTypeDB::bind_method(_MD("tile_set_texture_offset","id","texture_offset"),&TileSet::tile_set_texture_offset);
  225. ObjectTypeDB::bind_method(_MD("tile_get_texture_offset","id"),&TileSet::tile_get_texture_offset);
  226. ObjectTypeDB::bind_method(_MD("tile_set_shape_offset","id","shape_offset"),&TileSet::tile_set_shape_offset);
  227. ObjectTypeDB::bind_method(_MD("tile_get_shape_offset","id"),&TileSet::tile_get_shape_offset);
  228. ObjectTypeDB::bind_method(_MD("tile_set_region","id","region"),&TileSet::tile_set_region);
  229. ObjectTypeDB::bind_method(_MD("tile_get_region","id"),&TileSet::tile_get_region);
  230. ObjectTypeDB::bind_method(_MD("tile_set_shape","id","shape:Shape2D"),&TileSet::tile_set_shape);
  231. ObjectTypeDB::bind_method(_MD("tile_get_shape:Shape2D","id"),&TileSet::tile_get_shape);
  232. ObjectTypeDB::bind_method(_MD("tile_set_shapes","id","shapes"),&TileSet::_tile_set_shapes);
  233. ObjectTypeDB::bind_method(_MD("tile_get_shapes","id"),&TileSet::_tile_get_shapes);
  234. ObjectTypeDB::bind_method(_MD("remove_tile","id"),&TileSet::remove_tile);
  235. ObjectTypeDB::bind_method(_MD("clear"),&TileSet::clear);
  236. ObjectTypeDB::bind_method(_MD("get_last_unused_tile_id"),&TileSet::get_last_unused_tile_id);
  237. ObjectTypeDB::bind_method(_MD("find_tile_by_name","name"),&TileSet::find_tile_by_name);
  238. }
  239. TileSet::TileSet() {
  240. }