Skip to content

Commit 28a9c26

Browse files
committed
Make MeshLibrary use PropertyListHelper
1 parent 15a4311 commit 28a9c26

File tree

4 files changed

+93
-129
lines changed

4 files changed

+93
-129
lines changed

editor/scene/3d/mesh_library_editor_plugin.cpp

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,6 @@ void MeshLibraryEditor::edit(const Ref<MeshLibrary> &p_mesh_library) {
5151
}
5252
}
5353

54-
void MeshLibraryEditor::_menu_remove_confirm() {
55-
switch (option) {
56-
case MENU_OPTION_REMOVE_ITEM: {
57-
mesh_library->remove_item(to_erase);
58-
} break;
59-
default: {
60-
};
61-
}
62-
}
63-
6454
void MeshLibraryEditor::_menu_update_confirm(bool p_apply_xforms) {
6555
cd_update->hide();
6656
apply_xforms = p_apply_xforms;
@@ -233,17 +223,6 @@ Error MeshLibraryEditor::update_library_file(Node *p_base_scene, Ref<MeshLibrary
233223
void MeshLibraryEditor::_menu_cbk(int p_option) {
234224
option = p_option;
235225
switch (p_option) {
236-
case MENU_OPTION_ADD_ITEM: {
237-
mesh_library->create_item(mesh_library->get_last_unused_item_id());
238-
} break;
239-
case MENU_OPTION_REMOVE_ITEM: {
240-
String p = InspectorDock::get_inspector_singleton()->get_selected_path();
241-
if (p.begins_with("item") && p.get_slice_count("/") >= 2) {
242-
to_erase = p.get_slicec('/', 1).to_int();
243-
cd_remove->set_text(vformat(TTR("Remove item %d?"), to_erase));
244-
cd_remove->popup_centered(Size2(300, 60));
245-
}
246-
} break;
247226
case MENU_OPTION_IMPORT_FROM_SCENE: {
248227
apply_xforms = false;
249228
file->popup_file_dialog();
@@ -280,19 +259,13 @@ MeshLibraryEditor::MeshLibraryEditor() {
280259
menu->set_button_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("MeshLibrary"), EditorStringName(EditorIcons)));
281260
menu->set_flat(false);
282261
menu->set_theme_type_variation("FlatMenuButtonNoIconTint");
283-
menu->get_popup()->add_item(TTR("Add Item"), MENU_OPTION_ADD_ITEM);
284-
menu->get_popup()->add_item(TTR("Remove Selected Item"), MENU_OPTION_REMOVE_ITEM);
285-
menu->get_popup()->add_separator();
286262
menu->get_popup()->add_item(TTR("Import from Scene (Ignore Transforms)"), MENU_OPTION_IMPORT_FROM_SCENE);
287263
menu->get_popup()->add_item(TTR("Import from Scene (Apply Transforms)"), MENU_OPTION_IMPORT_FROM_SCENE_APPLY_XFORMS);
288264
menu->get_popup()->add_item(TTR("Update from Scene"), MENU_OPTION_UPDATE_FROM_SCENE);
289265
menu->get_popup()->set_item_disabled(menu->get_popup()->get_item_index(MENU_OPTION_UPDATE_FROM_SCENE), true);
290266
menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &MeshLibraryEditor::_menu_cbk));
291267
menu->hide();
292268

293-
cd_remove = memnew(ConfirmationDialog);
294-
add_child(cd_remove);
295-
cd_remove->get_ok_button()->connect(SceneStringName(pressed), callable_mp(this, &MeshLibraryEditor::_menu_remove_confirm));
296269
cd_update = memnew(ConfirmationDialog);
297270
add_child(cd_update);
298271
cd_update->set_ok_button_text(TTR("Apply without Transforms"));

editor/scene/3d/mesh_library_editor_plugin.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,12 @@ class MeshLibraryEditor : public Control {
4444
Ref<MeshLibrary> mesh_library;
4545

4646
MenuButton *menu = nullptr;
47-
ConfirmationDialog *cd_remove = nullptr;
4847
ConfirmationDialog *cd_update = nullptr;
4948
EditorFileDialog *file = nullptr;
5049
bool apply_xforms = false;
5150
int to_erase = 0;
5251

5352
enum {
54-
MENU_OPTION_ADD_ITEM,
55-
MENU_OPTION_REMOVE_ITEM,
5653
MENU_OPTION_UPDATE_FROM_SCENE,
5754
MENU_OPTION_IMPORT_FROM_SCENE,
5855
MENU_OPTION_IMPORT_FROM_SCENE_APPLY_XFORMS
@@ -61,7 +58,6 @@ class MeshLibraryEditor : public Control {
6158
int option = 0;
6259
void _import_scene_cbk(const String &p_str);
6360
void _menu_cbk(int p_option);
64-
void _menu_remove_confirm();
6561
void _menu_update_confirm(bool p_apply_xforms);
6662

6763
static void _import_scene(Node *p_scene, Ref<MeshLibrary> p_library, bool p_merge, bool p_apply_xforms);

scene/resources/3d/mesh_library.cpp

Lines changed: 83 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -38,69 +38,39 @@
3838

3939
bool MeshLibrary::_set(const StringName &p_name, const Variant &p_value) {
4040
String prop_name = p_name;
41-
if (prop_name.begins_with("item/")) {
42-
int idx = prop_name.get_slicec('/', 1).to_int();
43-
String what = prop_name.get_slicec('/', 2);
44-
if (!item_map.has(idx)) {
45-
create_item(idx);
46-
}
41+
if (!prop_name.begins_with("item/")) {
42+
return false;
43+
}
44+
45+
int idx = prop_name.get_slicec('/', 1).to_int();
46+
String what = prop_name.get_slicec('/', 2);
47+
if (!item_map.has(idx)) {
48+
create_item(idx);
49+
}
4750

48-
if (what == "name") {
49-
set_item_name(idx, p_value);
50-
} else if (what == "mesh") {
51-
set_item_mesh(idx, p_value);
52-
} else if (what == "mesh_transform") {
53-
set_item_mesh_transform(idx, p_value);
54-
} else if (what == "mesh_cast_shadow") {
55-
switch ((int)p_value) {
56-
case 0: {
57-
set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_OFF);
58-
} break;
59-
case 1: {
60-
set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_ON);
61-
} break;
62-
case 2: {
63-
set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_DOUBLE_SIDED);
64-
} break;
65-
case 3: {
66-
set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_SHADOWS_ONLY);
67-
} break;
68-
default: {
69-
set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_ON);
70-
} break;
71-
}
7251
#ifndef PHYSICS_3D_DISABLED
73-
} else if (what == "shape") {
74-
Vector<ShapeData> shapes;
75-
ShapeData sd;
76-
sd.shape = p_value;
77-
shapes.push_back(sd);
78-
set_item_shapes(idx, shapes);
79-
} else if (what == "shapes") {
80-
_set_item_shapes(idx, p_value);
52+
if (what == "shape") {
53+
Vector<ShapeData> shapes;
54+
ShapeData sd;
55+
sd.shape = p_value;
56+
shapes.push_back(sd);
57+
set_item_shapes(idx, shapes);
58+
return true;
59+
}
8160
#endif // PHYSICS_3D_DISABLED
82-
} else if (what == "preview") {
83-
set_item_preview(idx, p_value);
84-
} else if (what == "navigation_mesh") {
85-
set_item_navigation_mesh(idx, p_value);
86-
} else if (what == "navigation_mesh_transform") {
87-
set_item_navigation_mesh_transform(idx, p_value);
88-
#ifndef DISABLE_DEPRECATED
89-
} else if (what == "navmesh") { // Renamed in 4.0 beta 9.
90-
set_item_navigation_mesh(idx, p_value);
91-
} else if (what == "navmesh_transform") { // Renamed in 4.0 beta 9.
92-
set_item_navigation_mesh_transform(idx, p_value);
93-
#endif // DISABLE_DEPRECATED
94-
} else if (what == "navigation_layers") {
95-
set_item_navigation_layers(idx, p_value);
96-
} else {
97-
return false;
98-
}
9961

62+
#ifndef DISABLE_DEPRECATED
63+
if (what == "navmesh") { // Renamed in 4.0 beta 9.
64+
set_item_navigation_mesh(idx, p_value);
10065
return true;
10166
}
67+
if (what == "navmesh_transform") { // Renamed in 4.0 beta 9.
68+
set_item_navigation_mesh_transform(idx, p_value);
69+
return true;
70+
}
71+
#endif // DISABLE_DEPRECATED
10272

103-
return false;
73+
return property_helper.property_set_value(p_name, p_value);
10474
}
10575

10676
bool MeshLibrary::_get(const StringName &p_name, Variant &r_ret) const {
@@ -109,52 +79,18 @@ bool MeshLibrary::_get(const StringName &p_name, Variant &r_ret) const {
10979
ERR_FAIL_COND_V(!item_map.has(idx), false);
11080
String what = prop_name.get_slicec('/', 2);
11181

112-
if (what == "name") {
113-
r_ret = get_item_name(idx);
114-
} else if (what == "mesh") {
115-
r_ret = get_item_mesh(idx);
116-
} else if (what == "mesh_transform") {
117-
r_ret = get_item_mesh_transform(idx);
118-
} else if (what == "mesh_cast_shadow") {
119-
r_ret = (int)get_item_mesh_cast_shadow(idx);
120-
#ifndef PHYSICS_3D_DISABLED
121-
} else if (what == "shapes") {
122-
r_ret = _get_item_shapes(idx);
123-
#endif // PHYSICS_3D_DISABLED
124-
} else if (what == "navigation_mesh") {
125-
r_ret = get_item_navigation_mesh(idx);
126-
} else if (what == "navigation_mesh_transform") {
127-
r_ret = get_item_navigation_mesh_transform(idx);
12882
#ifndef DISABLE_DEPRECATED
129-
} else if (what == "navmesh") { // Renamed in 4.0 beta 9.
83+
if (what == "navmesh") { // Renamed in 4.0 beta 9.
13084
r_ret = get_item_navigation_mesh(idx);
131-
} else if (what == "navmesh_transform") { // Renamed in 4.0 beta 9.
85+
return true;
86+
}
87+
if (what == "navmesh_transform") { // Renamed in 4.0 beta 9.
13288
r_ret = get_item_navigation_mesh_transform(idx);
133-
#endif // DISABLE_DEPRECATED
134-
} else if (what == "navigation_layers") {
135-
r_ret = get_item_navigation_layers(idx);
136-
} else if (what == "preview") {
137-
r_ret = get_item_preview(idx);
138-
} else {
139-
return false;
89+
return true;
14090
}
91+
#endif // DISABLE_DEPRECATED
14192

142-
return true;
143-
}
144-
145-
void MeshLibrary::_get_property_list(List<PropertyInfo> *p_list) const {
146-
for (const KeyValue<int, Item> &E : item_map) {
147-
String prop_name = vformat("%s/%d/", PNAME("item"), E.key);
148-
p_list->push_back(PropertyInfo(Variant::STRING, prop_name + PNAME("name")));
149-
p_list->push_back(PropertyInfo(Variant::OBJECT, prop_name + PNAME("mesh"), PROPERTY_HINT_RESOURCE_TYPE, Mesh::get_class_static()));
150-
p_list->push_back(PropertyInfo(Variant::TRANSFORM3D, prop_name + PNAME("mesh_transform"), PROPERTY_HINT_NONE, "suffix:m"));
151-
p_list->push_back(PropertyInfo(Variant::INT, prop_name + PNAME("mesh_cast_shadow"), PROPERTY_HINT_ENUM, "Off,On,Double-Sided,Shadows Only"));
152-
p_list->push_back(PropertyInfo(Variant::ARRAY, prop_name + PNAME("shapes")));
153-
p_list->push_back(PropertyInfo(Variant::OBJECT, prop_name + PNAME("navigation_mesh"), PROPERTY_HINT_RESOURCE_TYPE, NavigationMesh::get_class_static()));
154-
p_list->push_back(PropertyInfo(Variant::TRANSFORM3D, prop_name + PNAME("navigation_mesh_transform"), PROPERTY_HINT_NONE, "suffix:m"));
155-
p_list->push_back(PropertyInfo(Variant::INT, prop_name + PNAME("navigation_layers"), PROPERTY_HINT_LAYERS_3D_NAVIGATION));
156-
p_list->push_back(PropertyInfo(Variant::OBJECT, prop_name + PNAME("preview"), PROPERTY_HINT_RESOURCE_TYPE, Texture2D::get_class_static(), PROPERTY_USAGE_DEFAULT));
157-
}
93+
return property_helper.property_get_value(prop_name, r_ret);
15894
}
15995

16096
void MeshLibrary::create_item(int p_item) {
@@ -222,6 +158,28 @@ void MeshLibrary::set_item_preview(int p_item, const Ref<Texture2D> &p_preview)
222158
emit_changed();
223159
}
224160

161+
void MeshLibrary::set_item_count(int p_count) {
162+
ERR_FAIL_COND(p_count < 0);
163+
int prev_size = item_map.size();
164+
if (prev_size == p_count) {
165+
return;
166+
}
167+
168+
if (prev_size < p_count) {
169+
for (int i = prev_size; i < p_count; i++) {
170+
item_map[i] = Item();
171+
}
172+
} else {
173+
while (prev_size > p_count) {
174+
item_map.remove(item_map.back());
175+
prev_size--;
176+
}
177+
}
178+
179+
emit_changed();
180+
notify_property_list_changed();
181+
}
182+
225183
String MeshLibrary::get_item_name(int p_item) const {
226184
ERR_FAIL_COND_V_MSG(!item_map.has(p_item), "", "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
227185
return item_map[p_item].name;
@@ -269,6 +227,10 @@ Ref<Texture2D> MeshLibrary::get_item_preview(int p_item) const {
269227
return item_map[p_item].preview;
270228
}
271229

230+
int MeshLibrary::get_item_count() const {
231+
return item_map.size();
232+
}
233+
272234
bool MeshLibrary::has_item(int p_item) const {
273235
return item_map.has(p_item);
274236
}
@@ -382,6 +344,7 @@ void MeshLibrary::_bind_methods() {
382344
ClassDB::bind_method(D_METHOD("set_item_shapes", "id", "shapes"), &MeshLibrary::_set_item_shapes);
383345
#endif // PHYSICS_3D_DISABLED
384346
ClassDB::bind_method(D_METHOD("set_item_preview", "id", "texture"), &MeshLibrary::set_item_preview);
347+
ClassDB::bind_method(D_METHOD("set_item_count", "count"), &MeshLibrary::set_item_count);
385348
ClassDB::bind_method(D_METHOD("get_item_name", "id"), &MeshLibrary::get_item_name);
386349
ClassDB::bind_method(D_METHOD("get_item_mesh", "id"), &MeshLibrary::get_item_mesh);
387350
ClassDB::bind_method(D_METHOD("get_item_mesh_transform", "id"), &MeshLibrary::get_item_mesh_transform);
@@ -393,15 +356,38 @@ void MeshLibrary::_bind_methods() {
393356
ClassDB::bind_method(D_METHOD("get_item_shapes", "id"), &MeshLibrary::_get_item_shapes);
394357
#endif // PHYSICS_3D_DISABLED
395358
ClassDB::bind_method(D_METHOD("get_item_preview", "id"), &MeshLibrary::get_item_preview);
359+
ClassDB::bind_method(D_METHOD("get_item_count"), &MeshLibrary::get_item_count);
396360
ClassDB::bind_method(D_METHOD("remove_item", "id"), &MeshLibrary::remove_item);
397361
ClassDB::bind_method(D_METHOD("find_item_by_name", "name"), &MeshLibrary::find_item_by_name);
398362

399363
ClassDB::bind_method(D_METHOD("clear"), &MeshLibrary::clear);
400364
ClassDB::bind_method(D_METHOD("get_item_list"), &MeshLibrary::get_item_list);
401365
ClassDB::bind_method(D_METHOD("get_last_unused_item_id"), &MeshLibrary::get_last_unused_item_id);
366+
367+
ADD_ARRAY_COUNT("Items", "item_count", "set_item_count", "get_item_count", "item/");
368+
369+
base_property_helper.set_prefix("item/");
370+
base_property_helper.set_array_length_getter(&MeshLibrary::get_item_count);
371+
372+
Item defaults;
373+
374+
base_property_helper.register_property(PropertyInfo(Variant::STRING, "name"), defaults.name, &MeshLibrary::set_item_name, &MeshLibrary::get_item_name);
375+
base_property_helper.register_property(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, Mesh::get_class_static()), defaults.mesh, &MeshLibrary::set_item_mesh, &MeshLibrary::get_item_mesh);
376+
base_property_helper.register_property(PropertyInfo(Variant::TRANSFORM3D, "mesh_transform", PROPERTY_HINT_NONE, "suffix:m"), defaults.mesh_transform, &MeshLibrary::set_item_mesh_transform, &MeshLibrary::get_item_mesh_transform);
377+
base_property_helper.register_property(PropertyInfo(Variant::INT, "mesh_cast_shadow", PROPERTY_HINT_ENUM, "Off,On,Double-Sided,Shadows Only"), defaults.mesh_cast_shadow, &MeshLibrary::set_item_mesh_cast_shadow, &MeshLibrary::get_item_mesh_cast_shadow);
378+
#ifndef PHYSICS_3D_DISABLED
379+
base_property_helper.register_property(PropertyInfo(Variant::ARRAY, "shapes"), Array(), &MeshLibrary::_set_item_shapes, &MeshLibrary::_get_item_shapes);
380+
#endif // PHYSICS_3D_DISABLED
381+
base_property_helper.register_property(PropertyInfo(Variant::OBJECT, "navigation_mesh", PROPERTY_HINT_RESOURCE_TYPE, NavigationMesh::get_class_static()), defaults.navigation_mesh, &MeshLibrary::set_item_navigation_mesh, &MeshLibrary::get_item_navigation_mesh);
382+
base_property_helper.register_property(PropertyInfo(Variant::TRANSFORM3D, "navigation_mesh_transform", PROPERTY_HINT_NONE, "suffix:m"), defaults.navigation_mesh_transform, &MeshLibrary::set_item_navigation_mesh_transform, &MeshLibrary::get_item_navigation_mesh_transform);
383+
base_property_helper.register_property(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_3D_NAVIGATION), defaults.navigation_layers, &MeshLibrary::set_item_navigation_layers, &MeshLibrary::get_item_navigation_layers);
384+
base_property_helper.register_property(PropertyInfo(Variant::OBJECT, "preview", PROPERTY_HINT_RESOURCE_TYPE, Texture2D::get_class_static(), PROPERTY_USAGE_DEFAULT), defaults.preview, &MeshLibrary::set_item_preview, &MeshLibrary::get_item_preview);
385+
386+
PropertyListHelper::register_base_helper(&base_property_helper);
402387
}
403388

404389
MeshLibrary::MeshLibrary() {
390+
property_helper.setup_for_instance(base_property_helper, this);
405391
}
406392

407393
MeshLibrary::~MeshLibrary() {

scene/resources/3d/mesh_library.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "core/io/resource.h"
3434
#include "core/templates/rb_map.h"
35+
#include "scene/property_list_helper.h"
3536
#include "scene/resources/mesh.h"
3637
#include "scene/resources/navigation_mesh.h"
3738
#include "servers/rendering/rendering_server.h"
@@ -44,6 +45,9 @@ class MeshLibrary : public Resource {
4445
GDCLASS(MeshLibrary, Resource);
4546
RES_BASE_EXTENSION("meshlib");
4647

48+
static inline PropertyListHelper base_property_helper;
49+
PropertyListHelper property_helper;
50+
4751
public:
4852
#ifndef PHYSICS_3D_DISABLED
4953
struct ShapeData {
@@ -75,7 +79,9 @@ class MeshLibrary : public Resource {
7579
protected:
7680
bool _set(const StringName &p_name, const Variant &p_value);
7781
bool _get(const StringName &p_name, Variant &r_ret) const;
78-
void _get_property_list(List<PropertyInfo> *p_list) const;
82+
void _get_property_list(List<PropertyInfo> *p_list) const { property_helper.get_property_list(p_list); }
83+
bool _property_can_revert(const StringName &p_name) const { return property_helper.property_can_revert(p_name); }
84+
bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return property_helper.property_get_revert(p_name, r_property); }
7985

8086
virtual void reset_state() override;
8187
static void _bind_methods();
@@ -93,6 +99,8 @@ class MeshLibrary : public Resource {
9399
void set_item_shapes(int p_item, const Vector<ShapeData> &p_shapes);
94100
#endif // PHYSICS_3D_DISABLED
95101
void set_item_preview(int p_item, const Ref<Texture2D> &p_preview);
102+
void set_item_count(int p_count);
103+
96104
String get_item_name(int p_item) const;
97105
Ref<Mesh> get_item_mesh(int p_item) const;
98106
Transform3D get_item_mesh_transform(int p_item) const;
@@ -104,6 +112,7 @@ class MeshLibrary : public Resource {
104112
Vector<ShapeData> get_item_shapes(int p_item) const;
105113
#endif // PHYSICS_3D_DISABLED
106114
Ref<Texture2D> get_item_preview(int p_item) const;
115+
int get_item_count() const;
107116

108117
void remove_item(int p_item);
109118
bool has_item(int p_item) const;

0 commit comments

Comments
 (0)