mesh_library.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*************************************************************************/
  2. /* mesh_library.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 "mesh_library.h"
  30. bool MeshLibrary::_set(const StringName& p_name, const Variant& p_value) {
  31. String name=p_name;
  32. if (name.begins_with("item/")) {
  33. int idx = name.get_slice("/",1).to_int();
  34. String what = name.get_slice("/",2);
  35. if (!item_map.has(idx))
  36. create_item(idx);
  37. if(what=="name")
  38. set_item_name(idx,p_value);
  39. else if(what=="mesh")
  40. set_item_mesh(idx,p_value);
  41. else if(what=="shape")
  42. set_item_shape(idx,p_value);
  43. else if(what=="preview")
  44. set_item_preview(idx,p_value);
  45. else
  46. return false;
  47. return true;
  48. }
  49. return false;
  50. }
  51. bool MeshLibrary::_get(const StringName& p_name,Variant &r_ret) const {
  52. String name=p_name;
  53. int idx = name.get_slice("/",1).to_int();
  54. ERR_FAIL_COND_V(!item_map.has(idx),false);
  55. String what = name.get_slice("/",2);
  56. if(what=="name")
  57. r_ret= get_item_name(idx);
  58. else if(what=="mesh")
  59. r_ret= get_item_mesh(idx);
  60. else if(what=="shape")
  61. r_ret= get_item_shape(idx);
  62. else if(what=="preview")
  63. r_ret= get_item_preview(idx);
  64. else
  65. return false;
  66. return true;
  67. }
  68. void MeshLibrary::_get_property_list( List<PropertyInfo> *p_list) const {
  69. for(Map<int,Item>::Element *E=item_map.front();E;E=E->next()) {
  70. String name="item/"+itos(E->key())+"/";
  71. p_list->push_back( PropertyInfo(Variant::STRING,name+"name"));
  72. p_list->push_back( PropertyInfo(Variant::OBJECT,name+"mesh",PROPERTY_HINT_RESOURCE_TYPE,"Mesh"));
  73. p_list->push_back( PropertyInfo(Variant::OBJECT,name+"shape",PROPERTY_HINT_RESOURCE_TYPE,"Shape"));
  74. p_list->push_back( PropertyInfo(Variant::OBJECT,name+"preview",PROPERTY_HINT_RESOURCE_TYPE,"Texture",PROPERTY_USAGE_DEFAULT|PROPERTY_USAGE_EDITOR_HELPER));
  75. }
  76. }
  77. void MeshLibrary::create_item(int p_item) {
  78. ERR_FAIL_COND(p_item<0);
  79. ERR_FAIL_COND(item_map.has(p_item));
  80. item_map[p_item]=Item();
  81. _change_notify();
  82. }
  83. void MeshLibrary::set_item_name(int p_item,const String& p_name) {
  84. ERR_FAIL_COND(!item_map.has(p_item));
  85. item_map[p_item].name=p_name;
  86. emit_changed();
  87. _change_notify();
  88. }
  89. void MeshLibrary::set_item_mesh(int p_item,const Ref<Mesh>& p_mesh) {
  90. ERR_FAIL_COND(!item_map.has(p_item));
  91. item_map[p_item].mesh=p_mesh;
  92. notify_change_to_owners();
  93. emit_changed();
  94. _change_notify();
  95. }
  96. void MeshLibrary::set_item_shape(int p_item,const Ref<Shape>& p_shape) {
  97. ERR_FAIL_COND(!item_map.has(p_item));
  98. item_map[p_item].shape=p_shape;
  99. _change_notify();
  100. notify_change_to_owners();
  101. emit_changed();
  102. _change_notify();
  103. }
  104. void MeshLibrary::set_item_preview(int p_item,const Ref<Texture>& p_preview) {
  105. ERR_FAIL_COND(!item_map.has(p_item));
  106. item_map[p_item].preview=p_preview;
  107. emit_changed();
  108. _change_notify();
  109. }
  110. String MeshLibrary::get_item_name(int p_item) const {
  111. ERR_FAIL_COND_V(!item_map.has(p_item),"");
  112. return item_map[p_item].name;
  113. }
  114. Ref<Mesh> MeshLibrary::get_item_mesh(int p_item) const {
  115. ERR_FAIL_COND_V(!item_map.has(p_item),Ref<Mesh>());
  116. return item_map[p_item].mesh;
  117. }
  118. Ref<Shape> MeshLibrary::get_item_shape(int p_item) const {
  119. ERR_FAIL_COND_V(!item_map.has(p_item),Ref<Shape>());
  120. return item_map[p_item].shape;
  121. }
  122. Ref<Texture> MeshLibrary::get_item_preview(int p_item) const {
  123. ERR_FAIL_COND_V(!item_map.has(p_item),Ref<Texture>());
  124. return item_map[p_item].preview;
  125. }
  126. bool MeshLibrary::has_item(int p_item) const {
  127. return item_map.has(p_item) ;
  128. }
  129. void MeshLibrary::remove_item(int p_item) {
  130. ERR_FAIL_COND(!item_map.has(p_item));
  131. item_map.erase(p_item);
  132. notify_change_to_owners();
  133. _change_notify();
  134. emit_changed();
  135. }
  136. void MeshLibrary::clear() {
  137. item_map.clear();
  138. notify_change_to_owners();
  139. _change_notify();
  140. emit_changed();
  141. }
  142. Vector<int> MeshLibrary::get_item_list() const {
  143. Vector<int> ret;
  144. ret.resize(item_map.size());
  145. int idx=0;
  146. for(Map<int,Item>::Element *E=item_map.front();E;E=E->next()) {
  147. ret[idx++]=E->key();
  148. }
  149. return ret;
  150. }
  151. int MeshLibrary::find_item_name(const String& p_name) const {
  152. for(Map<int,Item>::Element *E=item_map.front();E;E=E->next()) {
  153. if (E->get().name==p_name)
  154. return E->key();
  155. }
  156. return -1;
  157. }
  158. int MeshLibrary::get_last_unused_item_id() const {
  159. if (!item_map.size())
  160. return 0;
  161. else
  162. return item_map.back()->key()+1;
  163. }
  164. void MeshLibrary::_bind_methods() {
  165. ObjectTypeDB::bind_method(_MD("create_item","id"),&MeshLibrary::create_item);
  166. ObjectTypeDB::bind_method(_MD("set_item_name","id","name"),&MeshLibrary::set_item_name);
  167. ObjectTypeDB::bind_method(_MD("set_item_mesh","id","mesh:Mesh"),&MeshLibrary::set_item_mesh);
  168. ObjectTypeDB::bind_method(_MD("set_item_shape","id","shape:Shape"),&MeshLibrary::set_item_shape);
  169. ObjectTypeDB::bind_method(_MD("get_item_name","id"),&MeshLibrary::get_item_name);
  170. ObjectTypeDB::bind_method(_MD("get_item_mesh:Mesh","id"),&MeshLibrary::get_item_mesh);
  171. ObjectTypeDB::bind_method(_MD("get_item_shape:Shape","id"),&MeshLibrary::get_item_shape);
  172. ObjectTypeDB::bind_method(_MD("remove_item","id"),&MeshLibrary::remove_item);
  173. ObjectTypeDB::bind_method(_MD("clear"),&MeshLibrary::clear);
  174. ObjectTypeDB::bind_method(_MD("get_item_list"),&MeshLibrary::get_item_list);
  175. ObjectTypeDB::bind_method(_MD("get_last_unused_item_id"),&MeshLibrary::get_last_unused_item_id);
  176. }
  177. MeshLibrary::MeshLibrary() {
  178. }
  179. MeshLibrary::~MeshLibrary() {
  180. }