mesh_library.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. emit_changed();
  125. notify_property_list_changed();
  126. }
  127. void MeshLibrary::set_item_name(int p_item, const String &p_name) {
  128. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  129. item_map[p_item].name = p_name;
  130. emit_changed();
  131. }
  132. void MeshLibrary::set_item_mesh(int p_item, const Ref<Mesh> &p_mesh) {
  133. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  134. item_map[p_item].mesh = p_mesh;
  135. emit_changed();
  136. }
  137. void MeshLibrary::set_item_mesh_transform(int p_item, const Transform3D &p_transform) {
  138. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  139. item_map[p_item].mesh_transform = p_transform;
  140. emit_changed();
  141. }
  142. void MeshLibrary::set_item_shapes(int p_item, const Vector<ShapeData> &p_shapes) {
  143. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  144. item_map[p_item].shapes = p_shapes;
  145. emit_changed();
  146. notify_property_list_changed();
  147. }
  148. void MeshLibrary::set_item_navigation_mesh(int p_item, const Ref<NavigationMesh> &p_navigation_mesh) {
  149. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  150. item_map[p_item].navigation_mesh = p_navigation_mesh;
  151. emit_changed();
  152. }
  153. void MeshLibrary::set_item_navigation_mesh_transform(int p_item, const Transform3D &p_transform) {
  154. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  155. item_map[p_item].navigation_mesh_transform = p_transform;
  156. emit_changed();
  157. }
  158. void MeshLibrary::set_item_navigation_layers(int p_item, uint32_t p_navigation_layers) {
  159. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  160. item_map[p_item].navigation_layers = p_navigation_layers;
  161. emit_changed();
  162. }
  163. void MeshLibrary::set_item_preview(int p_item, const Ref<Texture2D> &p_preview) {
  164. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  165. item_map[p_item].preview = p_preview;
  166. emit_changed();
  167. }
  168. String MeshLibrary::get_item_name(int p_item) const {
  169. ERR_FAIL_COND_V_MSG(!item_map.has(p_item), "", "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  170. return item_map[p_item].name;
  171. }
  172. Ref<Mesh> MeshLibrary::get_item_mesh(int p_item) const {
  173. ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Ref<Mesh>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  174. return item_map[p_item].mesh;
  175. }
  176. Transform3D MeshLibrary::get_item_mesh_transform(int p_item) const {
  177. ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Transform3D(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  178. return item_map[p_item].mesh_transform;
  179. }
  180. Vector<MeshLibrary::ShapeData> MeshLibrary::get_item_shapes(int p_item) const {
  181. ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Vector<ShapeData>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  182. return item_map[p_item].shapes;
  183. }
  184. Ref<NavigationMesh> MeshLibrary::get_item_navigation_mesh(int p_item) const {
  185. ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Ref<NavigationMesh>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  186. return item_map[p_item].navigation_mesh;
  187. }
  188. Transform3D MeshLibrary::get_item_navigation_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].navigation_mesh_transform;
  191. }
  192. uint32_t MeshLibrary::get_item_navigation_layers(int p_item) const {
  193. ERR_FAIL_COND_V_MSG(!item_map.has(p_item), 0, "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  194. return item_map[p_item].navigation_layers;
  195. }
  196. Ref<Texture2D> MeshLibrary::get_item_preview(int p_item) const {
  197. ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Ref<Texture2D>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  198. return item_map[p_item].preview;
  199. }
  200. bool MeshLibrary::has_item(int p_item) const {
  201. return item_map.has(p_item);
  202. }
  203. void MeshLibrary::remove_item(int p_item) {
  204. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  205. item_map.erase(p_item);
  206. notify_property_list_changed();
  207. emit_changed();
  208. }
  209. void MeshLibrary::clear() {
  210. item_map.clear();
  211. notify_property_list_changed();
  212. emit_changed();
  213. }
  214. Vector<int> MeshLibrary::get_item_list() const {
  215. Vector<int> ret;
  216. ret.resize(item_map.size());
  217. int idx = 0;
  218. for (const KeyValue<int, Item> &E : item_map) {
  219. ret.write[idx++] = E.key;
  220. }
  221. return ret;
  222. }
  223. int MeshLibrary::find_item_by_name(const String &p_name) const {
  224. for (const KeyValue<int, Item> &E : item_map) {
  225. if (E.value.name == p_name) {
  226. return E.key;
  227. }
  228. }
  229. return -1;
  230. }
  231. int MeshLibrary::get_last_unused_item_id() const {
  232. if (!item_map.size()) {
  233. return 0;
  234. } else {
  235. return item_map.back()->key() + 1;
  236. }
  237. }
  238. void MeshLibrary::_set_item_shapes(int p_item, const Array &p_shapes) {
  239. Array arr_shapes = p_shapes;
  240. int size = p_shapes.size();
  241. if (size & 1) {
  242. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  243. int prev_size = item_map[p_item].shapes.size() * 2;
  244. if (prev_size < size) {
  245. // Check if last element is a shape.
  246. Ref<Shape3D> shape = arr_shapes[size - 1];
  247. if (shape.is_null()) {
  248. Ref<BoxShape3D> box_shape;
  249. box_shape.instantiate();
  250. arr_shapes[size - 1] = box_shape;
  251. }
  252. // Make sure the added element is a Transform3D.
  253. arr_shapes.push_back(Transform3D());
  254. size++;
  255. } else {
  256. size--;
  257. arr_shapes.resize(size);
  258. }
  259. }
  260. Vector<ShapeData> shapes;
  261. for (int i = 0; i < size; i += 2) {
  262. ShapeData sd;
  263. sd.shape = arr_shapes[i + 0];
  264. sd.local_transform = arr_shapes[i + 1];
  265. if (sd.shape.is_valid()) {
  266. shapes.push_back(sd);
  267. }
  268. }
  269. set_item_shapes(p_item, shapes);
  270. }
  271. Array MeshLibrary::_get_item_shapes(int p_item) const {
  272. Vector<ShapeData> shapes = get_item_shapes(p_item);
  273. Array ret;
  274. for (int i = 0; i < shapes.size(); i++) {
  275. ret.push_back(shapes[i].shape);
  276. ret.push_back(shapes[i].local_transform);
  277. }
  278. return ret;
  279. }
  280. void MeshLibrary::reset_state() {
  281. clear();
  282. }
  283. void MeshLibrary::_bind_methods() {
  284. ClassDB::bind_method(D_METHOD("create_item", "id"), &MeshLibrary::create_item);
  285. ClassDB::bind_method(D_METHOD("set_item_name", "id", "name"), &MeshLibrary::set_item_name);
  286. ClassDB::bind_method(D_METHOD("set_item_mesh", "id", "mesh"), &MeshLibrary::set_item_mesh);
  287. ClassDB::bind_method(D_METHOD("set_item_mesh_transform", "id", "mesh_transform"), &MeshLibrary::set_item_mesh_transform);
  288. ClassDB::bind_method(D_METHOD("set_item_navigation_mesh", "id", "navigation_mesh"), &MeshLibrary::set_item_navigation_mesh);
  289. ClassDB::bind_method(D_METHOD("set_item_navigation_mesh_transform", "id", "navigation_mesh"), &MeshLibrary::set_item_navigation_mesh_transform);
  290. ClassDB::bind_method(D_METHOD("set_item_navigation_layers", "id", "navigation_layers"), &MeshLibrary::set_item_navigation_layers);
  291. ClassDB::bind_method(D_METHOD("set_item_shapes", "id", "shapes"), &MeshLibrary::_set_item_shapes);
  292. ClassDB::bind_method(D_METHOD("set_item_preview", "id", "texture"), &MeshLibrary::set_item_preview);
  293. ClassDB::bind_method(D_METHOD("get_item_name", "id"), &MeshLibrary::get_item_name);
  294. ClassDB::bind_method(D_METHOD("get_item_mesh", "id"), &MeshLibrary::get_item_mesh);
  295. ClassDB::bind_method(D_METHOD("get_item_mesh_transform", "id"), &MeshLibrary::get_item_mesh_transform);
  296. ClassDB::bind_method(D_METHOD("get_item_navigation_mesh", "id"), &MeshLibrary::get_item_navigation_mesh);
  297. ClassDB::bind_method(D_METHOD("get_item_navigation_mesh_transform", "id"), &MeshLibrary::get_item_navigation_mesh_transform);
  298. ClassDB::bind_method(D_METHOD("get_item_navigation_layers", "id"), &MeshLibrary::get_item_navigation_layers);
  299. ClassDB::bind_method(D_METHOD("get_item_shapes", "id"), &MeshLibrary::_get_item_shapes);
  300. ClassDB::bind_method(D_METHOD("get_item_preview", "id"), &MeshLibrary::get_item_preview);
  301. ClassDB::bind_method(D_METHOD("remove_item", "id"), &MeshLibrary::remove_item);
  302. ClassDB::bind_method(D_METHOD("find_item_by_name", "name"), &MeshLibrary::find_item_by_name);
  303. ClassDB::bind_method(D_METHOD("clear"), &MeshLibrary::clear);
  304. ClassDB::bind_method(D_METHOD("get_item_list"), &MeshLibrary::get_item_list);
  305. ClassDB::bind_method(D_METHOD("get_last_unused_item_id"), &MeshLibrary::get_last_unused_item_id);
  306. }
  307. MeshLibrary::MeshLibrary() {
  308. }
  309. MeshLibrary::~MeshLibrary() {
  310. }