mesh_library.cpp 15 KB

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