diff options
Diffstat (limited to 'editor/property_editor.cpp')
-rw-r--r-- | editor/property_editor.cpp | 164 |
1 files changed, 91 insertions, 73 deletions
diff --git a/editor/property_editor.cpp b/editor/property_editor.cpp index c6efcc944b..07312e42b4 100644 --- a/editor/property_editor.cpp +++ b/editor/property_editor.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -30,16 +30,16 @@ #include "property_editor.h" -#include "core/class_db.h" +#include "core/config/project_settings.h" #include "core/input/input.h" #include "core/io/image_loader.h" #include "core/io/marshalls.h" #include "core/io/resource_loader.h" #include "core/math/expression.h" +#include "core/object/class_db.h" #include "core/os/keyboard.h" -#include "core/pair.h" -#include "core/print_string.h" -#include "core/project_settings.h" +#include "core/string/print_string.h" +#include "core/templates/pair.h" #include "editor/array_property_edit.h" #include "editor/create_dialog.h" #include "editor/dictionary_property_edit.h" @@ -152,7 +152,7 @@ void CustomPropertyEditor::_menu_option(int p_which) { file->add_filter("*." + E->get() + " ; " + E->get().to_upper()); } - file->popup_centered_ratio(); + file->popup_file_dialog(); } break; case OBJ_MENU_EDIT: { @@ -251,18 +251,18 @@ void CustomPropertyEditor::_menu_option(int p_which) { emit_signal("variant_changed"); break; } - ERR_FAIL_COND(inheritors_array.empty()); + ERR_FAIL_COND(inheritors_array.is_empty()); String intype = inheritors_array[p_which - TYPE_BASE_ID]; if (intype == "ViewportTexture") { scene_tree->set_title(TTR("Pick a Viewport")); - scene_tree->popup_centered_ratio(); + scene_tree->popup_scenetree_dialog(); picking_viewport = true; return; } - Object *obj = ClassDB::instance(intype); + Variant obj = ClassDB::instance(intype); if (!obj) { if (ScriptServer::is_global_class(intype)) { @@ -280,7 +280,7 @@ void CustomPropertyEditor::_menu_option(int p_which) { res->call("set_instance_base_type", owner->get_class()); } - v = res; + v = obj; emit_signal("variant_changed"); } break; @@ -326,6 +326,9 @@ bool CustomPropertyEditor::edit(Object *p_owner, const String &p_name, Variant:: menu->set_size(Size2(1, 1) * EDSCALE); for (int i = 0; i < MAX_VALUE_EDITORS; i++) { + if (i < MAX_VALUE_EDITORS / 4) { + value_hboxes[i]->hide(); + } value_editor[i]->hide(); value_label[i]->hide(); if (i < 4) { @@ -342,7 +345,7 @@ bool CustomPropertyEditor::edit(Object *p_owner, const String &p_name, Variant:: checks20[i]->hide(); } - type = (p_variant.get_type() != Variant::NIL && p_variant.get_type() != Variant::_RID && p_type != Variant::OBJECT) ? p_variant.get_type() : p_type; + type = (p_variant.get_type() != Variant::NIL && p_variant.get_type() != Variant::RID && p_type != Variant::OBJECT) ? p_variant.get_type() : p_type; switch (type) { case Variant::BOOL: { @@ -364,19 +367,19 @@ bool CustomPropertyEditor::edit(Object *p_owner, const String &p_name, Variant:: int c = hint_text.get_slice_count(","); float min = 0, max = 100, step = type == Variant::FLOAT ? .01 : 1; if (c >= 1) { - if (!hint_text.get_slice(",", 0).empty()) { - min = hint_text.get_slice(",", 0).to_double(); + if (!hint_text.get_slice(",", 0).is_empty()) { + min = hint_text.get_slice(",", 0).to_float(); } } if (c >= 2) { - if (!hint_text.get_slice(",", 1).empty()) { - max = hint_text.get_slice(",", 1).to_double(); + if (!hint_text.get_slice(",", 1).is_empty()) { + max = hint_text.get_slice(",", 1).to_float(); } } if (c >= 3) { - if (!hint_text.get_slice(",", 2).empty()) { - step = hint_text.get_slice(",", 2).to_double(); + if (!hint_text.get_slice(",", 2).is_empty()) { + step = hint_text.get_slice(",", 2).to_float(); } } @@ -453,14 +456,14 @@ bool CustomPropertyEditor::edit(Object *p_owner, const String &p_name, Variant:: set_size(Vector2(4, 4) * EDSCALE + checks20gc->get_position() + checks20gc->get_size()); } else if (hint == PROPERTY_HINT_EXP_EASING) { - easing_draw->set_anchor_and_margin(MARGIN_LEFT, Control::ANCHOR_BEGIN, 5 * EDSCALE); - easing_draw->set_anchor_and_margin(MARGIN_RIGHT, Control::ANCHOR_END, -5 * EDSCALE); - easing_draw->set_anchor_and_margin(MARGIN_TOP, Control::ANCHOR_BEGIN, 5 * EDSCALE); - easing_draw->set_anchor_and_margin(MARGIN_BOTTOM, Control::ANCHOR_END, -30 * EDSCALE); - type_button->set_anchor_and_margin(MARGIN_LEFT, Control::ANCHOR_BEGIN, 3 * EDSCALE); - type_button->set_anchor_and_margin(MARGIN_RIGHT, Control::ANCHOR_END, -3 * EDSCALE); - type_button->set_anchor_and_margin(MARGIN_TOP, Control::ANCHOR_END, -25 * EDSCALE); - type_button->set_anchor_and_margin(MARGIN_BOTTOM, Control::ANCHOR_END, -7 * EDSCALE); + easing_draw->set_anchor_and_offset(SIDE_LEFT, Control::ANCHOR_BEGIN, 5 * EDSCALE); + easing_draw->set_anchor_and_offset(SIDE_RIGHT, Control::ANCHOR_END, -5 * EDSCALE); + easing_draw->set_anchor_and_offset(SIDE_TOP, Control::ANCHOR_BEGIN, 5 * EDSCALE); + easing_draw->set_anchor_and_offset(SIDE_BOTTOM, Control::ANCHOR_END, -30 * EDSCALE); + type_button->set_anchor_and_offset(SIDE_LEFT, Control::ANCHOR_BEGIN, 3 * EDSCALE); + type_button->set_anchor_and_offset(SIDE_RIGHT, Control::ANCHOR_END, -3 * EDSCALE); + type_button->set_anchor_and_offset(SIDE_TOP, Control::ANCHOR_END, -25 * EDSCALE); + type_button->set_anchor_and_offset(SIDE_BOTTOM, Control::ANCHOR_END, -7 * EDSCALE); type_button->set_text(TTR("Preset...")); type_button->get_popup()->clear(); type_button->get_popup()->add_item(TTR("Linear"), EASING_LINEAR); @@ -498,7 +501,7 @@ bool CustomPropertyEditor::edit(Object *p_owner, const String &p_name, Variant:: List<String> names; names.push_back("value:"); config_value_editors(1, 1, 50, names); - value_editor[0]->set_text(String::num(v)); + value_editor[0]->set_text(TS->format_number(String::num(v))); } } break; @@ -533,10 +536,10 @@ bool CustomPropertyEditor::edit(Object *p_owner, const String &p_name, Variant:: int button_margin = text_edit->get_theme_constant("button_margin", "Dialogs"); int margin = text_edit->get_theme_constant("margin", "Dialogs"); - action_buttons[0]->set_anchor(MARGIN_LEFT, Control::ANCHOR_END); - action_buttons[0]->set_anchor(MARGIN_TOP, Control::ANCHOR_END); - action_buttons[0]->set_anchor(MARGIN_RIGHT, Control::ANCHOR_END); - action_buttons[0]->set_anchor(MARGIN_BOTTOM, Control::ANCHOR_END); + action_buttons[0]->set_anchor(SIDE_LEFT, Control::ANCHOR_END); + action_buttons[0]->set_anchor(SIDE_TOP, Control::ANCHOR_END); + action_buttons[0]->set_anchor(SIDE_RIGHT, Control::ANCHOR_END); + action_buttons[0]->set_anchor(SIDE_BOTTOM, Control::ANCHOR_END); action_buttons[0]->set_begin(Point2(-70 * EDSCALE, -button_margin + 5 * EDSCALE)); action_buttons[0]->set_end(Point2(-margin, -margin)); action_buttons[0]->set_text(TTR("Close")); @@ -594,7 +597,7 @@ bool CustomPropertyEditor::edit(Object *p_owner, const String &p_name, Variant:: } else if (hint == PROPERTY_HINT_METHOD_OF_INSTANCE) { MAKE_PROPSELECT - Object *instance = ObjectDB::get_instance(ObjectID(hint_text.to_int64())); + Object *instance = ObjectDB::get_instance(ObjectID(hint_text.to_int())); if (instance) { property_select->select_method_from_instance(instance, v); } @@ -604,7 +607,7 @@ bool CustomPropertyEditor::edit(Object *p_owner, const String &p_name, Variant:: } else if (hint == PROPERTY_HINT_METHOD_OF_SCRIPT) { MAKE_PROPSELECT - Object *obj = ObjectDB::get_instance(ObjectID(hint_text.to_int64())); + Object *obj = ObjectDB::get_instance(ObjectID(hint_text.to_int())); if (Object::cast_to<Script>(obj)) { property_select->select_method_from_script(Object::cast_to<Script>(obj), v); } @@ -643,7 +646,7 @@ bool CustomPropertyEditor::edit(Object *p_owner, const String &p_name, Variant:: } else if (hint == PROPERTY_HINT_PROPERTY_OF_INSTANCE) { MAKE_PROPSELECT - Object *instance = ObjectDB::get_instance(ObjectID(hint_text.to_int64())); + Object *instance = ObjectDB::get_instance(ObjectID(hint_text.to_int())); if (instance) { property_select->select_property_from_instance(instance, v); } @@ -654,7 +657,7 @@ bool CustomPropertyEditor::edit(Object *p_owner, const String &p_name, Variant:: } else if (hint == PROPERTY_HINT_PROPERTY_OF_SCRIPT) { MAKE_PROPSELECT - Object *obj = ObjectDB::get_instance(ObjectID(hint_text.to_int64())); + Object *obj = ObjectDB::get_instance(ObjectID(hint_text.to_int())); if (Object::cast_to<Script>(obj)) { property_select->select_property_from_script(Object::cast_to<Script>(obj), v); } @@ -879,7 +882,7 @@ bool CustomPropertyEditor::edit(Object *p_owner, const String &p_name, Variant:: bool is_custom_resource = false; Ref<Texture2D> icon; - if (!custom_resources.empty()) { + if (!custom_resources.is_empty()) { for (int k = 0; k < custom_resources.size(); k++) { if (custom_resources[k].name == t) { is_custom_resource = true; @@ -1061,7 +1064,7 @@ void CustomPropertyEditor::_type_create_selected(int p_idx) { String intype = inheritors_array[p_idx]; - Object *obj = ClassDB::instance(intype); + Variant obj = ClassDB::instance(intype); if (!obj) { if (ScriptServer::is_global_class(intype)) { @@ -1072,11 +1075,9 @@ void CustomPropertyEditor::_type_create_selected(int p_idx) { } ERR_FAIL_COND(!obj); + ERR_FAIL_COND(!Object::cast_to<Resource>(obj)); - Resource *res = Object::cast_to<Resource>(obj); - ERR_FAIL_COND(!res); - - v = res; + v = obj; emit_signal("variant_changed"); hide(); } @@ -1195,7 +1196,7 @@ void CustomPropertyEditor::_action_pressed(int p_which) { file->add_filter(filter + " ; " + extensions[i].to_upper()); } } - file->popup_centered_ratio(); + file->popup_file_dialog(); } else { v = ""; emit_signal("variant_changed"); @@ -1211,7 +1212,7 @@ void CustomPropertyEditor::_action_pressed(int p_which) { } file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_DIR); file->clear_filters(); - file->popup_centered_ratio(); + file->popup_file_dialog(); } else { v = ""; emit_signal("variant_changed"); @@ -1224,7 +1225,7 @@ void CustomPropertyEditor::_action_pressed(int p_which) { if (p_which == 0) { picking_viewport = false; scene_tree->set_title(TTR("Pick a Node")); - scene_tree->popup_centered_ratio(); + scene_tree->popup_scenetree_dialog(); } else if (p_which == 1) { v = NodePath(); @@ -1243,12 +1244,12 @@ void CustomPropertyEditor::_action_pressed(int p_which) { } break; case Variant::OBJECT: { if (p_which == 0) { - ERR_FAIL_COND(inheritors_array.empty()); + ERR_FAIL_COND(inheritors_array.is_empty()); String intype = inheritors_array[0]; if (hint == PROPERTY_HINT_RESOURCE_TYPE) { - Object *obj = ClassDB::instance(intype); + Variant obj = ClassDB::instance(intype); if (!obj) { if (ScriptServer::is_global_class(intype)) { @@ -1259,10 +1260,9 @@ void CustomPropertyEditor::_action_pressed(int p_which) { } ERR_BREAK(!obj); - Resource *res = Object::cast_to<Resource>(obj); - ERR_BREAK(!res); + ERR_BREAK(!Object::cast_to<Resource>(obj)); - v = res; + v = obj; emit_signal("variant_changed"); hide(); } @@ -1278,7 +1278,7 @@ void CustomPropertyEditor::_action_pressed(int p_which) { file->add_filter("*." + E->get() + " ; " + E->get().to_upper()); } - file->popup_centered_ratio(); + file->popup_file_dialog(); } else if (p_which == 2) { RES r = v; @@ -1386,6 +1386,7 @@ void CustomPropertyEditor::_draw_easing() { bool flip = hint_text == "attenuation"; Ref<Font> f = easing_draw->get_theme_font("font", "Label"); + int font_size = easing_draw->get_theme_font_size("font_size", "Label"); Color color = easing_draw->get_theme_color("font_color", "Label"); for (int i = 1; i <= points; i++) { @@ -1403,7 +1404,7 @@ void CustomPropertyEditor::_draw_easing() { prev = h; } - f->draw(ci, Point2(10, 10 + f->get_ascent()), String::num(exp, 2), color); + f->draw_string(ci, Point2(10, 10 + f->get_ascent(font_size)), String::num(exp, 2), HALIGN_LEFT, -1, font_size, color); } void CustomPropertyEditor::_text_edit_changed() { @@ -1429,7 +1430,7 @@ void CustomPropertyEditor::_modified(String p_string) { updating = true; switch (type) { case Variant::INT: { - String text = value_editor[0]->get_text(); + String text = TS->parse_number(value_editor[0]->get_text()); Ref<Expression> expr; expr.instance(); Error err = expr->parse(text); @@ -1444,7 +1445,7 @@ void CustomPropertyEditor::_modified(String p_string) { } break; case Variant::FLOAT: { if (hint != PROPERTY_HINT_EXP_EASING) { - String text = value_editor[0]->get_text(); + String text = TS->parse_number(value_editor[0]->get_text()); v = _parse_real_expression(text); emit_signal("variant_changed"); } @@ -1587,7 +1588,7 @@ real_t CustomPropertyEditor::_parse_real_expression(String text) { Error err = expr->parse(text); real_t out; if (err != OK) { - out = value_editor[0]->get_text().to_double(); + out = value_editor[0]->get_text().to_float(); } else { out = expr->execute(Array(), nullptr, false); } @@ -1657,10 +1658,10 @@ void CustomPropertyEditor::_focus_exit() { void CustomPropertyEditor::config_action_buttons(const List<String> &p_strings) { Ref<StyleBox> sb = action_buttons[0]->get_theme_stylebox("panel"); - int margin_top = sb->get_margin(MARGIN_TOP); - int margin_left = sb->get_margin(MARGIN_LEFT); - int margin_bottom = sb->get_margin(MARGIN_BOTTOM); - int margin_right = sb->get_margin(MARGIN_RIGHT); + int margin_top = sb->get_margin(SIDE_TOP); + int margin_left = sb->get_margin(SIDE_LEFT); + int margin_bottom = sb->get_margin(SIDE_BOTTOM); + int margin_right = sb->get_margin(SIDE_RIGHT); int max_width = 0; int height = 0; @@ -1694,23 +1695,30 @@ void CustomPropertyEditor::config_value_editors(int p_amount, int p_columns, int int cell_width = 95; int cell_height = 25; int cell_margin = 5; - int hor_spacing = 5; // Spacing between labels and their values - int rows = ((p_amount - 1) / p_columns) + 1; set_size(Size2(cell_margin + p_label_w + (cell_width + cell_margin + p_label_w) * p_columns, cell_margin + (cell_height + cell_margin) * rows) * EDSCALE); for (int i = 0; i < MAX_VALUE_EDITORS; i++) { - int c = i % p_columns; - int r = i / p_columns; + value_label[i]->get_parent()->remove_child(value_label[i]); + value_editor[i]->get_parent()->remove_child(value_editor[i]); + + int box_id = i / p_columns; + value_hboxes[box_id]->add_child(value_label[i]); + value_hboxes[box_id]->add_child(value_editor[i]); + + if (i < MAX_VALUE_EDITORS / 4) { + if (i <= p_amount / 4) { + value_hboxes[i]->show(); + } else { + value_hboxes[i]->hide(); + } + } if (i < p_amount) { value_editor[i]->show(); value_label[i]->show(); value_label[i]->set_text(i < p_strings.size() ? p_strings[i] : String("")); - value_editor[i]->set_position(Point2(cell_margin + p_label_w + hor_spacing + (cell_width + cell_margin + p_label_w + hor_spacing) * c, cell_margin + (cell_height + cell_margin) * r) * EDSCALE); - value_editor[i]->set_size(Size2(cell_width, cell_height)); - value_label[i]->set_position(Point2(cell_margin + (cell_width + cell_margin + p_label_w + hor_spacing) * c, cell_margin + (cell_height + cell_margin) * r) * EDSCALE); value_editor[i]->set_editable(!read_only); } else { value_editor[i]->hide(); @@ -1729,13 +1737,23 @@ CustomPropertyEditor::CustomPropertyEditor() { read_only = false; updating = false; + value_vbox = memnew(VBoxContainer); + add_child(value_vbox); + for (int i = 0; i < MAX_VALUE_EDITORS; i++) { - value_editor[i] = memnew(LineEdit); - add_child(value_editor[i]); + if (i < MAX_VALUE_EDITORS / 4) { + value_hboxes[i] = memnew(HBoxContainer); + value_vbox->add_child(value_hboxes[i]); + value_hboxes[i]->hide(); + } + int hbox_idx = i / 4; value_label[i] = memnew(Label); - add_child(value_label[i]); - value_editor[i]->hide(); + value_hboxes[hbox_idx]->add_child(value_label[i]); value_label[i]->hide(); + value_editor[i] = memnew(LineEdit); + value_hboxes[hbox_idx]->add_child(value_editor[i]); + value_editor[i]->set_h_size_flags(Control::SIZE_EXPAND_FILL); + value_editor[i]->hide(); value_editor[i]->connect("text_entered", callable_mp(this, &CustomPropertyEditor::_modified)); value_editor[i]->connect("focus_entered", callable_mp(this, &CustomPropertyEditor::_focus_enter)); value_editor[i]->connect("focus_exited", callable_mp(this, &CustomPropertyEditor::_focus_exit)); @@ -1773,8 +1791,8 @@ CustomPropertyEditor::CustomPropertyEditor() { text_edit = memnew(TextEdit); add_child(text_edit); - text_edit->set_anchors_and_margins_preset(Control::PRESET_WIDE, Control::PRESET_MODE_MINSIZE, 5); - text_edit->set_margin(MARGIN_BOTTOM, -30); + text_edit->set_anchors_and_offsets_preset(Control::PRESET_WIDE, Control::PRESET_MODE_MINSIZE, 5); + text_edit->set_offset(SIDE_BOTTOM, -30); text_edit->hide(); text_edit->connect("text_changed", callable_mp(this, &CustomPropertyEditor::_text_edit_changed)); @@ -1832,12 +1850,12 @@ CustomPropertyEditor::CustomPropertyEditor() { spinbox = memnew(SpinBox); add_child(spinbox); - spinbox->set_anchors_and_margins_preset(Control::PRESET_WIDE, Control::PRESET_MODE_MINSIZE, 5); + spinbox->set_anchors_and_offsets_preset(Control::PRESET_WIDE, Control::PRESET_MODE_MINSIZE, 5); spinbox->connect("value_changed", callable_mp(this, &CustomPropertyEditor::_range_modified)); slider = memnew(HSlider); add_child(slider); - slider->set_anchors_and_margins_preset(Control::PRESET_WIDE, Control::PRESET_MODE_MINSIZE, 5); + slider->set_anchors_and_offsets_preset(Control::PRESET_WIDE, Control::PRESET_MODE_MINSIZE, 5); slider->connect("value_changed", callable_mp(this, &CustomPropertyEditor::_range_modified)); create_dialog = nullptr; |