mesh_library.cpp 8.2 KB

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