Make editing properties more intuitive in VisualShader

This commit is contained in:
Hendrik Brucker 2024-03-05 13:27:33 +01:00
parent f5dbbf7fd0
commit d32e0f808c
2 changed files with 56 additions and 29 deletions

View File

@ -68,7 +68,7 @@ Size2 EditorProperty::get_minimum_size() const {
Size2 ms;
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) + 4 * EDSCALE;
ms.height = label.is_empty() ? 0 : font->get_height(font_size) + 4 * EDSCALE;
for (int i = 0; i < get_child_count(); i++) {
Control *c = Object::cast_to<Control>(get_child(i));
@ -106,7 +106,7 @@ Size2 EditorProperty::get_minimum_size() const {
}
if (bottom_editor != nullptr && bottom_editor->is_visible()) {
ms.height += get_theme_constant(SNAME("v_separation"));
ms.height += label.is_empty() ? 0 : get_theme_constant(SNAME("v_separation"));
Size2 bems = bottom_editor->get_combined_minimum_size();
//bems.width += get_constant("item_margin", "Tree");
ms.height += bems.height;
@ -138,7 +138,7 @@ void EditorProperty::_notification(int p_what) {
int child_room = size.width * (1.0 - split_ratio);
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) + 4 * EDSCALE;
int height = label.is_empty() ? 0 : font->get_height(font_size) + 4 * EDSCALE;
bool no_children = true;
//compute room needed
@ -176,9 +176,8 @@ 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(SNAME("v_separation")), size.width - m, bottom_editor->get_combined_minimum_size().height);
int v_offset = label.is_empty() ? 0 : get_theme_constant(SNAME("v_separation"));
bottom_rect = Rect2(0, rect.size.height + v_offset, size.width, bottom_editor->get_combined_minimum_size().height);
}
if (keying) {
@ -254,8 +253,13 @@ void EditorProperty::_notification(int p_what) {
size.height = label_reference->get_size().height;
}
Ref<StyleBox> sb = get_theme_stylebox(selected ? SNAME("bg_selected") : SNAME("bg"));
draw_style_box(sb, Rect2(Vector2(), size));
// Only draw the label if it's not empty.
if (label.is_empty()) {
size.height = 0;
} else {
Ref<StyleBox> sb = get_theme_stylebox(selected ? SNAME("bg_selected") : SNAME("bg"));
draw_style_box(sb, Rect2(Vector2(), size));
}
Ref<StyleBox> bg_stylebox = get_theme_stylebox(SNAME("child_bg"));
if (draw_top_bg && right_child_rect != Rect2()) {

View File

@ -818,6 +818,7 @@ void VisualShaderGraphPlugin::add_node(VisualShader::Type p_type, int p_id, bool
}
hb->add_theme_constant_override("separation", 7 * EDSCALE);
// Default value button/property editor.
Variant default_value;
if (valid_left && !port_left_used) {
@ -2744,6 +2745,8 @@ void VisualShaderEditor::_edit_port_default_input(Object *p_button, int p_node,
Variant value = vs_node->get_input_port_default_value(p_port);
edited_property_holder->set_edited_property(value);
editing_node = p_node;
editing_port = p_port;
if (property_editor) {
property_editor->disconnect("property_changed", callable_mp(this, &VisualShaderEditor::_port_edited));
@ -2751,29 +2754,49 @@ void VisualShaderEditor::_edit_port_default_input(Object *p_button, int p_node,
}
// TODO: Define these properties with actual PropertyInfo and feed it to the property editor widget.
property_editor = EditorInspector::instantiate_property_editor(edited_property_holder.ptr(), value.get_type(), "edited_property", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE);
if (property_editor) {
property_editor->set_object_and_property(edited_property_holder.ptr(), "edited_property");
property_editor->update_property();
property_editor->set_name_split_ratio(0);
property_editor_popup->add_child(property_editor);
property_editor = EditorInspector::instantiate_property_editor(edited_property_holder.ptr(), value.get_type(), "edited_property", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE, true);
ERR_FAIL_NULL_MSG(property_editor, "Failed to create property editor for type: " + Variant::get_type_name(value.get_type()));
property_editor->connect("property_changed", callable_mp(this, &VisualShaderEditor::_port_edited));
Button *button = Object::cast_to<Button>(p_button);
if (button) {
property_editor_popup->set_position(button->get_screen_position() + Vector2(0, button->get_size().height) * graph->get_zoom());
}
property_editor_popup->reset_size();
if (button) {
property_editor_popup->popup();
} else {
property_editor_popup->popup_centered_ratio();
}
// Determine the best size for the popup based on the property type.
// This is done here, since the property editors are also used in the inspector where they have different layout requirements, so we can't just change their default minimum size.
Size2 popup_pref_size;
switch (value.get_type()) {
case Variant::VECTOR3:
case Variant::BASIS:
popup_pref_size.width = 320;
break;
case Variant::VECTOR4:
case Variant::QUATERNION:
case Variant::PLANE:
case Variant::TRANSFORM2D:
case Variant::TRANSFORM3D:
case Variant::PROJECTION:
popup_pref_size.width = 480;
break;
default:
popup_pref_size.width = 180;
break;
}
property_editor_popup->set_min_size(popup_pref_size);
editing_node = p_node;
editing_port = p_port;
property_editor->set_object_and_property(edited_property_holder.ptr(), "edited_property");
property_editor->update_property();
property_editor->set_name_split_ratio(0);
property_editor_popup->add_child(property_editor);
property_editor->connect("property_changed", callable_mp(this, &VisualShaderEditor::_port_edited));
Button *button = Object::cast_to<Button>(p_button);
if (button) {
property_editor_popup->set_position(button->get_screen_position() + Vector2(0, button->get_size().height) * graph->get_zoom());
}
property_editor_popup->reset_size();
if (button) {
property_editor_popup->popup();
} else {
property_editor_popup->popup_centered_ratio();
}
property_editor->select(0); // Focus the first focusable control.
}
void VisualShaderEditor::_set_custom_node_option(int p_index, int p_node, int p_op) {
@ -6515,7 +6538,7 @@ VisualShaderEditor::VisualShaderEditor() {
graph_plugin->set_editor(this);
property_editor_popup = memnew(PopupPanel);
property_editor_popup->set_min_size(Size2(180, 0) * EDSCALE);
property_editor_popup->set_min_size(Size2(360, 0) * EDSCALE);
add_child(property_editor_popup);
edited_property_holder.instantiate();