Path: blob/master/editor/scene/gui/control_editor_plugin.cpp
9902 views
/**************************************************************************/1/* control_editor_plugin.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "control_editor_plugin.h"3132#include "editor/editor_node.h"33#include "editor/editor_undo_redo_manager.h"34#include "editor/scene/canvas_item_editor_plugin.h"35#include "editor/themes/editor_scale.h"36#include "scene/gui/button.h"37#include "scene/gui/check_box.h"38#include "scene/gui/check_button.h"39#include "scene/gui/grid_container.h"40#include "scene/gui/label.h"41#include "scene/gui/option_button.h"42#include "scene/gui/panel_container.h"43#include "scene/gui/separator.h"44#include "scene/gui/texture_rect.h"4546// Inspector controls.4748void ControlPositioningWarning::_update_warning() {49if (!control_node) {50title_icon->set_texture(nullptr);51title_label->set_text("");52hint_label->set_text("");53return;54}5556Node *parent_node = control_node->get_parent_control();57if (!parent_node) {58title_icon->set_texture(get_editor_theme_icon(SNAME("SubViewport")));59title_label->set_text(TTR("This node doesn't have a control parent."));60hint_label->set_text(TTR("Use the appropriate layout properties depending on where you are going to put it."));61} else if (Object::cast_to<Container>(parent_node)) {62title_icon->set_texture(get_editor_theme_icon(SNAME("ContainerLayout")));63title_label->set_text(TTR("This node is a child of a container."));64hint_label->set_text(TTR("Use container properties for positioning."));65} else {66title_icon->set_texture(get_editor_theme_icon(SNAME("ControlLayout")));67title_label->set_text(TTR("This node is a child of a regular control."));68hint_label->set_text(TTR("Use anchors and the rectangle for positioning."));69}7071bg_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("bg_group_note"), SNAME("EditorProperty")));72}7374void ControlPositioningWarning::_update_toggler() {75Ref<Texture2D> arrow;76if (hint_label->is_visible()) {77arrow = get_theme_icon(SNAME("arrow"), SNAME("Tree"));78set_tooltip_text(TTR("Collapse positioning hint."));79} else {80if (is_layout_rtl()) {81arrow = get_theme_icon(SNAME("arrow_collapsed"), SNAME("Tree"));82} else {83arrow = get_theme_icon(SNAME("arrow_collapsed_mirrored"), SNAME("Tree"));84}85set_tooltip_text(TTR("Expand positioning hint."));86}8788hint_icon->set_texture(arrow);89}9091void ControlPositioningWarning::set_control(Control *p_node) {92control_node = p_node;93_update_warning();94}9596void ControlPositioningWarning::gui_input(const Ref<InputEvent> &p_event) {97Ref<InputEventMouseButton> mb = p_event;98if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {99bool state = !hint_label->is_visible();100101hint_filler_left->set_visible(state);102hint_label->set_visible(state);103hint_filler_right->set_visible(state);104105_update_toggler();106}107}108109void ControlPositioningWarning::_notification(int p_notification) {110switch (p_notification) {111case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:112case NOTIFICATION_TRANSLATION_CHANGED:113case NOTIFICATION_THEME_CHANGED:114_update_warning();115_update_toggler();116break;117}118}119120ControlPositioningWarning::ControlPositioningWarning() {121set_mouse_filter(MOUSE_FILTER_STOP);122123bg_panel = memnew(PanelContainer);124bg_panel->set_mouse_filter(MOUSE_FILTER_IGNORE);125add_child(bg_panel);126127grid = memnew(GridContainer);128grid->set_columns(3);129bg_panel->add_child(grid);130131title_icon = memnew(TextureRect);132title_icon->set_stretch_mode(TextureRect::StretchMode::STRETCH_KEEP_CENTERED);133grid->add_child(title_icon);134135title_label = memnew(Label);136title_label->set_autowrap_mode(TextServer::AutowrapMode::AUTOWRAP_WORD);137title_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);138title_label->set_vertical_alignment(VerticalAlignment::VERTICAL_ALIGNMENT_CENTER);139grid->add_child(title_label);140141hint_icon = memnew(TextureRect);142hint_icon->set_stretch_mode(TextureRect::StretchMode::STRETCH_KEEP_CENTERED);143grid->add_child(hint_icon);144145// Filler.146hint_filler_left = memnew(Control);147hint_filler_left->hide();148grid->add_child(hint_filler_left);149150hint_label = memnew(Label);151hint_label->set_autowrap_mode(TextServer::AutowrapMode::AUTOWRAP_WORD);152hint_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);153hint_label->set_vertical_alignment(VerticalAlignment::VERTICAL_ALIGNMENT_CENTER);154hint_label->hide();155grid->add_child(hint_label);156157// Filler.158hint_filler_right = memnew(Control);159hint_filler_right->hide();160grid->add_child(hint_filler_right);161}162163void EditorPropertyAnchorsPreset::_set_read_only(bool p_read_only) {164options->set_disabled(p_read_only);165}166167void EditorPropertyAnchorsPreset::_notification(int p_what) {168switch (p_what) {169case NOTIFICATION_THEME_CHANGED: {170for (int i = 0; i < options->get_item_count(); i++) {171if (options->is_item_separator(i)) {172continue;173}174int64_t preset = options->get_item_metadata(i);175if (preset < 0 || PRESET_FULL_RECT < preset) {176continue;177}178static const StringName icon_names[] = {179StringName("ControlAlignTopLeft", true),180StringName("ControlAlignTopRight", true),181StringName("ControlAlignBottomLeft", true),182StringName("ControlAlignBottomRight", true),183StringName("ControlAlignCenterLeft", true),184StringName("ControlAlignCenterTop", true),185StringName("ControlAlignCenterRight", true),186StringName("ControlAlignCenterBottom", true),187StringName("ControlAlignCenter", true),188StringName("ControlAlignLeftWide", true),189StringName("ControlAlignTopWide", true),190StringName("ControlAlignRightWide", true),191StringName("ControlAlignBottomWide", true),192StringName("ControlAlignVCenterWide", true),193StringName("ControlAlignHCenterWide", true),194StringName("ControlAlignFullRect", true),195};196options->set_item_icon(i, get_editor_theme_icon(icon_names[preset]));197}198} break;199}200}201202void EditorPropertyAnchorsPreset::_option_selected(int p_which) {203int64_t val = options->get_item_metadata(p_which);204emit_changed(get_edited_property(), val);205}206207void EditorPropertyAnchorsPreset::update_property() {208int64_t which = get_edited_property_value();209210for (int i = 0; i < options->get_item_count(); i++) {211Variant val = options->get_item_metadata(i);212if (val != Variant() && which == (int64_t)val) {213options->select(i);214return;215}216}217}218219void EditorPropertyAnchorsPreset::setup(const Vector<String> &p_options) {220options->clear();221222const Vector<int> split_after = {223-1,224PRESET_FULL_RECT,225PRESET_BOTTOM_LEFT,226PRESET_CENTER,227};228229for (int i = 0; i < p_options.size(); i++) {230Vector<String> text_split = p_options[i].split(":");231int64_t current_val = text_split[1].to_int();232233const String &option_name = text_split[0];234options->add_item(option_name);235options->set_item_metadata(-1, current_val);236if (split_after.has(current_val)) {237options->add_separator();238}239}240}241242EditorPropertyAnchorsPreset::EditorPropertyAnchorsPreset() {243options = memnew(OptionButton);244options->set_clip_text(true);245options->set_flat(true);246add_child(options);247add_focusable(options);248options->connect(SceneStringName(item_selected), callable_mp(this, &EditorPropertyAnchorsPreset::_option_selected));249}250251void EditorPropertySizeFlags::_set_read_only(bool p_read_only) {252for (CheckBox *check : flag_checks) {253check->set_disabled(p_read_only);254}255flag_presets->set_disabled(p_read_only);256}257258void EditorPropertySizeFlags::_preset_selected(int p_which) {259int preset = flag_presets->get_item_id(p_which);260if (preset == SIZE_FLAGS_PRESET_CUSTOM) {261flag_options->set_visible(true);262return;263}264flag_options->set_visible(false);265266uint32_t value = 0;267switch (preset) {268case SIZE_FLAGS_PRESET_FILL:269value = Control::SIZE_FILL;270break;271case SIZE_FLAGS_PRESET_SHRINK_BEGIN:272value = Control::SIZE_SHRINK_BEGIN;273break;274case SIZE_FLAGS_PRESET_SHRINK_CENTER:275value = Control::SIZE_SHRINK_CENTER;276break;277case SIZE_FLAGS_PRESET_SHRINK_END:278value = Control::SIZE_SHRINK_END;279break;280}281282bool is_expand = flag_expand->is_visible() && flag_expand->is_pressed();283if (is_expand) {284value |= Control::SIZE_EXPAND;285}286287emit_changed(get_edited_property(), value);288}289290void EditorPropertySizeFlags::_expand_toggled() {291uint32_t value = get_edited_property_value();292293if (flag_expand->is_visible() && flag_expand->is_pressed()) {294value |= Control::SIZE_EXPAND;295} else {296value ^= Control::SIZE_EXPAND;297}298299// Keep the custom preset selected as we toggle individual flags.300keep_selected_preset = true;301emit_changed(get_edited_property(), value);302}303304void EditorPropertySizeFlags::_flag_toggled() {305uint32_t value = 0;306for (int i = 0; i < flag_checks.size(); i++) {307if (flag_checks[i]->is_pressed()) {308int flag_value = flag_checks[i]->get_meta("_value");309value |= flag_value;310}311}312313bool is_expand = flag_expand->is_visible() && flag_expand->is_pressed();314if (is_expand) {315value |= Control::SIZE_EXPAND;316}317318// Keep the custom preset selected as we toggle individual flags.319keep_selected_preset = true;320emit_changed(get_edited_property(), value);321}322323void EditorPropertySizeFlags::update_property() {324uint32_t value = get_edited_property_value();325326for (int i = 0; i < flag_checks.size(); i++) {327int flag_value = flag_checks[i]->get_meta("_value");328if (value & flag_value) {329flag_checks[i]->set_pressed(true);330} else {331flag_checks[i]->set_pressed(false);332}333}334335bool is_expand = value & Control::SIZE_EXPAND;336flag_expand->set_pressed(is_expand);337338if (keep_selected_preset) {339keep_selected_preset = false;340return;341}342343FlagPreset preset = SIZE_FLAGS_PRESET_CUSTOM;344if (value == Control::SIZE_FILL || value == (Control::SIZE_FILL | Control::SIZE_EXPAND)) {345preset = SIZE_FLAGS_PRESET_FILL;346} else if (value == Control::SIZE_SHRINK_BEGIN || value == (Control::SIZE_SHRINK_BEGIN | Control::SIZE_EXPAND)) {347preset = SIZE_FLAGS_PRESET_SHRINK_BEGIN;348} else if (value == Control::SIZE_SHRINK_CENTER || value == (Control::SIZE_SHRINK_CENTER | Control::SIZE_EXPAND)) {349preset = SIZE_FLAGS_PRESET_SHRINK_CENTER;350} else if (value == Control::SIZE_SHRINK_END || value == (Control::SIZE_SHRINK_END | Control::SIZE_EXPAND)) {351preset = SIZE_FLAGS_PRESET_SHRINK_END;352}353354int preset_idx = flag_presets->get_item_index(preset);355if (preset_idx >= 0) {356flag_presets->select(preset_idx);357}358flag_options->set_visible(preset == SIZE_FLAGS_PRESET_CUSTOM);359}360361void EditorPropertySizeFlags::setup(const Vector<String> &p_options, bool p_vertical) {362vertical = p_vertical;363364if (p_options.is_empty()) {365flag_presets->clear();366flag_presets->add_item(TTR("Container Default"));367flag_presets->set_disabled(true);368flag_expand->set_visible(false);369return;370}371372HashMap<int, String> flags;373for (int i = 0, j = 0; i < p_options.size(); i++, j++) {374Vector<String> text_split = p_options[i].split(":");375int64_t current_val = text_split[1].to_int();376flags[current_val] = text_split[0];377378if (current_val == SIZE_EXPAND) {379continue;380}381382CheckBox *cb = memnew(CheckBox);383cb->set_text(text_split[0]);384cb->set_clip_text(true);385cb->set_meta("_value", current_val);386cb->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertySizeFlags::_flag_toggled));387add_focusable(cb);388389flag_options->add_child(cb);390flag_checks.append(cb);391}392393Control *gui_base = EditorNode::get_singleton()->get_gui_base();394StringName wide_preset_icon = SNAME("ControlAlignHCenterWide");395StringName begin_preset_icon = SNAME("ControlAlignCenterLeft");396StringName end_preset_icon = SNAME("ControlAlignCenterRight");397if (vertical) {398wide_preset_icon = SNAME("ControlAlignVCenterWide");399begin_preset_icon = SNAME("ControlAlignCenterTop");400end_preset_icon = SNAME("ControlAlignCenterBottom");401}402403flag_presets->clear();404if (flags.has(SIZE_FILL)) {405flag_presets->add_icon_item(gui_base->get_editor_theme_icon(wide_preset_icon), TTR("Fill"), SIZE_FLAGS_PRESET_FILL);406}407// Shrink Begin is the same as no flags at all, as such it cannot be disabled.408flag_presets->add_icon_item(gui_base->get_editor_theme_icon(begin_preset_icon), TTR("Shrink Begin"), SIZE_FLAGS_PRESET_SHRINK_BEGIN);409if (flags.has(SIZE_SHRINK_CENTER)) {410flag_presets->add_icon_item(gui_base->get_editor_theme_icon(SNAME("ControlAlignCenter")), TTR("Shrink Center"), SIZE_FLAGS_PRESET_SHRINK_CENTER);411}412if (flags.has(SIZE_SHRINK_END)) {413flag_presets->add_icon_item(gui_base->get_editor_theme_icon(end_preset_icon), TTR("Shrink End"), SIZE_FLAGS_PRESET_SHRINK_END);414}415flag_presets->add_separator();416flag_presets->add_item(TTR("Custom"), SIZE_FLAGS_PRESET_CUSTOM);417418flag_expand->set_visible(flags.has(SIZE_EXPAND));419}420421EditorPropertySizeFlags::EditorPropertySizeFlags() {422VBoxContainer *vb = memnew(VBoxContainer);423add_child(vb);424425flag_presets = memnew(OptionButton);426flag_presets->set_clip_text(true);427flag_presets->set_flat(true);428vb->add_child(flag_presets);429add_focusable(flag_presets);430set_label_reference(flag_presets);431flag_presets->connect(SceneStringName(item_selected), callable_mp(this, &EditorPropertySizeFlags::_preset_selected));432433flag_options = memnew(VBoxContainer);434flag_options->hide();435vb->add_child(flag_options);436437flag_expand = memnew(CheckBox);438flag_expand->set_text(TTR("Expand"));439vb->add_child(flag_expand);440add_focusable(flag_expand);441flag_expand->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertySizeFlags::_expand_toggled));442}443444bool EditorInspectorPluginControl::can_handle(Object *p_object) {445return Object::cast_to<Control>(p_object) != nullptr;446}447448void EditorInspectorPluginControl::parse_category(Object *p_object, const String &p_category) {449inside_control_category = p_category == "Control";450}451452void EditorInspectorPluginControl::parse_group(Object *p_object, const String &p_group) {453if (!inside_control_category) {454return;455}456457Control *control = Object::cast_to<Control>(p_object);458if (!control || p_group != "Layout") {459return;460}461462ControlPositioningWarning *pos_warning = memnew(ControlPositioningWarning);463pos_warning->set_control(control);464add_custom_control(pos_warning);465}466467bool EditorInspectorPluginControl::parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide) {468Control *control = Object::cast_to<Control>(p_object);469if (!control) {470return false;471}472473if (p_path == "anchors_preset") {474EditorPropertyAnchorsPreset *prop_editor = memnew(EditorPropertyAnchorsPreset);475Vector<String> options = p_hint_text.split(",");476prop_editor->setup(options);477add_property_editor(p_path, prop_editor);478479return true;480}481482if (p_path == "size_flags_horizontal" || p_path == "size_flags_vertical") {483EditorPropertySizeFlags *prop_editor = memnew(EditorPropertySizeFlags);484Vector<String> options;485if (!p_hint_text.is_empty()) {486options = p_hint_text.split(",");487}488prop_editor->setup(options, p_path == "size_flags_vertical");489add_property_editor(p_path, prop_editor);490491return true;492}493494return false;495}496497// Toolbars controls.498499Size2 ControlEditorPopupButton::get_minimum_size() const {500Vector2 base_size = Vector2(26, 26) * EDSCALE;501502if (arrow_icon.is_null()) {503return base_size;504}505506Vector2 final_size;507final_size.x = base_size.x + arrow_icon->get_width();508final_size.y = MAX(base_size.y, arrow_icon->get_height());509510return final_size;511}512513void ControlEditorPopupButton::toggled(bool p_pressed) {514if (!p_pressed) {515return;516}517518Size2 size = get_size() * get_viewport()->get_canvas_transform().get_scale();519520popup_panel->set_size(Size2(size.width, 0));521Point2 gp = get_screen_position();522gp.y += size.y;523if (is_layout_rtl()) {524gp.x += size.width - popup_panel->get_size().width;525}526popup_panel->set_position(gp);527528popup_panel->popup();529}530531void ControlEditorPopupButton::_popup_visibility_changed(bool p_visible) {532set_pressed(p_visible);533}534535void ControlEditorPopupButton::_notification(int p_what) {536switch (p_what) {537case NOTIFICATION_THEME_CHANGED: {538arrow_icon = get_theme_icon("select_arrow", "Tree");539} break;540541case NOTIFICATION_DRAW: {542if (arrow_icon.is_valid()) {543Vector2 arrow_pos = Point2(26, 0) * EDSCALE;544if (is_layout_rtl()) {545arrow_pos.x = get_size().x - arrow_pos.x - arrow_icon->get_width();546}547arrow_pos.y = get_size().y / 2 - arrow_icon->get_height() / 2;548draw_texture(arrow_icon, arrow_pos);549}550} break;551552case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {553popup_panel->set_layout_direction((Window::LayoutDirection)get_layout_direction());554} break;555556case NOTIFICATION_VISIBILITY_CHANGED: {557if (!is_visible_in_tree()) {558popup_panel->hide();559}560} break;561}562}563564ControlEditorPopupButton::ControlEditorPopupButton() {565set_theme_type_variation(SceneStringName(FlatButton));566set_toggle_mode(true);567set_focus_mode(FOCUS_NONE);568569popup_panel = memnew(PopupPanel);570add_child(popup_panel);571popup_panel->connect("about_to_popup", callable_mp(this, &ControlEditorPopupButton::_popup_visibility_changed).bind(true));572popup_panel->connect("popup_hide", callable_mp(this, &ControlEditorPopupButton::_popup_visibility_changed).bind(false));573574popup_vbox = memnew(VBoxContainer);575popup_panel->add_child(popup_vbox);576}577578void ControlEditorPresetPicker::_add_row_button(HBoxContainer *p_row, const int p_preset, const String &p_name) {579ERR_FAIL_COND(preset_buttons.has(p_preset));580581Button *b = memnew(Button);582b->set_custom_minimum_size(Size2i(36, 36) * EDSCALE);583b->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER);584b->set_tooltip_text(p_name);585b->set_flat(true);586p_row->add_child(b);587b->connect(SceneStringName(pressed), callable_mp(this, &ControlEditorPresetPicker::_preset_button_pressed).bind(p_preset));588589preset_buttons[p_preset] = b;590}591592void ControlEditorPresetPicker::_add_separator(BoxContainer *p_box, Separator *p_separator) {593p_separator->add_theme_constant_override("separation", grid_separation);594p_separator->set_custom_minimum_size(Size2i(1, 1));595p_box->add_child(p_separator);596}597598void AnchorPresetPicker::_preset_button_pressed(const int p_preset) {599emit_signal("anchors_preset_selected", p_preset);600}601602void AnchorPresetPicker::_notification(int p_notification) {603switch (p_notification) {604case NOTIFICATION_THEME_CHANGED: {605preset_buttons[PRESET_TOP_LEFT]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignTopLeft")));606preset_buttons[PRESET_CENTER_TOP]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenterTop")));607preset_buttons[PRESET_TOP_RIGHT]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignTopRight")));608609preset_buttons[PRESET_CENTER_LEFT]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenterLeft")));610preset_buttons[PRESET_CENTER]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenter")));611preset_buttons[PRESET_CENTER_RIGHT]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenterRight")));612613preset_buttons[PRESET_BOTTOM_LEFT]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignBottomLeft")));614preset_buttons[PRESET_CENTER_BOTTOM]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenterBottom")));615preset_buttons[PRESET_BOTTOM_RIGHT]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignBottomRight")));616617preset_buttons[PRESET_TOP_WIDE]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignTopWide")));618preset_buttons[PRESET_HCENTER_WIDE]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignHCenterWide")));619preset_buttons[PRESET_BOTTOM_WIDE]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignBottomWide")));620621preset_buttons[PRESET_LEFT_WIDE]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignLeftWide")));622preset_buttons[PRESET_VCENTER_WIDE]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignVCenterWide")));623preset_buttons[PRESET_RIGHT_WIDE]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignRightWide")));624625preset_buttons[PRESET_FULL_RECT]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignFullRect")));626} break;627}628}629630void AnchorPresetPicker::_bind_methods() {631ADD_SIGNAL(MethodInfo("anchors_preset_selected", PropertyInfo(Variant::INT, "preset")));632}633634AnchorPresetPicker::AnchorPresetPicker() {635VBoxContainer *main_vb = memnew(VBoxContainer);636main_vb->add_theme_constant_override("separation", grid_separation);637add_child(main_vb);638639HBoxContainer *top_row = memnew(HBoxContainer);640top_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);641top_row->add_theme_constant_override("separation", grid_separation);642main_vb->add_child(top_row);643644_add_row_button(top_row, PRESET_TOP_LEFT, TTRC("Top Left"));645_add_row_button(top_row, PRESET_CENTER_TOP, TTRC("Center Top"));646_add_row_button(top_row, PRESET_TOP_RIGHT, TTRC("Top Right"));647_add_separator(top_row, memnew(VSeparator));648_add_row_button(top_row, PRESET_TOP_WIDE, TTRC("Top Wide"));649650HBoxContainer *mid_row = memnew(HBoxContainer);651mid_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);652mid_row->add_theme_constant_override("separation", grid_separation);653main_vb->add_child(mid_row);654655_add_row_button(mid_row, PRESET_CENTER_LEFT, TTRC("Center Left"));656_add_row_button(mid_row, PRESET_CENTER, TTRC("Center"));657_add_row_button(mid_row, PRESET_CENTER_RIGHT, TTRC("Center Right"));658_add_separator(mid_row, memnew(VSeparator));659_add_row_button(mid_row, PRESET_HCENTER_WIDE, TTRC("HCenter Wide"));660661HBoxContainer *bot_row = memnew(HBoxContainer);662bot_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);663bot_row->add_theme_constant_override("separation", grid_separation);664main_vb->add_child(bot_row);665666_add_row_button(bot_row, PRESET_BOTTOM_LEFT, TTRC("Bottom Left"));667_add_row_button(bot_row, PRESET_CENTER_BOTTOM, TTRC("Center Bottom"));668_add_row_button(bot_row, PRESET_BOTTOM_RIGHT, TTRC("Bottom Right"));669_add_separator(bot_row, memnew(VSeparator));670_add_row_button(bot_row, PRESET_BOTTOM_WIDE, TTRC("Bottom Wide"));671672_add_separator(main_vb, memnew(HSeparator));673674HBoxContainer *extra_row = memnew(HBoxContainer);675extra_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);676extra_row->add_theme_constant_override("separation", grid_separation);677main_vb->add_child(extra_row);678679_add_row_button(extra_row, PRESET_LEFT_WIDE, TTRC("Left Wide"));680_add_row_button(extra_row, PRESET_VCENTER_WIDE, TTRC("VCenter Wide"));681_add_row_button(extra_row, PRESET_RIGHT_WIDE, TTRC("Right Wide"));682_add_separator(extra_row, memnew(VSeparator));683_add_row_button(extra_row, PRESET_FULL_RECT, TTRC("Full Rect"));684}685686void SizeFlagPresetPicker::_preset_button_pressed(const int p_preset) {687int flags = (SizeFlags)p_preset;688if (expand_button->is_pressed()) {689flags |= SIZE_EXPAND;690}691692emit_signal("size_flags_selected", flags);693}694695void SizeFlagPresetPicker::_expand_button_pressed() {696emit_signal("expand_flag_toggled", expand_button->is_pressed());697}698699void SizeFlagPresetPicker::set_allowed_flags(Vector<SizeFlags> &p_flags) {700preset_buttons[SIZE_SHRINK_BEGIN]->set_disabled(!p_flags.has(SIZE_SHRINK_BEGIN));701preset_buttons[SIZE_SHRINK_CENTER]->set_disabled(!p_flags.has(SIZE_SHRINK_CENTER));702preset_buttons[SIZE_SHRINK_END]->set_disabled(!p_flags.has(SIZE_SHRINK_END));703preset_buttons[SIZE_FILL]->set_disabled(!p_flags.has(SIZE_FILL));704705expand_button->set_disabled(!p_flags.has(SIZE_EXPAND));706if (p_flags.has(SIZE_EXPAND)) {707expand_button->set_tooltip_text(TTR("Enable to also set the Expand flag.\nDisable to only set Shrink/Fill flags."));708} else {709expand_button->set_pressed(false);710expand_button->set_tooltip_text(TTR("Some parents of the selected nodes do not support the Expand flag."));711}712}713714void SizeFlagPresetPicker::set_expand_flag(bool p_expand) {715expand_button->set_pressed(p_expand);716}717718void SizeFlagPresetPicker::_notification(int p_notification) {719switch (p_notification) {720case NOTIFICATION_THEME_CHANGED: {721if (vertical) {722preset_buttons[SIZE_SHRINK_BEGIN]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenterTop")));723preset_buttons[SIZE_SHRINK_CENTER]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenter")));724preset_buttons[SIZE_SHRINK_END]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenterBottom")));725726preset_buttons[SIZE_FILL]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignVCenterWide")));727} else {728preset_buttons[SIZE_SHRINK_BEGIN]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenterLeft")));729preset_buttons[SIZE_SHRINK_CENTER]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenter")));730preset_buttons[SIZE_SHRINK_END]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenterRight")));731732preset_buttons[SIZE_FILL]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignHCenterWide")));733}734} break;735}736}737738void SizeFlagPresetPicker::_bind_methods() {739ADD_SIGNAL(MethodInfo("size_flags_selected", PropertyInfo(Variant::INT, "size_flags")));740ADD_SIGNAL(MethodInfo("expand_flag_toggled", PropertyInfo(Variant::BOOL, "expand_flag")));741}742743SizeFlagPresetPicker::SizeFlagPresetPicker(bool p_vertical) {744vertical = p_vertical;745746VBoxContainer *main_vb = memnew(VBoxContainer);747add_child(main_vb);748749HBoxContainer *main_row = memnew(HBoxContainer);750main_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);751main_row->add_theme_constant_override("separation", grid_separation);752main_vb->add_child(main_row);753754_add_row_button(main_row, SIZE_SHRINK_BEGIN, TTR("Shrink Begin"));755_add_row_button(main_row, SIZE_SHRINK_CENTER, TTR("Shrink Center"));756_add_row_button(main_row, SIZE_SHRINK_END, TTR("Shrink End"));757_add_separator(main_row, memnew(VSeparator));758_add_row_button(main_row, SIZE_FILL, TTR("Fill"));759760expand_button = memnew(CheckButton);761expand_button->set_flat(true);762expand_button->set_text(TTR("Expand"));763expand_button->set_tooltip_text(TTR("Enable to also set the Expand flag.\nDisable to only set Shrink/Fill flags."));764expand_button->connect(SceneStringName(pressed), callable_mp(this, &SizeFlagPresetPicker::_expand_button_pressed));765main_vb->add_child(expand_button);766}767768// Toolbar.769770void ControlEditorToolbar::_anchors_preset_selected(int p_preset) {771LayoutPreset preset = (LayoutPreset)p_preset;772List<Node *> selection = editor_selection->get_top_selected_node_list();773774EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();775undo_redo->create_action(TTR("Change Anchors, Offsets, Grow Direction"));776777for (Node *E : selection) {778Control *control = Object::cast_to<Control>(E);779if (control) {780undo_redo->add_do_property(control, "layout_mode", LayoutMode::LAYOUT_MODE_ANCHORS);781undo_redo->add_do_property(control, "anchors_preset", preset);782undo_redo->add_undo_method(control, "_edit_set_state", control->_edit_get_state());783}784}785786undo_redo->commit_action();787788anchors_mode = false;789anchor_mode_button->set_pressed(anchors_mode);790}791792void ControlEditorToolbar::_anchors_to_current_ratio() {793List<Node *> selection = editor_selection->get_top_selected_node_list();794795EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();796undo_redo->create_action(TTR("Change Anchors, Offsets (Keep Ratio)"));797798for (Node *E : selection) {799Control *control = Object::cast_to<Control>(E);800if (control) {801Point2 top_left_anchor = _position_to_anchor(control, Point2());802Point2 bottom_right_anchor = _position_to_anchor(control, control->get_size());803undo_redo->add_do_method(control, "set_anchor", SIDE_LEFT, top_left_anchor.x, false, true);804undo_redo->add_do_method(control, "set_anchor", SIDE_RIGHT, bottom_right_anchor.x, false, true);805undo_redo->add_do_method(control, "set_anchor", SIDE_TOP, top_left_anchor.y, false, true);806undo_redo->add_do_method(control, "set_anchor", SIDE_BOTTOM, bottom_right_anchor.y, false, true);807undo_redo->add_do_method(control, "set_meta", "_edit_use_anchors_", true);808809const bool use_anchors = control->get_meta("_edit_use_anchors_", false);810undo_redo->add_undo_method(control, "_edit_set_state", control->_edit_get_state());811if (use_anchors) {812undo_redo->add_undo_method(control, "set_meta", "_edit_use_anchors_", true);813} else {814undo_redo->add_undo_method(control, "remove_meta", "_edit_use_anchors_");815}816817anchors_mode = true;818anchor_mode_button->set_pressed(anchors_mode);819}820}821822undo_redo->commit_action();823}824825void ControlEditorToolbar::_anchor_mode_toggled(bool p_status) {826List<Control *> selection = _get_edited_controls();827for (Control *E : selection) {828if (Object::cast_to<Container>(E->get_parent())) {829continue;830}831832if (p_status) {833E->set_meta("_edit_use_anchors_", true);834} else {835E->remove_meta("_edit_use_anchors_");836}837}838839anchors_mode = p_status;840CanvasItemEditor::get_singleton()->update_viewport();841}842843void ControlEditorToolbar::_container_flags_selected(int p_flags, bool p_vertical) {844List<Node *> selection = editor_selection->get_top_selected_node_list();845846EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();847if (p_vertical) {848undo_redo->create_action(TTR("Change Vertical Size Flags"));849} else {850undo_redo->create_action(TTR("Change Horizontal Size Flags"));851}852853for (Node *E : selection) {854Control *control = Object::cast_to<Control>(E);855if (control) {856int old_flags = p_vertical ? control->get_v_size_flags() : control->get_h_size_flags();857if (p_vertical) {858undo_redo->add_do_method(control, "set_v_size_flags", p_flags);859undo_redo->add_undo_method(control, "set_v_size_flags", old_flags);860} else {861undo_redo->add_do_method(control, "set_h_size_flags", p_flags);862undo_redo->add_undo_method(control, "set_h_size_flags", old_flags);863}864}865}866867undo_redo->commit_action();868}869870void ControlEditorToolbar::_expand_flag_toggled(bool p_expand, bool p_vertical) {871List<Node *> selection = editor_selection->get_top_selected_node_list();872873EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();874if (p_vertical) {875undo_redo->create_action(TTR("Change Vertical Expand Flag"));876} else {877undo_redo->create_action(TTR("Change Horizontal Expand Flag"));878}879880for (Node *E : selection) {881Control *control = Object::cast_to<Control>(E);882if (control) {883int old_flags = p_vertical ? control->get_v_size_flags() : control->get_h_size_flags();884int new_flags = old_flags;885886if (p_expand) {887new_flags |= Control::SIZE_EXPAND;888} else {889new_flags &= ~Control::SIZE_EXPAND;890}891892if (p_vertical) {893undo_redo->add_do_method(control, "set_v_size_flags", new_flags);894undo_redo->add_undo_method(control, "set_v_size_flags", old_flags);895} else {896undo_redo->add_do_method(control, "set_h_size_flags", new_flags);897undo_redo->add_undo_method(control, "set_h_size_flags", old_flags);898}899}900}901902undo_redo->commit_action();903}904905Vector2 ControlEditorToolbar::_position_to_anchor(const Control *p_control, Vector2 position) {906ERR_FAIL_NULL_V(p_control, Vector2());907908Rect2 parent_rect = p_control->get_parent_anchorable_rect();909910Vector2 output;911if (p_control->is_layout_rtl()) {912output.x = (parent_rect.size.x == 0) ? 0.0 : (parent_rect.size.x - p_control->get_transform().xform(position).x - parent_rect.position.x) / parent_rect.size.x;913} else {914output.x = (parent_rect.size.x == 0) ? 0.0 : (p_control->get_transform().xform(position).x - parent_rect.position.x) / parent_rect.size.x;915}916output.y = (parent_rect.size.y == 0) ? 0.0 : (p_control->get_transform().xform(position).y - parent_rect.position.y) / parent_rect.size.y;917return output;918}919920bool ControlEditorToolbar::_is_node_locked(const Node *p_node) {921return p_node->get_meta("_edit_lock_", false);922}923924List<Control *> ControlEditorToolbar::_get_edited_controls() {925List<Control *> selection;926for (const KeyValue<Node *, Object *> &E : editor_selection->get_selection()) {927Control *control = Object::cast_to<Control>(E.key);928if (control && control->is_visible_in_tree() && control->get_viewport() == EditorNode::get_singleton()->get_scene_root() && !_is_node_locked(control)) {929selection.push_back(control);930}931}932933return selection;934}935936void ControlEditorToolbar::_selection_changed() {937// Update toolbar visibility.938bool has_controls = false;939bool has_control_parents = false;940bool has_container_parents = false;941942// Also update which size flags can be configured for the selected nodes.943Vector<SizeFlags> allowed_h_flags = {944SIZE_SHRINK_BEGIN,945SIZE_SHRINK_CENTER,946SIZE_SHRINK_END,947SIZE_FILL,948SIZE_EXPAND,949};950Vector<SizeFlags> allowed_v_flags = {951SIZE_SHRINK_BEGIN,952SIZE_SHRINK_CENTER,953SIZE_SHRINK_END,954SIZE_FILL,955SIZE_EXPAND,956};957958for (const KeyValue<Node *, Object *> &E : editor_selection->get_selection()) {959Control *control = Object::cast_to<Control>(E.key);960if (!control) {961continue;962}963has_controls = true;964965if (Object::cast_to<Control>(control->get_parent())) {966has_control_parents = true;967}968if (Object::cast_to<Container>(control->get_parent())) {969has_container_parents = true;970971Container *parent_container = Object::cast_to<Container>(control->get_parent());972973Vector<int> container_h_flags = parent_container->get_allowed_size_flags_horizontal();974Vector<SizeFlags> tmp_flags = allowed_h_flags.duplicate();975for (int i = 0; i < allowed_h_flags.size(); i++) {976if (!container_h_flags.has((int)allowed_h_flags[i])) {977tmp_flags.erase(allowed_h_flags[i]);978}979}980allowed_h_flags = tmp_flags;981982Vector<int> container_v_flags = parent_container->get_allowed_size_flags_vertical();983tmp_flags = allowed_v_flags.duplicate();984for (int i = 0; i < allowed_v_flags.size(); i++) {985if (!container_v_flags.has((int)allowed_v_flags[i])) {986tmp_flags.erase(allowed_v_flags[i]);987}988}989allowed_v_flags = tmp_flags;990}991}992993// Set general toolbar visibility.994set_visible(has_controls);995996// Set anchor tools visibility.997if (has_controls && (!has_control_parents || !has_container_parents)) {998anchors_button->set_visible(true);999anchor_mode_button->set_visible(true);10001001// Update anchor mode.1002int nb_valid_controls = 0;1003int nb_anchors_mode = 0;10041005List<Node *> selection = editor_selection->get_top_selected_node_list();1006for (Node *E : selection) {1007Control *control = Object::cast_to<Control>(E);1008if (!control) {1009continue;1010}1011if (Object::cast_to<Container>(control->get_parent())) {1012continue;1013}10141015nb_valid_controls++;1016if (control->get_meta("_edit_use_anchors_", false)) {1017nb_anchors_mode++;1018}1019}10201021anchors_mode = (nb_valid_controls == nb_anchors_mode);1022anchor_mode_button->set_pressed(anchors_mode);1023} else {1024anchors_button->set_visible(false);1025anchor_mode_button->set_visible(false);1026anchor_mode_button->set_pressed(false);1027}10281029// Set container tools visibility.1030if (has_controls && (!has_control_parents || has_container_parents)) {1031containers_button->set_visible(true);10321033// Update allowed size flags.1034if (has_container_parents) {1035container_h_picker->set_allowed_flags(allowed_h_flags);1036container_v_picker->set_allowed_flags(allowed_v_flags);1037} else {1038Vector<SizeFlags> allowed_all_flags = {1039SIZE_SHRINK_BEGIN,1040SIZE_SHRINK_CENTER,1041SIZE_SHRINK_END,1042SIZE_FILL,1043SIZE_EXPAND,1044};10451046container_h_picker->set_allowed_flags(allowed_all_flags);1047container_v_picker->set_allowed_flags(allowed_all_flags);1048}10491050// Update expand toggles.1051int nb_valid_controls = 0;1052int nb_h_expand = 0;1053int nb_v_expand = 0;10541055List<Node *> selection = editor_selection->get_top_selected_node_list();1056for (Node *E : selection) {1057Control *control = Object::cast_to<Control>(E);1058if (!control) {1059continue;1060}10611062nb_valid_controls++;1063if (control->get_h_size_flags() & Control::SIZE_EXPAND) {1064nb_h_expand++;1065}1066if (control->get_v_size_flags() & Control::SIZE_EXPAND) {1067nb_v_expand++;1068}1069}10701071container_h_picker->set_expand_flag(nb_valid_controls == nb_h_expand);1072container_v_picker->set_expand_flag(nb_valid_controls == nb_v_expand);1073} else {1074containers_button->set_visible(false);1075}1076}10771078void ControlEditorToolbar::_notification(int p_what) {1079switch (p_what) {1080case NOTIFICATION_THEME_CHANGED: {1081anchors_button->set_button_icon(get_editor_theme_icon(SNAME("ControlLayout")));1082anchor_mode_button->set_button_icon(get_editor_theme_icon(SNAME("Anchor")));1083containers_button->set_button_icon(get_editor_theme_icon(SNAME("ContainerLayout")));1084} break;1085}1086}10871088ControlEditorToolbar::ControlEditorToolbar() {1089// Anchor and offset tools.1090anchors_button = memnew(ControlEditorPopupButton);1091anchors_button->set_tooltip_text(TTR("Presets for the anchor and offset values of a Control node."));1092add_child(anchors_button);10931094Label *anchors_label = memnew(Label);1095anchors_label->set_text(TTR("Anchor preset"));1096anchors_button->get_popup_hbox()->add_child(anchors_label);1097AnchorPresetPicker *anchors_picker = memnew(AnchorPresetPicker);1098anchors_picker->set_h_size_flags(SIZE_SHRINK_CENTER);1099anchors_button->get_popup_hbox()->add_child(anchors_picker);1100anchors_picker->connect("anchors_preset_selected", callable_mp(this, &ControlEditorToolbar::_anchors_preset_selected));11011102anchors_button->get_popup_hbox()->add_child(memnew(HSeparator));11031104Button *keep_ratio_button = memnew(Button);1105keep_ratio_button->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);1106keep_ratio_button->set_text(TTR("Set to Current Ratio"));1107keep_ratio_button->set_tooltip_text(TTR("Adjust anchors and offsets to match the current rect size."));1108anchors_button->get_popup_hbox()->add_child(keep_ratio_button);1109keep_ratio_button->connect(SceneStringName(pressed), callable_mp(this, &ControlEditorToolbar::_anchors_to_current_ratio));11101111anchor_mode_button = memnew(Button);1112anchor_mode_button->set_theme_type_variation(SceneStringName(FlatButton));1113anchor_mode_button->set_toggle_mode(true);1114anchor_mode_button->set_tooltip_text(TTR("When active, moving Control nodes changes their anchors instead of their offsets."));1115anchor_mode_button->set_accessibility_name(TTRC("Change Anchors"));1116add_child(anchor_mode_button);1117anchor_mode_button->connect(SceneStringName(toggled), callable_mp(this, &ControlEditorToolbar::_anchor_mode_toggled));11181119// Container tools.1120containers_button = memnew(ControlEditorPopupButton);1121containers_button->set_tooltip_text(TTR("Sizing settings for children of a Container node."));1122add_child(containers_button);11231124Label *container_h_label = memnew(Label);1125container_h_label->set_text(TTR("Horizontal alignment"));1126containers_button->get_popup_hbox()->add_child(container_h_label);1127container_h_picker = memnew(SizeFlagPresetPicker(false));1128containers_button->get_popup_hbox()->add_child(container_h_picker);1129container_h_picker->connect("size_flags_selected", callable_mp(this, &ControlEditorToolbar::_container_flags_selected).bind(false));1130container_h_picker->connect("expand_flag_toggled", callable_mp(this, &ControlEditorToolbar::_expand_flag_toggled).bind(false));11311132containers_button->get_popup_hbox()->add_child(memnew(HSeparator));11331134Label *container_v_label = memnew(Label);1135container_v_label->set_text(TTR("Vertical alignment"));1136containers_button->get_popup_hbox()->add_child(container_v_label);1137container_v_picker = memnew(SizeFlagPresetPicker(true));1138containers_button->get_popup_hbox()->add_child(container_v_picker);1139container_v_picker->connect("size_flags_selected", callable_mp(this, &ControlEditorToolbar::_container_flags_selected).bind(true));1140container_v_picker->connect("expand_flag_toggled", callable_mp(this, &ControlEditorToolbar::_expand_flag_toggled).bind(true));11411142// Editor connections.1143editor_selection = EditorNode::get_singleton()->get_editor_selection();1144editor_selection->add_editor_plugin(this);1145editor_selection->connect("selection_changed", callable_mp(this, &ControlEditorToolbar::_selection_changed));11461147singleton = this;1148}11491150ControlEditorToolbar *ControlEditorToolbar::singleton = nullptr;11511152// Editor plugin.11531154ControlEditorPlugin::ControlEditorPlugin() {1155toolbar = memnew(ControlEditorToolbar);1156toolbar->hide();1157add_control_to_container(CONTAINER_CANVAS_EDITOR_MENU, toolbar);11581159Ref<EditorInspectorPluginControl> plugin;1160plugin.instantiate();1161add_inspector_plugin(plugin);1162}116311641165