mesh_library.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*************************************************************************/
  2. /* mesh_library.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 "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. Vector<ShapeData> shapes;
  44. ShapeData sd;
  45. sd.shape = p_value;
  46. shapes.push_back(sd);
  47. set_item_shapes(idx, shapes);
  48. } else if (what == "shapes") {
  49. _set_item_shapes(idx, p_value);
  50. } else if (what == "preview")
  51. set_item_preview(idx, p_value);
  52. else if (what == "navmesh")
  53. set_item_navmesh(idx, p_value);
  54. else
  55. return false;
  56. return true;
  57. }
  58. return false;
  59. }
  60. bool MeshLibrary::_get(const StringName &p_name, Variant &r_ret) const {
  61. String name = p_name;
  62. int idx = name.get_slicec('/', 1).to_int();
  63. ERR_FAIL_COND_V(!item_map.has(idx), false);
  64. String what = name.get_slicec('/', 2);
  65. if (what == "name")
  66. r_ret = get_item_name(idx);
  67. else if (what == "mesh")
  68. r_ret = get_item_mesh(idx);
  69. else if (what == "shapes")
  70. r_ret = _get_item_shapes(idx);
  71. else if (what == "navmesh")
  72. r_ret = get_item_navmesh(idx);
  73. else if (what == "preview")
  74. r_ret = get_item_preview(idx);
  75. else
  76. return false;
  77. return true;
  78. }
  79. void MeshLibrary::_get_property_list(List<PropertyInfo> *p_list) const {
  80. for (Map<int, Item>::Element *E = item_map.front(); E; E = E->next()) {
  81. String name = "item/" + itos(E->key()) + "/";
  82. p_list->push_back(PropertyInfo(Variant::STRING, name + "name"));
  83. p_list->push_back(PropertyInfo(Variant::OBJECT, name + "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"));
  84. p_list->push_back(PropertyInfo(Variant::ARRAY, name + "shapes"));
  85. p_list->push_back(PropertyInfo(Variant::OBJECT, name + "navmesh", PROPERTY_HINT_RESOURCE_TYPE, "NavigationMesh"));
  86. p_list->push_back(PropertyInfo(Variant::OBJECT, name + "preview", PROPERTY_HINT_RESOURCE_TYPE, "Texture", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_HELPER));
  87. }
  88. }
  89. void MeshLibrary::create_item(int p_item) {
  90. ERR_FAIL_COND(p_item < 0);
  91. ERR_FAIL_COND(item_map.has(p_item));
  92. item_map[p_item] = Item();
  93. _change_notify();
  94. }
  95. void MeshLibrary::set_item_name(int p_item, const String &p_name) {
  96. ERR_FAIL_COND(!item_map.has(p_item));
  97. item_map[p_item].name = p_name;
  98. emit_changed();
  99. _change_notify();
  100. }
  101. void MeshLibrary::set_item_mesh(int p_item, const Ref<Mesh> &p_mesh) {
  102. ERR_FAIL_COND(!item_map.has(p_item));
  103. item_map[p_item].mesh = p_mesh;
  104. notify_change_to_owners();
  105. emit_changed();
  106. _change_notify();
  107. }
  108. void MeshLibrary::set_item_shapes(int p_item, const Vector<ShapeData> &p_shapes) {
  109. ERR_FAIL_COND(!item_map.has(p_item));
  110. item_map[p_item].shapes = p_shapes;
  111. _change_notify();
  112. notify_change_to_owners();
  113. emit_changed();
  114. _change_notify();
  115. }
  116. void MeshLibrary::set_item_navmesh(int p_item, const Ref<NavigationMesh> &p_navmesh) {
  117. ERR_FAIL_COND(!item_map.has(p_item));
  118. item_map[p_item].navmesh = p_navmesh;
  119. _change_notify();
  120. notify_change_to_owners();
  121. emit_changed();
  122. _change_notify();
  123. }
  124. void MeshLibrary::set_item_preview(int p_item, const Ref<Texture> &p_preview) {
  125. ERR_FAIL_COND(!item_map.has(p_item));
  126. item_map[p_item].preview = p_preview;
  127. emit_changed();
  128. _change_notify();
  129. }
  130. String MeshLibrary::get_item_name(int p_item) const {
  131. ERR_FAIL_COND_V(!item_map.has(p_item), "");
  132. return item_map[p_item].name;
  133. }
  134. Ref<Mesh> MeshLibrary::get_item_mesh(int p_item) const {
  135. ERR_FAIL_COND_V(!item_map.has(p_item), Ref<Mesh>());
  136. return item_map[p_item].mesh;
  137. }
  138. Vector<MeshLibrary::ShapeData> MeshLibrary::get_item_shapes(int p_item) const {
  139. ERR_FAIL_COND_V(!item_map.has(p_item), Vector<ShapeData>());
  140. return item_map[p_item].shapes;
  141. }
  142. Ref<NavigationMesh> MeshLibrary::get_item_navmesh(int p_item) const {
  143. ERR_FAIL_COND_V(!item_map.has(p_item), Ref<NavigationMesh>());
  144. return item_map[p_item].navmesh;
  145. }
  146. Ref<Texture> MeshLibrary::get_item_preview(int p_item) const {
  147. ERR_FAIL_COND_V(!item_map.has(p_item), Ref<Texture>());
  148. return item_map[p_item].preview;
  149. }
  150. bool MeshLibrary::has_item(int p_item) const {
  151. return item_map.has(p_item);
  152. }
  153. void MeshLibrary::remove_item(int p_item) {
  154. ERR_FAIL_COND(!item_map.has(p_item));
  155. item_map.erase(p_item);
  156. notify_change_to_owners();
  157. _change_notify();
  158. emit_changed();
  159. }
  160. void MeshLibrary::clear() {
  161. item_map.clear();
  162. notify_change_to_owners();
  163. _change_notify();
  164. emit_changed();
  165. }
  166. Vector<int> MeshLibrary::get_item_list() const {
  167. Vector<int> ret;
  168. ret.resize(item_map.size());
  169. int idx = 0;
  170. for (Map<int, Item>::Element *E = item_map.front(); E; E = E->next()) {
  171. ret.write[idx++] = E->key();
  172. }
  173. return ret;
  174. }
  175. int MeshLibrary::find_item_by_name(const String &p_name) const {
  176. for (Map<int, Item>::Element *E = item_map.front(); E; E = E->next()) {
  177. if (E->get().name == p_name)
  178. return E->key();
  179. }
  180. return -1;
  181. }
  182. int MeshLibrary::get_last_unused_item_id() const {
  183. if (!item_map.size())
  184. return 0;
  185. else
  186. return item_map.back()->key() + 1;
  187. }
  188. void MeshLibrary::_set_item_shapes(int p_item, const Array &p_shapes) {
  189. ERR_FAIL_COND(p_shapes.size() & 1);
  190. Vector<ShapeData> shapes;
  191. for (int i = 0; i < p_shapes.size(); i += 2) {
  192. ShapeData sd;
  193. sd.shape = p_shapes[i + 0];
  194. sd.local_transform = p_shapes[i + 1];
  195. if (sd.shape.is_valid()) {
  196. shapes.push_back(sd);
  197. }
  198. }
  199. set_item_shapes(p_item, shapes);
  200. }
  201. Array MeshLibrary::_get_item_shapes(int p_item) const {
  202. Vector<ShapeData> shapes = get_item_shapes(p_item);
  203. Array ret;
  204. for (int i = 0; i < shapes.size(); i++) {
  205. ret.push_back(shapes[i].shape);
  206. ret.push_back(shapes[i].local_transform);
  207. }
  208. return ret;
  209. }
  210. void MeshLibrary::_bind_methods() {
  211. ClassDB::bind_method(D_METHOD("create_item", "id"), &MeshLibrary::create_item);
  212. ClassDB::bind_method(D_METHOD("set_item_name", "id", "name"), &MeshLibrary::set_item_name);
  213. ClassDB::bind_method(D_METHOD("set_item_mesh", "id", "mesh"), &MeshLibrary::set_item_mesh);
  214. ClassDB::bind_method(D_METHOD("set_item_navmesh", "id", "navmesh"), &MeshLibrary::set_item_navmesh);
  215. ClassDB::bind_method(D_METHOD("set_item_shapes", "id", "shapes"), &MeshLibrary::_set_item_shapes);
  216. ClassDB::bind_method(D_METHOD("set_item_preview", "id", "texture"), &MeshLibrary::set_item_preview);
  217. ClassDB::bind_method(D_METHOD("get_item_name", "id"), &MeshLibrary::get_item_name);
  218. ClassDB::bind_method(D_METHOD("get_item_mesh", "id"), &MeshLibrary::get_item_mesh);
  219. ClassDB::bind_method(D_METHOD("get_item_navmesh", "id"), &MeshLibrary::get_item_navmesh);
  220. ClassDB::bind_method(D_METHOD("get_item_shapes", "id"), &MeshLibrary::_get_item_shapes);
  221. ClassDB::bind_method(D_METHOD("get_item_preview", "id"), &MeshLibrary::get_item_preview);
  222. ClassDB::bind_method(D_METHOD("remove_item", "id"), &MeshLibrary::remove_item);
  223. ClassDB::bind_method(D_METHOD("find_item_by_name", "name"), &MeshLibrary::find_item_by_name);
  224. ClassDB::bind_method(D_METHOD("clear"), &MeshLibrary::clear);
  225. ClassDB::bind_method(D_METHOD("get_item_list"), &MeshLibrary::get_item_list);
  226. ClassDB::bind_method(D_METHOD("get_last_unused_item_id"), &MeshLibrary::get_last_unused_item_id);
  227. }
  228. MeshLibrary::MeshLibrary() {
  229. }
  230. MeshLibrary::~MeshLibrary() {
  231. }