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