mesh_library.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*************************************************************************/
  2. /* mesh_library.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. #include "box_shape_3d.h"
  32. bool MeshLibrary::_set(const StringName &p_name, const Variant &p_value) {
  33. String name = p_name;
  34. if (name.begins_with("item/")) {
  35. int idx = name.get_slicec('/', 1).to_int();
  36. String what = name.get_slicec('/', 2);
  37. if (!item_map.has(idx)) {
  38. create_item(idx);
  39. }
  40. if (what == "name") {
  41. set_item_name(idx, p_value);
  42. } else if (what == "mesh") {
  43. set_item_mesh(idx, p_value);
  44. } else if (what == "mesh_transform") {
  45. set_item_mesh_transform(idx, p_value);
  46. } else if (what == "shape") {
  47. Vector<ShapeData> shapes;
  48. ShapeData sd;
  49. sd.shape = p_value;
  50. shapes.push_back(sd);
  51. set_item_shapes(idx, shapes);
  52. } else if (what == "shapes") {
  53. _set_item_shapes(idx, p_value);
  54. } else if (what == "preview") {
  55. set_item_preview(idx, p_value);
  56. } else if (what == "navmesh") {
  57. set_item_navmesh(idx, p_value);
  58. } else if (what == "navmesh_transform") {
  59. set_item_navmesh_transform(idx, p_value);
  60. } else {
  61. return false;
  62. }
  63. return true;
  64. }
  65. return false;
  66. }
  67. bool MeshLibrary::_get(const StringName &p_name, Variant &r_ret) const {
  68. String name = p_name;
  69. int idx = name.get_slicec('/', 1).to_int();
  70. ERR_FAIL_COND_V(!item_map.has(idx), false);
  71. String what = name.get_slicec('/', 2);
  72. if (what == "name") {
  73. r_ret = get_item_name(idx);
  74. } else if (what == "mesh") {
  75. r_ret = get_item_mesh(idx);
  76. } else if (what == "mesh_transform") {
  77. r_ret = get_item_mesh_transform(idx);
  78. } else if (what == "shapes") {
  79. r_ret = _get_item_shapes(idx);
  80. } else if (what == "navmesh") {
  81. r_ret = get_item_navmesh(idx);
  82. } else if (what == "navmesh_transform") {
  83. r_ret = get_item_navmesh_transform(idx);
  84. } else if (what == "preview") {
  85. r_ret = get_item_preview(idx);
  86. } else {
  87. return false;
  88. }
  89. return true;
  90. }
  91. void MeshLibrary::_get_property_list(List<PropertyInfo> *p_list) const {
  92. for (const KeyValue<int, Item> &E : item_map) {
  93. String name = vformat("%s/%d/", PNAME("item"), E.key);
  94. p_list->push_back(PropertyInfo(Variant::STRING, name + PNAME("name")));
  95. p_list->push_back(PropertyInfo(Variant::OBJECT, name + PNAME("mesh"), PROPERTY_HINT_RESOURCE_TYPE, "Mesh"));
  96. p_list->push_back(PropertyInfo(Variant::TRANSFORM3D, name + PNAME("mesh_transform"), PROPERTY_HINT_NONE, "suffix:m"));
  97. p_list->push_back(PropertyInfo(Variant::ARRAY, name + PNAME("shapes")));
  98. p_list->push_back(PropertyInfo(Variant::OBJECT, name + PNAME("navmesh"), PROPERTY_HINT_RESOURCE_TYPE, "NavigationMesh"));
  99. p_list->push_back(PropertyInfo(Variant::TRANSFORM3D, name + PNAME("navmesh_transform"), PROPERTY_HINT_NONE, "suffix:m"));
  100. p_list->push_back(PropertyInfo(Variant::OBJECT, name + PNAME("preview"), PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_DEFAULT));
  101. }
  102. }
  103. void MeshLibrary::create_item(int p_item) {
  104. ERR_FAIL_COND(p_item < 0);
  105. ERR_FAIL_COND(item_map.has(p_item));
  106. item_map[p_item] = Item();
  107. notify_property_list_changed();
  108. }
  109. void MeshLibrary::set_item_name(int p_item, const String &p_name) {
  110. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  111. item_map[p_item].name = p_name;
  112. emit_changed();
  113. notify_property_list_changed();
  114. }
  115. void MeshLibrary::set_item_mesh(int p_item, const Ref<Mesh> &p_mesh) {
  116. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  117. item_map[p_item].mesh = p_mesh;
  118. notify_change_to_owners();
  119. emit_changed();
  120. notify_property_list_changed();
  121. }
  122. void MeshLibrary::set_item_mesh_transform(int p_item, const Transform3D &p_transform) {
  123. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  124. item_map[p_item].mesh_transform = p_transform;
  125. notify_change_to_owners();
  126. emit_changed();
  127. notify_property_list_changed();
  128. }
  129. void MeshLibrary::set_item_shapes(int p_item, const Vector<ShapeData> &p_shapes) {
  130. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  131. item_map[p_item].shapes = p_shapes;
  132. notify_property_list_changed();
  133. notify_change_to_owners();
  134. emit_changed();
  135. notify_property_list_changed();
  136. }
  137. void MeshLibrary::set_item_navmesh(int p_item, const Ref<NavigationMesh> &p_navmesh) {
  138. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  139. item_map[p_item].navmesh = p_navmesh;
  140. notify_property_list_changed();
  141. notify_change_to_owners();
  142. emit_changed();
  143. notify_property_list_changed();
  144. }
  145. void MeshLibrary::set_item_navmesh_transform(int p_item, const Transform3D &p_transform) {
  146. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  147. item_map[p_item].navmesh_transform = p_transform;
  148. notify_change_to_owners();
  149. emit_changed();
  150. notify_property_list_changed();
  151. }
  152. void MeshLibrary::set_item_preview(int p_item, const Ref<Texture2D> &p_preview) {
  153. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  154. item_map[p_item].preview = p_preview;
  155. emit_changed();
  156. notify_property_list_changed();
  157. }
  158. String MeshLibrary::get_item_name(int p_item) const {
  159. ERR_FAIL_COND_V_MSG(!item_map.has(p_item), "", "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  160. return item_map[p_item].name;
  161. }
  162. Ref<Mesh> MeshLibrary::get_item_mesh(int p_item) const {
  163. ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Ref<Mesh>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  164. return item_map[p_item].mesh;
  165. }
  166. Transform3D MeshLibrary::get_item_mesh_transform(int p_item) const {
  167. ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Transform3D(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  168. return item_map[p_item].mesh_transform;
  169. }
  170. Vector<MeshLibrary::ShapeData> MeshLibrary::get_item_shapes(int p_item) const {
  171. ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Vector<ShapeData>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  172. return item_map[p_item].shapes;
  173. }
  174. Ref<NavigationMesh> MeshLibrary::get_item_navmesh(int p_item) const {
  175. ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Ref<NavigationMesh>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  176. return item_map[p_item].navmesh;
  177. }
  178. Transform3D MeshLibrary::get_item_navmesh_transform(int p_item) const {
  179. ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Transform3D(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  180. return item_map[p_item].navmesh_transform;
  181. }
  182. Ref<Texture2D> MeshLibrary::get_item_preview(int p_item) const {
  183. ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Ref<Texture2D>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  184. return item_map[p_item].preview;
  185. }
  186. bool MeshLibrary::has_item(int p_item) const {
  187. return item_map.has(p_item);
  188. }
  189. void MeshLibrary::remove_item(int p_item) {
  190. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  191. item_map.erase(p_item);
  192. notify_change_to_owners();
  193. notify_property_list_changed();
  194. emit_changed();
  195. }
  196. void MeshLibrary::clear() {
  197. item_map.clear();
  198. notify_change_to_owners();
  199. notify_property_list_changed();
  200. emit_changed();
  201. }
  202. Vector<int> MeshLibrary::get_item_list() const {
  203. Vector<int> ret;
  204. ret.resize(item_map.size());
  205. int idx = 0;
  206. for (const KeyValue<int, Item> &E : item_map) {
  207. ret.write[idx++] = E.key;
  208. }
  209. return ret;
  210. }
  211. int MeshLibrary::find_item_by_name(const String &p_name) const {
  212. for (const KeyValue<int, Item> &E : item_map) {
  213. if (E.value.name == p_name) {
  214. return E.key;
  215. }
  216. }
  217. return -1;
  218. }
  219. int MeshLibrary::get_last_unused_item_id() const {
  220. if (!item_map.size()) {
  221. return 0;
  222. } else {
  223. return item_map.back()->key() + 1;
  224. }
  225. }
  226. void MeshLibrary::_set_item_shapes(int p_item, const Array &p_shapes) {
  227. Array arr_shapes = p_shapes;
  228. int size = p_shapes.size();
  229. if (size & 1) {
  230. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  231. int prev_size = item_map[p_item].shapes.size() * 2;
  232. if (prev_size < size) {
  233. // Check if last element is a shape.
  234. Ref<Shape3D> shape = arr_shapes[size - 1];
  235. if (shape.is_null()) {
  236. Ref<BoxShape3D> box_shape;
  237. box_shape.instantiate();
  238. arr_shapes[size - 1] = box_shape;
  239. }
  240. // Make sure the added element is a Transform3D.
  241. arr_shapes.push_back(Transform3D());
  242. size++;
  243. } else {
  244. size--;
  245. arr_shapes.resize(size);
  246. }
  247. }
  248. Vector<ShapeData> shapes;
  249. for (int i = 0; i < size; i += 2) {
  250. ShapeData sd;
  251. sd.shape = arr_shapes[i + 0];
  252. sd.local_transform = arr_shapes[i + 1];
  253. if (sd.shape.is_valid()) {
  254. shapes.push_back(sd);
  255. }
  256. }
  257. set_item_shapes(p_item, shapes);
  258. }
  259. Array MeshLibrary::_get_item_shapes(int p_item) const {
  260. Vector<ShapeData> shapes = get_item_shapes(p_item);
  261. Array ret;
  262. for (int i = 0; i < shapes.size(); i++) {
  263. ret.push_back(shapes[i].shape);
  264. ret.push_back(shapes[i].local_transform);
  265. }
  266. return ret;
  267. }
  268. void MeshLibrary::reset_state() {
  269. clear();
  270. }
  271. void MeshLibrary::_bind_methods() {
  272. ClassDB::bind_method(D_METHOD("create_item", "id"), &MeshLibrary::create_item);
  273. ClassDB::bind_method(D_METHOD("set_item_name", "id", "name"), &MeshLibrary::set_item_name);
  274. ClassDB::bind_method(D_METHOD("set_item_mesh", "id", "mesh"), &MeshLibrary::set_item_mesh);
  275. ClassDB::bind_method(D_METHOD("set_item_mesh_transform", "id", "mesh_transform"), &MeshLibrary::set_item_mesh_transform);
  276. ClassDB::bind_method(D_METHOD("set_item_navmesh", "id", "navmesh"), &MeshLibrary::set_item_navmesh);
  277. ClassDB::bind_method(D_METHOD("set_item_navmesh_transform", "id", "navmesh"), &MeshLibrary::set_item_navmesh_transform);
  278. ClassDB::bind_method(D_METHOD("set_item_shapes", "id", "shapes"), &MeshLibrary::_set_item_shapes);
  279. ClassDB::bind_method(D_METHOD("set_item_preview", "id", "texture"), &MeshLibrary::set_item_preview);
  280. ClassDB::bind_method(D_METHOD("get_item_name", "id"), &MeshLibrary::get_item_name);
  281. ClassDB::bind_method(D_METHOD("get_item_mesh", "id"), &MeshLibrary::get_item_mesh);
  282. ClassDB::bind_method(D_METHOD("get_item_mesh_transform", "id"), &MeshLibrary::get_item_mesh_transform);
  283. ClassDB::bind_method(D_METHOD("get_item_navmesh", "id"), &MeshLibrary::get_item_navmesh);
  284. ClassDB::bind_method(D_METHOD("get_item_navmesh_transform", "id"), &MeshLibrary::get_item_navmesh_transform);
  285. ClassDB::bind_method(D_METHOD("get_item_shapes", "id"), &MeshLibrary::_get_item_shapes);
  286. ClassDB::bind_method(D_METHOD("get_item_preview", "id"), &MeshLibrary::get_item_preview);
  287. ClassDB::bind_method(D_METHOD("remove_item", "id"), &MeshLibrary::remove_item);
  288. ClassDB::bind_method(D_METHOD("find_item_by_name", "name"), &MeshLibrary::find_item_by_name);
  289. ClassDB::bind_method(D_METHOD("clear"), &MeshLibrary::clear);
  290. ClassDB::bind_method(D_METHOD("get_item_list"), &MeshLibrary::get_item_list);
  291. ClassDB::bind_method(D_METHOD("get_last_unused_item_id"), &MeshLibrary::get_last_unused_item_id);
  292. }
  293. MeshLibrary::MeshLibrary() {
  294. }
  295. MeshLibrary::~MeshLibrary() {
  296. }