diff options
Diffstat (limited to 'editor/editor_inspector.cpp')
-rw-r--r-- | editor/editor_inspector.cpp | 2093 |
1 files changed, 1527 insertions, 566 deletions
diff --git a/editor/editor_inspector.cpp b/editor/editor_inspector.cpp index df330d8685..4d0f27c5d4 100644 --- a/editor/editor_inspector.cpp +++ b/editor/editor_inspector.cpp @@ -31,18 +31,20 @@ #include "editor_inspector.h" #include "array_property_edit.h" +#include "core/os/keyboard.h" #include "dictionary_property_edit.h" #include "editor/doc_tools.h" #include "editor_feature_profile.h" #include "editor_node.h" #include "editor_scale.h" +#include "editor_settings.h" #include "multi_node_edit.h" #include "scene/resources/packed_scene.h" Size2 EditorProperty::get_minimum_size() const { Size2 ms; - Ref<Font> font = get_theme_font("font", "Tree"); - int font_size = get_theme_font_size("font_size", "Tree"); + Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Tree")); + int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Tree")); ms.height = font->get_height(font_size); for (int i = 0; i < get_child_count(); i++) { @@ -66,22 +68,22 @@ Size2 EditorProperty::get_minimum_size() const { } if (keying) { - Ref<Texture2D> key = get_theme_icon("Key", "EditorIcons"); - ms.width += key->get_width() + get_theme_constant("hseparator", "Tree"); + Ref<Texture2D> key = get_theme_icon(SNAME("Key"), SNAME("EditorIcons")); + ms.width += key->get_width() + get_theme_constant(SNAME("hseparator"), SNAME("Tree")); } if (deletable) { - Ref<Texture2D> key = get_theme_icon("Close", "EditorIcons"); - ms.width += key->get_width() + get_theme_constant("hseparator", "Tree"); + Ref<Texture2D> key = get_theme_icon(SNAME("Close"), SNAME("EditorIcons")); + ms.width += key->get_width() + get_theme_constant(SNAME("hseparator"), SNAME("Tree")); } if (checkable) { - Ref<Texture2D> check = get_theme_icon("checked", "CheckBox"); - ms.width += check->get_width() + get_theme_constant("hseparation", "CheckBox") + get_theme_constant("hseparator", "Tree"); + Ref<Texture2D> check = get_theme_icon(SNAME("checked"), SNAME("CheckBox")); + ms.width += check->get_width() + get_theme_constant(SNAME("hseparation"), SNAME("CheckBox")) + get_theme_constant(SNAME("hseparator"), SNAME("Tree")); } if (bottom_editor != nullptr && bottom_editor->is_visible()) { - ms.height += get_theme_constant("vseparation", "Tree"); + ms.height += get_theme_constant(SNAME("vseparation")); Size2 bems = bottom_editor->get_combined_minimum_size(); //bems.width += get_constant("item_margin", "Tree"); ms.height += bems.height; @@ -95,7 +97,8 @@ void EditorProperty::emit_changed(const StringName &p_property, const Variant &p Variant args[4] = { p_property, p_value, p_field, p_changing }; const Variant *argptrs[4] = { &args[0], &args[1], &args[2], &args[3] }; - emit_signal("property_changed", (const Variant **)argptrs, 4); + cache[p_property] = p_value; + emit_signal(SNAME("property_changed"), (const Variant **)argptrs, 4); } void EditorProperty::_notification(int p_what) { @@ -109,8 +112,8 @@ void EditorProperty::_notification(int p_what) { { int child_room = size.width * (1.0 - split_ratio); - Ref<Font> font = get_theme_font("font", "Tree"); - int font_size = get_theme_font_size("font_size", "Tree"); + Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Tree")); + int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Tree")); int height = font->get_height(font_size); bool no_children = true; @@ -148,21 +151,21 @@ void EditorProperty::_notification(int p_what) { if (bottom_editor) { int m = 0; //get_constant("item_margin", "Tree"); - bottom_rect = Rect2(m, rect.size.height + get_theme_constant("vseparation", "Tree"), size.width - m, bottom_editor->get_combined_minimum_size().height); + bottom_rect = Rect2(m, rect.size.height + get_theme_constant(SNAME("vseparation")), size.width - m, bottom_editor->get_combined_minimum_size().height); } if (keying) { Ref<Texture2D> key; if (use_keying_next()) { - key = get_theme_icon("KeyNext", "EditorIcons"); + key = get_theme_icon(SNAME("KeyNext"), SNAME("EditorIcons")); } else { - key = get_theme_icon("Key", "EditorIcons"); + key = get_theme_icon(SNAME("Key"), SNAME("EditorIcons")); } - rect.size.x -= key->get_width() + get_theme_constant("hseparator", "Tree"); + rect.size.x -= key->get_width() + get_theme_constant(SNAME("hseparator"), SNAME("Tree")); if (is_layout_rtl()) { - rect.position.x += key->get_width() + get_theme_constant("hseparator", "Tree"); + rect.position.x += key->get_width() + get_theme_constant(SNAME("hseparator"), SNAME("Tree")); } if (no_children) { @@ -173,12 +176,12 @@ void EditorProperty::_notification(int p_what) { if (deletable) { Ref<Texture2D> close; - close = get_theme_icon("Close", "EditorIcons"); + close = get_theme_icon(SNAME("Close"), SNAME("EditorIcons")); - rect.size.x -= close->get_width() + get_theme_constant("hseparator", "Tree"); + rect.size.x -= close->get_width() + get_theme_constant(SNAME("hseparator"), SNAME("Tree")); if (is_layout_rtl()) { - rect.position.x += close->get_width() + get_theme_constant("hseparator", "Tree"); + rect.position.x += close->get_width() + get_theme_constant(SNAME("hseparator"), SNAME("Tree")); } if (no_children) { @@ -213,9 +216,9 @@ void EditorProperty::_notification(int p_what) { } if (p_what == NOTIFICATION_DRAW) { - Ref<Font> font = get_theme_font("font", "Tree"); - int font_size = get_theme_font_size("font_size", "Tree"); - Color dark_color = get_theme_color("dark_color_2", "Editor"); + Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Tree")); + int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Tree")); + Color dark_color = get_theme_color(SNAME("dark_color_2"), SNAME("Editor")); bool rtl = is_layout_rtl(); Size2 size = get_size(); @@ -225,11 +228,15 @@ void EditorProperty::_notification(int p_what) { size.height = label_reference->get_size().height; } + Ref<StyleBox> sb; if (selected) { - Ref<StyleBox> sb = get_theme_stylebox("selected", "Tree"); - draw_style_box(sb, Rect2(Vector2(), size)); + sb = get_theme_stylebox(SNAME("bg_selected")); + } else { + sb = get_theme_stylebox(SNAME("bg")); } + draw_style_box(sb, Rect2(Vector2(), size)); + if (draw_top_bg && right_child_rect != Rect2()) { draw_rect(right_child_rect, dark_color); } @@ -239,23 +246,23 @@ void EditorProperty::_notification(int p_what) { Color color; if (draw_red) { - color = get_theme_color("error_color", "Editor"); + color = get_theme_color(is_read_only() ? SNAME("readonly_error_color") : SNAME("error_color")); } else { - color = get_theme_color("property_color", "Editor"); + color = get_theme_color(is_read_only() ? SNAME("readonly_color") : SNAME("property_color")); } if (label.find(".") != -1) { color.a = 0.5; //this should be un-hacked honestly, as it's used for editor overrides } - int ofs = 0; + int ofs = get_theme_constant(SNAME("font_offset")); int text_limit = text_size; if (checkable) { Ref<Texture2D> checkbox; if (checked) { - checkbox = get_theme_icon("GuiChecked", "EditorIcons"); + checkbox = get_theme_icon(SNAME("GuiChecked"), SNAME("EditorIcons")); } else { - checkbox = get_theme_icon("GuiUnchecked", "EditorIcons"); + checkbox = get_theme_icon(SNAME("GuiUnchecked"), SNAME("EditorIcons")); } Color color2(1, 1, 1); @@ -270,16 +277,16 @@ void EditorProperty::_notification(int p_what) { } else { draw_texture(checkbox, check_rect.position, color2); } - ofs += get_theme_constant("hseparator", "Tree") + checkbox->get_width() + get_theme_constant("hseparation", "CheckBox"); + ofs += get_theme_constant(SNAME("hseparator"), SNAME("Tree")) + checkbox->get_width() + get_theme_constant(SNAME("hseparation"), SNAME("CheckBox")); text_limit -= ofs; } else { check_rect = Rect2(); } - if (can_revert) { - Ref<Texture2D> reload_icon = get_theme_icon("ReloadSmall", "EditorIcons"); - text_limit -= reload_icon->get_width() + get_theme_constant("hseparator", "Tree") * 2; - revert_rect = Rect2(text_limit + get_theme_constant("hseparator", "Tree"), (size.height - reload_icon->get_height()) / 2, reload_icon->get_width(), reload_icon->get_height()); + if (can_revert && !is_read_only()) { + Ref<Texture2D> reload_icon = get_theme_icon(SNAME("ReloadSmall"), SNAME("EditorIcons")); + text_limit -= reload_icon->get_width() + get_theme_constant(SNAME("hseparator"), SNAME("Tree")) * 2; + revert_rect = Rect2(text_limit + get_theme_constant(SNAME("hseparator"), SNAME("Tree")), (size.height - reload_icon->get_height()) / 2, reload_icon->get_width(), reload_icon->get_height()); Color color2(1, 1, 1); if (revert_hover) { @@ -307,12 +314,12 @@ void EditorProperty::_notification(int p_what) { Ref<Texture2D> key; if (use_keying_next()) { - key = get_theme_icon("KeyNext", "EditorIcons"); + key = get_theme_icon(SNAME("KeyNext"), SNAME("EditorIcons")); } else { - key = get_theme_icon("Key", "EditorIcons"); + key = get_theme_icon(SNAME("Key"), SNAME("EditorIcons")); } - ofs = size.width - key->get_width() - get_theme_constant("hseparator", "Tree"); + ofs = size.width - key->get_width() - get_theme_constant(SNAME("hseparator"), SNAME("Tree")); Color color2(1, 1, 1); if (keying_hover) { @@ -334,9 +341,9 @@ void EditorProperty::_notification(int p_what) { if (deletable) { Ref<Texture2D> close; - close = get_theme_icon("Close", "EditorIcons"); + close = get_theme_icon(SNAME("Close"), SNAME("EditorIcons")); - ofs = size.width - close->get_width() - get_theme_constant("hseparator", "Tree"); + ofs = size.width - close->get_width() - get_theme_constant(SNAME("hseparator"), SNAME("Tree")); Color color2(1, 1, 1); if (delete_hover) { @@ -374,13 +381,15 @@ StringName EditorProperty::get_edited_property() { } void EditorProperty::update_property() { - if (get_script_instance()) { - get_script_instance()->call("update_property"); - } + GDVIRTUAL_CALL(_update_property); +} + +void EditorProperty::_set_read_only(bool p_read_only) { } void EditorProperty::set_read_only(bool p_read_only) { read_only = p_read_only; + _set_read_only(p_read_only); } bool EditorProperty::is_read_only() const { @@ -412,7 +421,7 @@ bool EditorPropertyRevert::may_node_be_in_instance(Node *p_node) { return might_be; // or might not be } -bool EditorPropertyRevert::get_instanced_node_original_property(Node *p_node, const StringName &p_prop, Variant &value) { +bool EditorPropertyRevert::get_instantiated_node_original_property(Node *p_node, const StringName &p_prop, Variant &value, bool p_check_class_default) { Node *node = p_node; Node *orig = node; @@ -450,7 +459,7 @@ bool EditorPropertyRevert::get_instanced_node_original_property(Node *p_node, co node = node->get_owner(); } - if (!found && p_node) { + if (p_check_class_default && !found && p_node) { //if not found, try default class value Variant attempt = ClassDB::class_get_default_property_value(p_node->get_class_name(), p_prop); if (attempt.get_type() != Variant::NIL) { @@ -488,6 +497,7 @@ bool EditorPropertyRevert::is_node_property_different(Node *p_node, const Varian if (ss.is_valid()) { found_state = true; + break; } if (node == edited_scene) { //just in case @@ -501,59 +511,71 @@ bool EditorPropertyRevert::is_node_property_different(Node *p_node, const Varian } } - if (p_current.get_type() == Variant::FLOAT && p_orig.get_type() == Variant::FLOAT) { - float a = p_current; - float b = p_orig; + return is_property_value_different(p_current, p_orig); +} - return !Math::is_equal_approx(a, b); //this must be done because, as some scenes save as text, there might be a tiny difference in floats due to numerical error +bool EditorPropertyRevert::is_property_value_different(const Variant &p_a, const Variant &p_b) { + if (p_a.get_type() == Variant::FLOAT && p_b.get_type() == Variant::FLOAT) { + //this must be done because, as some scenes save as text, there might be a tiny difference in floats due to numerical error + return !Math::is_equal_approx((float)p_a, (float)p_b); + } else { + return p_a != p_b; } - - return bool(Variant::evaluate(Variant::OP_NOT_EQUAL, p_current, p_orig)); } -bool EditorPropertyRevert::can_property_revert(Object *p_object, const StringName &p_property) { - bool has_revert = false; +Variant EditorPropertyRevert::get_property_revert_value(Object *p_object, const StringName &p_property) { + // If the object implements property_can_revert, rely on that completely + // (i.e. don't then try to revert to default value - the property_get_revert implementation + // can do that if so desired) + if (p_object->has_method("property_can_revert") && p_object->call("property_can_revert", p_property)) { + return p_object->call("property_get_revert", p_property); + } + Ref<Script> scr = p_object->get_script(); Node *node = Object::cast_to<Node>(p_object); - if (node && EditorPropertyRevert::may_node_be_in_instance(node)) { - //check for difference including instantiation - Variant vorig; - if (EditorPropertyRevert::get_instanced_node_original_property(node, p_property, vorig)) { - Variant v = p_object->get(p_property); - - if (EditorPropertyRevert::is_node_property_different(node, v, vorig)) { - has_revert = true; + //if this node is an instance or inherits, but it has a script attached which is unrelated + //to the one set for the parent and also has a default value for the property, consider that + //has precedence over the value from the parent, because that is an explicit source of defaults + //closer in the tree to the current node + bool ignore_parent = false; + if (scr.is_valid()) { + Variant sorig; + if (EditorPropertyRevert::get_instantiated_node_original_property(node, "script", sorig) && !scr->inherits_script(sorig)) { + Variant dummy; + if (scr->get_property_default_value(p_property, dummy)) { + ignore_parent = true; + } } } - } else { - //check for difference against default class value instead - Variant default_value = ClassDB::class_get_default_property_value(p_object->get_class_name(), p_property); - if (default_value != Variant() && default_value != p_object->get(p_property)) { - has_revert = true; + + if (!ignore_parent) { + //check for difference including instantiation + Variant vorig; + if (EditorPropertyRevert::get_instantiated_node_original_property(node, p_property, vorig, false)) { + return vorig; + } } } - // If the object implements property_can_revert, rely on that completely - // (i.e. don't then try to revert to default value - the property_get_revert implementation - // can do that if so desired) - if (p_object->has_method("property_can_revert")) { - has_revert = p_object->call("property_can_revert", p_property).operator bool(); - } else { - if (!has_revert && !p_object->get_script().is_null()) { - Ref<Script> scr = p_object->get_script(); - if (scr.is_valid()) { - Variant orig_value; - if (scr->get_property_default_value(p_property, orig_value)) { - if (orig_value != p_object->get(p_property)) { - has_revert = true; - } - } - } + if (scr.is_valid()) { + Variant orig_value; + if (scr->get_property_default_value(p_property, orig_value)) { + return orig_value; } } - return has_revert; + //report default class value instead + return ClassDB::class_get_default_property_value(p_object->get_class_name(), p_property); +} + +bool EditorPropertyRevert::can_property_revert(Object *p_object, const StringName &p_property) { + Variant revert_value = EditorPropertyRevert::get_property_revert_value(p_object, p_property); + if (revert_value.get_type() == Variant::NIL) { + return false; + } + Variant current_value = p_object->get(p_property); + return EditorPropertyRevert::is_property_value_different(current_value, revert_value); } void EditorProperty::update_reload_status() { @@ -641,7 +663,7 @@ void EditorProperty::_focusable_focused(int p_index) { selected_focusable = p_index; update(); if (!already_selected && selected) { - emit_signal("selected", property, selected_focusable); + emit_signal(SNAME("selected"), property, selected_focusable); } } @@ -662,7 +684,7 @@ void EditorProperty::select(int p_focusable) { } if (!already_selected && selected) { - emit_signal("selected", property, selected_focusable); + emit_signal(SNAME("selected"), property, selected_focusable); } } @@ -676,7 +698,9 @@ bool EditorProperty::is_selected() const { return selected; } -void EditorProperty::_gui_input(const Ref<InputEvent> &p_event) { +void EditorProperty::gui_input(const Ref<InputEvent> &p_event) { + ERR_FAIL_COND(p_event.is_null()); + if (property == StringName()) { return; } @@ -688,7 +712,7 @@ void EditorProperty::_gui_input(const Ref<InputEvent> &p_event) { if (is_layout_rtl()) { mpos.x = get_size().x - mpos.x; } - bool button_left = me->get_button_mask() & BUTTON_MASK_LEFT; + bool button_left = me->get_button_mask() & MOUSE_BUTTON_MASK_LEFT; bool new_keying_hover = keying_rect.has_point(mpos) && !button_left; if (new_keying_hover != keying_hover) { @@ -717,7 +741,7 @@ void EditorProperty::_gui_input(const Ref<InputEvent> &p_event) { Ref<InputEventMouseButton> mb = p_event; - if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) { + if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MOUSE_BUTTON_LEFT) { Vector2 mpos = mb->get_position(); if (is_layout_rtl()) { mpos.x = get_size().x - mpos.x; @@ -725,78 +749,89 @@ void EditorProperty::_gui_input(const Ref<InputEvent> &p_event) { if (!selected && selectable) { selected = true; - emit_signal("selected", property, -1); + emit_signal(SNAME("selected"), property, -1); update(); } if (keying_rect.has_point(mpos)) { - emit_signal("property_keyed", property, use_keying_next()); + emit_signal(SNAME("property_keyed"), property, use_keying_next()); if (use_keying_next()) { if (property == "frame_coords" && (object->is_class("Sprite2D") || object->is_class("Sprite3D"))) { - Vector2 new_coords = object->get(property); + Vector2i new_coords = object->get(property); new_coords.x++; if (new_coords.x >= object->get("hframes").operator int64_t()) { new_coords.x = 0; new_coords.y++; } - call_deferred("emit_changed", property, new_coords, "", false); + call_deferred(SNAME("emit_changed"), property, new_coords, "", false); } else { - call_deferred("emit_changed", property, object->get(property).operator int64_t() + 1, "", false); + call_deferred(SNAME("emit_changed"), property, object->get(property).operator int64_t() + 1, "", false); } - call_deferred("update_property"); + call_deferred(SNAME("update_property")); } } if (delete_rect.has_point(mpos)) { - emit_signal("property_deleted", property); + emit_signal(SNAME("property_deleted"), property); } if (revert_rect.has_point(mpos)) { - Variant vorig; - - Node *node = Object::cast_to<Node>(object); - if (node && EditorPropertyRevert::may_node_be_in_instance(node) && EditorPropertyRevert::get_instanced_node_original_property(node, property, vorig)) { - emit_changed(property, vorig.duplicate(true)); - update_property(); - return; - } - - if (object->call("property_can_revert", property).operator bool()) { - Variant rev = object->call("property_get_revert", property); - emit_changed(property, rev); - update_property(); - return; - } - - if (!object->get_script().is_null()) { - Ref<Script> scr = object->get_script(); - if (scr.is_valid()) { - Variant orig_value; - if (scr->get_property_default_value(property, orig_value)) { - emit_changed(property, orig_value); - update_property(); - return; - } - } - } - - Variant default_value = ClassDB::class_get_default_property_value(object->get_class_name(), property); - if (default_value != Variant()) { - emit_changed(property, default_value); - update_property(); - return; - } + Variant revert_value = EditorPropertyRevert::get_property_revert_value(object, property); + emit_changed(property, revert_value); + update_property(); } + if (check_rect.has_point(mpos)) { checked = !checked; update(); - emit_signal("property_checked", property, checked); + emit_signal(SNAME("property_checked"), property, checked); + } + } else if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MOUSE_BUTTON_RIGHT) { + _ensure_popup(); + menu->set_position(get_screen_position() + get_local_mouse_position()); + menu->set_size(Vector2(1, 1)); + menu->popup(); + select(); + return; + } +} + +void EditorProperty::unhandled_key_input(const Ref<InputEvent> &p_event) { + if (!selected || !p_event->is_pressed()) { + return; + } + + const Ref<InputEventKey> k = p_event; + + if (k.is_valid() && k->is_pressed()) { + if (ED_IS_SHORTCUT("property_editor/copy_property", p_event)) { + menu_option(MENU_COPY_PROPERTY); + accept_event(); + } else if (ED_IS_SHORTCUT("property_editor/paste_property", p_event) && !is_read_only()) { + menu_option(MENU_PASTE_PROPERTY); + accept_event(); + } else if (ED_IS_SHORTCUT("property_editor/copy_property_path", p_event)) { + menu_option(MENU_COPY_PROPERTY_PATH); + accept_event(); } } } +const Color *EditorProperty::_get_property_colors() { + const Color base = get_theme_color(SNAME("accent_color"), SNAME("Editor")); + const float saturation = base.get_s() * 0.75; + const float value = base.get_v(); + + static Color c[4]; + c[0].set_hsv(0.0 / 3.0 + 0.05, saturation, value); + c[1].set_hsv(1.0 / 3.0 + 0.05, saturation, value); + c[2].set_hsv(2.0 / 3.0 + 0.05, saturation, value); + c[3].set_hsv(1.5 / 3.0 + 0.05, saturation, value); + return c; +} + void EditorProperty::set_label_reference(Control *p_control) { label_reference = p_control; } @@ -805,6 +840,28 @@ void EditorProperty::set_bottom_editor(Control *p_control) { bottom_editor = p_control; } +bool EditorProperty::is_cache_valid() const { + if (object) { + for (const KeyValue<StringName, Variant> &E : cache) { + bool valid; + Variant value = object->get(E.key, &valid); + if (!valid || value != E.value) { + return false; + } + } + } + return true; +} +void EditorProperty::update_cache() { + cache.clear(); + if (object && property != StringName()) { + bool valid; + Variant value = object->get(property, &valid); + if (valid) { + cache[property] = value; + } + } +} Variant EditorProperty::get_drag_data(const Point2 &p_point) { if (property == StringName()) { return Variant(); @@ -860,7 +917,7 @@ void EditorProperty::set_object_and_property(Object *p_object, const StringName Control *EditorProperty::make_custom_tooltip(const String &p_text) const { tooltip_text = p_text; EditorHelpBit *help_bit = memnew(EditorHelpBit); - //help_bit->add_theme_style_override("panel", get_theme_stylebox("panel", "TooltipPanel")); + //help_bit->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), SNAME("TooltipPanel"))); help_bit->get_rich_text()->set_fixed_size_to_width(360 * EDSCALE); String text; @@ -875,7 +932,7 @@ Control *EditorProperty::make_custom_tooltip(const String &p_text) const { text += "\n" + property_doc; } } - help_bit->set_text(text); + help_bit->call_deferred(SNAME("set_text"), text); //hack so it uses proper theme once inside scene } return help_bit; @@ -885,6 +942,20 @@ String EditorProperty::get_tooltip_text() const { return tooltip_text; } +void EditorProperty::menu_option(int p_option) { + switch (p_option) { + case MENU_COPY_PROPERTY: { + EditorNode::get_singleton()->get_inspector()->set_property_clipboard(object->get(property)); + } break; + case MENU_PASTE_PROPERTY: { + emit_changed(property, EditorNode::get_singleton()->get_inspector()->get_property_clipboard()); + } break; + case MENU_COPY_PROPERTY_PATH: { + DisplayServer::get_singleton()->clipboard_set(property); + } break; + } +} + void EditorProperty::_bind_methods() { ClassDB::bind_method(D_METHOD("set_label", "text"), &EditorProperty::set_label); ClassDB::bind_method(D_METHOD("get_label"), &EditorProperty::get_label); @@ -910,9 +981,8 @@ void EditorProperty::_bind_methods() { ClassDB::bind_method(D_METHOD("get_edited_property"), &EditorProperty::get_edited_property); ClassDB::bind_method(D_METHOD("get_edited_object"), &EditorProperty::get_edited_object); - ClassDB::bind_method(D_METHOD("_gui_input"), &EditorProperty::_gui_input); - ClassDB::bind_method(D_METHOD("get_tooltip_text"), &EditorProperty::get_tooltip_text); + ClassDB::bind_method(D_METHOD("update_property"), &EditorProperty::update_property); ClassDB::bind_method(D_METHOD("add_focusable", "control"), &EditorProperty::add_focusable); ClassDB::bind_method(D_METHOD("set_bottom_editor", "editor"), &EditorProperty::set_bottom_editor); @@ -936,9 +1006,7 @@ void EditorProperty::_bind_methods() { ADD_SIGNAL(MethodInfo("object_id_selected", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::INT, "id"))); ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::INT, "focusable_idx"))); - MethodInfo vm; - vm.name = "update_property"; - BIND_VMETHOD(vm); + GDVIRTUAL_BIND(_update_property) } EditorProperty::EditorProperty() { @@ -964,6 +1032,21 @@ EditorProperty::EditorProperty() { label_reference = nullptr; bottom_editor = nullptr; delete_hover = false; + menu = nullptr; + set_process_unhandled_key_input(true); +} + +void EditorProperty::_ensure_popup() { + if (menu) { + return; + } + menu = memnew(PopupMenu); + menu->add_shortcut(ED_GET_SHORTCUT("property_editor/copy_property"), MENU_COPY_PROPERTY); + menu->add_shortcut(ED_GET_SHORTCUT("property_editor/paste_property"), MENU_PASTE_PROPERTY); + menu->add_shortcut(ED_GET_SHORTCUT("property_editor/copy_property_path"), MENU_COPY_PROPERTY_PATH); + menu->connect("id_pressed", callable_mp(this, &EditorProperty::menu_option)); + menu->set_item_disabled(MENU_PASTE_PROPERTY, is_read_only()); + add_child(menu); } //////////////////////////////////////////////// @@ -993,43 +1076,31 @@ void EditorInspectorPlugin::add_property_editor_for_multiple_properties(const St } bool EditorInspectorPlugin::can_handle(Object *p_object) { - if (get_script_instance()) { - return get_script_instance()->call("can_handle", p_object); + bool success; + if (GDVIRTUAL_CALL(_can_handle, p_object, success)) { + return success; } return false; } void EditorInspectorPlugin::parse_begin(Object *p_object) { - if (get_script_instance()) { - get_script_instance()->call("parse_begin", p_object); - } + GDVIRTUAL_CALL(_parse_begin); } void EditorInspectorPlugin::parse_category(Object *p_object, const String &p_parse_category) { - if (get_script_instance()) { - get_script_instance()->call("parse_category", p_object, p_parse_category); - } + GDVIRTUAL_CALL(_parse_category, p_object, p_parse_category); } -bool EditorInspectorPlugin::parse_property(Object *p_object, Variant::Type p_type, const String &p_path, PropertyHint p_hint, const String &p_hint_text, int p_usage, bool p_wide) { - if (get_script_instance()) { - Variant arg[6] = { - p_object, p_type, p_path, p_hint, p_hint_text, p_usage - }; - const Variant *argptr[6] = { - &arg[0], &arg[1], &arg[2], &arg[3], &arg[4], &arg[5] - }; - - Callable::CallError err; - return get_script_instance()->call("parse_property", (const Variant **)&argptr, 6, err); +bool EditorInspectorPlugin::parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const uint32_t p_usage, const bool p_wide) { + bool ret; + if (GDVIRTUAL_CALL(_parse_property, p_object, p_type, p_path, p_hint, p_hint_text, p_usage, p_wide, ret)) { + return ret; } return false; } void EditorInspectorPlugin::parse_end() { - if (get_script_instance()) { - get_script_instance()->call("parse_end"); - } + GDVIRTUAL_CALL(_parse_end); } void EditorInspectorPlugin::_bind_methods() { @@ -1037,30 +1108,11 @@ void EditorInspectorPlugin::_bind_methods() { ClassDB::bind_method(D_METHOD("add_property_editor", "property", "editor"), &EditorInspectorPlugin::add_property_editor); ClassDB::bind_method(D_METHOD("add_property_editor_for_multiple_properties", "label", "properties", "editor"), &EditorInspectorPlugin::add_property_editor_for_multiple_properties); - MethodInfo vm; - vm.name = "can_handle"; - vm.return_val.type = Variant::BOOL; - vm.arguments.push_back(PropertyInfo(Variant::OBJECT, "object")); - BIND_VMETHOD(vm); - vm.name = "parse_begin"; - vm.return_val.type = Variant::NIL; - BIND_VMETHOD(vm); - vm.name = "parse_category"; - vm.arguments.push_back(PropertyInfo(Variant::STRING, "category")); - BIND_VMETHOD(vm); - vm.arguments.pop_back(); - vm.name = "parse_property"; - vm.return_val.type = Variant::BOOL; - vm.arguments.push_back(PropertyInfo(Variant::INT, "type")); - vm.arguments.push_back(PropertyInfo(Variant::STRING, "path")); - vm.arguments.push_back(PropertyInfo(Variant::INT, "hint")); - vm.arguments.push_back(PropertyInfo(Variant::STRING, "hint_text")); - vm.arguments.push_back(PropertyInfo(Variant::INT, "usage")); - BIND_VMETHOD(vm); - vm.arguments.clear(); - vm.name = "parse_end"; - vm.return_val.type = Variant::NIL; - BIND_VMETHOD(vm); + GDVIRTUAL_BIND(_can_handle, "object") + GDVIRTUAL_BIND(_parse_begin) + GDVIRTUAL_BIND(_parse_category, "object", "category") + GDVIRTUAL_BIND(_parse_property, "object", "type", "name", "hint_type", "hint_string", "usage_flags", "wide"); + GDVIRTUAL_BIND(_parse_end) } //////////////////////////////////////////////// @@ -1068,11 +1120,14 @@ void EditorInspectorPlugin::_bind_methods() { void EditorInspectorCategory::_notification(int p_what) { if (p_what == NOTIFICATION_DRAW) { - draw_rect(Rect2(Vector2(), get_size()), bg_color); - Ref<Font> font = get_theme_font("font", "Tree"); - int font_size = get_theme_font_size("font_size", "Tree"); + Ref<StyleBox> sb = get_theme_stylebox(SNAME("prop_category_style"), SNAME("Editor")); - int hs = get_theme_constant("hseparation", "Tree"); + draw_style_box(sb, Rect2(Vector2(), get_size())); + + Ref<Font> font = get_theme_font(SNAME("bold"), SNAME("EditorFonts")); + int font_size = get_theme_font_size(SNAME("bold_size"), SNAME("EditorFonts")); + + int hs = get_theme_constant(SNAME("hseparation"), SNAME("Tree")); int w = font->get_string_size(label, font_size).width; if (icon.is_valid()) { @@ -1086,7 +1141,7 @@ void EditorInspectorCategory::_notification(int p_what) { ofs += hs + icon->get_width(); } - Color color = get_theme_color("font_color", "Tree"); + Color color = get_theme_color(SNAME("font_color"), SNAME("Tree")); draw_string(font, Point2(ofs, font->get_ascent(font_size) + (get_size().height - font->get_height(font_size)) / 2).floor(), label, HALIGN_LEFT, get_size().width, font_size, color); } } @@ -1094,7 +1149,7 @@ void EditorInspectorCategory::_notification(int p_what) { Control *EditorInspectorCategory::make_custom_tooltip(const String &p_text) const { tooltip_text = p_text; EditorHelpBit *help_bit = memnew(EditorHelpBit); - help_bit->add_theme_style_override("panel", get_theme_stylebox("panel", "TooltipPanel")); + help_bit->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), SNAME("TooltipPanel"))); help_bit->get_rich_text()->set_fixed_size_to_width(360 * EDSCALE); PackedStringArray slices = p_text.split("::", false); @@ -1108,15 +1163,15 @@ Control *EditorInspectorCategory::make_custom_tooltip(const String &p_text) cons text += "\n" + property_doc; } } - help_bit->set_text(text); //hack so it uses proper theme once inside scene + help_bit->call_deferred(SNAME("set_text"), text); //hack so it uses proper theme once inside scene } return help_bit; } Size2 EditorInspectorCategory::get_minimum_size() const { - Ref<Font> font = get_theme_font("font", "Tree"); - int font_size = get_theme_font_size("font_size", "Tree"); + Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Tree")); + int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Tree")); Size2 ms; ms.width = 1; @@ -1124,7 +1179,7 @@ Size2 EditorInspectorCategory::get_minimum_size() const { if (icon.is_valid()) { ms.height = MAX(icon->get_height(), ms.height); } - ms.height += get_theme_constant("vseparation", "Tree"); + ms.height += get_theme_constant(SNAME("vseparation"), SNAME("Tree")); return ms; } @@ -1146,135 +1201,150 @@ EditorInspectorCategory::EditorInspectorCategory() { void EditorInspectorSection::_test_unfold() { if (!vbox_added) { add_child(vbox); + move_child(vbox, 0); vbox_added = true; } } void EditorInspectorSection::_notification(int p_what) { - if (p_what == NOTIFICATION_SORT_CHILDREN) { - Ref<Font> font = get_theme_font("font", "Tree"); - int font_size = get_theme_font_size("font_size", "Tree"); - Ref<Texture2D> arrow; + switch (p_what) { + case NOTIFICATION_THEME_CHANGED: { + minimum_size_changed(); + } break; + case NOTIFICATION_SORT_CHILDREN: { + if (!vbox_added) { + return; + } + // Get the section header font. + Ref<Font> font = get_theme_font(SNAME("bold"), SNAME("EditorFonts")); + int font_size = get_theme_font_size(SNAME("bold_size"), SNAME("EditorFonts")); - if (foldable) { - if (object->editor_is_section_unfolded(section)) { - arrow = get_theme_icon("arrow", "Tree"); - } else { - if (is_layout_rtl()) { - arrow = get_theme_icon("arrow_collapsed_mirrored", "Tree"); + // Get the right direction arrow texture, if the section is foldable. + Ref<Texture2D> arrow; + if (foldable) { + if (object->editor_is_section_unfolded(section)) { + arrow = get_theme_icon(SNAME("arrow"), SNAME("Tree")); } else { - arrow = get_theme_icon("arrow_collapsed", "Tree"); + if (is_layout_rtl()) { + arrow = get_theme_icon(SNAME("arrow_collapsed_mirrored"), SNAME("Tree")); + } else { + arrow = get_theme_icon(SNAME("arrow_collapsed"), SNAME("Tree")); + } } } - } - - Size2 size = get_size(); - Point2 offset; - offset.y = font->get_height(font_size); - if (arrow.is_valid()) { - offset.y = MAX(offset.y, arrow->get_height()); - } - - offset.y += get_theme_constant("vseparation", "Tree"); - offset.x += get_theme_constant("inspector_margin", "Editor"); - Rect2 rect(offset, size - offset); - - //set children - for (int i = 0; i < get_child_count(); i++) { - Control *c = Object::cast_to<Control>(get_child(i)); - if (!c) { - continue; - } - if (c->is_set_as_top_level()) { - continue; - } - if (!c->is_visible_in_tree()) { - continue; + // Compute the height of the section header. + int header_height = font->get_height(font_size); + if (arrow.is_valid()) { + header_height = MAX(header_height, arrow->get_height()); } + header_height += get_theme_constant(SNAME("vseparation"), SNAME("Tree")); - fit_child_in_rect(c, rect); - } - - update(); //need to redraw text - } - - if (p_what == NOTIFICATION_DRAW) { - Ref<Texture2D> arrow; - bool rtl = is_layout_rtl(); + int inspector_margin = get_theme_constant(SNAME("inspector_margin"), SNAME("Editor")); + Size2 size = get_size() - Vector2(inspector_margin, 0); + Vector2 offset = Vector2(is_layout_rtl() ? 0 : inspector_margin, header_height); + for (int i = 0; i < get_child_count(); i++) { + Control *c = Object::cast_to<Control>(get_child(i)); + if (!c) { + continue; + } + if (c->is_set_as_top_level()) { + continue; + } - if (foldable) { - if (rtl) { - arrow = get_theme_icon("arrow_collapsed_mirrored", "Tree"); - } else { - arrow = get_theme_icon("arrow_collapsed", "Tree"); + fit_child_in_rect(c, Rect2(offset, size)); } - } - - Ref<Font> font = get_theme_font("font", "Tree"); - int font_size = get_theme_font_size("font_size", "Tree"); - - int h = font->get_height(font_size); - if (arrow.is_valid()) { - h = MAX(h, arrow->get_height()); - } - h += get_theme_constant("vseparation", "Tree"); + } break; + case NOTIFICATION_DRAW: { + // Get the section header font. + Ref<Font> font = get_theme_font(SNAME("bold"), SNAME("EditorFonts")); + int font_size = get_theme_font_size(SNAME("bold_size"), SNAME("EditorFonts")); - draw_rect(Rect2(Vector2(), Vector2(get_size().width, h)), bg_color); + // Get the right direction arrow texture, if the section is foldable. + Ref<Texture2D> arrow; + if (foldable) { + if (object->editor_is_section_unfolded(section)) { + arrow = get_theme_icon(SNAME("arrow"), SNAME("Tree")); + } else { + if (is_layout_rtl()) { + arrow = get_theme_icon(SNAME("arrow_collapsed_mirrored"), SNAME("Tree")); + } else { + arrow = get_theme_icon(SNAME("arrow_collapsed"), SNAME("Tree")); + } + } + } - const int arrow_margin = 3; - Color color = get_theme_color("font_color", "Tree"); - float text_width = get_size().width - Math::round((16 + arrow_margin) * EDSCALE); - draw_string(font, Point2(rtl ? 0 : Math::round((16 + arrow_margin) * EDSCALE), font->get_ascent(font_size) + (h - font->get_height(font_size)) / 2).floor(), label, rtl ? HALIGN_RIGHT : HALIGN_LEFT, text_width, font_size, color); + bool rtl = is_layout_rtl(); - if (arrow.is_valid()) { - if (rtl) { - draw_texture(arrow, Point2(get_size().width - arrow->get_width() - Math::round(arrow_margin * EDSCALE), (h - arrow->get_height()) / 2).floor()); - } else { - draw_texture(arrow, Point2(Math::round(arrow_margin * EDSCALE), (h - arrow->get_height()) / 2).floor()); + // Compute the height of the section header. + int header_height = font->get_height(font_size); + if (arrow.is_valid()) { + header_height = MAX(header_height, arrow->get_height()); } - } + header_height += get_theme_constant(SNAME("vseparation"), SNAME("Tree")); - if (dropping && !vbox->is_visible_in_tree()) { - Color accent_color = get_theme_color("accent_color", "Editor"); - draw_rect(Rect2(Point2(), get_size()), accent_color, false); - } - } + Rect2 header_rect = Rect2(Vector2(), Vector2(get_size().width, header_height)); + Color c = bg_color; + c.a *= 0.4; + if (foldable && header_rect.has_point(get_local_mouse_position())) { + c = c.lightened(Input::get_singleton()->is_mouse_button_pressed(MOUSE_BUTTON_LEFT) ? -0.05 : 0.2); + } + draw_rect(header_rect, c); - if (p_what == NOTIFICATION_DRAG_BEGIN) { - Dictionary dd = get_viewport()->gui_get_drag_data(); + const int arrow_margin = 2; + const int arrow_width = arrow.is_valid() ? arrow->get_width() : 0; + Color color = get_theme_color(SNAME("font_color")); + float text_width = get_size().width - Math::round(arrow_width + arrow_margin * EDSCALE); + draw_string(font, Point2(rtl ? 0 : Math::round(arrow_width + arrow_margin * EDSCALE), font->get_ascent(font_size) + (header_height - font->get_height(font_size)) / 2).floor(), label, rtl ? HALIGN_RIGHT : HALIGN_LEFT, text_width, font_size, color); - // Only allow dropping if the section contains properties which can take the dragged data. - bool children_can_drop = false; - for (int child_idx = 0; child_idx < vbox->get_child_count(); child_idx++) { - Control *editor_property = Object::cast_to<Control>(vbox->get_child(child_idx)); + if (arrow.is_valid()) { + if (rtl) { + draw_texture(arrow, Point2(get_size().width - arrow->get_width() - Math::round(arrow_margin * EDSCALE), (header_height - arrow->get_height()) / 2).floor()); + } else { + draw_texture(arrow, Point2(Math::round(arrow_margin * EDSCALE), (header_height - arrow->get_height()) / 2).floor()); + } + } - // Test can_drop_data and can_drop_data_fw, since can_drop_data only works if set up with forwarding or if script attached. - if (editor_property && (editor_property->can_drop_data(Point2(), dd) || editor_property->call("can_drop_data_fw", Point2(), dd, this))) { - children_can_drop = true; - break; + if (dropping && !vbox->is_visible_in_tree()) { + Color accent_color = get_theme_color(SNAME("accent_color"), SNAME("Editor")); + draw_rect(Rect2(Point2(), get_size()), accent_color, false); } - } + } break; + case NOTIFICATION_DRAG_BEGIN: { + Dictionary dd = get_viewport()->gui_get_drag_data(); - dropping = children_can_drop; - update(); - } + // Only allow dropping if the section contains properties which can take the dragged data. + bool children_can_drop = false; + for (int child_idx = 0; child_idx < vbox->get_child_count(); child_idx++) { + Control *editor_property = Object::cast_to<Control>(vbox->get_child(child_idx)); - if (p_what == NOTIFICATION_DRAG_END) { - dropping = false; - update(); - } + // Test can_drop_data and can_drop_data_fw, since can_drop_data only works if set up with forwarding or if script attached. + if (editor_property && (editor_property->can_drop_data(Point2(), dd) || editor_property->call("_can_drop_data_fw", Point2(), dd, this))) { + children_can_drop = true; + break; + } + } - if (p_what == NOTIFICATION_MOUSE_ENTER) { - if (dropping) { - dropping_unfold_timer->start(); - } - } + dropping = children_can_drop; + update(); + } break; + case NOTIFICATION_DRAG_END: { + dropping = false; + update(); + } break; + case NOTIFICATION_MOUSE_ENTER: { + if (dropping) { + dropping_unfold_timer->start(); + } + update(); + } break; - if (p_what == NOTIFICATION_MOUSE_EXIT) { - if (dropping) { - dropping_unfold_timer->stop(); - } + case NOTIFICATION_MOUSE_EXIT: { + if (dropping) { + dropping_unfold_timer->stop(); + } + update(); + } break; } } @@ -1296,10 +1366,10 @@ Size2 EditorInspectorSection::get_minimum_size() const { ms.height = MAX(ms.height, minsize.height); } - Ref<Font> font = get_theme_font("font", "Tree"); - int font_size = get_theme_font_size("font_size", "Tree"); - ms.height += font->get_height(font_size) + get_theme_constant("vseparation", "Tree"); - ms.width += get_theme_constant("inspector_margin", "Editor"); + Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Tree")); + int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Tree")); + ms.height += font->get_height(font_size) + get_theme_constant(SNAME("vseparation"), SNAME("Tree")); + ms.width += get_theme_constant(SNAME("inspector_margin"), SNAME("Editor")); return ms; } @@ -1313,6 +1383,7 @@ void EditorInspectorSection::setup(const String &p_section, const String &p_labe if (!foldable && !vbox_added) { add_child(vbox); + move_child(vbox, 0); vbox_added = true; } @@ -1326,15 +1397,17 @@ void EditorInspectorSection::setup(const String &p_section, const String &p_labe } } -void EditorInspectorSection::_gui_input(const Ref<InputEvent> &p_event) { +void EditorInspectorSection::gui_input(const Ref<InputEvent> &p_event) { + ERR_FAIL_COND(p_event.is_null()); + if (!foldable) { return; } Ref<InputEventMouseButton> mb = p_event; - if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) { - Ref<Font> font = get_theme_font("font", "Tree"); - int font_size = get_theme_font_size("font_size", "Tree"); + if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MOUSE_BUTTON_LEFT) { + Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Tree")); + int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Tree")); if (mb->get_position().y > font->get_height(font_size)) { //clicked outside return; } @@ -1345,6 +1418,8 @@ void EditorInspectorSection::_gui_input(const Ref<InputEvent> &p_event) { } else { fold(); } + } else if (mb.is_valid() && !mb->is_pressed()) { + update(); } } @@ -1370,7 +1445,7 @@ void EditorInspectorSection::fold() { } if (!vbox_added) { - return; //kinda pointless + return; } object->editor_set_section_unfold(section, false); @@ -1383,7 +1458,6 @@ void EditorInspectorSection::_bind_methods() { ClassDB::bind_method(D_METHOD("get_vbox"), &EditorInspectorSection::get_vbox); ClassDB::bind_method(D_METHOD("unfold"), &EditorInspectorSection::unfold); ClassDB::bind_method(D_METHOD("fold"), &EditorInspectorSection::fold); - ClassDB::bind_method(D_METHOD("_gui_input"), &EditorInspectorSection::_gui_input); } EditorInspectorSection::EditorInspectorSection() { @@ -1408,11 +1482,737 @@ EditorInspectorSection::~EditorInspectorSection() { //////////////////////////////////////////////// //////////////////////////////////////////////// +int EditorInspectorArray::_get_array_count() { + if (mode == MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION) { + List<PropertyInfo> object_property_list; + object->get_property_list(&object_property_list); + return _extract_properties_as_array(object_property_list).size(); + } else if (mode == MODE_USE_COUNT_PROPERTY) { + bool valid; + int count = object->get(count_property, &valid); + ERR_FAIL_COND_V_MSG(!valid, 0, vformat("%s is not a valid property to be used as array count.", count_property)); + return count; + } + return 0; +} + +void EditorInspectorArray::_add_button_pressed() { + _move_element(-1, -1); +} + +void EditorInspectorArray::_first_page_button_pressed() { + emit_signal("page_change_request", 0); +} + +void EditorInspectorArray::_prev_page_button_pressed() { + emit_signal("page_change_request", MAX(0, page - 1)); +} + +void EditorInspectorArray::_page_line_edit_text_submitted(String p_text) { + if (p_text.is_valid_int()) { + int new_page = p_text.to_int() - 1; + new_page = MIN(MAX(0, new_page), max_page); + page_line_edit->set_text(Variant(new_page)); + emit_signal("page_change_request", new_page); + } else { + page_line_edit->set_text(Variant(page)); + } +} + +void EditorInspectorArray::_next_page_button_pressed() { + emit_signal("page_change_request", MIN(max_page, page + 1)); +} + +void EditorInspectorArray::_last_page_button_pressed() { + emit_signal("page_change_request", max_page); +} + +void EditorInspectorArray::_rmb_popup_id_pressed(int p_id) { + switch (p_id) { + case OPTION_MOVE_UP: + if (popup_array_index_pressed > 0) { + _move_element(popup_array_index_pressed, popup_array_index_pressed - 1); + } + break; + case OPTION_MOVE_DOWN: + if (popup_array_index_pressed < count - 1) { + _move_element(popup_array_index_pressed, popup_array_index_pressed + 2); + } + break; + case OPTION_NEW_BEFORE: + _move_element(-1, popup_array_index_pressed); + break; + case OPTION_NEW_AFTER: + _move_element(-1, popup_array_index_pressed + 1); + break; + case OPTION_REMOVE: + _move_element(popup_array_index_pressed, -1); + break; + case OPTION_CLEAR_ARRAY: + _clear_array(); + break; + case OPTION_RESIZE_ARRAY: + new_size = count; + new_size_line_edit->set_text(Variant(new_size)); + resize_dialog->get_ok_button()->set_disabled(true); + resize_dialog->popup_centered(); + new_size_line_edit->grab_focus(); + new_size_line_edit->select_all(); + break; + default: + break; + } +} + +void EditorInspectorArray::_control_dropping_draw() { + int drop_position = _drop_position(); + + if (dropping && drop_position >= 0) { + Vector2 from; + Vector2 to; + if (drop_position < elements_vbox->get_child_count()) { + Transform2D xform = Object::cast_to<Control>(elements_vbox->get_child(drop_position))->get_transform(); + from = xform.xform(Vector2()); + to = xform.xform(Vector2(elements_vbox->get_size().x, 0)); + } else { + Control *child = Object::cast_to<Control>(elements_vbox->get_child(drop_position - 1)); + Transform2D xform = child->get_transform(); + from = xform.xform(Vector2(0, child->get_size().y)); + to = xform.xform(Vector2(elements_vbox->get_size().x, child->get_size().y)); + } + Color color = get_theme_color(SNAME("accent_color"), SNAME("Editor")); + control_dropping->draw_line(from, to, color, 2); + } +} + +void EditorInspectorArray::_vbox_visibility_changed() { + control_dropping->set_visible(vbox->is_visible_in_tree()); +} + +void EditorInspectorArray::_panel_draw(int p_index) { + ERR_FAIL_INDEX(p_index, (int)array_elements.size()); + + Ref<StyleBox> style = get_theme_stylebox("Focus", "EditorStyles"); + if (!style.is_valid()) { + return; + } + if (array_elements[p_index].panel->has_focus()) { + array_elements[p_index].panel->draw_style_box(style, Rect2(Vector2(), array_elements[p_index].panel->get_size())); + } +} + +void EditorInspectorArray::_panel_gui_input(Ref<InputEvent> p_event, int p_index) { + ERR_FAIL_INDEX(p_index, (int)array_elements.size()); + + Ref<InputEventKey> key_ref = p_event; + if (key_ref.is_valid()) { + const InputEventKey &key = **key_ref; + + if (array_elements[p_index].panel->has_focus() && key.is_pressed() && key.get_keycode() == KEY_DELETE) { + _move_element(begin_array_index + p_index, -1); + array_elements[p_index].panel->accept_event(); + } + } + + Ref<InputEventMouseButton> mb = p_event; + if (mb.is_valid()) { + if (mb->get_button_index() == MOUSE_BUTTON_RIGHT) { + popup_array_index_pressed = begin_array_index + p_index; + rmb_popup->set_item_disabled(OPTION_MOVE_UP, popup_array_index_pressed == 0); + rmb_popup->set_item_disabled(OPTION_MOVE_DOWN, popup_array_index_pressed == count - 1); + rmb_popup->set_position(mb->get_global_position()); + rmb_popup->set_size(Vector2()); + rmb_popup->popup(); + } + } +} + +void EditorInspectorArray::_move_element(int p_element_index, int p_to_pos) { + String action_name; + if (p_element_index < 0) { + action_name = vformat("Add element to property array with prefix %s.", array_element_prefix); + } else if (p_to_pos < 0) { + action_name = vformat("Remove element %d from property array with prefix %s.", p_element_index, array_element_prefix); + } else { + action_name = vformat("Move element %d to position %d in property array with prefix %s.", p_element_index, p_to_pos, array_element_prefix); + } + undo_redo->create_action(action_name); + if (mode == MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION) { + // Call the function. + Callable move_function = EditorNode::get_singleton()->get_editor_data().get_move_array_element_function(object->get_class_name()); + if (move_function.is_valid()) { + Variant args[] = { (Object *)undo_redo, object, array_element_prefix, p_element_index, p_to_pos }; + const Variant *args_p[] = { &args[0], &args[1], &args[2], &args[3], &args[4] }; + Variant return_value; + Callable::CallError call_error; + move_function.call(args_p, 5, return_value, call_error); + } else { + WARN_PRINT(vformat("Could not find a function to move arrays elements for class %s. Register a move element function using EditorData::add_move_array_element_function", object->get_class_name())); + } + } else if (mode == MODE_USE_COUNT_PROPERTY) { + ERR_FAIL_COND(p_to_pos < -1 || p_to_pos > count); + List<PropertyInfo> object_property_list; + object->get_property_list(&object_property_list); + + Array properties_as_array = _extract_properties_as_array(object_property_list); + properties_as_array.resize(count); + + // For undoing things + undo_redo->add_undo_property(object, count_property, properties_as_array.size()); + for (int i = 0; i < (int)properties_as_array.size(); i++) { + Dictionary d = Dictionary(properties_as_array[i]); + Array keys = d.keys(); + for (int j = 0; j < keys.size(); j++) { + String key = keys[j]; + undo_redo->add_undo_property(object, vformat(key, i), d[key]); + } + } + + if (p_element_index < 0) { + // Add an element. + properties_as_array.insert(p_to_pos < 0 ? properties_as_array.size() : p_to_pos, Dictionary()); + } else if (p_to_pos < 0) { + // Delete the element. + properties_as_array.remove(p_element_index); + } else { + // Move the element. + properties_as_array.insert(p_to_pos, properties_as_array[p_element_index].duplicate()); + properties_as_array.remove(p_to_pos < p_element_index ? p_element_index + 1 : p_element_index); + } + + // Change the array size then set the properties. + undo_redo->add_do_property(object, count_property, properties_as_array.size()); + for (int i = 0; i < (int)properties_as_array.size(); i++) { + Dictionary d = properties_as_array[i]; + Array keys = d.keys(); + for (int j = 0; j < keys.size(); j++) { + String key = keys[j]; + undo_redo->add_do_property(object, vformat(key, i), d[key]); + } + } + } + undo_redo->commit_action(); + + // Handle page change and update counts. + if (p_element_index < 0) { + int added_index = p_to_pos < 0 ? count : p_to_pos; + emit_signal("page_change_request", added_index / page_lenght); + count += 1; + } else if (p_to_pos < 0) { + count -= 1; + if (page == max_page && (MAX(0, count - 1) / page_lenght != max_page)) { + emit_signal("page_change_request", max_page - 1); + } + } + begin_array_index = page * page_lenght; + end_array_index = MIN(count, (page + 1) * page_lenght); + max_page = MAX(0, count - 1) / page_lenght; +} + +void EditorInspectorArray::_clear_array() { + undo_redo->create_action(vformat("Clear property array with prefix %s.", array_element_prefix)); + if (mode == MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION) { + for (int i = count - 1; i >= 0; i--) { + // Call the function. + Callable move_function = EditorNode::get_singleton()->get_editor_data().get_move_array_element_function(object->get_class_name()); + if (move_function.is_valid()) { + Variant args[] = { (Object *)undo_redo, object, array_element_prefix, i, -1 }; + const Variant *args_p[] = { &args[0], &args[1], &args[2], &args[3], &args[4] }; + Variant return_value; + Callable::CallError call_error; + move_function.call(args_p, 5, return_value, call_error); + } else { + WARN_PRINT(vformat("Could not find a function to move arrays elements for class %s. Register a move element function using EditorData::add_move_array_element_function", object->get_class_name())); + } + } + } else if (mode == MODE_USE_COUNT_PROPERTY) { + List<PropertyInfo> object_property_list; + object->get_property_list(&object_property_list); + + Array properties_as_array = _extract_properties_as_array(object_property_list); + properties_as_array.resize(count); + + // For undoing things + undo_redo->add_undo_property(object, count_property, count); + for (int i = 0; i < (int)properties_as_array.size(); i++) { + Dictionary d = Dictionary(properties_as_array[i]); + Array keys = d.keys(); + for (int j = 0; j < keys.size(); j++) { + String key = keys[j]; + undo_redo->add_undo_property(object, vformat(key, i), d[key]); + } + } + + // Change the array size then set the properties. + undo_redo->add_do_property(object, count_property, 0); + } + undo_redo->commit_action(); + + // Handle page change and update counts. + emit_signal("page_change_request", 0); + count = 0; + begin_array_index = 0; + end_array_index = 0; + max_page = 0; +} + +void EditorInspectorArray::_resize_array(int p_size) { + ERR_FAIL_COND(p_size < 0); + if (p_size == count) { + return; + } + + undo_redo->create_action(vformat("Resize property array with prefix %s.", array_element_prefix)); + if (p_size > count) { + if (mode == MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION) { + for (int i = count; i < p_size; i++) { + // Call the function. + Callable move_function = EditorNode::get_singleton()->get_editor_data().get_move_array_element_function(object->get_class_name()); + if (move_function.is_valid()) { + Variant args[] = { (Object *)undo_redo, object, array_element_prefix, -1, -1 }; + const Variant *args_p[] = { &args[0], &args[1], &args[2], &args[3], &args[4] }; + Variant return_value; + Callable::CallError call_error; + move_function.call(args_p, 5, return_value, call_error); + } else { + WARN_PRINT(vformat("Could not find a function to move arrays elements for class %s. Register a move element function using EditorData::add_move_array_element_function", object->get_class_name())); + } + } + } else if (mode == MODE_USE_COUNT_PROPERTY) { + undo_redo->add_undo_property(object, count_property, count); + undo_redo->add_do_property(object, count_property, p_size); + } + } else { + if (mode == MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION) { + for (int i = count - 1; i > p_size - 1; i--) { + // Call the function. + Callable move_function = EditorNode::get_singleton()->get_editor_data().get_move_array_element_function(object->get_class_name()); + if (move_function.is_valid()) { + Variant args[] = { (Object *)undo_redo, object, array_element_prefix, i, -1 }; + const Variant *args_p[] = { &args[0], &args[1], &args[2], &args[3], &args[4] }; + Variant return_value; + Callable::CallError call_error; + move_function.call(args_p, 5, return_value, call_error); + } else { + WARN_PRINT(vformat("Could not find a function to move arrays elements for class %s. Register a move element function using EditorData::add_move_array_element_function", object->get_class_name())); + } + } + } else if (mode == MODE_USE_COUNT_PROPERTY) { + List<PropertyInfo> object_property_list; + object->get_property_list(&object_property_list); + + Array properties_as_array = _extract_properties_as_array(object_property_list); + properties_as_array.resize(count); + + // For undoing things + undo_redo->add_undo_property(object, count_property, count); + for (int i = count - 1; i > p_size - 1; i--) { + Dictionary d = Dictionary(properties_as_array[i]); + Array keys = d.keys(); + for (int j = 0; j < keys.size(); j++) { + String key = keys[j]; + undo_redo->add_undo_property(object, vformat(key, i), d[key]); + } + } + + // Change the array size then set the properties. + undo_redo->add_do_property(object, count_property, p_size); + } + } + undo_redo->commit_action(); + + // Handle page change and update counts. + emit_signal("page_change_request", 0); + /* + count = 0; + begin_array_index = 0; + end_array_index = 0; + max_page = 0; + */ +} + +Array EditorInspectorArray::_extract_properties_as_array(const List<PropertyInfo> &p_list) { + Array output; + + for (const PropertyInfo &pi : p_list) { + if (pi.name.begins_with(array_element_prefix)) { + String str = pi.name.trim_prefix(array_element_prefix); + + int to_char_index = 0; + while (to_char_index < str.length()) { + if (str[to_char_index] < '0' || str[to_char_index] > '9') { + break; + } + to_char_index++; + } + if (to_char_index > 0) { + int array_index = str.left(to_char_index).to_int(); + Error error = OK; + if (array_index >= output.size()) { + error = output.resize(array_index + 1); + } + if (error == OK) { + String format_string = String(array_element_prefix) + "%d" + str.substr(to_char_index); + Dictionary dict = output[array_index]; + dict[format_string] = object->get(pi.name); + output[array_index] = dict; + } else { + WARN_PRINT(vformat("Array element %s has an index too high. Array allocaiton failed.", pi.name)); + } + } + } + } + return output; +} + +int EditorInspectorArray::_drop_position() const { + for (int i = 0; i < (int)array_elements.size(); i++) { + const ArrayElement &ae = array_elements[i]; + + Size2 size = ae.panel->get_size(); + Vector2 mp = ae.panel->get_local_mouse_position(); + + if (Rect2(Vector2(), size).has_point(mp)) { + if (mp.y < size.y / 2) { + return i; + } else { + return i + 1; + } + } + } + return -1; +} + +void EditorInspectorArray::_new_size_line_edit_text_changed(String p_text) { + bool valid = false; + ; + if (p_text.is_valid_int()) { + int val = p_text.to_int(); + if (val > 0 && val != count) { + valid = true; + } + } + resize_dialog->get_ok_button()->set_disabled(!valid); +} + +void EditorInspectorArray::_new_size_line_edit_text_submitted(String p_text) { + bool valid = false; + ; + if (p_text.is_valid_int()) { + int val = p_text.to_int(); + if (val > 0 && val != count) { + new_size = val; + valid = true; + } + } + if (valid) { + resize_dialog->hide(); + _resize_array(new_size); + } else { + new_size_line_edit->set_text(Variant(new_size)); + } +} + +void EditorInspectorArray::_resize_dialog_confirmed() { + _new_size_line_edit_text_submitted(new_size_line_edit->get_text()); +} + +void EditorInspectorArray::_setup() { + // Setup counts. + count = _get_array_count(); + begin_array_index = page * page_lenght; + end_array_index = MIN(count, (page + 1) * page_lenght); + max_page = MAX(0, count - 1) / page_lenght; + array_elements.resize(MAX(0, end_array_index - begin_array_index)); + if (page < 0 || page > max_page) { + WARN_PRINT(vformat("Invalid page number %d", page)); + page = CLAMP(page, 0, max_page); + } + + for (int i = 0; i < (int)array_elements.size(); i++) { + ArrayElement &ae = array_elements[i]; + + // Panel and its hbox. + ae.panel = memnew(PanelContainer); + ae.panel->set_focus_mode(FOCUS_ALL); + ae.panel->set_mouse_filter(MOUSE_FILTER_PASS); + ae.panel->set_drag_forwarding(this); + ae.panel->set_meta("index", begin_array_index + i); + ae.panel->set_tooltip(vformat(TTR("Element %d: %s%d*"), i, array_element_prefix, i)); + ae.panel->connect("focus_entered", callable_mp((CanvasItem *)ae.panel, &PanelContainer::update)); + ae.panel->connect("focus_exited", callable_mp((CanvasItem *)ae.panel, &PanelContainer::update)); + ae.panel->connect("draw", callable_bind(callable_mp(this, &EditorInspectorArray::_panel_draw), i)); + ae.panel->connect("gui_input", callable_bind(callable_mp(this, &EditorInspectorArray::_panel_gui_input), i)); + ae.panel->add_theme_style_override(SNAME("panel"), i % 2 ? odd_style : even_style); + elements_vbox->add_child(ae.panel); + + ae.margin = memnew(MarginContainer); + ae.margin->set_mouse_filter(MOUSE_FILTER_PASS); + if (is_inside_tree()) { + Size2 min_size = get_theme_stylebox("Focus", "EditorStyles")->get_minimum_size(); + ae.margin->add_theme_constant_override("margin_left", min_size.x / 2); + ae.margin->add_theme_constant_override("margin_top", min_size.y / 2); + ae.margin->add_theme_constant_override("margin_right", min_size.x / 2); + ae.margin->add_theme_constant_override("margin_bottom", min_size.y / 2); + } + ae.panel->add_child(ae.margin); + + ae.hbox = memnew(HBoxContainer); + ae.hbox->set_h_size_flags(SIZE_EXPAND_FILL); + ae.hbox->set_v_size_flags(SIZE_EXPAND_FILL); + ae.margin->add_child(ae.hbox); + + // Move button. + ae.move_texture_rect = memnew(TextureRect); + ae.move_texture_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED); + if (is_inside_tree()) { + ae.move_texture_rect->set_texture(get_theme_icon(SNAME("TripleBar"), SNAME("EditorIcons"))); + } + ae.hbox->add_child(ae.move_texture_rect); + + // Right vbox. + ae.vbox = memnew(VBoxContainer); + ae.vbox->set_h_size_flags(SIZE_EXPAND_FILL); + ae.vbox->set_v_size_flags(SIZE_EXPAND_FILL); + ae.hbox->add_child(ae.vbox); + } + + // Hide/show the add button. + add_button->set_visible(page == max_page); + + if (max_page == 0) { + hbox_pagination->hide(); + } else { + // Update buttons. + first_page_button->set_disabled(page == 0); + prev_page_button->set_disabled(page == 0); + next_page_button->set_disabled(page == max_page); + last_page_button->set_disabled(page == max_page); + + // Update page number and page count. + page_line_edit->set_text(vformat("%d", page + 1)); + page_count_label->set_text(vformat("/ %d", max_page + 1)); + } +} + +Variant EditorInspectorArray::get_drag_data_fw(const Point2 &p_point, Control *p_from) { + int index = p_from->get_meta("index"); + Dictionary dict; + dict["type"] = "property_array_element"; + dict["property_array_prefix"] = array_element_prefix; + dict["index"] = index; + + return dict; +} + +void EditorInspectorArray::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) { + Dictionary dict = p_data; + + int to_drop = dict["index"]; + int drop_position = _drop_position(); + if (drop_position < 0) { + return; + } + _move_element(to_drop, begin_array_index + drop_position); +} + +bool EditorInspectorArray::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const { + // First, update drawing. + control_dropping->update(); + + if (p_data.get_type() != Variant::DICTIONARY) { + return false; + } + Dictionary dict = p_data; + int drop_position = _drop_position(); + if (!dict.has("type") || dict["type"] != "property_array_element" || String(dict["property_array_prefix"]) != array_element_prefix || drop_position < 0) { + return false; + } + + // Check in dropping at the given index does indeed move the item. + int moved_array_index = (int)dict["index"]; + int drop_array_index = begin_array_index + drop_position; + + return drop_array_index != moved_array_index && drop_array_index - 1 != moved_array_index; +} + +void EditorInspectorArray::_notification(int p_what) { + switch (p_what) { + case NOTIFICATION_ENTER_TREE: + case NOTIFICATION_THEME_CHANGED: { + Color color = get_theme_color(SNAME("dark_color_1"), SNAME("Editor")); + odd_style->set_bg_color(color.lightened(0.15)); + even_style->set_bg_color(color.darkened(0.15)); + + for (int i = 0; i < (int)array_elements.size(); i++) { + ArrayElement &ae = array_elements[i]; + ae.move_texture_rect->set_texture(get_theme_icon(SNAME("TripleBar"), SNAME("EditorIcons"))); + + Size2 min_size = get_theme_stylebox("Focus", "EditorStyles")->get_minimum_size(); + ae.margin->add_theme_constant_override("margin_left", min_size.x / 2); + ae.margin->add_theme_constant_override("margin_top", min_size.y / 2); + ae.margin->add_theme_constant_override("margin_right", min_size.x / 2); + ae.margin->add_theme_constant_override("margin_bottom", min_size.y / 2); + } + + add_button->set_icon(get_theme_icon(SNAME("Add"), SNAME("EditorIcons"))); + first_page_button->set_icon(get_theme_icon(SNAME("PageFirst"), SNAME("EditorIcons"))); + prev_page_button->set_icon(get_theme_icon(SNAME("PagePrevious"), SNAME("EditorIcons"))); + next_page_button->set_icon(get_theme_icon(SNAME("PageNext"), SNAME("EditorIcons"))); + last_page_button->set_icon(get_theme_icon(SNAME("PageLast"), SNAME("EditorIcons"))); + minimum_size_changed(); + } break; + case NOTIFICATION_DRAG_BEGIN: { + Dictionary dict = get_viewport()->gui_get_drag_data(); + if (dict.has("type") && dict["type"] == "property_array_element" && String(dict["property_array_prefix"]) == array_element_prefix) { + dropping = true; + control_dropping->update(); + } + } break; + case NOTIFICATION_DRAG_END: { + if (dropping) { + dropping = false; + control_dropping->update(); + } + } break; + } +} + +void EditorInspectorArray::_bind_methods() { + ClassDB::bind_method(D_METHOD("_get_drag_data_fw"), &EditorInspectorArray::get_drag_data_fw); + ClassDB::bind_method(D_METHOD("_can_drop_data_fw"), &EditorInspectorArray::can_drop_data_fw); + ClassDB::bind_method(D_METHOD("_drop_data_fw"), &EditorInspectorArray::drop_data_fw); + + ADD_SIGNAL(MethodInfo("page_change_request")); +} + +void EditorInspectorArray::set_undo_redo(UndoRedo *p_undo_redo) { + undo_redo = p_undo_redo; +} + +void EditorInspectorArray::setup_with_move_element_function(Object *p_object, String p_label, const StringName &p_array_element_prefix, int p_page, const Color &p_bg_color, bool p_foldable) { + count_property = ""; + mode = MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION; + array_element_prefix = p_array_element_prefix; + page = p_page; + + EditorInspectorSection::setup(String(p_array_element_prefix) + "_array", p_label, p_object, p_bg_color, p_foldable); + + _setup(); +} + +void EditorInspectorArray::setup_with_count_property(Object *p_object, String p_label, const StringName &p_count_property, const StringName &p_array_element_prefix, int p_page, const Color &p_bg_color, bool p_foldable) { + count_property = p_count_property; + mode = MODE_USE_COUNT_PROPERTY; + array_element_prefix = p_array_element_prefix; + page = p_page; + + EditorInspectorSection::setup(String(count_property) + "_array", p_label, p_object, p_bg_color, p_foldable); + + _setup(); +} + +VBoxContainer *EditorInspectorArray::get_vbox(int p_index) { + if (p_index >= begin_array_index && p_index < end_array_index) { + return array_elements[p_index - begin_array_index].vbox; + } else if (p_index < 0) { + return vbox; + } else { + return nullptr; + } +} + +EditorInspectorArray::EditorInspectorArray() { + set_mouse_filter(Control::MOUSE_FILTER_STOP); + + odd_style.instantiate(); + even_style.instantiate(); + + rmb_popup = memnew(PopupMenu); + rmb_popup->add_item(TTR("Move Up"), OPTION_MOVE_UP); + rmb_popup->add_item(TTR("Move Down"), OPTION_MOVE_DOWN); + rmb_popup->add_separator(); + rmb_popup->add_item(TTR("Insert New Before"), OPTION_NEW_BEFORE); + rmb_popup->add_item(TTR("Insert New After"), OPTION_NEW_AFTER); + rmb_popup->add_separator(); + rmb_popup->add_item(TTR("Remove"), OPTION_REMOVE); + rmb_popup->add_separator(); + rmb_popup->add_item(TTR("Clear Array"), OPTION_CLEAR_ARRAY); + rmb_popup->add_item(TTR("Resize Array..."), OPTION_RESIZE_ARRAY); + rmb_popup->connect("id_pressed", callable_mp(this, &EditorInspectorArray::_rmb_popup_id_pressed)); + add_child(rmb_popup); + + elements_vbox = memnew(VBoxContainer); + elements_vbox->add_theme_constant_override("separation", 0); + vbox->add_child(elements_vbox); + + add_button = memnew(Button); + add_button->set_text(TTR("Add Element")); + add_button->set_text_align(Button::ALIGN_CENTER); + add_button->connect("pressed", callable_mp(this, &EditorInspectorArray::_add_button_pressed)); + vbox->add_child(add_button); + + hbox_pagination = memnew(HBoxContainer); + hbox_pagination->set_h_size_flags(SIZE_EXPAND_FILL); + hbox_pagination->set_alignment(HBoxContainer::ALIGN_CENTER); + vbox->add_child(hbox_pagination); + + first_page_button = memnew(Button); + first_page_button->set_flat(true); + first_page_button->connect("pressed", callable_mp(this, &EditorInspectorArray::_first_page_button_pressed)); + hbox_pagination->add_child(first_page_button); + + prev_page_button = memnew(Button); + prev_page_button->set_flat(true); + prev_page_button->connect("pressed", callable_mp(this, &EditorInspectorArray::_prev_page_button_pressed)); + hbox_pagination->add_child(prev_page_button); + + page_line_edit = memnew(LineEdit); + page_line_edit->connect("text_submitted", callable_mp(this, &EditorInspectorArray::_page_line_edit_text_submitted)); + page_line_edit->add_theme_constant_override("minimum_character_width", 2); + hbox_pagination->add_child(page_line_edit); + + page_count_label = memnew(Label); + hbox_pagination->add_child(page_count_label); + next_page_button = memnew(Button); + next_page_button->set_flat(true); + next_page_button->connect("pressed", callable_mp(this, &EditorInspectorArray::_next_page_button_pressed)); + hbox_pagination->add_child(next_page_button); + + last_page_button = memnew(Button); + last_page_button->set_flat(true); + last_page_button->connect("pressed", callable_mp(this, &EditorInspectorArray::_last_page_button_pressed)); + hbox_pagination->add_child(last_page_button); + + control_dropping = memnew(Control); + control_dropping->connect("draw", callable_mp(this, &EditorInspectorArray::_control_dropping_draw)); + control_dropping->set_mouse_filter(Control::MOUSE_FILTER_IGNORE); + add_child(control_dropping); + + resize_dialog = memnew(AcceptDialog); + resize_dialog->set_title(TTRC("Resize Array")); + resize_dialog->add_cancel_button(); + resize_dialog->connect("confirmed", callable_mp(this, &EditorInspectorArray::_resize_dialog_confirmed)); + add_child(resize_dialog); + + VBoxContainer *resize_dialog_vbox = memnew(VBoxContainer); + resize_dialog->add_child(resize_dialog_vbox); + + new_size_line_edit = memnew(LineEdit); + new_size_line_edit->connect("text_changed", callable_mp(this, &EditorInspectorArray::_new_size_line_edit_text_changed)); + new_size_line_edit->connect("text_submitted", callable_mp(this, &EditorInspectorArray::_new_size_line_edit_text_submitted)); + resize_dialog_vbox->add_margin_child(TTRC("New Size:"), new_size_line_edit); + + vbox->connect("visibility_changed", callable_mp(this, &EditorInspectorArray::_vbox_visibility_changed)); +} + +//////////////////////////////////////////////// +//////////////////////////////////////////////// Ref<EditorInspectorPlugin> EditorInspector::inspector_plugins[MAX_PLUGINS]; int EditorInspector::inspector_plugin_count = 0; -EditorProperty *EditorInspector::instantiate_property_editor(Object *p_object, Variant::Type p_type, const String &p_path, PropertyHint p_hint, const String &p_hint_text, int p_usage, bool p_wide) { +EditorProperty *EditorInspector::instantiate_property_editor(Object *p_object, const Variant::Type p_type, const String &p_path, PropertyHint p_hint, const String &p_hint_text, const uint32_t p_usage, const bool p_wide) { for (int i = inspector_plugin_count - 1; i >= 0; i--) { inspector_plugins[i]->parse_property(p_object, p_type, p_path, p_hint, p_hint_text, p_usage, p_wide); if (inspector_plugins[i]->added_editors.size()) { @@ -1483,9 +2283,9 @@ String EditorInspector::get_selected_path() const { } void EditorInspector::_parse_added_editors(VBoxContainer *current_vbox, Ref<EditorInspectorPlugin> ped) { - for (List<EditorInspectorPlugin::AddedEditor>::Element *F = ped->added_editors.front(); F; F = F->next()) { - EditorProperty *ep = Object::cast_to<EditorProperty>(F->get().property_editor); - current_vbox->add_child(F->get().property_editor); + for (const EditorInspectorPlugin::AddedEditor &F : ped->added_editors) { + EditorProperty *ep = Object::cast_to<EditorProperty>(F.property_editor); + current_vbox->add_child(F.property_editor); if (ep) { ep->object = object; @@ -1499,19 +2299,19 @@ void EditorInspector::_parse_added_editors(VBoxContainer *current_vbox, Ref<Edit ep->connect("resource_selected", callable_mp(this, &EditorInspector::_resource_selected), varray(), CONNECT_DEFERRED); ep->connect("object_id_selected", callable_mp(this, &EditorInspector::_object_id_selected), varray(), CONNECT_DEFERRED); - if (F->get().properties.size()) { - if (F->get().properties.size() == 1) { + if (F.properties.size()) { + if (F.properties.size() == 1) { //since it's one, associate: - ep->property = F->get().properties[0]; + ep->property = F.properties[0]; ep->property_usage = 0; } - if (F->get().label != String()) { - ep->set_label(F->get().label); + if (F.label != String()) { + ep->set_label(F.label); } - for (int i = 0; i < F->get().properties.size(); i++) { - String prop = F->get().properties[i]; + for (int i = 0; i < F.properties.size(); i++) { + String prop = F.properties[i]; if (!editor_property_map.has(prop)) { editor_property_map[prop] = List<EditorProperty *>(); @@ -1524,6 +2324,7 @@ void EditorInspector::_parse_added_editors(VBoxContainer *current_vbox, Ref<Edit ep->update_property(); ep->update_reload_status(); ep->set_deletable(deletable_properties); + ep->update_cache(); } } ped->added_editors.clear(); @@ -1592,18 +2393,17 @@ void EditorInspector::update_tree() { valid_plugins.push_back(inspector_plugins[i]); } + // Decide if properties should be drawn in red. bool draw_red = false; - - { + if (is_inside_tree()) { Node *nod = Object::cast_to<Node>(object); Node *es = EditorNode::get_singleton()->get_edited_scene(); if (nod && es != nod && nod->get_owner() != es) { + // Draw in red edited nodes that are not in the currently edited scene. draw_red = true; } } - // TreeItem *current_category = nullptr; - String filter = search_box ? search_box->get_text() : ""; String group; String group_base; @@ -1615,31 +2415,30 @@ void EditorInspector::update_tree() { object->get_property_list(&plist, true); _update_script_class_properties(*object, plist); - HashMap<String, VBoxContainer *> item_path; - Map<VBoxContainer *, EditorInspectorSection *> section_map; - - item_path[""] = main_vbox; + Map<VBoxContainer *, HashMap<String, VBoxContainer *>> vbox_per_path; + Map<String, EditorInspectorArray *> editor_inspector_array_per_prefix; - Color sscolor = get_theme_color("prop_subsection", "Editor"); + Color sscolor = get_theme_color(SNAME("prop_subsection"), SNAME("Editor")); - for (List<Ref<EditorInspectorPlugin>>::Element *E = valid_plugins.front(); E; E = E->next()) { - Ref<EditorInspectorPlugin> ped = E->get(); + // Get the lists of editors to add the beginning. + for (Ref<EditorInspectorPlugin> &ped : valid_plugins) { ped->parse_begin(object); _parse_added_editors(main_vbox, ped); } - for (List<PropertyInfo>::Element *I = plist.front(); I; I = I->next()) { - PropertyInfo &p = I->get(); - - //make sure the property can be edited + // Get the lists of editors for properties. + for (List<PropertyInfo>::Element *E_property = plist.front(); E_property; E_property = E_property->next()) { + PropertyInfo &p = E_property->get(); if (p.usage & PROPERTY_USAGE_SUBGROUP) { + // Setup a property sub-group. subgroup = p.name; subgroup_base = p.hint_string; continue; } else if (p.usage & PROPERTY_USAGE_GROUP) { + // Setup a property group. group = p.name; group_base = p.hint_string; subgroup = ""; @@ -1648,6 +2447,7 @@ void EditorInspector::update_tree() { continue; } else if (p.usage & PROPERTY_USAGE_CATEGORY) { + // Setup a property category. group = ""; group_base = ""; subgroup = ""; @@ -1657,11 +2457,11 @@ void EditorInspector::update_tree() { continue; } - List<PropertyInfo>::Element *N = I->next(); + // Iterate over remaining properties. If no properties in category, skip the category. + List<PropertyInfo>::Element *N = E_property->next(); bool valid = true; - //if no properties in category, skip while (N) { - if (N->get().usage & PROPERTY_USAGE_EDITOR) { + if (N->get().usage & PROPERTY_USAGE_EDITOR && (!restrict_to_basic || (N->get().usage & PROPERTY_USAGE_EDITOR_BASIC_SETTING))) { break; } if (N->get().usage & PROPERTY_USAGE_CATEGORY) { @@ -1671,31 +2471,35 @@ void EditorInspector::update_tree() { N = N->next(); } if (!valid) { - continue; //empty, ignore + continue; // Empty, ignore it. } + // Create an EditorInspectorCategory and add it to the inspector. EditorInspectorCategory *category = memnew(EditorInspectorCategory); main_vbox->add_child(category); category_vbox = nullptr; //reset String type = p.name; + + // Set the category icon. if (!ClassDB::class_exists(type) && !ScriptServer::is_global_class(type) && p.hint_string.length() && FileAccess::exists(p.hint_string)) { - Ref<Script> s = ResourceLoader::load(p.hint_string, "Script"); + // If we have a category inside a script, search for the first script with a valid icon. + Ref<Script> script = ResourceLoader::load(p.hint_string, "Script"); String base_type; - if (s.is_valid()) { - base_type = s->get_instance_base_type(); + if (script.is_valid()) { + base_type = script->get_instance_base_type(); } - while (s.is_valid()) { - StringName name = EditorNode::get_editor_data().script_class_get_name(s->get_path()); + while (script.is_valid()) { + StringName name = EditorNode::get_editor_data().script_class_get_name(script->get_path()); String icon_path = EditorNode::get_editor_data().script_class_get_icon_path(name); if (name != StringName() && icon_path.length()) { category->icon = ResourceLoader::load(icon_path, "Texture"); break; } - s = s->get_base_script(); + script = script->get_base_script(); } - if (category->icon.is_null() && has_theme_icon(base_type, "EditorIcons")) { - category->icon = get_theme_icon(base_type, "EditorIcons"); + if (category->icon.is_null() && has_theme_icon(base_type, SNAME("EditorIcons"))) { + category->icon = get_theme_icon(base_type, SNAME("EditorIcons")); } } if (category->icon.is_null()) { @@ -1703,10 +2507,12 @@ void EditorInspector::update_tree() { category->icon = EditorNode::get_singleton()->get_class_icon(type, "Object"); } } + + // Set the category label. category->label = type; - category->bg_color = get_theme_color("prop_category", "Editor"); if (use_doc_hints) { + // Sets the category tooltip to show documentation. StringName type2 = p.name; if (!class_descr_cache.has(type2)) { String descr; @@ -1721,133 +2527,224 @@ void EditorInspector::update_tree() { category->set_tooltip(p.name + "::" + (class_descr_cache[type2] == "" ? "" : class_descr_cache[type2])); } - for (List<Ref<EditorInspectorPlugin>>::Element *E = valid_plugins.front(); E; E = E->next()) { - Ref<EditorInspectorPlugin> ped = E->get(); + // Add editors at the start of a category. + for (Ref<EditorInspectorPlugin> &ped : valid_plugins) { ped->parse_category(object, p.name); _parse_added_editors(main_vbox, ped); } continue; - } else if (!(p.usage & PROPERTY_USAGE_EDITOR) || _is_property_disabled_by_feature_profile(p.name)) { + } else if (!(p.usage & PROPERTY_USAGE_EDITOR) || _is_property_disabled_by_feature_profile(p.name) || (restrict_to_basic && !(p.usage & PROPERTY_USAGE_EDITOR_BASIC_SETTING))) { + // Ignore properties that are not supposed to be in the inspector. continue; } + if (p.name == "script") { + // Script should go into its own category. + category_vbox = nullptr; + } + if (p.usage & PROPERTY_USAGE_HIGH_END_GFX && RS::get_singleton()->is_low_end()) { - continue; //do not show this property in low end gfx + // Do not show this property in low end gfx. + continue; } if (p.name == "script" && (hide_script || bool(object->call("_hide_script_from_inspector")))) { + // Hide script variables from inspector if required. continue; } - String basename = p.name; + // Get the path for property. + String path = p.name; - if (subgroup != "") { - if (subgroup_base != "") { - if (basename.begins_with(subgroup_base)) { - basename = basename.replace_first(subgroup_base, ""); - } else if (subgroup_base.begins_with(basename)) { - //keep it, this is used pretty often - } else { - subgroup = ""; //no longer using subgroup base, clear + // First check if we have an array that fits the prefix. + String array_prefix = ""; + int array_index = -1; + for (Map<String, EditorInspectorArray *>::Element *E = editor_inspector_array_per_prefix.front(); E; E = E->next()) { + if (p.name.begins_with(E->key()) && E->key().length() > array_prefix.length()) { + array_prefix = E->key(); + } + } + + if (!array_prefix.is_empty()) { + // If we have an array element, find the according index in array. + String str = p.name.trim_prefix(array_prefix); + int to_char_index = 0; + while (to_char_index < str.length()) { + if (str[to_char_index] < '0' || str[to_char_index] > '9') { + break; } + to_char_index++; + } + if (to_char_index > 0) { + array_index = str.left(to_char_index).to_int(); + } else { + array_prefix = ""; } } - if (group != "") { - if (group_base != "" && subgroup == "") { - if (basename.begins_with(group_base)) { - basename = basename.replace_first(group_base, ""); - } else if (group_base.begins_with(basename)) { - //keep it, this is used pretty often + + if (!array_prefix.is_empty()) { + path = path.trim_prefix(array_prefix); + int char_index = path.find("/"); + if (char_index >= 0) { + path = path.right(-char_index - 1); + } else { + path = vformat(TTR("Element %s"), array_index); + } + } else { + // Check if we exit or not a subgroup. If there is a prefix, remove it from the property label string. + if (subgroup != "" && subgroup_base != "") { + if (path.begins_with(subgroup_base)) { + path = path.trim_prefix(subgroup_base); + } else if (subgroup_base.begins_with(path)) { + // Keep it, this is used pretty often. + } else { + subgroup = ""; // The prefix changed, we are no longer in the subgroup. + } + } + + // Check if we exit or not a group. If there is a prefix, remove it from the property label string. + if (group != "" && group_base != "" && subgroup == "") { + if (path.begins_with(group_base)) { + path = path.trim_prefix(group_base); + } else if (group_base.begins_with(path)) { + // Keep it, this is used pretty often. } else { - group = ""; //no longer using group base, clear + group = ""; // The prefix changed, we are no longer in the group. subgroup = ""; } } - } - if (subgroup != "") { - basename = subgroup + "/" + basename; - } - if (group != "") { - basename = group + "/" + basename; - } - String name = (basename.find("/") != -1) ? basename.right(basename.rfind("/") + 1) : basename; + // Add the group and subgroup to the path. + if (subgroup != "") { + path = subgroup + "/" + path; + } + if (group != "") { + path = group + "/" + path; + } + } + // Get the property label's string. + String property_label_string = (path.find("/") != -1) ? path.substr(path.rfind("/") + 1) : path; if (capitalize_paths) { - int dot = name.find("."); + // Capitalize paths. + int dot = property_label_string.find("."); if (dot != -1) { - String ov = name.right(dot); - name = name.substr(0, dot); - name = name.capitalize(); - name += ov; - + String ov = property_label_string.substr(dot); + property_label_string = property_label_string.substr(0, dot); + property_label_string = property_label_string.capitalize(); + property_label_string += ov; } else { - name = name.capitalize(); + property_label_string = property_label_string.capitalize(); } } - String path = basename.left(basename.rfind("/")); + // Remove the property from the path. + int idx = path.rfind("/"); + if (idx > -1) { + path = path.left(idx); + } else { + path = ""; + } + // Ignore properties that do not fit the filter. if (use_filter && filter != "") { - String cat = path; - - if (capitalize_paths) { - cat = cat.capitalize(); - } - - if (!filter.is_subsequence_ofi(cat) && !filter.is_subsequence_ofi(name) && property_prefix.to_lower().find(filter.to_lower()) == -1) { + if (!filter.is_subsequence_ofi(path) && !filter.is_subsequence_ofi(property_label_string) && property_prefix.to_lower().find(filter.to_lower()) == -1) { continue; } } + // Recreate the category vbox if it was reset. if (category_vbox == nullptr) { category_vbox = memnew(VBoxContainer); main_vbox->add_child(category_vbox); } - VBoxContainer *current_vbox = main_vbox; + // Find the correct section/vbox to add the property editor to. + VBoxContainer *root_vbox = array_prefix.is_empty() ? main_vbox : editor_inspector_array_per_prefix[array_prefix]->get_vbox(array_index); + if (!root_vbox) { + continue; + } - { - String acc_path = ""; - int level = 1; - for (int i = 0; i < path.get_slice_count("/"); i++) { - String path_name = path.get_slice("/", i); - if (i > 0) { - acc_path += "/"; - } - acc_path += path_name; - if (!item_path.has(acc_path)) { - EditorInspectorSection *section = memnew(EditorInspectorSection); - current_vbox->add_child(section); - sections.push_back(section); - - if (capitalize_paths) { - path_name = path_name.capitalize(); - } + if (!vbox_per_path.has(root_vbox)) { + vbox_per_path[root_vbox] = HashMap<String, VBoxContainer *>(); + vbox_per_path[root_vbox][""] = root_vbox; + } - Color c = sscolor; - c.a /= level; - section->setup(acc_path, path_name, object, c, use_folding); + VBoxContainer *current_vbox = root_vbox; + String acc_path = ""; + int level = 1; - VBoxContainer *vb = section->get_vbox(); - item_path[acc_path] = vb; - section_map[vb] = section; + Vector<String> components = path.split("/"); + for (int i = 0; i < components.size(); i++) { + String component = components[i]; + acc_path += (i > 0) ? "/" + component : component; + + if (!vbox_per_path[root_vbox].has(acc_path)) { + // If the section does not exists, create it. + EditorInspectorSection *section = memnew(EditorInspectorSection); + current_vbox->add_child(section); + sections.push_back(section); + + if (capitalize_paths) { + component = component.capitalize(); } - current_vbox = item_path[acc_path]; - level = (MIN(level + 1, 4)); - } - if (current_vbox == main_vbox) { - //do not add directly to the main vbox, given it has no spacing - if (category_vbox == nullptr) { - category_vbox = memnew(VBoxContainer); + Color c = sscolor; + c.a /= level; + section->setup(acc_path, component, object, c, use_folding); + + vbox_per_path[root_vbox][acc_path] = section->get_vbox(); + } + + current_vbox = vbox_per_path[root_vbox][acc_path]; + level = (MIN(level + 1, 4)); + } + + // If we did not find a section to add the property to, add it to the category vbox instead (the category vbox handles margins correctly). + if (current_vbox == main_vbox) { + current_vbox = category_vbox; + } + + // Check if the property is an array counter, if so create a dedicated array editor for the array. + if (p.usage & PROPERTY_USAGE_ARRAY) { + EditorInspectorArray *editor_inspector_array = nullptr; + StringName array_element_prefix; + Color c = sscolor; + c.a /= level; + if (p.type == Variant::NIL) { + // Setup the array to use a method to create/move/delete elements. + array_element_prefix = p.class_name; + editor_inspector_array = memnew(EditorInspectorArray); + + String array_label = (path.find("/") != -1) ? path.substr(path.rfind("/") + 1) : path; + array_label = property_label_string.capitalize(); + int page = per_array_page.has(array_element_prefix) ? per_array_page[array_element_prefix] : 0; + editor_inspector_array->setup_with_move_element_function(object, array_label, array_element_prefix, page, c, use_folding); + editor_inspector_array->connect("page_change_request", callable_mp(this, &EditorInspector::_page_change_request), varray(array_element_prefix)); + editor_inspector_array->set_undo_redo(undo_redo); + } else if (p.type == Variant::INT) { + // Setup the array to use the count property and built-in functions to create/move/delete elements. + Vector<String> class_name_components = String(p.class_name).split(","); + if (class_name_components.size() == 2) { + array_element_prefix = class_name_components[1]; + editor_inspector_array = memnew(EditorInspectorArray); + int page = per_array_page.has(array_element_prefix) ? per_array_page[array_element_prefix] : 0; + editor_inspector_array->setup_with_count_property(object, class_name_components[0], p.name, array_element_prefix, page, c, use_folding); + editor_inspector_array->connect("page_change_request", callable_mp(this, &EditorInspector::_page_change_request), varray(array_element_prefix)); + editor_inspector_array->set_undo_redo(undo_redo); } - current_vbox = category_vbox; } + + if (editor_inspector_array) { + current_vbox->add_child(editor_inspector_array); + editor_inspector_array_per_prefix[array_element_prefix] = editor_inspector_array; + } + continue; } + // Checkable and checked properties. bool checkable = false; bool checked = false; if (p.usage & PROPERTY_USAGE_CHECKABLE) { @@ -1855,6 +2752,9 @@ void EditorInspector::update_tree() { checked = p.usage & PROPERTY_USAGE_CHECKED; } + bool property_read_only = (p.usage & PROPERTY_USAGE_READ_ONLY) || read_only; + + // Mark properties that would require an editor restart (mostly when editing editor settings). if (p.usage & PROPERTY_USAGE_RESTART_IF_CHANGED) { restart_request_props.insert(p.name); } @@ -1862,14 +2762,19 @@ void EditorInspector::update_tree() { String doc_hint; if (use_doc_hints) { + // Build the doc hint, to use as tooltip. + + // Get the class name. StringName classname = object->get_class_name(); if (object_class != String()) { classname = object_class; } + StringName propname = property_prefix + p.name; String descr; bool found = false; + // Search for the property description in the cache. Map<StringName, Map<StringName, String>>::Element *E = descr_cache.find(classname); if (E) { Map<StringName, String>::Element *F = E->get().find(propname); @@ -1880,6 +2785,7 @@ void EditorInspector::update_tree() { } if (!found) { + // Build the property description String and add it to the cache. DocTools *dd = EditorHelp::get_doc_data(); Map<String, DocData::ClassDoc>::Element *F = dd->class_list.find(classname); while (F && descr == String()) { @@ -1891,8 +2797,7 @@ void EditorInspector::update_tree() { } Vector<String> slices = propname.operator String().split("/"); - if (slices.size() == 2 && slices[0].begins_with("custom_")) { - // Likely a theme property. + if (slices.size() == 2 && slices[0].begins_with("theme_override_")) { for (int i = 0; i < F->get().theme_properties.size(); i++) { if (F->get().theme_properties[i].name == slices[1]) { descr = DTR(F->get().theme_properties[i].description); @@ -1913,36 +2818,36 @@ void EditorInspector::update_tree() { doc_hint = descr; } - for (List<Ref<EditorInspectorPlugin>>::Element *E = valid_plugins.front(); E; E = E->next()) { - Ref<EditorInspectorPlugin> ped = E->get(); + // Seach for the inspector plugin that will handle the properties. Then add the correct property editor to it. + for (Ref<EditorInspectorPlugin> &ped : valid_plugins) { bool exclusive = ped->parse_property(object, p.type, p.name, p.hint, p.hint_string, p.usage, wide_editors); - List<EditorInspectorPlugin::AddedEditor> editors = ped->added_editors; //make a copy, since plugins may be used again in a sub-inspector + List<EditorInspectorPlugin::AddedEditor> editors = ped->added_editors; // Make a copy, since plugins may be used again in a sub-inspector. ped->added_editors.clear(); - for (List<EditorInspectorPlugin::AddedEditor>::Element *F = editors.front(); F; F = F->next()) { - EditorProperty *ep = Object::cast_to<EditorProperty>(F->get().property_editor); + for (const EditorInspectorPlugin::AddedEditor &F : editors) { + EditorProperty *ep = Object::cast_to<EditorProperty>(F.property_editor); if (ep) { - //set all this before the control gets the ENTER_TREE notification + // Set all this before the control gets the ENTER_TREE notification. ep->object = object; - if (F->get().properties.size()) { - if (F->get().properties.size() == 1) { + if (F.properties.size()) { + if (F.properties.size() == 1) { //since it's one, associate: - ep->property = F->get().properties[0]; + ep->property = F.properties[0]; ep->property_usage = p.usage; //and set label? } - if (F->get().label != String()) { - ep->set_label(F->get().label); + if (F.label != String()) { + ep->set_label(F.label); } else { - //use existin one - ep->set_label(name); + // Use the existing one. + ep->set_label(property_label_string); } - for (int i = 0; i < F->get().properties.size(); i++) { - String prop = F->get().properties[i]; + for (int i = 0; i < F.properties.size(); i++) { + String prop = F.properties[i]; if (!editor_property_map.has(prop)) { editor_property_map[prop] = List<EditorProperty *>(); @@ -1955,18 +2860,16 @@ void EditorInspector::update_tree() { ep->set_checkable(checkable); ep->set_checked(checked); ep->set_keying(keying); - - ep->set_read_only(read_only); + ep->set_read_only(property_read_only); ep->set_deletable(deletable_properties); } - current_vbox->add_child(F->get().property_editor); + current_vbox->add_child(F.property_editor); if (ep) { - ep->connect("property_changed", callable_mp(this, &EditorInspector::_property_changed)); - if (p.usage & PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED) { - ep->connect("property_changed", callable_mp(this, &EditorInspector::_property_changed_update_all), varray(), CONNECT_DEFERRED); - } + // Eventually, set other properties/signals after the property editor got added to the tree. + bool update_all = (p.usage & PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED); + ep->connect("property_changed", callable_mp(this, &EditorInspector::_property_changed), varray(update_all)); ep->connect("property_keyed", callable_mp(this, &EditorInspector::_property_keyed)); ep->connect("property_deleted", callable_mp(this, &EditorInspector::_property_deleted), varray(), CONNECT_DEFERRED); ep->connect("property_keyed_with_value", callable_mp(this, &EditorInspector::_property_keyed_with_value)); @@ -1982,6 +2885,7 @@ void EditorInspector::update_tree() { } ep->update_property(); ep->update_reload_status(); + ep->update_cache(); if (current_selected && ep->property == current_selected) { ep->select(current_focusable); @@ -1990,18 +2894,17 @@ void EditorInspector::update_tree() { } if (exclusive) { + // If we know the plugin is exclusive, we don't need to go through other plugins. break; } } } - for (List<Ref<EditorInspectorPlugin>>::Element *E = valid_plugins.front(); E; E = E->next()) { - Ref<EditorInspectorPlugin> ped = E->get(); + // Get the lists of to add at the end. + for (Ref<EditorInspectorPlugin> &ped : valid_plugins) { ped->parse_end(); _parse_added_editors(main_vbox, ped); } - - //see if this property exists and should be kept } void EditorInspector::update_property(const String &p_prop) { @@ -2009,9 +2912,10 @@ void EditorInspector::update_property(const String &p_prop) { return; } - for (List<EditorProperty *>::Element *E = editor_property_map[p_prop].front(); E; E = E->next()) { - E->get()->update_property(); - E->get()->update_reload_status(); + for (EditorProperty *E : editor_property_map[p_prop]) { + E->update_property(); + E->update_reload_status(); + E->update_cache(); } } @@ -2027,13 +2931,6 @@ void EditorInspector::_clear() { restart_request_props.clear(); } -void EditorInspector::refresh() { - if (refresh_countdown > 0 || changing) { - return; - } - refresh_countdown = EditorSettings::get_singleton()->get("docks/property_editor/auto_refresh_interval"); -} - Object *EditorInspector::get_edited_object() { return object; } @@ -2044,8 +2941,9 @@ void EditorInspector::edit(Object *p_object) { } if (object) { _clear(); - object->remove_change_receptor(this); + object->disconnect("property_list_changed", callable_mp(this, &EditorInspector::_changed_callback)); } + per_array_page.clear(); object = p_object; @@ -2054,7 +2952,7 @@ void EditorInspector::edit(Object *p_object) { if (scroll_cache.has(object->get_instance_id())) { //if exists, set something else update_scroll_request = scroll_cache[object->get_instance_id()]; //done this way because wait until full size is accommodated } - object->add_change_receptor(this); + object->connect("property_list_changed", callable_mp(this, &EditorInspector::_changed_callback)); update_tree(); } } @@ -2127,24 +3025,24 @@ bool EditorInspector::is_using_folding() { } void EditorInspector::collapse_all_folding() { - for (List<EditorInspectorSection *>::Element *E = sections.front(); E; E = E->next()) { - E->get()->fold(); + for (EditorInspectorSection *E : sections) { + E->fold(); } - for (Map<StringName, List<EditorProperty *>>::Element *F = editor_property_map.front(); F; F = F->next()) { - for (List<EditorProperty *>::Element *E = F->get().front(); E; E = E->next()) { - E->get()->collapse_all_folding(); + for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) { + for (EditorProperty *E : F.value) { + E->collapse_all_folding(); } } } void EditorInspector::expand_all_folding() { - for (List<EditorInspectorSection *>::Element *E = sections.front(); E; E = E->next()) { - E->get()->unfold(); + for (EditorInspectorSection *E : sections) { + E->unfold(); } - for (Map<StringName, List<EditorProperty *>>::Element *F = editor_property_map.front(); F; F = F->next()) { - for (List<EditorProperty *>::Element *E = F->get().front(); E; E = E->next()) { - E->get()->expand_all_folding(); + for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) { + for (EditorProperty *E : F.value) { + E->expand_all_folding(); } } } @@ -2161,23 +3059,45 @@ void EditorInspector::set_use_wide_editors(bool p_enable) { wide_editors = p_enable; } +void EditorInspector::_update_inspector_bg() { + if (sub_inspector) { + int count_subinspectors = 0; + Node *n = get_parent(); + while (n) { + EditorInspector *ei = Object::cast_to<EditorInspector>(n); + if (ei && ei->sub_inspector) { + count_subinspectors++; + } + n = n->get_parent(); + } + count_subinspectors = MIN(15, count_subinspectors); + add_theme_style_override("bg", get_theme_stylebox("sub_inspector_bg" + itos(count_subinspectors), "Editor")); + } else { + add_theme_style_override("bg", get_theme_stylebox(SNAME("bg"), SNAME("Tree"))); + } +} void EditorInspector::set_sub_inspector(bool p_enable) { sub_inspector = p_enable; if (!is_inside_tree()) { return; } - if (sub_inspector) { - add_theme_style_override("bg", get_theme_stylebox("sub_inspector_bg", "Editor")); - } else { - add_theme_style_override("bg", get_theme_stylebox("bg", "Tree")); - } + _update_inspector_bg(); } void EditorInspector::set_use_deletable_properties(bool p_enabled) { deletable_properties = p_enabled; } +void EditorInspector::_page_change_request(int p_new_page, const StringName &p_array_prefix) { + int prev_page = per_array_page.has(p_array_prefix) ? per_array_page[p_array_prefix] : 0; + int new_page = MAX(0, p_new_page); + if (new_page != prev_page) { + per_array_page[p_array_prefix] = new_page; + update_tree_pending = true; + } +} + void EditorInspector::_edit_request_change(Object *p_object, const String &p_property) { if (object != p_object) { //may be undoing/redoing for a non edited object, so ignore return; @@ -2196,9 +3116,9 @@ void EditorInspector::_edit_request_change(Object *p_object, const String &p_pro void EditorInspector::_edit_set(const String &p_name, const Variant &p_value, bool p_refresh_all, const String &p_changed_field) { if (autoclear && editor_property_map.has(p_name)) { - for (List<EditorProperty *>::Element *E = editor_property_map[p_name].front(); E; E = E->next()) { - if (E->get()->is_checkable()) { - E->get()->set_checked(true); + for (EditorProperty *E : editor_property_map[p_name]) { + if (E->is_checkable()) { + E->set_checked(true); } } } @@ -2218,10 +3138,33 @@ void EditorInspector::_edit_set(const String &p_name, const Variant &p_value, bo _edit_request_change(object, p_name); emit_signal(_prop_edited, p_name); } else { - undo_redo->create_action(TTR("Set") + " " + p_name, UndoRedo::MERGE_ENDS); + undo_redo->create_action(vformat(TTR("Set %s"), p_name), UndoRedo::MERGE_ENDS); undo_redo->add_do_property(object, p_name, p_value); undo_redo->add_undo_property(object, p_name, object->get(p_name)); + PropertyInfo prop_info; + if (ClassDB::get_property_info(object->get_class_name(), p_name, &prop_info)) { + for (const String &linked_prop : prop_info.linked_properties) { + undo_redo->add_undo_property(object, linked_prop, object->get(linked_prop)); + } + } + + Variant v_undo_redo = (Object *)undo_redo; + Variant v_object = object; + Variant v_name = p_name; + for (int i = 0; i < EditorNode::get_singleton()->get_editor_data().get_undo_redo_inspector_hook_callback().size(); i++) { + const Callable &callback = EditorNode::get_singleton()->get_editor_data().get_undo_redo_inspector_hook_callback()[i]; + + const Variant *p_arguments[] = { &v_undo_redo, &v_object, &v_name, &p_value }; + Variant return_value; + Callable::CallError call_error; + + callback.call(p_arguments, 4, return_value, call_error); + if (call_error.error != Callable::CallError::CALL_OK) { + ERR_PRINT("Invalid UndoRedo callback."); + } + } + if (p_refresh_all) { undo_redo->add_do_method(this, "_edit_request_change", object, ""); undo_redo->add_undo_method(this, "_edit_request_change", object, ""); @@ -2249,35 +3192,31 @@ void EditorInspector::_edit_set(const String &p_name, const Variant &p_value, bo } if (editor_property_map.has(p_name)) { - for (List<EditorProperty *>::Element *E = editor_property_map[p_name].front(); E; E = E->next()) { - E->get()->update_reload_status(); + for (EditorProperty *E : editor_property_map[p_name]) { + E->update_reload_status(); } } } -void EditorInspector::_property_changed(const String &p_path, const Variant &p_value, const String &p_name, bool p_changing) { +void EditorInspector::_property_changed(const String &p_path, const Variant &p_value, const String &p_name, bool p_changing, bool p_update_all) { // The "changing" variable must be true for properties that trigger events as typing occurs, // like "text_changed" signal. E.g. text property of Label, Button, RichTextLabel, etc. if (p_changing) { this->changing++; } - _edit_set(p_path, p_value, false, p_name); + _edit_set(p_path, p_value, p_update_all, p_name); if (p_changing) { this->changing--; } if (restart_request_props.has(p_path)) { - emit_signal("restart_requested"); + emit_signal(SNAME("restart_requested")); } } -void EditorInspector::_property_changed_update_all(const String &p_path, const Variant &p_value, const String &p_name, bool p_changing) { - update_tree(); -} - -void EditorInspector::_multiple_properties_changed(Vector<String> p_paths, Array p_values) { +void EditorInspector::_multiple_properties_changed(Vector<String> p_paths, Array p_values, bool p_changing) { ERR_FAIL_COND(p_paths.size() == 0 || p_values.size() == 0); ERR_FAIL_COND(p_paths.size() != p_values.size()); String names; @@ -2291,12 +3230,16 @@ void EditorInspector::_multiple_properties_changed(Vector<String> p_paths, Array for (int i = 0; i < p_paths.size(); i++) { _edit_set(p_paths[i], p_values[i], false, ""); if (restart_request_props.has(p_paths[i])) { - emit_signal("restart_requested"); + emit_signal(SNAME("restart_requested")); } } - changing++; + if (p_changing) { + changing++; + } undo_redo->commit_action(); - changing--; + if (p_changing) { + changing--; + } } void EditorInspector::_property_keyed(const String &p_path, bool p_advance) { @@ -2304,16 +3247,15 @@ void EditorInspector::_property_keyed(const String &p_path, bool p_advance) { return; } - emit_signal("property_keyed", p_path, object->get(p_path), p_advance); //second param is deprecated + emit_signal(SNAME("property_keyed"), p_path, object->get(p_path), p_advance); //second param is deprecated } void EditorInspector::_property_deleted(const String &p_path) { - print_line("deleted pressed?"); if (!object) { return; } - emit_signal("property_deleted", p_path); //second param is deprecated + emit_signal(SNAME("property_deleted"), p_path); //second param is deprecated } void EditorInspector::_property_keyed_with_value(const String &p_path, const Variant &p_value, bool p_advance) { @@ -2321,7 +3263,7 @@ void EditorInspector::_property_keyed_with_value(const String &p_path, const Var return; } - emit_signal("property_keyed", p_path, p_value, p_advance); //second param is deprecated + emit_signal(SNAME("property_keyed"), p_path, p_value, p_advance); //second param is deprecated } void EditorInspector::_property_checked(const String &p_path, bool p_checked) { @@ -2337,10 +3279,10 @@ void EditorInspector::_property_checked(const String &p_path, bool p_checked) { Variant to_create; List<PropertyInfo> pinfo; object->get_property_list(&pinfo); - for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) { - if (E->get().name == p_path) { + for (const PropertyInfo &E : pinfo) { + if (E.name == p_path) { Callable::CallError ce; - Variant::construct(E->get().type, to_create, nullptr, 0, ce); + Variant::construct(E.type, to_create, nullptr, 0, ce); break; } } @@ -2348,14 +3290,15 @@ void EditorInspector::_property_checked(const String &p_path, bool p_checked) { } if (editor_property_map.has(p_path)) { - for (List<EditorProperty *>::Element *E = editor_property_map[p_path].front(); E; E = E->next()) { - E->get()->update_property(); - E->get()->update_reload_status(); + for (EditorProperty *E : editor_property_map[p_path]) { + E->update_property(); + E->update_reload_status(); + E->update_cache(); } } } else { - emit_signal("property_toggled", p_path, p_checked); + emit_signal(SNAME("property_toggled"), p_path, p_checked); } } @@ -2363,26 +3306,26 @@ void EditorInspector::_property_selected(const String &p_path, int p_focusable) property_selected = p_path; property_focusable = p_focusable; //deselect the others - for (Map<StringName, List<EditorProperty *>>::Element *F = editor_property_map.front(); F; F = F->next()) { - if (F->key() == property_selected) { + for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) { + if (F.key == property_selected) { continue; } - for (List<EditorProperty *>::Element *E = F->get().front(); E; E = E->next()) { - if (E->get()->is_selected()) { - E->get()->deselect(); + for (EditorProperty *E : F.value) { + if (E->is_selected()) { + E->deselect(); } } } - emit_signal("property_selected", p_path); + emit_signal(SNAME("property_selected"), p_path); } void EditorInspector::_object_id_selected(const String &p_path, ObjectID p_id) { - emit_signal("object_id_selected", p_id); + emit_signal(SNAME("object_id_selected"), p_id); } void EditorInspector::_resource_selected(const String &p_path, RES p_resource) { - emit_signal("resource_selected", p_resource, p_path); + emit_signal(SNAME("resource_selected"), p_resource, p_path); } void EditorInspector::_node_removed(Node *p_node) { @@ -2394,13 +3337,12 @@ void EditorInspector::_node_removed(Node *p_node) { void EditorInspector::_notification(int p_what) { if (p_what == NOTIFICATION_READY) { EditorFeatureProfileManager::get_singleton()->connect("current_feature_profile_changed", callable_mp(this, &EditorInspector::_feature_profile_changed)); + set_process(is_visible_in_tree()); + _update_inspector_bg(); } if (p_what == NOTIFICATION_ENTER_TREE) { - if (sub_inspector) { - add_theme_style_override("bg", get_theme_stylebox("sub_inspector_bg", "Editor")); - } else { - add_theme_style_override("bg", get_theme_stylebox("bg", "Tree")); + if (!sub_inspector) { get_tree()->connect("node_removed", callable_mp(this, &EditorInspector::_node_removed)); } } @@ -2414,20 +3356,28 @@ void EditorInspector::_notification(int p_what) { edit(nullptr); } + if (p_what == NOTIFICATION_VISIBILITY_CHANGED) { + set_process(is_visible_in_tree()); + } + if (p_what == NOTIFICATION_PROCESS) { if (update_scroll_request >= 0) { - get_v_scrollbar()->call_deferred("set_value", update_scroll_request); + get_v_scrollbar()->call_deferred(SNAME("set_value"), update_scroll_request); update_scroll_request = -1; } if (refresh_countdown > 0) { refresh_countdown -= get_process_delta_time(); if (refresh_countdown <= 0) { - for (Map<StringName, List<EditorProperty *>>::Element *F = editor_property_map.front(); F; F = F->next()) { - for (List<EditorProperty *>::Element *E = F->get().front(); E; E = E->next()) { - E->get()->update_property(); - E->get()->update_reload_status(); + for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) { + for (EditorProperty *E : F.value) { + if (!E->is_cache_valid()) { + E->update_property(); + E->update_reload_status(); + E->update_cache(); + } } } + refresh_countdown = float(EditorSettings::get_singleton()->get("docks/property_editor/auto_refresh_interval")); } } @@ -2442,9 +3392,10 @@ void EditorInspector::_notification(int p_what) { while (pending.size()) { StringName prop = pending.front()->get(); if (editor_property_map.has(prop)) { - for (List<EditorProperty *>::Element *E = editor_property_map[prop].front(); E; E = E->next()) { - E->get()->update_property(); - E->get()->update_reload_status(); + for (EditorProperty *E : editor_property_map[prop]) { + E->update_property(); + E->update_reload_status(); + E->update_cache(); } } pending.erase(pending.front()); @@ -2455,19 +3406,17 @@ void EditorInspector::_notification(int p_what) { } if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) { - if (sub_inspector) { - add_theme_style_override("bg", get_theme_stylebox("sub_inspector_bg", "Editor")); - } else if (is_inside_tree()) { - add_theme_style_override("bg", get_theme_stylebox("bg", "Tree")); - } + _update_inspector_bg(); update_tree(); } } -void EditorInspector::_changed_callback(Object *p_changed, const char *p_prop) { - //this is called when property change is notified via _change_notify() - _edit_request_change(p_changed, p_prop); +void EditorInspector::_changed_callback() { + //this is called when property change is notified via notify_property_list_changed() + if (object != nullptr) { + _edit_request_change(object, String()); + } } void EditorInspector::_vscroll_changed(double p_offset) { @@ -2506,22 +3455,11 @@ void EditorInspector::_update_script_class_properties(const Object &p_object, Li return; } - List<StringName> classes; - Map<StringName, String> paths; + List<Ref<Script>> classes; // NodeC -> NodeB -> NodeA while (script.is_valid()) { - String n = EditorNode::get_editor_data().script_class_get_name(script->get_path()); - if (n.length()) { - classes.push_front(n); - } else if (script->get_path() != String() && script->get_path().find("::") == -1) { - n = script->get_path().get_file(); - classes.push_front(n); - } else { - n = TTR("Built-in script"); - classes.push_front(n); - } - paths[n] = script->get_path(); + classes.push_front(script); script = script->get_base_script(); } @@ -2530,9 +3468,9 @@ void EditorInspector::_update_script_class_properties(const Object &p_object, Li } // Script Variables -> to insert: NodeC..B..A -> bottom (insert_here) - List<PropertyInfo>::Element *script_variables = NULL; - List<PropertyInfo>::Element *bottom = NULL; - List<PropertyInfo>::Element *insert_here = NULL; + List<PropertyInfo>::Element *script_variables = nullptr; + List<PropertyInfo>::Element *bottom = nullptr; + List<PropertyInfo>::Element *insert_here = nullptr; for (List<PropertyInfo>::Element *E = r_list.front(); E; E = E->next()) { PropertyInfo &pi = E->get(); if (pi.name != "Script Variables") { @@ -2545,17 +3483,17 @@ void EditorInspector::_update_script_class_properties(const Object &p_object, Li } Set<StringName> added; - for (List<StringName>::Element *E = classes.front(); E; E = E->next()) { - StringName name = E->get(); - String path = paths[name]; - Ref<Script> s; - if (path == String()) { - // Built-in script. It can't be inherited, so must be the script attached to the object. - s = p_object.get_script(); - } else { - s = ResourceLoader::load(path, "Script"); + for (const Ref<Script> &s : classes) { + String path = s->get_path(); + String name = EditorNode::get_editor_data().script_class_get_name(path); + if (name.is_empty()) { + if (!path.is_empty() && path.find("::") == -1) { + name = path.get_file(); + } else { + name = TTR("Built-in script"); + } } - ERR_FAIL_COND(!s->is_valid()); + List<PropertyInfo> props; s->get_script_property_list(&props); @@ -2578,20 +3516,33 @@ void EditorInspector::_update_script_class_properties(const Object &p_object, Li } // NodeC -> C props... -> NodeB..C.. - r_list.erase(script_variables); - List<PropertyInfo>::Element *to_delete = bottom->next(); - while (to_delete && !(to_delete->get().usage & PROPERTY_USAGE_CATEGORY)) { - r_list.erase(to_delete); - to_delete = bottom->next(); + if (script_variables) { + r_list.erase(script_variables); + List<PropertyInfo>::Element *to_delete = bottom->next(); + while (to_delete && !(to_delete->get().usage & PROPERTY_USAGE_CATEGORY)) { + r_list.erase(to_delete); + to_delete = bottom->next(); + } + r_list.erase(bottom); } - r_list.erase(bottom); +} + +void EditorInspector::set_restrict_to_basic_settings(bool p_restrict) { + restrict_to_basic = p_restrict; + update_tree(); +} + +void EditorInspector::set_property_clipboard(const Variant &p_value) { + property_clipboard = p_value; +} + +Variant EditorInspector::get_property_clipboard() const { + return property_clipboard; } void EditorInspector::_bind_methods() { ClassDB::bind_method("_edit_request_change", &EditorInspector::_edit_request_change); - ClassDB::bind_method("refresh", &EditorInspector::refresh); - ADD_SIGNAL(MethodInfo("property_selected", PropertyInfo(Variant::STRING, "property"))); ADD_SIGNAL(MethodInfo("property_keyed", PropertyInfo(Variant::STRING, "property"))); ADD_SIGNAL(MethodInfo("property_deleted", PropertyInfo(Variant::STRING, "property"))); @@ -2623,16 +3574,26 @@ EditorInspector::EditorInspector() { use_folding = false; update_all_pending = false; update_tree_pending = false; - refresh_countdown = 0; read_only = false; search_box = nullptr; keying = false; _prop_edited = "property_edited"; - set_process(true); + set_process(false); property_focusable = -1; sub_inspector = false; deletable_properties = false; + property_clipboard = Variant(); get_v_scrollbar()->connect("value_changed", callable_mp(this, &EditorInspector::_vscroll_changed)); update_scroll_request = -1; + if (EditorSettings::get_singleton()) { + refresh_countdown = float(EditorSettings::get_singleton()->get("docks/property_editor/auto_refresh_interval")); + } else { + //used when class is created by the docgen to dump default values of everything bindable, editorsettings may not be created + refresh_countdown = 0.33; + } + + ED_SHORTCUT("property_editor/copy_property", TTR("Copy Property"), KEY_MASK_CMD | KEY_C); + ED_SHORTCUT("property_editor/paste_property", TTR("Paste Property"), KEY_MASK_CMD | KEY_V); + ED_SHORTCUT("property_editor/copy_property_path", TTR("Copy Property Path"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_C); } |