diff options
Diffstat (limited to 'editor/editor_properties.cpp')
-rw-r--r-- | editor/editor_properties.cpp | 183 |
1 files changed, 150 insertions, 33 deletions
diff --git a/editor/editor_properties.cpp b/editor/editor_properties.cpp index e679222567..6bb4b5e81b 100644 --- a/editor/editor_properties.cpp +++ b/editor/editor_properties.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ @@ -35,6 +35,9 @@ #include "editor_node.h" #include "editor_properties_array_dict.h" #include "editor_scale.h" +#include "scene/2d/gpu_particles_2d.h" +#include "scene/3d/fog_volume.h" +#include "scene/3d/gpu_particles_3d.h" #include "scene/main/window.h" #include "scene/resources/font.h" @@ -340,6 +343,64 @@ EditorPropertyTextEnum::EditorPropertyTextEnum() { add_focusable(cancel_button); } +//////////////////// LOCALE //////////////////////// + +void EditorPropertyLocale::_locale_selected(const String &p_locale) { + emit_changed(get_edited_property(), p_locale); + update_property(); +} + +void EditorPropertyLocale::_locale_pressed() { + if (!dialog) { + dialog = memnew(EditorLocaleDialog); + dialog->connect("locale_selected", callable_mp(this, &EditorPropertyLocale::_locale_selected)); + add_child(dialog); + } + + String locale_code = get_edited_object()->get(get_edited_property()); + dialog->set_locale(locale_code); + dialog->popup_locale_dialog(); +} + +void EditorPropertyLocale::update_property() { + String locale_code = get_edited_object()->get(get_edited_property()); + locale->set_text(locale_code); + locale->set_tooltip(locale_code); +} + +void EditorPropertyLocale::setup(const String &p_hint_text) { +} + +void EditorPropertyLocale::_notification(int p_what) { + if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) { + locale_edit->set_icon(get_theme_icon(SNAME("Translation"), SNAME("EditorIcons"))); + } +} + +void EditorPropertyLocale::_locale_focus_exited() { + _locale_selected(locale->get_text()); +} + +void EditorPropertyLocale::_bind_methods() { +} + +EditorPropertyLocale::EditorPropertyLocale() { + HBoxContainer *locale_hb = memnew(HBoxContainer); + add_child(locale_hb); + locale = memnew(LineEdit); + locale_hb->add_child(locale); + locale->connect("text_submitted", callable_mp(this, &EditorPropertyLocale::_locale_selected)); + locale->connect("focus_exited", callable_mp(this, &EditorPropertyLocale::_locale_focus_exited)); + locale->set_h_size_flags(SIZE_EXPAND_FILL); + + locale_edit = memnew(Button); + locale_edit->set_clip_text(true); + locale_hb->add_child(locale_edit); + add_focusable(locale); + dialog = nullptr; + locale_edit->connect("pressed", callable_mp(this, &EditorPropertyLocale::_locale_pressed)); +} + ///////////////////// PATH ///////////////////////// void EditorPropertyPath::_set_read_only(bool p_read_only) { @@ -377,7 +438,7 @@ void EditorPropertyPath::_path_pressed() { dialog->set_file_mode(save_mode ? EditorFileDialog::FILE_MODE_SAVE_FILE : EditorFileDialog::FILE_MODE_OPEN_FILE); for (int i = 0; i < extensions.size(); i++) { String e = extensions[i].strip_edges(); - if (e != String()) { + if (!e.is_empty()) { dialog->add_filter(extensions[i].strip_edges()); } } @@ -703,7 +764,7 @@ void EditorPropertyFlags::setup(const Vector<String> &p_options) { bool first = true; for (int i = 0; i < p_options.size(); i++) { String option = p_options[i].strip_edges(); - if (option != "") { + if (!option.is_empty()) { CheckBox *cb = memnew(CheckBox); cb->set_text(option); cb->set_clip_text(true); @@ -815,7 +876,7 @@ public: } const Ref<InputEventMouseButton> mb = p_ev; - if (mb.is_valid() && mb->get_button_index() == MOUSE_BUTTON_LEFT && mb->is_pressed()) { + if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) { if (hovered_index >= 0) { // Toggle the flag. // We base our choice on the hovered flag, so that it always matches the hovered flag. @@ -829,7 +890,7 @@ public: update(); } else if (expand_hovered) { expanded = !expanded; - minimum_size_changed(); + update_minimum_size(); update(); } } @@ -888,7 +949,7 @@ public: Vector2 offset; offset.y = rect2.size.y * 0.75; - draw_string(font, rect2.position + offset, itos(layer_index + 1), HALIGN_CENTER, rect2.size.x, font_size, on ? text_color_on : text_color); + draw_string(font, rect2.position + offset, itos(layer_index + 1), HORIZONTAL_ALIGNMENT_CENTER, rect2.size.x, font_size, on ? text_color_on : text_color); ofs.x += bsize + 1; @@ -932,7 +993,7 @@ public: } if ((expansion_rows != prev_expansion_rows) && expanded) { - minimum_size_changed(); + update_minimum_size(); } if ((expansion_rows == 0) && (layer_index == layer_count)) { @@ -1052,7 +1113,7 @@ void EditorPropertyLayers::setup(LayerType p_layer_type) { name = ProjectSettings::get_singleton()->get(basename + vformat("/layer_%d", i + 1)); } - if (name == "") { + if (name.is_empty()) { name = vformat(TTR("Layer %d"), i + 1); } @@ -1183,7 +1244,7 @@ void EditorPropertyObjectID::_edit_pressed() { void EditorPropertyObjectID::update_property() { String type = base_type; - if (type == "") { + if (type.is_empty()) { type = "Object"; } @@ -1275,12 +1336,13 @@ void EditorPropertyEasing::_drag_easing(const Ref<InputEvent> &p_ev) { } const Ref<InputEventMouseButton> mb = p_ev; if (mb.is_valid()) { - if (mb->is_double_click() && mb->get_button_index() == MOUSE_BUTTON_LEFT) { + if (mb->is_double_click() && mb->get_button_index() == MouseButton::LEFT) { _setup_spin(); } - if (mb->is_pressed() && mb->get_button_index() == MOUSE_BUTTON_RIGHT) { - preset->set_position(easing_draw->get_screen_transform().xform(mb->get_position())); + if (mb->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) { + preset->set_position(easing_draw->get_screen_position() + mb->get_position()); + preset->reset_size(); preset->popup(); // Ensure the easing doesn't appear as being dragged @@ -1288,7 +1350,7 @@ void EditorPropertyEasing::_drag_easing(const Ref<InputEvent> &p_ev) { easing_draw->update(); } - if (mb->get_button_index() == MOUSE_BUTTON_LEFT) { + if (mb->get_button_index() == MouseButton::LEFT) { dragging = mb->is_pressed(); // Update to display the correct dragging color easing_draw->update(); @@ -1297,7 +1359,7 @@ void EditorPropertyEasing::_drag_easing(const Ref<InputEvent> &p_ev) { const Ref<InputEventMouseMotion> mm = p_ev; - if (dragging && mm.is_valid() && mm->get_button_mask() & MOUSE_BUTTON_MASK_LEFT) { + if (dragging && mm.is_valid() && (mm->get_button_mask() & MouseButton::MASK_LEFT) != MouseButton::NONE) { float rel = mm->get_relative().x; if (rel == 0) { return; @@ -1379,7 +1441,7 @@ void EditorPropertyEasing::_draw_easing() { } else { decimals = 1; } - f->draw_string(ci, Point2(10, 10 + f->get_ascent(font_size)), TS->format_number(rtos(exp).pad_decimals(decimals)), HALIGN_LEFT, -1, font_size, font_color); + f->draw_string(ci, Point2(10, 10 + f->get_ascent(font_size)), TS->format_number(rtos(exp).pad_decimals(decimals)), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_color); } void EditorPropertyEasing::update_property() { @@ -2723,6 +2785,29 @@ void EditorPropertyNodePath::_node_clear() { update_property(); } +bool EditorPropertyNodePath::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const { + return !is_read_only() && is_drop_valid(p_data); +} + +void EditorPropertyNodePath::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) { + ERR_FAIL_COND(!is_drop_valid(p_data)); + Dictionary data = p_data; + Array nodes = data["nodes"]; + Node *node = get_tree()->get_edited_scene_root()->get_node(nodes[0]); + + if (node) { + _node_selected(node->get_path()); + } +} + +bool EditorPropertyNodePath::is_drop_valid(const Dictionary &p_drag_data) const { + if (p_drag_data["type"] != "nodes") { + return false; + } + Array nodes = p_drag_data["nodes"]; + return nodes.size() == 1; +} + void EditorPropertyNodePath::update_property() { NodePath p = get_edited_object()->get(get_edited_property()); @@ -2777,6 +2862,8 @@ void EditorPropertyNodePath::_notification(int p_what) { } void EditorPropertyNodePath::_bind_methods() { + ClassDB::bind_method(D_METHOD("_can_drop_data_fw", "position", "data", "from"), &EditorPropertyNodePath::can_drop_data_fw); + ClassDB::bind_method(D_METHOD("_drop_data_fw", "position", "data", "from"), &EditorPropertyNodePath::drop_data_fw); } EditorPropertyNodePath::EditorPropertyNodePath() { @@ -2787,6 +2874,7 @@ EditorPropertyNodePath::EditorPropertyNodePath() { assign->set_h_size_flags(SIZE_EXPAND_FILL); assign->set_clip_text(true); assign->connect("pressed", callable_mp(this, &EditorPropertyNodePath::_node_assign)); + assign->set_drag_forwarding(this); hbc->add_child(assign); clear = memnew(Button); @@ -2821,8 +2909,8 @@ void EditorPropertyResource::_set_read_only(bool p_read_only) { resource_picker->set_editable(!p_read_only); }; -void EditorPropertyResource::_resource_selected(const RES &p_resource) { - if (use_sub_inspector) { +void EditorPropertyResource::_resource_selected(const RES &p_resource, bool p_edit) { + if (!p_edit && use_sub_inspector) { bool unfold = !get_edited_object()->editor_is_section_unfolded(get_edited_property()); get_edited_object()->editor_set_section_unfold(get_edited_property(), unfold); update_property(); @@ -2879,8 +2967,11 @@ void EditorPropertyResource::_resource_changed(const RES &p_resource) { } } -void EditorPropertyResource::_sub_inspector_property_keyed(const String &p_property, const Variant &p_value, bool) { - emit_signal(SNAME("property_keyed_with_value"), String(get_edited_property()) + ":" + p_property, p_value, false); +void EditorPropertyResource::_sub_inspector_property_keyed(const String &p_property, const Variant &p_value, bool p_advance) { + // The second parameter could be null, causing the event to fire with less arguments, so use the pointer call which preserves it. + const Variant args[3] = { String(get_edited_property()) + ":" + p_property, p_value, p_advance }; + const Variant *argp[3] = { &args[0], &args[1], &args[2] }; + emit_signal(SNAME("property_keyed_with_value"), argp, 3); } void EditorPropertyResource::_sub_inspector_resource_selected(const RES &p_resource, const String &p_property) { @@ -2969,6 +3060,35 @@ void EditorPropertyResource::_update_property_bg() { update(); } +void EditorPropertyResource::_update_preferred_shader() { + Node *parent = get_parent(); + EditorProperty *parent_property = nullptr; + + while (parent && !parent_property) { + parent_property = Object::cast_to<EditorProperty>(parent); + parent = parent->get_parent(); + } + + if (parent_property) { + EditorShaderPicker *shader_picker = Object::cast_to<EditorShaderPicker>(resource_picker); + Object *object = parent_property->get_edited_object(); + const StringName &property = parent_property->get_edited_property(); + + // Set preferred shader based on edited parent type. + if ((Object::cast_to<GPUParticles2D>(object) || Object::cast_to<GPUParticles3D>(object)) && property == SNAME("process_material")) { + shader_picker->set_preferred_mode(Shader::MODE_PARTICLES); + } else if (Object::cast_to<FogVolume>(object)) { + shader_picker->set_preferred_mode(Shader::MODE_FOG); + } else if (Object::cast_to<CanvasItem>(object)) { + shader_picker->set_preferred_mode(Shader::MODE_CANVAS_ITEM); + } else if (Object::cast_to<Node3D>(object)) { + shader_picker->set_preferred_mode(Shader::MODE_SPATIAL); + } else if (Object::cast_to<Sky>(object)) { + shader_picker->set_preferred_mode(Shader::MODE_SKY); + } + } +} + void EditorPropertyResource::_viewport_selected(const NodePath &p_path) { Node *to_node = get_node(p_path); if (!Object::cast_to<Viewport>(to_node)) { @@ -3000,6 +3120,7 @@ void EditorPropertyResource::setup(Object *p_object, const String &p_path, const EditorShaderPicker *shader_picker = memnew(EditorShaderPicker); shader_picker->set_edited_material(Object::cast_to<ShaderMaterial>(p_object)); resource_picker = shader_picker; + connect(SNAME("ready"), callable_mp(this, &EditorPropertyResource::_update_preferred_shader)); } else { resource_picker = memnew(EditorResourcePicker); } @@ -3031,7 +3152,7 @@ void EditorPropertyResource::update_property() { if (res.is_valid() && get_edited_object()->editor_is_section_unfolded(get_edited_property())) { if (!sub_inspector) { sub_inspector = memnew(EditorInspector); - sub_inspector->set_enable_v_scroll(false); + sub_inspector->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED); sub_inspector->set_use_doc_hints(true); sub_inspector->set_sub_inspector(true); @@ -3137,11 +3258,7 @@ EditorPropertyResource::EditorPropertyResource() { ////////////// DEFAULT PLUGIN ////////////////////// bool EditorInspectorDefaultPlugin::can_handle(Object *p_object) { - return true; //can handle everything -} - -void EditorInspectorDefaultPlugin::parse_begin(Object *p_object) { - //do none + return true; // Can handle everything. } bool EditorInspectorDefaultPlugin::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) { @@ -3152,10 +3269,6 @@ bool EditorInspectorDefaultPlugin::parse_property(Object *p_object, const Varian return false; } -void EditorInspectorDefaultPlugin::parse_end() { - //do none -} - struct EditorPropertyRangeHint { bool angle_in_degrees = false; bool greater = true; @@ -3203,7 +3316,7 @@ static EditorPropertyRangeHint _parse_range_hint(PropertyHint p_hint, const Stri } } - if ((hint.radians || degrees) && hint.suffix == String()) { + if ((hint.radians || degrees) && hint.suffix.is_empty()) { hint.suffix = U"\u00B0"; } @@ -3324,6 +3437,10 @@ EditorProperty *EditorInspectorDefaultPlugin::get_editor_for_property(Object *p_ EditorPropertyClassName *editor = memnew(EditorPropertyClassName); editor->setup("Object", p_hint_text); return editor; + } else if (p_hint == PROPERTY_HINT_LOCALE_ID) { + EditorPropertyLocale *editor = memnew(EditorPropertyLocale); + editor->setup(p_hint_text); + return editor; } else if (p_hint == PROPERTY_HINT_DIR || p_hint == PROPERTY_HINT_FILE || p_hint == PROPERTY_HINT_SAVE_FILE || p_hint == PROPERTY_HINT_GLOBAL_DIR || p_hint == PROPERTY_HINT_GLOBAL_FILE) { Vector<String> extensions = p_hint_text.split(","); bool global = p_hint == PROPERTY_HINT_GLOBAL_DIR || p_hint == PROPERTY_HINT_GLOBAL_FILE; @@ -3488,10 +3605,10 @@ EditorProperty *EditorInspectorDefaultPlugin::get_editor_for_property(Object *p_ } break; case Variant::NODE_PATH: { EditorPropertyNodePath *editor = memnew(EditorPropertyNodePath); - if (p_hint == PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE && p_hint_text != String()) { + if (p_hint == PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE && !p_hint_text.is_empty()) { editor->setup(p_hint_text, Vector<StringName>(), (p_usage & PROPERTY_USAGE_NODE_PATH_FROM_SCENE_ROOT)); } - if (p_hint == PROPERTY_HINT_NODE_PATH_VALID_TYPES && p_hint_text != String()) { + if (p_hint == PROPERTY_HINT_NODE_PATH_VALID_TYPES && !p_hint_text.is_empty()) { Vector<String> types = p_hint_text.split(",", false); Vector<StringName> sn = Variant(types); //convert via variant editor->setup(NodePath(), sn, (p_usage & PROPERTY_USAGE_NODE_PATH_FROM_SCENE_ROOT)); |