mesh_library.cpp 11 KB

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