Path: blob/master/editor/scene/gui/control_editor_plugin.cpp
20974 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);246options->set_theme_type_variation(SNAME("EditorInspectorButton"));247add_child(options);248add_focusable(options);249options->connect(SceneStringName(item_selected), callable_mp(this, &EditorPropertyAnchorsPreset::_option_selected));250}251252void EditorPropertySizeFlags::_set_read_only(bool p_read_only) {253for (CheckBox *check : flag_checks) {254check->set_disabled(p_read_only);255}256flag_presets->set_disabled(p_read_only);257}258259void EditorPropertySizeFlags::_preset_selected(int p_which) {260int preset = flag_presets->get_item_id(p_which);261if (preset == SIZE_FLAGS_PRESET_CUSTOM) {262flag_options->set_visible(true);263return;264}265flag_options->set_visible(false);266267uint32_t value = 0;268switch (preset) {269case SIZE_FLAGS_PRESET_FILL:270value = Control::SIZE_FILL;271break;272case SIZE_FLAGS_PRESET_SHRINK_BEGIN:273value = Control::SIZE_SHRINK_BEGIN;274break;275case SIZE_FLAGS_PRESET_SHRINK_CENTER:276value = Control::SIZE_SHRINK_CENTER;277break;278case SIZE_FLAGS_PRESET_SHRINK_END:279value = Control::SIZE_SHRINK_END;280break;281}282283bool is_expand = flag_expand->is_visible() && flag_expand->is_pressed();284if (is_expand) {285value |= Control::SIZE_EXPAND;286}287288emit_changed(get_edited_property(), value);289}290291void EditorPropertySizeFlags::_expand_toggled() {292uint32_t value = get_edited_property_value();293294if (flag_expand->is_visible() && flag_expand->is_pressed()) {295value |= Control::SIZE_EXPAND;296} else {297value ^= Control::SIZE_EXPAND;298}299300// Keep the custom preset selected as we toggle individual flags.301keep_selected_preset = true;302emit_changed(get_edited_property(), value);303}304305void EditorPropertySizeFlags::_flag_toggled() {306uint32_t value = 0;307for (int i = 0; i < flag_checks.size(); i++) {308if (flag_checks[i]->is_pressed()) {309int flag_value = flag_checks[i]->get_meta("_value");310value |= flag_value;311}312}313314bool is_expand = flag_expand->is_visible() && flag_expand->is_pressed();315if (is_expand) {316value |= Control::SIZE_EXPAND;317}318319// Keep the custom preset selected as we toggle individual flags.320keep_selected_preset = true;321emit_changed(get_edited_property(), value);322}323324void EditorPropertySizeFlags::update_property() {325uint32_t value = get_edited_property_value();326327for (int i = 0; i < flag_checks.size(); i++) {328int flag_value = flag_checks[i]->get_meta("_value");329if (value & flag_value) {330flag_checks[i]->set_pressed(true);331} else {332flag_checks[i]->set_pressed(false);333}334}335336bool is_expand = value & Control::SIZE_EXPAND;337flag_expand->set_pressed(is_expand);338339if (keep_selected_preset) {340keep_selected_preset = false;341return;342}343344FlagPreset preset = SIZE_FLAGS_PRESET_CUSTOM;345if (value == Control::SIZE_FILL || value == (Control::SIZE_FILL | Control::SIZE_EXPAND)) {346preset = SIZE_FLAGS_PRESET_FILL;347} else if (value == Control::SIZE_SHRINK_BEGIN || value == (Control::SIZE_SHRINK_BEGIN | Control::SIZE_EXPAND)) {348preset = SIZE_FLAGS_PRESET_SHRINK_BEGIN;349} else if (value == Control::SIZE_SHRINK_CENTER || value == (Control::SIZE_SHRINK_CENTER | Control::SIZE_EXPAND)) {350preset = SIZE_FLAGS_PRESET_SHRINK_CENTER;351} else if (value == Control::SIZE_SHRINK_END || value == (Control::SIZE_SHRINK_END | Control::SIZE_EXPAND)) {352preset = SIZE_FLAGS_PRESET_SHRINK_END;353}354355int preset_idx = flag_presets->get_item_index(preset);356if (preset_idx >= 0) {357flag_presets->select(preset_idx);358}359flag_options->set_visible(preset == SIZE_FLAGS_PRESET_CUSTOM);360}361362void EditorPropertySizeFlags::setup(const Vector<String> &p_options, bool p_vertical) {363vertical = p_vertical;364365if (p_options.is_empty()) {366flag_presets->clear();367flag_presets->add_item(TTR("Container Default"));368flag_presets->set_disabled(true);369flag_expand->set_visible(false);370return;371}372373HashMap<int, String> flags;374for (int i = 0, j = 0; i < p_options.size(); i++, j++) {375Vector<String> text_split = p_options[i].split(":");376int64_t current_val = text_split[1].to_int();377flags[current_val] = text_split[0];378379if (current_val == SIZE_EXPAND) {380continue;381}382383CheckBox *cb = memnew(CheckBox);384cb->set_text(text_split[0]);385cb->set_clip_text(true);386cb->set_meta("_value", current_val);387cb->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertySizeFlags::_flag_toggled));388add_focusable(cb);389390flag_options->add_child(cb);391flag_checks.append(cb);392}393394Control *gui_base = EditorNode::get_singleton()->get_gui_base();395StringName wide_preset_icon = SNAME("ControlAlignHCenterWide");396StringName begin_preset_icon = SNAME("ControlAlignCenterLeft");397StringName end_preset_icon = SNAME("ControlAlignCenterRight");398if (vertical) {399wide_preset_icon = SNAME("ControlAlignVCenterWide");400begin_preset_icon = SNAME("ControlAlignCenterTop");401end_preset_icon = SNAME("ControlAlignCenterBottom");402}403404flag_presets->clear();405if (flags.has(SIZE_FILL)) {406flag_presets->add_icon_item(gui_base->get_editor_theme_icon(wide_preset_icon), TTR("Fill"), SIZE_FLAGS_PRESET_FILL);407}408// Shrink Begin is the same as no flags at all, as such it cannot be disabled.409flag_presets->add_icon_item(gui_base->get_editor_theme_icon(begin_preset_icon), TTR("Shrink Begin"), SIZE_FLAGS_PRESET_SHRINK_BEGIN);410if (flags.has(SIZE_SHRINK_CENTER)) {411flag_presets->add_icon_item(gui_base->get_editor_theme_icon(SNAME("ControlAlignCenter")), TTR("Shrink Center"), SIZE_FLAGS_PRESET_SHRINK_CENTER);412}413if (flags.has(SIZE_SHRINK_END)) {414flag_presets->add_icon_item(gui_base->get_editor_theme_icon(end_preset_icon), TTR("Shrink End"), SIZE_FLAGS_PRESET_SHRINK_END);415}416flag_presets->add_separator();417flag_presets->add_item(TTR("Custom"), SIZE_FLAGS_PRESET_CUSTOM);418419flag_expand->set_visible(flags.has(SIZE_EXPAND));420}421422EditorPropertySizeFlags::EditorPropertySizeFlags() {423VBoxContainer *vb = memnew(VBoxContainer);424add_child(vb);425426flag_presets = memnew(OptionButton);427flag_presets->set_clip_text(true);428flag_presets->set_flat(true);429flag_presets->set_theme_type_variation(SNAME("EditorInspectorButton"));430vb->add_child(flag_presets);431add_focusable(flag_presets);432set_label_reference(flag_presets);433flag_presets->connect(SceneStringName(item_selected), callable_mp(this, &EditorPropertySizeFlags::_preset_selected));434435flag_options = memnew(VBoxContainer);436flag_options->hide();437vb->add_child(flag_options);438439flag_expand = memnew(CheckBox);440flag_expand->set_text(TTR("Expand"));441flag_expand->set_clip_text(true);442vb->add_child(flag_expand);443add_focusable(flag_expand);444flag_expand->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertySizeFlags::_expand_toggled));445}446447bool EditorInspectorPluginControl::can_handle(Object *p_object) {448return Object::cast_to<Control>(p_object) != nullptr;449}450451void EditorInspectorPluginControl::parse_category(Object *p_object, const String &p_category) {452inside_control_category = p_category == "Control";453}454455void EditorInspectorPluginControl::parse_group(Object *p_object, const String &p_group) {456if (!inside_control_category) {457return;458}459460Control *control = Object::cast_to<Control>(p_object);461if (!control || p_group != "Layout") {462return;463}464465ControlPositioningWarning *pos_warning = memnew(ControlPositioningWarning);466pos_warning->set_control(control);467add_custom_control(pos_warning);468}469470bool 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) {471Control *control = Object::cast_to<Control>(p_object);472if (!control) {473return false;474}475476if (p_path == "anchors_preset") {477EditorPropertyAnchorsPreset *prop_editor = memnew(EditorPropertyAnchorsPreset);478Vector<String> options = p_hint_text.split(",");479prop_editor->setup(options);480add_property_editor(p_path, prop_editor);481482return true;483}484485if (p_path == "size_flags_horizontal" || p_path == "size_flags_vertical") {486EditorPropertySizeFlags *prop_editor = memnew(EditorPropertySizeFlags);487Vector<String> options;488if (!p_hint_text.is_empty()) {489options = p_hint_text.split(",");490}491prop_editor->setup(options, p_path == "size_flags_vertical");492add_property_editor(p_path, prop_editor);493494return true;495}496497return false;498}499500// Toolbars controls.501502Size2 ControlEditorPopupButton::get_minimum_size() const {503Vector2 base_size = Vector2(26, 26) * EDSCALE;504505if (arrow_icon.is_null()) {506return base_size;507}508509Vector2 final_size;510final_size.x = base_size.x + arrow_icon->get_width();511final_size.y = MAX(base_size.y, arrow_icon->get_height());512513return final_size;514}515516void ControlEditorPopupButton::toggled(bool p_pressed) {517if (!p_pressed) {518return;519}520521Size2 size = get_size() * get_viewport()->get_canvas_transform().get_scale();522523popup_panel->set_size(Size2(size.width, 0));524Point2 gp = get_screen_position();525gp.y += size.y;526if (is_layout_rtl()) {527gp.x += size.width - popup_panel->get_size().width;528}529popup_panel->set_position(gp);530531popup_panel->popup();532}533534void ControlEditorPopupButton::_popup_visibility_changed(bool p_visible) {535set_pressed(p_visible);536}537538void ControlEditorPopupButton::_notification(int p_what) {539switch (p_what) {540case NOTIFICATION_THEME_CHANGED: {541arrow_icon = get_theme_icon("select_arrow", "Tree");542} break;543544case NOTIFICATION_DRAW: {545if (arrow_icon.is_valid()) {546Vector2 arrow_pos = Point2(26, 0) * EDSCALE;547if (is_layout_rtl()) {548arrow_pos.x = get_size().x - arrow_pos.x - arrow_icon->get_width();549}550arrow_pos.y = get_size().y / 2 - arrow_icon->get_height() / 2;551draw_texture(arrow_icon, arrow_pos);552}553} break;554555case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {556popup_panel->set_layout_direction((Window::LayoutDirection)get_layout_direction());557} break;558559case NOTIFICATION_VISIBILITY_CHANGED: {560if (!is_visible_in_tree()) {561popup_panel->hide();562}563} break;564}565}566567ControlEditorPopupButton::ControlEditorPopupButton() {568set_theme_type_variation(SceneStringName(FlatButton));569set_toggle_mode(true);570set_focus_mode(FOCUS_NONE);571572popup_panel = memnew(PopupPanel);573add_child(popup_panel);574popup_panel->connect("about_to_popup", callable_mp(this, &ControlEditorPopupButton::_popup_visibility_changed).bind(true));575popup_panel->connect("popup_hide", callable_mp(this, &ControlEditorPopupButton::_popup_visibility_changed).bind(false));576577popup_vbox = memnew(VBoxContainer);578popup_panel->add_child(popup_vbox);579}580581void ControlEditorPresetPicker::_add_row_button(HBoxContainer *p_row, const int p_preset, const String &p_name) {582ERR_FAIL_COND(preset_buttons.has(p_preset));583584Button *b = memnew(Button);585b->set_custom_minimum_size(Size2i(36, 36) * EDSCALE);586b->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER);587b->set_tooltip_text(p_name);588b->set_flat(true);589p_row->add_child(b);590b->connect(SceneStringName(pressed), callable_mp(this, &ControlEditorPresetPicker::_preset_button_pressed).bind(p_preset));591592preset_buttons[p_preset] = b;593}594595void ControlEditorPresetPicker::_add_separator(BoxContainer *p_box, Separator *p_separator) {596p_separator->add_theme_constant_override("separation", grid_separation);597p_separator->set_custom_minimum_size(Size2i(1, 1));598p_box->add_child(p_separator);599}600601void AnchorPresetPicker::_preset_button_pressed(const int p_preset) {602emit_signal("anchors_preset_selected", p_preset);603}604605void AnchorPresetPicker::_notification(int p_notification) {606switch (p_notification) {607case NOTIFICATION_THEME_CHANGED: {608preset_buttons[PRESET_TOP_LEFT]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignTopLeft")));609preset_buttons[PRESET_CENTER_TOP]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenterTop")));610preset_buttons[PRESET_TOP_RIGHT]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignTopRight")));611612preset_buttons[PRESET_CENTER_LEFT]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenterLeft")));613preset_buttons[PRESET_CENTER]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenter")));614preset_buttons[PRESET_CENTER_RIGHT]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenterRight")));615616preset_buttons[PRESET_BOTTOM_LEFT]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignBottomLeft")));617preset_buttons[PRESET_CENTER_BOTTOM]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenterBottom")));618preset_buttons[PRESET_BOTTOM_RIGHT]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignBottomRight")));619620preset_buttons[PRESET_TOP_WIDE]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignTopWide")));621preset_buttons[PRESET_HCENTER_WIDE]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignHCenterWide")));622preset_buttons[PRESET_BOTTOM_WIDE]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignBottomWide")));623624preset_buttons[PRESET_LEFT_WIDE]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignLeftWide")));625preset_buttons[PRESET_VCENTER_WIDE]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignVCenterWide")));626preset_buttons[PRESET_RIGHT_WIDE]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignRightWide")));627628preset_buttons[PRESET_FULL_RECT]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignFullRect")));629} break;630}631}632633void AnchorPresetPicker::_bind_methods() {634ADD_SIGNAL(MethodInfo("anchors_preset_selected", PropertyInfo(Variant::INT, "preset")));635}636637AnchorPresetPicker::AnchorPresetPicker() {638VBoxContainer *main_vb = memnew(VBoxContainer);639main_vb->add_theme_constant_override("separation", grid_separation);640add_child(main_vb);641642HBoxContainer *top_row = memnew(HBoxContainer);643top_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);644top_row->add_theme_constant_override("separation", grid_separation);645main_vb->add_child(top_row);646647_add_row_button(top_row, PRESET_TOP_LEFT, TTRC("Top Left"));648_add_row_button(top_row, PRESET_CENTER_TOP, TTRC("Center Top"));649_add_row_button(top_row, PRESET_TOP_RIGHT, TTRC("Top Right"));650_add_separator(top_row, memnew(VSeparator));651_add_row_button(top_row, PRESET_TOP_WIDE, TTRC("Top Wide"));652653HBoxContainer *mid_row = memnew(HBoxContainer);654mid_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);655mid_row->add_theme_constant_override("separation", grid_separation);656main_vb->add_child(mid_row);657658_add_row_button(mid_row, PRESET_CENTER_LEFT, TTRC("Center Left"));659_add_row_button(mid_row, PRESET_CENTER, TTRC("Center"));660_add_row_button(mid_row, PRESET_CENTER_RIGHT, TTRC("Center Right"));661_add_separator(mid_row, memnew(VSeparator));662_add_row_button(mid_row, PRESET_HCENTER_WIDE, TTRC("HCenter Wide"));663664HBoxContainer *bot_row = memnew(HBoxContainer);665bot_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);666bot_row->add_theme_constant_override("separation", grid_separation);667main_vb->add_child(bot_row);668669_add_row_button(bot_row, PRESET_BOTTOM_LEFT, TTRC("Bottom Left"));670_add_row_button(bot_row, PRESET_CENTER_BOTTOM, TTRC("Center Bottom"));671_add_row_button(bot_row, PRESET_BOTTOM_RIGHT, TTRC("Bottom Right"));672_add_separator(bot_row, memnew(VSeparator));673_add_row_button(bot_row, PRESET_BOTTOM_WIDE, TTRC("Bottom Wide"));674675_add_separator(main_vb, memnew(HSeparator));676677HBoxContainer *extra_row = memnew(HBoxContainer);678extra_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);679extra_row->add_theme_constant_override("separation", grid_separation);680main_vb->add_child(extra_row);681682_add_row_button(extra_row, PRESET_LEFT_WIDE, TTRC("Left Wide"));683_add_row_button(extra_row, PRESET_VCENTER_WIDE, TTRC("VCenter Wide"));684_add_row_button(extra_row, PRESET_RIGHT_WIDE, TTRC("Right Wide"));685_add_separator(extra_row, memnew(VSeparator));686_add_row_button(extra_row, PRESET_FULL_RECT, TTRC("Full Rect"));687}688689void SizeFlagPresetPicker::_preset_button_pressed(const int p_preset) {690int flags = (SizeFlags)p_preset;691if (expand_button->is_pressed()) {692flags |= SIZE_EXPAND;693}694695emit_signal("size_flags_selected", flags);696}697698void SizeFlagPresetPicker::_expand_button_pressed() {699emit_signal("expand_flag_toggled", expand_button->is_pressed());700}701702void SizeFlagPresetPicker::set_allowed_flags(Vector<SizeFlags> &p_flags) {703preset_buttons[SIZE_SHRINK_BEGIN]->set_disabled(!p_flags.has(SIZE_SHRINK_BEGIN));704preset_buttons[SIZE_SHRINK_CENTER]->set_disabled(!p_flags.has(SIZE_SHRINK_CENTER));705preset_buttons[SIZE_SHRINK_END]->set_disabled(!p_flags.has(SIZE_SHRINK_END));706preset_buttons[SIZE_FILL]->set_disabled(!p_flags.has(SIZE_FILL));707708expand_button->set_disabled(!p_flags.has(SIZE_EXPAND));709if (p_flags.has(SIZE_EXPAND)) {710expand_button->set_tooltip_text(TTR("Enable to also set the Expand flag.\nDisable to only set Shrink/Fill flags."));711} else {712expand_button->set_pressed(false);713expand_button->set_tooltip_text(TTR("Some parents of the selected nodes do not support the Expand flag."));714}715}716717void SizeFlagPresetPicker::set_expand_flag(bool p_expand) {718expand_button->set_pressed(p_expand);719}720721void SizeFlagPresetPicker::_notification(int p_notification) {722switch (p_notification) {723case NOTIFICATION_THEME_CHANGED: {724if (vertical) {725preset_buttons[SIZE_SHRINK_BEGIN]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenterTop")));726preset_buttons[SIZE_SHRINK_CENTER]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenter")));727preset_buttons[SIZE_SHRINK_END]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenterBottom")));728729preset_buttons[SIZE_FILL]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignVCenterWide")));730} else {731preset_buttons[SIZE_SHRINK_BEGIN]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenterLeft")));732preset_buttons[SIZE_SHRINK_CENTER]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenter")));733preset_buttons[SIZE_SHRINK_END]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenterRight")));734735preset_buttons[SIZE_FILL]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignHCenterWide")));736}737} break;738}739}740741void SizeFlagPresetPicker::_bind_methods() {742ADD_SIGNAL(MethodInfo("size_flags_selected", PropertyInfo(Variant::INT, "size_flags")));743ADD_SIGNAL(MethodInfo("expand_flag_toggled", PropertyInfo(Variant::BOOL, "expand_flag")));744}745746SizeFlagPresetPicker::SizeFlagPresetPicker(bool p_vertical) {747vertical = p_vertical;748749VBoxContainer *main_vb = memnew(VBoxContainer);750add_child(main_vb);751752HBoxContainer *main_row = memnew(HBoxContainer);753main_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);754main_row->add_theme_constant_override("separation", grid_separation);755main_vb->add_child(main_row);756757_add_row_button(main_row, SIZE_SHRINK_BEGIN, TTR("Shrink Begin"));758_add_row_button(main_row, SIZE_SHRINK_CENTER, TTR("Shrink Center"));759_add_row_button(main_row, SIZE_SHRINK_END, TTR("Shrink End"));760_add_separator(main_row, memnew(VSeparator));761_add_row_button(main_row, SIZE_FILL, TTR("Fill"));762763expand_button = memnew(CheckButton);764expand_button->set_flat(true);765expand_button->set_text(TTR("Expand"));766expand_button->set_tooltip_text(TTR("Enable to also set the Expand flag.\nDisable to only set Shrink/Fill flags."));767expand_button->connect(SceneStringName(pressed), callable_mp(this, &SizeFlagPresetPicker::_expand_button_pressed));768main_vb->add_child(expand_button);769}770771// Toolbar.772773void ControlEditorToolbar::_anchors_preset_selected(int p_preset) {774LayoutPreset preset = (LayoutPreset)p_preset;775const List<Node *> &selection = editor_selection->get_top_selected_node_list();776777EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();778undo_redo->create_action(TTR("Change Anchors, Offsets, Grow Direction"));779780for (Node *E : selection) {781Control *control = Object::cast_to<Control>(E);782if (control) {783undo_redo->add_do_property(control, "layout_mode", LayoutMode::LAYOUT_MODE_ANCHORS);784undo_redo->add_do_property(control, "anchors_preset", preset);785undo_redo->add_undo_method(control, "_edit_set_state", control->_edit_get_state());786}787}788789undo_redo->commit_action();790791anchors_mode = false;792anchor_mode_button->set_pressed(anchors_mode);793}794795void ControlEditorToolbar::_anchors_to_current_ratio() {796const List<Node *> &selection = editor_selection->get_top_selected_node_list();797798EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();799undo_redo->create_action(TTR("Change Anchors, Offsets (Keep Ratio)"));800801for (Node *E : selection) {802Control *control = Object::cast_to<Control>(E);803if (control) {804Point2 top_left_anchor = _position_to_anchor(control, Point2());805Point2 bottom_right_anchor = _position_to_anchor(control, control->get_size());806undo_redo->add_do_method(control, "set_anchor", SIDE_LEFT, top_left_anchor.x, false, true);807undo_redo->add_do_method(control, "set_anchor", SIDE_RIGHT, bottom_right_anchor.x, false, true);808undo_redo->add_do_method(control, "set_anchor", SIDE_TOP, top_left_anchor.y, false, true);809undo_redo->add_do_method(control, "set_anchor", SIDE_BOTTOM, bottom_right_anchor.y, false, true);810undo_redo->add_do_method(control, "set_meta", "_edit_use_anchors_", true);811812const bool use_anchors = control->get_meta("_edit_use_anchors_", false);813undo_redo->add_undo_method(control, "_edit_set_state", control->_edit_get_state());814if (use_anchors) {815undo_redo->add_undo_method(control, "set_meta", "_edit_use_anchors_", true);816} else {817undo_redo->add_undo_method(control, "remove_meta", "_edit_use_anchors_");818}819820anchors_mode = true;821anchor_mode_button->set_pressed(anchors_mode);822}823}824825undo_redo->commit_action();826}827828void ControlEditorToolbar::_anchor_mode_toggled(bool p_status) {829List<Control *> selection = _get_edited_controls();830for (Control *E : selection) {831if (Object::cast_to<Container>(E->get_parent())) {832continue;833}834835if (p_status) {836E->set_meta("_edit_use_anchors_", true);837} else {838E->remove_meta("_edit_use_anchors_");839}840}841842anchors_mode = p_status;843CanvasItemEditor::get_singleton()->update_viewport();844}845846void ControlEditorToolbar::_container_flags_selected(int p_flags, bool p_vertical) {847const List<Node *> &selection = editor_selection->get_top_selected_node_list();848849EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();850if (p_vertical) {851undo_redo->create_action(TTR("Change Vertical Size Flags"));852} else {853undo_redo->create_action(TTR("Change Horizontal Size Flags"));854}855856for (Node *E : selection) {857Control *control = Object::cast_to<Control>(E);858if (control) {859int old_flags = p_vertical ? control->get_v_size_flags() : control->get_h_size_flags();860if (p_vertical) {861undo_redo->add_do_method(control, "set_v_size_flags", p_flags);862undo_redo->add_undo_method(control, "set_v_size_flags", old_flags);863} else {864undo_redo->add_do_method(control, "set_h_size_flags", p_flags);865undo_redo->add_undo_method(control, "set_h_size_flags", old_flags);866}867}868}869870undo_redo->commit_action();871}872873void ControlEditorToolbar::_expand_flag_toggled(bool p_expand, bool p_vertical) {874const List<Node *> &selection = editor_selection->get_top_selected_node_list();875876EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();877if (p_vertical) {878undo_redo->create_action(TTR("Change Vertical Expand Flag"));879} else {880undo_redo->create_action(TTR("Change Horizontal Expand Flag"));881}882883for (Node *E : selection) {884Control *control = Object::cast_to<Control>(E);885if (control) {886int old_flags = p_vertical ? control->get_v_size_flags() : control->get_h_size_flags();887int new_flags = old_flags;888889if (p_expand) {890new_flags |= Control::SIZE_EXPAND;891} else {892new_flags &= ~Control::SIZE_EXPAND;893}894895if (p_vertical) {896undo_redo->add_do_method(control, "set_v_size_flags", new_flags);897undo_redo->add_undo_method(control, "set_v_size_flags", old_flags);898} else {899undo_redo->add_do_method(control, "set_h_size_flags", new_flags);900undo_redo->add_undo_method(control, "set_h_size_flags", old_flags);901}902}903}904905undo_redo->commit_action();906}907908Vector2 ControlEditorToolbar::_position_to_anchor(const Control *p_control, Vector2 position) {909ERR_FAIL_NULL_V(p_control, Vector2());910911Rect2 parent_rect = p_control->get_parent_anchorable_rect();912913Vector2 output;914if (p_control->is_layout_rtl()) {915output.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;916} else {917output.x = (parent_rect.size.x == 0) ? 0.0 : (p_control->get_transform().xform(position).x - parent_rect.position.x) / parent_rect.size.x;918}919output.y = (parent_rect.size.y == 0) ? 0.0 : (p_control->get_transform().xform(position).y - parent_rect.position.y) / parent_rect.size.y;920return output;921}922923bool ControlEditorToolbar::_is_node_locked(const Node *p_node) {924return p_node->get_meta("_edit_lock_", false);925}926927List<Control *> ControlEditorToolbar::_get_edited_controls() {928List<Control *> selection;929for (const KeyValue<ObjectID, Object *> &E : editor_selection->get_selection()) {930Control *control = ObjectDB::get_instance<Control>(E.key);931if (control && control->is_visible_in_tree() && control->get_viewport() == EditorNode::get_singleton()->get_scene_root() && !_is_node_locked(control)) {932selection.push_back(control);933}934}935936return selection;937}938939void ControlEditorToolbar::_selection_changed() {940// Update toolbar visibility.941bool has_controls = false;942bool has_control_parents = false;943bool has_container_parents = false;944945// Also update which size flags can be configured for the selected nodes.946Vector<SizeFlags> allowed_h_flags = {947SIZE_SHRINK_BEGIN,948SIZE_SHRINK_CENTER,949SIZE_SHRINK_END,950SIZE_FILL,951SIZE_EXPAND,952};953Vector<SizeFlags> allowed_v_flags = {954SIZE_SHRINK_BEGIN,955SIZE_SHRINK_CENTER,956SIZE_SHRINK_END,957SIZE_FILL,958SIZE_EXPAND,959};960961for (const KeyValue<ObjectID, Object *> &E : editor_selection->get_selection()) {962Control *control = ObjectDB::get_instance<Control>(E.key);963if (!control) {964continue;965}966has_controls = true;967968if (Object::cast_to<Control>(control->get_parent())) {969has_control_parents = true;970}971if (Object::cast_to<Container>(control->get_parent())) {972has_container_parents = true;973974Container *parent_container = Object::cast_to<Container>(control->get_parent());975976Vector<int> container_h_flags = parent_container->get_allowed_size_flags_horizontal();977Vector<SizeFlags> tmp_flags = allowed_h_flags.duplicate();978for (int i = 0; i < allowed_h_flags.size(); i++) {979if (!container_h_flags.has((int)allowed_h_flags[i])) {980tmp_flags.erase(allowed_h_flags[i]);981}982}983allowed_h_flags = tmp_flags;984985Vector<int> container_v_flags = parent_container->get_allowed_size_flags_vertical();986tmp_flags = allowed_v_flags.duplicate();987for (int i = 0; i < allowed_v_flags.size(); i++) {988if (!container_v_flags.has((int)allowed_v_flags[i])) {989tmp_flags.erase(allowed_v_flags[i]);990}991}992allowed_v_flags = tmp_flags;993}994}995996// Set general toolbar visibility.997set_visible(has_controls);998999// Set anchor tools visibility.1000if (has_controls && (!has_control_parents || !has_container_parents)) {1001anchors_button->set_visible(true);1002anchor_mode_button->set_visible(true);10031004// Update anchor mode.1005int nb_valid_controls = 0;1006int nb_anchors_mode = 0;10071008const List<Node *> &selection = editor_selection->get_top_selected_node_list();1009for (Node *E : selection) {1010Control *control = Object::cast_to<Control>(E);1011if (!control) {1012continue;1013}1014if (Object::cast_to<Container>(control->get_parent())) {1015continue;1016}10171018nb_valid_controls++;1019if (control->get_meta("_edit_use_anchors_", false)) {1020nb_anchors_mode++;1021}1022}10231024anchors_mode = (nb_valid_controls == nb_anchors_mode);1025anchor_mode_button->set_pressed(anchors_mode);1026} else {1027anchors_button->set_visible(false);1028anchor_mode_button->set_visible(false);1029anchor_mode_button->set_pressed(false);1030}10311032// Set container tools visibility.1033if (has_controls && (!has_control_parents || has_container_parents)) {1034containers_button->set_visible(true);10351036// Update allowed size flags.1037if (has_container_parents) {1038container_h_picker->set_allowed_flags(allowed_h_flags);1039container_v_picker->set_allowed_flags(allowed_v_flags);1040} else {1041Vector<SizeFlags> allowed_all_flags = {1042SIZE_SHRINK_BEGIN,1043SIZE_SHRINK_CENTER,1044SIZE_SHRINK_END,1045SIZE_FILL,1046SIZE_EXPAND,1047};10481049container_h_picker->set_allowed_flags(allowed_all_flags);1050container_v_picker->set_allowed_flags(allowed_all_flags);1051}10521053// Update expand toggles.1054int nb_valid_controls = 0;1055int nb_h_expand = 0;1056int nb_v_expand = 0;10571058const List<Node *> &selection = editor_selection->get_top_selected_node_list();1059for (Node *E : selection) {1060Control *control = Object::cast_to<Control>(E);1061if (!control) {1062continue;1063}10641065nb_valid_controls++;1066if (control->get_h_size_flags() & Control::SIZE_EXPAND) {1067nb_h_expand++;1068}1069if (control->get_v_size_flags() & Control::SIZE_EXPAND) {1070nb_v_expand++;1071}1072}10731074container_h_picker->set_expand_flag(nb_valid_controls == nb_h_expand);1075container_v_picker->set_expand_flag(nb_valid_controls == nb_v_expand);1076} else {1077containers_button->set_visible(false);1078}1079}10801081void ControlEditorToolbar::_notification(int p_what) {1082switch (p_what) {1083case NOTIFICATION_THEME_CHANGED: {1084anchors_button->set_button_icon(get_editor_theme_icon(SNAME("ControlLayout")));1085anchor_mode_button->set_button_icon(get_editor_theme_icon(SNAME("Anchor")));1086containers_button->set_button_icon(get_editor_theme_icon(SNAME("ContainerLayout")));1087} break;1088}1089}10901091ControlEditorToolbar::ControlEditorToolbar() {1092// Anchor and offset tools.1093anchors_button = memnew(ControlEditorPopupButton);1094anchors_button->set_tooltip_text(TTR("Presets for the anchor and offset values of a Control node."));1095add_child(anchors_button);10961097Label *anchors_label = memnew(Label);1098anchors_label->set_text(TTR("Anchor preset"));1099anchors_button->get_popup_hbox()->add_child(anchors_label);1100AnchorPresetPicker *anchors_picker = memnew(AnchorPresetPicker);1101anchors_picker->set_h_size_flags(SIZE_SHRINK_CENTER);1102anchors_button->get_popup_hbox()->add_child(anchors_picker);1103anchors_picker->connect("anchors_preset_selected", callable_mp(this, &ControlEditorToolbar::_anchors_preset_selected));11041105anchors_button->get_popup_hbox()->add_child(memnew(HSeparator));11061107Button *keep_ratio_button = memnew(Button);1108keep_ratio_button->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);1109keep_ratio_button->set_text(TTR("Set to Current Ratio"));1110keep_ratio_button->set_tooltip_text(TTR("Adjust anchors and offsets to match the current rect size."));1111anchors_button->get_popup_hbox()->add_child(keep_ratio_button);1112keep_ratio_button->connect(SceneStringName(pressed), callable_mp(this, &ControlEditorToolbar::_anchors_to_current_ratio));11131114anchor_mode_button = memnew(Button);1115anchor_mode_button->set_theme_type_variation(SceneStringName(FlatButton));1116anchor_mode_button->set_toggle_mode(true);1117anchor_mode_button->set_tooltip_text(TTR("When active, moving Control nodes changes their anchors instead of their offsets."));1118anchor_mode_button->set_accessibility_name(TTRC("Change Anchors"));1119add_child(anchor_mode_button);1120anchor_mode_button->connect(SceneStringName(toggled), callable_mp(this, &ControlEditorToolbar::_anchor_mode_toggled));11211122// Container tools.1123containers_button = memnew(ControlEditorPopupButton);1124containers_button->set_tooltip_text(TTR("Sizing settings for children of a Container node."));1125add_child(containers_button);11261127Label *container_h_label = memnew(Label);1128container_h_label->set_text(TTR("Horizontal alignment"));1129containers_button->get_popup_hbox()->add_child(container_h_label);1130container_h_picker = memnew(SizeFlagPresetPicker(false));1131containers_button->get_popup_hbox()->add_child(container_h_picker);1132container_h_picker->connect("size_flags_selected", callable_mp(this, &ControlEditorToolbar::_container_flags_selected).bind(false));1133container_h_picker->connect("expand_flag_toggled", callable_mp(this, &ControlEditorToolbar::_expand_flag_toggled).bind(false));11341135containers_button->get_popup_hbox()->add_child(memnew(HSeparator));11361137Label *container_v_label = memnew(Label);1138container_v_label->set_text(TTR("Vertical alignment"));1139containers_button->get_popup_hbox()->add_child(container_v_label);1140container_v_picker = memnew(SizeFlagPresetPicker(true));1141containers_button->get_popup_hbox()->add_child(container_v_picker);1142container_v_picker->connect("size_flags_selected", callable_mp(this, &ControlEditorToolbar::_container_flags_selected).bind(true));1143container_v_picker->connect("expand_flag_toggled", callable_mp(this, &ControlEditorToolbar::_expand_flag_toggled).bind(true));11441145// Editor connections.1146editor_selection = EditorNode::get_singleton()->get_editor_selection();1147editor_selection->add_editor_plugin(this);1148editor_selection->connect("selection_changed", callable_mp(this, &ControlEditorToolbar::_selection_changed));11491150singleton = this;1151}11521153ControlEditorToolbar *ControlEditorToolbar::singleton = nullptr;11541155// Editor plugin.11561157ControlEditorPlugin::ControlEditorPlugin() {1158toolbar = memnew(ControlEditorToolbar);1159toolbar->hide();1160add_control_to_container(CONTAINER_CANVAS_EDITOR_MENU, toolbar);11611162Ref<EditorInspectorPluginControl> plugin;1163plugin.instantiate();1164add_inspector_plugin(plugin);1165}116611671168