2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* control_editor_plugin.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2021-11-08 21:53:41 +01:00
|
|
|
|
|
|
|
#include "control_editor_plugin.h"
|
|
|
|
|
2022-02-14 14:00:03 +01:00
|
|
|
#include "editor/editor_node.h"
|
2022-07-23 00:28:05 +02:00
|
|
|
#include "editor/editor_scale.h"
|
2022-07-31 20:14:15 +02:00
|
|
|
#include "editor/editor_settings.h"
|
2022-03-25 18:06:46 +01:00
|
|
|
#include "editor/editor_undo_redo_manager.h"
|
2021-11-08 21:53:41 +01:00
|
|
|
#include "editor/plugins/canvas_item_editor_plugin.h"
|
2022-11-11 20:12:48 +01:00
|
|
|
#include "scene/gui/grid_container.h"
|
2022-07-31 20:14:15 +02:00
|
|
|
#include "scene/gui/separator.h"
|
2021-11-08 21:53:41 +01:00
|
|
|
|
2022-07-23 00:28:05 +02:00
|
|
|
// Inspector controls.
|
|
|
|
|
2021-11-08 21:53:41 +01:00
|
|
|
void ControlPositioningWarning::_update_warning() {
|
|
|
|
if (!control_node) {
|
|
|
|
title_icon->set_texture(nullptr);
|
|
|
|
title_label->set_text("");
|
|
|
|
hint_label->set_text("");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *parent_node = control_node->get_parent_control();
|
|
|
|
if (!parent_node) {
|
2023-08-13 02:33:39 +02:00
|
|
|
title_icon->set_texture(get_editor_theme_icon(SNAME("SubViewport")));
|
2021-11-08 21:53:41 +01:00
|
|
|
title_label->set_text(TTR("This node doesn't have a control parent."));
|
|
|
|
hint_label->set_text(TTR("Use the appropriate layout properties depending on where you are going to put it."));
|
|
|
|
} else if (Object::cast_to<Container>(parent_node)) {
|
2023-08-13 02:33:39 +02:00
|
|
|
title_icon->set_texture(get_editor_theme_icon(SNAME("ContainerLayout")));
|
2021-11-08 21:53:41 +01:00
|
|
|
title_label->set_text(TTR("This node is a child of a container."));
|
|
|
|
hint_label->set_text(TTR("Use container properties for positioning."));
|
|
|
|
} else {
|
2023-08-13 02:33:39 +02:00
|
|
|
title_icon->set_texture(get_editor_theme_icon(SNAME("ControlLayout")));
|
2021-11-08 21:53:41 +01:00
|
|
|
title_label->set_text(TTR("This node is a child of a regular control."));
|
|
|
|
hint_label->set_text(TTR("Use anchors and the rectangle for positioning."));
|
|
|
|
}
|
|
|
|
|
|
|
|
bg_panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("bg_group_note"), SNAME("EditorProperty")));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlPositioningWarning::_update_toggler() {
|
|
|
|
Ref<Texture2D> arrow;
|
|
|
|
if (hint_label->is_visible()) {
|
|
|
|
arrow = get_theme_icon(SNAME("arrow"), SNAME("Tree"));
|
2022-08-25 12:42:17 +02:00
|
|
|
set_tooltip_text(TTR("Collapse positioning hint."));
|
2021-11-08 21:53:41 +01:00
|
|
|
} else {
|
|
|
|
if (is_layout_rtl()) {
|
|
|
|
arrow = get_theme_icon(SNAME("arrow_collapsed"), SNAME("Tree"));
|
|
|
|
} else {
|
|
|
|
arrow = get_theme_icon(SNAME("arrow_collapsed_mirrored"), SNAME("Tree"));
|
|
|
|
}
|
2022-08-25 12:42:17 +02:00
|
|
|
set_tooltip_text(TTR("Expand positioning hint."));
|
2021-11-08 21:53:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
hint_icon->set_texture(arrow);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlPositioningWarning::set_control(Control *p_node) {
|
|
|
|
control_node = p_node;
|
|
|
|
_update_warning();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlPositioningWarning::gui_input(const Ref<InputEvent> &p_event) {
|
|
|
|
Ref<InputEventMouseButton> mb = p_event;
|
|
|
|
if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
|
|
|
|
bool state = !hint_label->is_visible();
|
|
|
|
|
|
|
|
hint_filler_left->set_visible(state);
|
|
|
|
hint_label->set_visible(state);
|
|
|
|
hint_filler_right->set_visible(state);
|
|
|
|
|
|
|
|
_update_toggler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlPositioningWarning::_notification(int p_notification) {
|
|
|
|
switch (p_notification) {
|
2022-08-29 11:04:31 +02:00
|
|
|
case NOTIFICATION_ENTER_TREE:
|
2021-11-08 21:53:41 +01:00
|
|
|
case NOTIFICATION_THEME_CHANGED:
|
|
|
|
_update_warning();
|
|
|
|
_update_toggler();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ControlPositioningWarning::ControlPositioningWarning() {
|
|
|
|
set_mouse_filter(MOUSE_FILTER_STOP);
|
|
|
|
|
|
|
|
bg_panel = memnew(PanelContainer);
|
|
|
|
bg_panel->set_mouse_filter(MOUSE_FILTER_IGNORE);
|
|
|
|
add_child(bg_panel);
|
|
|
|
|
|
|
|
grid = memnew(GridContainer);
|
|
|
|
grid->set_columns(3);
|
|
|
|
bg_panel->add_child(grid);
|
|
|
|
|
|
|
|
title_icon = memnew(TextureRect);
|
|
|
|
title_icon->set_stretch_mode(TextureRect::StretchMode::STRETCH_KEEP_CENTERED);
|
|
|
|
grid->add_child(title_icon);
|
|
|
|
|
|
|
|
title_label = memnew(Label);
|
2022-06-15 10:01:45 +02:00
|
|
|
title_label->set_autowrap_mode(TextServer::AutowrapMode::AUTOWRAP_WORD);
|
2021-11-08 21:53:41 +01:00
|
|
|
title_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
|
|
|
|
title_label->set_vertical_alignment(VerticalAlignment::VERTICAL_ALIGNMENT_CENTER);
|
|
|
|
grid->add_child(title_label);
|
|
|
|
|
|
|
|
hint_icon = memnew(TextureRect);
|
|
|
|
hint_icon->set_stretch_mode(TextureRect::StretchMode::STRETCH_KEEP_CENTERED);
|
|
|
|
grid->add_child(hint_icon);
|
|
|
|
|
|
|
|
// Filler.
|
|
|
|
hint_filler_left = memnew(Control);
|
|
|
|
hint_filler_left->hide();
|
|
|
|
grid->add_child(hint_filler_left);
|
|
|
|
|
|
|
|
hint_label = memnew(Label);
|
2022-06-15 10:01:45 +02:00
|
|
|
hint_label->set_autowrap_mode(TextServer::AutowrapMode::AUTOWRAP_WORD);
|
2021-11-08 21:53:41 +01:00
|
|
|
hint_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
|
|
|
|
hint_label->set_vertical_alignment(VerticalAlignment::VERTICAL_ALIGNMENT_CENTER);
|
|
|
|
hint_label->hide();
|
|
|
|
grid->add_child(hint_label);
|
|
|
|
|
|
|
|
// Filler.
|
|
|
|
hint_filler_right = memnew(Control);
|
|
|
|
hint_filler_right->hide();
|
|
|
|
grid->add_child(hint_filler_right);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorPropertyAnchorsPreset::_set_read_only(bool p_read_only) {
|
|
|
|
options->set_disabled(p_read_only);
|
|
|
|
};
|
|
|
|
|
|
|
|
void EditorPropertyAnchorsPreset::_option_selected(int p_which) {
|
|
|
|
int64_t val = options->get_item_metadata(p_which);
|
|
|
|
emit_changed(get_edited_property(), val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorPropertyAnchorsPreset::update_property() {
|
2023-05-26 09:13:24 +02:00
|
|
|
int64_t which = get_edited_property_value();
|
2021-11-08 21:53:41 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < options->get_item_count(); i++) {
|
|
|
|
Variant val = options->get_item_metadata(i);
|
|
|
|
if (val != Variant() && which == (int64_t)val) {
|
|
|
|
options->select(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorPropertyAnchorsPreset::setup(const Vector<String> &p_options) {
|
|
|
|
options->clear();
|
|
|
|
|
|
|
|
Vector<String> split_after;
|
|
|
|
split_after.append("Custom");
|
2022-07-20 12:15:23 +02:00
|
|
|
split_after.append("PresetFullRect");
|
2021-11-08 21:53:41 +01:00
|
|
|
split_after.append("PresetBottomLeft");
|
|
|
|
split_after.append("PresetCenter");
|
|
|
|
|
|
|
|
for (int i = 0, j = 0; i < p_options.size(); i++, j++) {
|
|
|
|
Vector<String> text_split = p_options[i].split(":");
|
|
|
|
int64_t current_val = text_split[1].to_int();
|
|
|
|
|
2022-07-20 12:15:23 +02:00
|
|
|
String option_name = text_split[0];
|
|
|
|
if (option_name.begins_with("Preset")) {
|
|
|
|
String preset_name = option_name.trim_prefix("Preset");
|
|
|
|
String humanized_name = preset_name.capitalize();
|
|
|
|
String icon_name = "ControlAlign" + preset_name;
|
2023-08-13 02:33:39 +02:00
|
|
|
options->add_icon_item(EditorNode::get_singleton()->get_gui_base()->get_editor_theme_icon(icon_name), humanized_name);
|
2021-11-08 21:53:41 +01:00
|
|
|
} else {
|
2022-07-20 12:15:23 +02:00
|
|
|
options->add_item(option_name);
|
2021-11-08 21:53:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
options->set_item_metadata(j, current_val);
|
2022-07-20 12:15:23 +02:00
|
|
|
if (split_after.has(option_name)) {
|
2021-11-08 21:53:41 +01:00
|
|
|
options->add_separator();
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorPropertyAnchorsPreset::EditorPropertyAnchorsPreset() {
|
|
|
|
options = memnew(OptionButton);
|
|
|
|
options->set_clip_text(true);
|
|
|
|
options->set_flat(true);
|
|
|
|
add_child(options);
|
|
|
|
add_focusable(options);
|
|
|
|
options->connect("item_selected", callable_mp(this, &EditorPropertyAnchorsPreset::_option_selected));
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorPropertySizeFlags::_set_read_only(bool p_read_only) {
|
|
|
|
for (CheckBox *check : flag_checks) {
|
|
|
|
check->set_disabled(p_read_only);
|
|
|
|
}
|
|
|
|
flag_presets->set_disabled(p_read_only);
|
|
|
|
};
|
|
|
|
|
|
|
|
void EditorPropertySizeFlags::_preset_selected(int p_which) {
|
|
|
|
int preset = flag_presets->get_item_id(p_which);
|
|
|
|
if (preset == SIZE_FLAGS_PRESET_CUSTOM) {
|
|
|
|
flag_options->set_visible(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
flag_options->set_visible(false);
|
|
|
|
|
|
|
|
uint32_t value = 0;
|
|
|
|
switch (preset) {
|
|
|
|
case SIZE_FLAGS_PRESET_FILL:
|
|
|
|
value = Control::SIZE_FILL;
|
|
|
|
break;
|
|
|
|
case SIZE_FLAGS_PRESET_SHRINK_BEGIN:
|
|
|
|
value = Control::SIZE_SHRINK_BEGIN;
|
|
|
|
break;
|
|
|
|
case SIZE_FLAGS_PRESET_SHRINK_CENTER:
|
|
|
|
value = Control::SIZE_SHRINK_CENTER;
|
|
|
|
break;
|
|
|
|
case SIZE_FLAGS_PRESET_SHRINK_END:
|
|
|
|
value = Control::SIZE_SHRINK_END;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_expand = flag_expand->is_visible() && flag_expand->is_pressed();
|
|
|
|
if (is_expand) {
|
|
|
|
value |= Control::SIZE_EXPAND;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit_changed(get_edited_property(), value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorPropertySizeFlags::_expand_toggled() {
|
2023-05-26 09:13:24 +02:00
|
|
|
uint32_t value = get_edited_property_value();
|
2021-11-08 21:53:41 +01:00
|
|
|
|
|
|
|
if (flag_expand->is_visible() && flag_expand->is_pressed()) {
|
|
|
|
value |= Control::SIZE_EXPAND;
|
|
|
|
} else {
|
|
|
|
value ^= Control::SIZE_EXPAND;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep the custom preset selected as we toggle individual flags.
|
|
|
|
keep_selected_preset = true;
|
|
|
|
emit_changed(get_edited_property(), value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorPropertySizeFlags::_flag_toggled() {
|
|
|
|
uint32_t value = 0;
|
|
|
|
for (int i = 0; i < flag_checks.size(); i++) {
|
|
|
|
if (flag_checks[i]->is_pressed()) {
|
|
|
|
int flag_value = flag_checks[i]->get_meta("_value");
|
|
|
|
value |= flag_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_expand = flag_expand->is_visible() && flag_expand->is_pressed();
|
|
|
|
if (is_expand) {
|
|
|
|
value |= Control::SIZE_EXPAND;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep the custom preset selected as we toggle individual flags.
|
|
|
|
keep_selected_preset = true;
|
|
|
|
emit_changed(get_edited_property(), value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorPropertySizeFlags::update_property() {
|
2023-05-26 09:13:24 +02:00
|
|
|
uint32_t value = get_edited_property_value();
|
2021-11-08 21:53:41 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < flag_checks.size(); i++) {
|
|
|
|
int flag_value = flag_checks[i]->get_meta("_value");
|
|
|
|
if (value & flag_value) {
|
|
|
|
flag_checks[i]->set_pressed(true);
|
|
|
|
} else {
|
|
|
|
flag_checks[i]->set_pressed(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_expand = value & Control::SIZE_EXPAND;
|
|
|
|
flag_expand->set_pressed(is_expand);
|
|
|
|
|
|
|
|
if (keep_selected_preset) {
|
|
|
|
keep_selected_preset = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
FlagPreset preset = SIZE_FLAGS_PRESET_CUSTOM;
|
|
|
|
if (value == Control::SIZE_FILL || value == (Control::SIZE_FILL | Control::SIZE_EXPAND)) {
|
|
|
|
preset = SIZE_FLAGS_PRESET_FILL;
|
|
|
|
} else if (value == Control::SIZE_SHRINK_BEGIN || value == (Control::SIZE_SHRINK_BEGIN | Control::SIZE_EXPAND)) {
|
|
|
|
preset = SIZE_FLAGS_PRESET_SHRINK_BEGIN;
|
|
|
|
} else if (value == Control::SIZE_SHRINK_CENTER || value == (Control::SIZE_SHRINK_CENTER | Control::SIZE_EXPAND)) {
|
|
|
|
preset = SIZE_FLAGS_PRESET_SHRINK_CENTER;
|
|
|
|
} else if (value == Control::SIZE_SHRINK_END || value == (Control::SIZE_SHRINK_END | Control::SIZE_EXPAND)) {
|
|
|
|
preset = SIZE_FLAGS_PRESET_SHRINK_END;
|
|
|
|
}
|
|
|
|
|
|
|
|
int preset_idx = flag_presets->get_item_index(preset);
|
|
|
|
if (preset_idx >= 0) {
|
|
|
|
flag_presets->select(preset_idx);
|
|
|
|
}
|
|
|
|
flag_options->set_visible(preset == SIZE_FLAGS_PRESET_CUSTOM);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorPropertySizeFlags::setup(const Vector<String> &p_options, bool p_vertical) {
|
|
|
|
vertical = p_vertical;
|
|
|
|
|
|
|
|
if (p_options.size() == 0) {
|
|
|
|
flag_presets->clear();
|
|
|
|
flag_presets->add_item(TTR("Container Default"));
|
|
|
|
flag_presets->set_disabled(true);
|
|
|
|
flag_expand->set_visible(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-13 15:04:37 +02:00
|
|
|
HashMap<int, String> flags;
|
2021-11-08 21:53:41 +01:00
|
|
|
for (int i = 0, j = 0; i < p_options.size(); i++, j++) {
|
|
|
|
Vector<String> text_split = p_options[i].split(":");
|
|
|
|
int64_t current_val = text_split[1].to_int();
|
|
|
|
flags[current_val] = text_split[0];
|
|
|
|
|
|
|
|
if (current_val == SIZE_EXPAND) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckBox *cb = memnew(CheckBox);
|
|
|
|
cb->set_text(text_split[0]);
|
|
|
|
cb->set_clip_text(true);
|
|
|
|
cb->set_meta("_value", current_val);
|
|
|
|
cb->connect("pressed", callable_mp(this, &EditorPropertySizeFlags::_flag_toggled));
|
|
|
|
add_focusable(cb);
|
|
|
|
|
|
|
|
flag_options->add_child(cb);
|
|
|
|
flag_checks.append(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
Control *gui_base = EditorNode::get_singleton()->get_gui_base();
|
|
|
|
String wide_preset_icon = SNAME("ControlAlignHCenterWide");
|
2022-02-19 18:23:53 +01:00
|
|
|
String begin_preset_icon = SNAME("ControlAlignCenterLeft");
|
|
|
|
String end_preset_icon = SNAME("ControlAlignCenterRight");
|
2021-11-08 21:53:41 +01:00
|
|
|
if (vertical) {
|
|
|
|
wide_preset_icon = SNAME("ControlAlignVCenterWide");
|
2022-02-19 18:23:53 +01:00
|
|
|
begin_preset_icon = SNAME("ControlAlignCenterTop");
|
|
|
|
end_preset_icon = SNAME("ControlAlignCenterBottom");
|
2021-11-08 21:53:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
flag_presets->clear();
|
|
|
|
if (flags.has(SIZE_FILL)) {
|
2023-08-13 02:33:39 +02:00
|
|
|
flag_presets->add_icon_item(gui_base->get_editor_theme_icon(wide_preset_icon), TTR("Fill"), SIZE_FLAGS_PRESET_FILL);
|
2021-11-08 21:53:41 +01:00
|
|
|
}
|
|
|
|
// Shrink Begin is the same as no flags at all, as such it cannot be disabled.
|
2023-08-13 02:33:39 +02:00
|
|
|
flag_presets->add_icon_item(gui_base->get_editor_theme_icon(begin_preset_icon), TTR("Shrink Begin"), SIZE_FLAGS_PRESET_SHRINK_BEGIN);
|
2021-11-08 21:53:41 +01:00
|
|
|
if (flags.has(SIZE_SHRINK_CENTER)) {
|
2023-08-13 02:33:39 +02:00
|
|
|
flag_presets->add_icon_item(gui_base->get_editor_theme_icon(SNAME("ControlAlignCenter")), TTR("Shrink Center"), SIZE_FLAGS_PRESET_SHRINK_CENTER);
|
2021-11-08 21:53:41 +01:00
|
|
|
}
|
|
|
|
if (flags.has(SIZE_SHRINK_END)) {
|
2023-08-13 02:33:39 +02:00
|
|
|
flag_presets->add_icon_item(gui_base->get_editor_theme_icon(end_preset_icon), TTR("Shrink End"), SIZE_FLAGS_PRESET_SHRINK_END);
|
2021-11-08 21:53:41 +01:00
|
|
|
}
|
|
|
|
flag_presets->add_separator();
|
|
|
|
flag_presets->add_item(TTR("Custom"), SIZE_FLAGS_PRESET_CUSTOM);
|
|
|
|
|
|
|
|
flag_expand->set_visible(flags.has(SIZE_EXPAND));
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorPropertySizeFlags::EditorPropertySizeFlags() {
|
|
|
|
VBoxContainer *vb = memnew(VBoxContainer);
|
|
|
|
add_child(vb);
|
|
|
|
|
|
|
|
flag_presets = memnew(OptionButton);
|
|
|
|
flag_presets->set_clip_text(true);
|
|
|
|
flag_presets->set_flat(true);
|
|
|
|
vb->add_child(flag_presets);
|
|
|
|
add_focusable(flag_presets);
|
|
|
|
set_label_reference(flag_presets);
|
|
|
|
flag_presets->connect("item_selected", callable_mp(this, &EditorPropertySizeFlags::_preset_selected));
|
|
|
|
|
|
|
|
flag_options = memnew(VBoxContainer);
|
|
|
|
flag_options->hide();
|
|
|
|
vb->add_child(flag_options);
|
|
|
|
|
|
|
|
flag_expand = memnew(CheckBox);
|
|
|
|
flag_expand->set_text(TTR("Expand"));
|
|
|
|
vb->add_child(flag_expand);
|
|
|
|
add_focusable(flag_expand);
|
|
|
|
flag_expand->connect("pressed", callable_mp(this, &EditorPropertySizeFlags::_expand_toggled));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditorInspectorPluginControl::can_handle(Object *p_object) {
|
|
|
|
return Object::cast_to<Control>(p_object) != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorInspectorPluginControl::parse_group(Object *p_object, const String &p_group) {
|
|
|
|
Control *control = Object::cast_to<Control>(p_object);
|
|
|
|
if (!control || p_group != "Layout") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ControlPositioningWarning *pos_warning = memnew(ControlPositioningWarning);
|
|
|
|
pos_warning->set_control(control);
|
|
|
|
add_custom_control(pos_warning);
|
|
|
|
}
|
|
|
|
|
2023-01-31 19:08:46 +01:00
|
|
|
bool 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) {
|
2021-11-08 21:53:41 +01:00
|
|
|
Control *control = Object::cast_to<Control>(p_object);
|
|
|
|
if (!control) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_path == "anchors_preset") {
|
|
|
|
EditorPropertyAnchorsPreset *prop_editor = memnew(EditorPropertyAnchorsPreset);
|
|
|
|
Vector<String> options = p_hint_text.split(",");
|
|
|
|
prop_editor->setup(options);
|
|
|
|
add_property_editor(p_path, prop_editor);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_path == "size_flags_horizontal" || p_path == "size_flags_vertical") {
|
|
|
|
EditorPropertySizeFlags *prop_editor = memnew(EditorPropertySizeFlags);
|
|
|
|
Vector<String> options;
|
|
|
|
if (!p_hint_text.is_empty()) {
|
|
|
|
options = p_hint_text.split(",");
|
|
|
|
}
|
|
|
|
prop_editor->setup(options, p_path == "size_flags_vertical");
|
|
|
|
add_property_editor(p_path, prop_editor);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-23 00:28:05 +02:00
|
|
|
// Toolbars controls.
|
|
|
|
|
|
|
|
Size2 ControlEditorPopupButton::get_minimum_size() const {
|
|
|
|
Vector2 base_size = Vector2(26, 26) * EDSCALE;
|
|
|
|
|
|
|
|
if (arrow_icon.is_null()) {
|
|
|
|
return base_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2 final_size;
|
|
|
|
final_size.x = base_size.x + arrow_icon->get_width();
|
|
|
|
final_size.y = MAX(base_size.y, arrow_icon->get_height());
|
|
|
|
|
|
|
|
return final_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlEditorPopupButton::toggled(bool p_pressed) {
|
|
|
|
if (!p_pressed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Size2 size = get_size() * get_viewport()->get_canvas_transform().get_scale();
|
|
|
|
|
|
|
|
popup_panel->set_size(Size2(size.width, 0));
|
|
|
|
Point2 gp = get_screen_position();
|
|
|
|
gp.y += size.y;
|
|
|
|
if (is_layout_rtl()) {
|
|
|
|
gp.x += size.width - popup_panel->get_size().width;
|
|
|
|
}
|
|
|
|
popup_panel->set_position(gp);
|
|
|
|
|
|
|
|
popup_panel->popup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlEditorPopupButton::_popup_visibility_changed(bool p_visible) {
|
|
|
|
set_pressed(p_visible);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlEditorPopupButton::_notification(int p_what) {
|
|
|
|
switch (p_what) {
|
2022-08-29 11:04:31 +02:00
|
|
|
case NOTIFICATION_ENTER_TREE:
|
2022-07-23 00:28:05 +02:00
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
|
|
|
arrow_icon = get_theme_icon("select_arrow", "Tree");
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case NOTIFICATION_DRAW: {
|
|
|
|
if (arrow_icon.is_valid()) {
|
|
|
|
Vector2 arrow_pos = Point2(26, 0) * EDSCALE;
|
|
|
|
arrow_pos.y = get_size().y / 2 - arrow_icon->get_height() / 2;
|
|
|
|
draw_texture(arrow_icon, arrow_pos);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
|
|
|
|
popup_panel->set_layout_direction((Window::LayoutDirection)get_layout_direction());
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case NOTIFICATION_VISIBILITY_CHANGED: {
|
|
|
|
if (!is_visible_in_tree()) {
|
|
|
|
popup_panel->hide();
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ControlEditorPopupButton::ControlEditorPopupButton() {
|
|
|
|
set_flat(true);
|
|
|
|
set_toggle_mode(true);
|
|
|
|
set_focus_mode(FOCUS_NONE);
|
|
|
|
|
|
|
|
popup_panel = memnew(PopupPanel);
|
2022-08-29 17:01:41 +02:00
|
|
|
popup_panel->set_theme_type_variation("ControlEditorPopupPanel");
|
2022-07-23 00:28:05 +02:00
|
|
|
add_child(popup_panel);
|
|
|
|
popup_panel->connect("about_to_popup", callable_mp(this, &ControlEditorPopupButton::_popup_visibility_changed).bind(true));
|
|
|
|
popup_panel->connect("popup_hide", callable_mp(this, &ControlEditorPopupButton::_popup_visibility_changed).bind(false));
|
|
|
|
|
|
|
|
popup_vbox = memnew(VBoxContainer);
|
|
|
|
popup_panel->add_child(popup_vbox);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlEditorPresetPicker::_add_row_button(HBoxContainer *p_row, const int p_preset, const String &p_name) {
|
|
|
|
ERR_FAIL_COND(preset_buttons.has(p_preset));
|
|
|
|
|
|
|
|
Button *b = memnew(Button);
|
|
|
|
b->set_custom_minimum_size(Size2i(36, 36) * EDSCALE);
|
|
|
|
b->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER);
|
2022-08-25 12:42:17 +02:00
|
|
|
b->set_tooltip_text(p_name);
|
2022-07-23 00:28:05 +02:00
|
|
|
b->set_flat(true);
|
|
|
|
p_row->add_child(b);
|
|
|
|
b->connect("pressed", callable_mp(this, &ControlEditorPresetPicker::_preset_button_pressed).bind(p_preset));
|
|
|
|
|
|
|
|
preset_buttons[p_preset] = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlEditorPresetPicker::_add_separator(BoxContainer *p_box, Separator *p_separator) {
|
|
|
|
p_separator->add_theme_constant_override("separation", grid_separation);
|
|
|
|
p_separator->set_custom_minimum_size(Size2i(1, 1));
|
|
|
|
p_box->add_child(p_separator);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnchorPresetPicker::_preset_button_pressed(const int p_preset) {
|
|
|
|
emit_signal("anchors_preset_selected", p_preset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnchorPresetPicker::_notification(int p_notification) {
|
|
|
|
switch (p_notification) {
|
2022-08-29 11:04:31 +02:00
|
|
|
case NOTIFICATION_ENTER_TREE:
|
2022-07-23 00:28:05 +02:00
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
2023-08-13 02:33:39 +02:00
|
|
|
preset_buttons[PRESET_TOP_LEFT]->set_icon(get_editor_theme_icon(SNAME("ControlAlignTopLeft")));
|
|
|
|
preset_buttons[PRESET_CENTER_TOP]->set_icon(get_editor_theme_icon(SNAME("ControlAlignCenterTop")));
|
|
|
|
preset_buttons[PRESET_TOP_RIGHT]->set_icon(get_editor_theme_icon(SNAME("ControlAlignTopRight")));
|
2022-07-23 00:28:05 +02:00
|
|
|
|
2023-08-13 02:33:39 +02:00
|
|
|
preset_buttons[PRESET_CENTER_LEFT]->set_icon(get_editor_theme_icon(SNAME("ControlAlignCenterLeft")));
|
|
|
|
preset_buttons[PRESET_CENTER]->set_icon(get_editor_theme_icon(SNAME("ControlAlignCenter")));
|
|
|
|
preset_buttons[PRESET_CENTER_RIGHT]->set_icon(get_editor_theme_icon(SNAME("ControlAlignCenterRight")));
|
2022-07-23 00:28:05 +02:00
|
|
|
|
2023-08-13 02:33:39 +02:00
|
|
|
preset_buttons[PRESET_BOTTOM_LEFT]->set_icon(get_editor_theme_icon(SNAME("ControlAlignBottomLeft")));
|
|
|
|
preset_buttons[PRESET_CENTER_BOTTOM]->set_icon(get_editor_theme_icon(SNAME("ControlAlignCenterBottom")));
|
|
|
|
preset_buttons[PRESET_BOTTOM_RIGHT]->set_icon(get_editor_theme_icon(SNAME("ControlAlignBottomRight")));
|
2022-07-23 00:28:05 +02:00
|
|
|
|
2023-08-13 02:33:39 +02:00
|
|
|
preset_buttons[PRESET_TOP_WIDE]->set_icon(get_editor_theme_icon(SNAME("ControlAlignTopWide")));
|
|
|
|
preset_buttons[PRESET_HCENTER_WIDE]->set_icon(get_editor_theme_icon(SNAME("ControlAlignHCenterWide")));
|
|
|
|
preset_buttons[PRESET_BOTTOM_WIDE]->set_icon(get_editor_theme_icon(SNAME("ControlAlignBottomWide")));
|
2022-07-23 00:28:05 +02:00
|
|
|
|
2023-08-13 02:33:39 +02:00
|
|
|
preset_buttons[PRESET_LEFT_WIDE]->set_icon(get_editor_theme_icon(SNAME("ControlAlignLeftWide")));
|
|
|
|
preset_buttons[PRESET_VCENTER_WIDE]->set_icon(get_editor_theme_icon(SNAME("ControlAlignVCenterWide")));
|
|
|
|
preset_buttons[PRESET_RIGHT_WIDE]->set_icon(get_editor_theme_icon(SNAME("ControlAlignRightWide")));
|
2022-07-23 00:28:05 +02:00
|
|
|
|
2023-08-13 02:33:39 +02:00
|
|
|
preset_buttons[PRESET_FULL_RECT]->set_icon(get_editor_theme_icon(SNAME("ControlAlignFullRect")));
|
2022-07-23 00:28:05 +02:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnchorPresetPicker::_bind_methods() {
|
|
|
|
ADD_SIGNAL(MethodInfo("anchors_preset_selected", PropertyInfo(Variant::INT, "preset")));
|
|
|
|
}
|
|
|
|
|
|
|
|
AnchorPresetPicker::AnchorPresetPicker() {
|
|
|
|
VBoxContainer *main_vb = memnew(VBoxContainer);
|
|
|
|
main_vb->add_theme_constant_override("separation", grid_separation);
|
|
|
|
add_child(main_vb);
|
|
|
|
|
|
|
|
HBoxContainer *top_row = memnew(HBoxContainer);
|
|
|
|
top_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);
|
|
|
|
top_row->add_theme_constant_override("separation", grid_separation);
|
|
|
|
main_vb->add_child(top_row);
|
|
|
|
|
|
|
|
_add_row_button(top_row, PRESET_TOP_LEFT, TTR("Top Left"));
|
|
|
|
_add_row_button(top_row, PRESET_CENTER_TOP, TTR("Center Top"));
|
|
|
|
_add_row_button(top_row, PRESET_TOP_RIGHT, TTR("Top Right"));
|
|
|
|
_add_separator(top_row, memnew(VSeparator));
|
|
|
|
_add_row_button(top_row, PRESET_TOP_WIDE, TTR("Top Wide"));
|
|
|
|
|
|
|
|
HBoxContainer *mid_row = memnew(HBoxContainer);
|
|
|
|
mid_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);
|
|
|
|
mid_row->add_theme_constant_override("separation", grid_separation);
|
|
|
|
main_vb->add_child(mid_row);
|
|
|
|
|
|
|
|
_add_row_button(mid_row, PRESET_CENTER_LEFT, TTR("Center Left"));
|
|
|
|
_add_row_button(mid_row, PRESET_CENTER, TTR("Center"));
|
|
|
|
_add_row_button(mid_row, PRESET_CENTER_RIGHT, TTR("Center Right"));
|
|
|
|
_add_separator(mid_row, memnew(VSeparator));
|
|
|
|
_add_row_button(mid_row, PRESET_HCENTER_WIDE, TTR("HCenter Wide"));
|
|
|
|
|
|
|
|
HBoxContainer *bot_row = memnew(HBoxContainer);
|
|
|
|
bot_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);
|
|
|
|
bot_row->add_theme_constant_override("separation", grid_separation);
|
|
|
|
main_vb->add_child(bot_row);
|
|
|
|
|
|
|
|
_add_row_button(bot_row, PRESET_BOTTOM_LEFT, TTR("Bottom Left"));
|
|
|
|
_add_row_button(bot_row, PRESET_CENTER_BOTTOM, TTR("Center Bottom"));
|
|
|
|
_add_row_button(bot_row, PRESET_BOTTOM_RIGHT, TTR("Bottom Right"));
|
|
|
|
_add_separator(bot_row, memnew(VSeparator));
|
|
|
|
_add_row_button(bot_row, PRESET_BOTTOM_WIDE, TTR("Bottom Wide"));
|
|
|
|
|
|
|
|
_add_separator(main_vb, memnew(HSeparator));
|
|
|
|
|
|
|
|
HBoxContainer *extra_row = memnew(HBoxContainer);
|
|
|
|
extra_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);
|
|
|
|
extra_row->add_theme_constant_override("separation", grid_separation);
|
|
|
|
main_vb->add_child(extra_row);
|
|
|
|
|
|
|
|
_add_row_button(extra_row, PRESET_LEFT_WIDE, TTR("Left Wide"));
|
|
|
|
_add_row_button(extra_row, PRESET_VCENTER_WIDE, TTR("VCenter Wide"));
|
|
|
|
_add_row_button(extra_row, PRESET_RIGHT_WIDE, TTR("Right Wide"));
|
|
|
|
_add_separator(extra_row, memnew(VSeparator));
|
|
|
|
_add_row_button(extra_row, PRESET_FULL_RECT, TTR("Full Rect"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SizeFlagPresetPicker::_preset_button_pressed(const int p_preset) {
|
|
|
|
int flags = (SizeFlags)p_preset;
|
|
|
|
if (expand_button->is_pressed()) {
|
|
|
|
flags |= SIZE_EXPAND;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit_signal("size_flags_selected", flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SizeFlagPresetPicker::set_allowed_flags(Vector<SizeFlags> &p_flags) {
|
|
|
|
preset_buttons[SIZE_SHRINK_BEGIN]->set_disabled(!p_flags.has(SIZE_SHRINK_BEGIN));
|
|
|
|
preset_buttons[SIZE_SHRINK_CENTER]->set_disabled(!p_flags.has(SIZE_SHRINK_CENTER));
|
|
|
|
preset_buttons[SIZE_SHRINK_END]->set_disabled(!p_flags.has(SIZE_SHRINK_END));
|
|
|
|
preset_buttons[SIZE_FILL]->set_disabled(!p_flags.has(SIZE_FILL));
|
|
|
|
|
|
|
|
expand_button->set_disabled(!p_flags.has(SIZE_EXPAND));
|
|
|
|
if (p_flags.has(SIZE_EXPAND)) {
|
2022-08-25 12:42:17 +02:00
|
|
|
expand_button->set_tooltip_text(TTR("Enable to also set the Expand flag.\nDisable to only set Shrink/Fill flags."));
|
2022-07-23 00:28:05 +02:00
|
|
|
} else {
|
|
|
|
expand_button->set_pressed(false);
|
2022-08-25 12:42:17 +02:00
|
|
|
expand_button->set_tooltip_text(TTR("Some parents of the selected nodes do not support the Expand flag."));
|
2022-07-23 00:28:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SizeFlagPresetPicker::_notification(int p_notification) {
|
|
|
|
switch (p_notification) {
|
2022-08-29 11:04:31 +02:00
|
|
|
case NOTIFICATION_ENTER_TREE:
|
2022-07-23 00:28:05 +02:00
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
|
|
|
if (vertical) {
|
2023-08-13 02:33:39 +02:00
|
|
|
preset_buttons[SIZE_SHRINK_BEGIN]->set_icon(get_editor_theme_icon(SNAME("ControlAlignCenterTop")));
|
|
|
|
preset_buttons[SIZE_SHRINK_CENTER]->set_icon(get_editor_theme_icon(SNAME("ControlAlignCenter")));
|
|
|
|
preset_buttons[SIZE_SHRINK_END]->set_icon(get_editor_theme_icon(SNAME("ControlAlignCenterBottom")));
|
2022-07-23 00:28:05 +02:00
|
|
|
|
2023-08-13 02:33:39 +02:00
|
|
|
preset_buttons[SIZE_FILL]->set_icon(get_editor_theme_icon(SNAME("ControlAlignVCenterWide")));
|
2022-07-23 00:28:05 +02:00
|
|
|
} else {
|
2023-08-13 02:33:39 +02:00
|
|
|
preset_buttons[SIZE_SHRINK_BEGIN]->set_icon(get_editor_theme_icon(SNAME("ControlAlignCenterLeft")));
|
|
|
|
preset_buttons[SIZE_SHRINK_CENTER]->set_icon(get_editor_theme_icon(SNAME("ControlAlignCenter")));
|
|
|
|
preset_buttons[SIZE_SHRINK_END]->set_icon(get_editor_theme_icon(SNAME("ControlAlignCenterRight")));
|
2022-07-23 00:28:05 +02:00
|
|
|
|
2023-08-13 02:33:39 +02:00
|
|
|
preset_buttons[SIZE_FILL]->set_icon(get_editor_theme_icon(SNAME("ControlAlignHCenterWide")));
|
2022-07-23 00:28:05 +02:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SizeFlagPresetPicker::_bind_methods() {
|
|
|
|
ADD_SIGNAL(MethodInfo("size_flags_selected", PropertyInfo(Variant::INT, "size_flags")));
|
|
|
|
}
|
|
|
|
|
|
|
|
SizeFlagPresetPicker::SizeFlagPresetPicker(bool p_vertical) {
|
|
|
|
vertical = p_vertical;
|
|
|
|
|
|
|
|
VBoxContainer *main_vb = memnew(VBoxContainer);
|
|
|
|
add_child(main_vb);
|
|
|
|
|
|
|
|
HBoxContainer *main_row = memnew(HBoxContainer);
|
|
|
|
main_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);
|
|
|
|
main_row->add_theme_constant_override("separation", grid_separation);
|
|
|
|
main_vb->add_child(main_row);
|
|
|
|
|
|
|
|
_add_row_button(main_row, SIZE_SHRINK_BEGIN, TTR("Shrink Begin"));
|
|
|
|
_add_row_button(main_row, SIZE_SHRINK_CENTER, TTR("Shrink Center"));
|
|
|
|
_add_row_button(main_row, SIZE_SHRINK_END, TTR("Shrink End"));
|
|
|
|
_add_separator(main_row, memnew(VSeparator));
|
|
|
|
_add_row_button(main_row, SIZE_FILL, TTR("Fill"));
|
|
|
|
|
|
|
|
expand_button = memnew(CheckBox);
|
|
|
|
expand_button->set_flat(true);
|
|
|
|
expand_button->set_text(TTR("Align with Expand"));
|
2022-08-25 12:42:17 +02:00
|
|
|
expand_button->set_tooltip_text(TTR("Enable to also set the Expand flag.\nDisable to only set Shrink/Fill flags."));
|
2022-07-23 00:28:05 +02:00
|
|
|
main_vb->add_child(expand_button);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Toolbar.
|
|
|
|
|
|
|
|
void ControlEditorToolbar::_anchors_preset_selected(int p_preset) {
|
|
|
|
LayoutPreset preset = (LayoutPreset)p_preset;
|
2021-11-08 21:53:41 +01:00
|
|
|
List<Node *> selection = editor_selection->get_selected_node_list();
|
|
|
|
|
2022-12-23 23:53:16 +01:00
|
|
|
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
|
2022-07-23 00:28:05 +02:00
|
|
|
undo_redo->create_action(TTR("Change Anchors, Offsets, Grow Direction"));
|
2021-11-08 21:53:41 +01:00
|
|
|
|
|
|
|
for (Node *E : selection) {
|
|
|
|
Control *control = Object::cast_to<Control>(E);
|
|
|
|
if (control) {
|
2023-01-10 21:07:57 +01:00
|
|
|
undo_redo->add_do_property(control, "layout_mode", LayoutMode::LAYOUT_MODE_ANCHORS);
|
2022-07-23 00:28:05 +02:00
|
|
|
undo_redo->add_do_property(control, "anchors_preset", preset);
|
2021-11-08 21:53:41 +01:00
|
|
|
undo_redo->add_undo_method(control, "_edit_set_state", control->_edit_get_state());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
undo_redo->commit_action();
|
|
|
|
|
|
|
|
anchors_mode = false;
|
|
|
|
anchor_mode_button->set_pressed(anchors_mode);
|
|
|
|
}
|
|
|
|
|
2022-07-23 00:28:05 +02:00
|
|
|
void ControlEditorToolbar::_anchors_to_current_ratio() {
|
2021-11-08 21:53:41 +01:00
|
|
|
List<Node *> selection = editor_selection->get_selected_node_list();
|
|
|
|
|
2022-12-23 23:53:16 +01:00
|
|
|
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
|
2022-07-23 00:28:05 +02:00
|
|
|
undo_redo->create_action(TTR("Change Anchors, Offsets (Keep Ratio)"));
|
2021-11-08 21:53:41 +01:00
|
|
|
|
|
|
|
for (Node *E : selection) {
|
|
|
|
Control *control = Object::cast_to<Control>(E);
|
|
|
|
if (control) {
|
|
|
|
Point2 top_left_anchor = _position_to_anchor(control, Point2());
|
|
|
|
Point2 bottom_right_anchor = _position_to_anchor(control, control->get_size());
|
|
|
|
undo_redo->add_do_method(control, "set_anchor", SIDE_LEFT, top_left_anchor.x, false, true);
|
|
|
|
undo_redo->add_do_method(control, "set_anchor", SIDE_RIGHT, bottom_right_anchor.x, false, true);
|
|
|
|
undo_redo->add_do_method(control, "set_anchor", SIDE_TOP, top_left_anchor.y, false, true);
|
|
|
|
undo_redo->add_do_method(control, "set_anchor", SIDE_BOTTOM, bottom_right_anchor.y, false, true);
|
|
|
|
undo_redo->add_do_method(control, "set_meta", "_edit_use_anchors_", true);
|
|
|
|
|
2022-04-01 20:30:23 +02:00
|
|
|
const bool use_anchors = control->get_meta("_edit_use_anchors_", false);
|
2021-11-08 21:53:41 +01:00
|
|
|
undo_redo->add_undo_method(control, "_edit_set_state", control->_edit_get_state());
|
2022-02-14 01:26:57 +01:00
|
|
|
if (use_anchors) {
|
|
|
|
undo_redo->add_undo_method(control, "set_meta", "_edit_use_anchors_", true);
|
|
|
|
} else {
|
|
|
|
undo_redo->add_undo_method(control, "remove_meta", "_edit_use_anchors_");
|
|
|
|
}
|
2021-11-08 21:53:41 +01:00
|
|
|
|
|
|
|
anchors_mode = true;
|
|
|
|
anchor_mode_button->set_pressed(anchors_mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
undo_redo->commit_action();
|
|
|
|
}
|
|
|
|
|
2022-07-23 00:28:05 +02:00
|
|
|
void ControlEditorToolbar::_anchor_mode_toggled(bool p_status) {
|
|
|
|
List<Control *> selection = _get_edited_controls();
|
|
|
|
for (Control *E : selection) {
|
|
|
|
if (Object::cast_to<Container>(E->get_parent())) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-11-08 21:53:41 +01:00
|
|
|
|
2022-07-23 00:28:05 +02:00
|
|
|
if (p_status) {
|
|
|
|
E->set_meta("_edit_use_anchors_", true);
|
|
|
|
} else {
|
|
|
|
E->remove_meta("_edit_use_anchors_");
|
2021-11-08 21:53:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-23 00:28:05 +02:00
|
|
|
anchors_mode = p_status;
|
|
|
|
CanvasItemEditor::get_singleton()->update_viewport();
|
2021-11-08 21:53:41 +01:00
|
|
|
}
|
|
|
|
|
2022-07-23 00:28:05 +02:00
|
|
|
void ControlEditorToolbar::_container_flags_selected(int p_flags, bool p_vertical) {
|
2021-11-08 21:53:41 +01:00
|
|
|
List<Node *> selection = editor_selection->get_selected_node_list();
|
|
|
|
|
2022-12-23 23:53:16 +01:00
|
|
|
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
|
2022-07-23 00:28:05 +02:00
|
|
|
if (p_vertical) {
|
|
|
|
undo_redo->create_action(TTR("Change Vertical Size Flags"));
|
|
|
|
} else {
|
|
|
|
undo_redo->create_action(TTR("Change Horizontal Size Flags"));
|
2021-11-08 21:53:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (Node *E : selection) {
|
|
|
|
Control *control = Object::cast_to<Control>(E);
|
|
|
|
if (control) {
|
2022-07-23 00:28:05 +02:00
|
|
|
if (p_vertical) {
|
|
|
|
undo_redo->add_do_method(control, "set_v_size_flags", p_flags);
|
|
|
|
} else {
|
|
|
|
undo_redo->add_do_method(control, "set_h_size_flags", p_flags);
|
|
|
|
}
|
2021-11-08 21:53:41 +01:00
|
|
|
undo_redo->add_undo_method(control, "_edit_set_state", control->_edit_get_state());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
undo_redo->commit_action();
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2 ControlEditorToolbar::_position_to_anchor(const Control *p_control, Vector2 position) {
|
|
|
|
ERR_FAIL_COND_V(!p_control, Vector2());
|
|
|
|
|
|
|
|
Rect2 parent_rect = p_control->get_parent_anchorable_rect();
|
|
|
|
|
2022-11-14 18:21:06 +01:00
|
|
|
Vector2 output;
|
2021-11-08 21:53:41 +01:00
|
|
|
if (p_control->is_layout_rtl()) {
|
|
|
|
output.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;
|
|
|
|
} else {
|
|
|
|
output.x = (parent_rect.size.x == 0) ? 0.0 : (p_control->get_transform().xform(position).x - parent_rect.position.x) / parent_rect.size.x;
|
|
|
|
}
|
|
|
|
output.y = (parent_rect.size.y == 0) ? 0.0 : (p_control->get_transform().xform(position).y - parent_rect.position.y) / parent_rect.size.y;
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ControlEditorToolbar::_is_node_locked(const Node *p_node) {
|
2022-04-01 20:30:23 +02:00
|
|
|
return p_node->get_meta("_edit_lock_", false);
|
2021-11-08 21:53:41 +01:00
|
|
|
}
|
|
|
|
|
2022-07-23 00:28:05 +02:00
|
|
|
List<Control *> ControlEditorToolbar::_get_edited_controls() {
|
2021-11-08 21:53:41 +01:00
|
|
|
List<Control *> selection;
|
|
|
|
for (const KeyValue<Node *, Object *> &E : editor_selection->get_selection()) {
|
|
|
|
Control *control = Object::cast_to<Control>(E.key);
|
2022-07-23 00:28:05 +02:00
|
|
|
if (control && control->is_visible_in_tree() && control->get_viewport() == EditorNode::get_singleton()->get_scene_root() && !_is_node_locked(control)) {
|
2021-11-08 21:53:41 +01:00
|
|
|
selection.push_back(control);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return selection;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlEditorToolbar::_selection_changed() {
|
2022-07-23 00:28:05 +02:00
|
|
|
// Update toolbar visibility.
|
|
|
|
bool has_controls = false;
|
|
|
|
bool has_control_parents = false;
|
|
|
|
bool has_container_parents = false;
|
|
|
|
|
|
|
|
// Also update which size flags can be configured for the selected nodes.
|
|
|
|
Vector<SizeFlags> allowed_h_flags = {
|
|
|
|
SIZE_SHRINK_BEGIN,
|
|
|
|
SIZE_SHRINK_CENTER,
|
|
|
|
SIZE_SHRINK_END,
|
|
|
|
SIZE_FILL,
|
|
|
|
SIZE_EXPAND,
|
|
|
|
};
|
|
|
|
Vector<SizeFlags> allowed_v_flags = {
|
|
|
|
SIZE_SHRINK_BEGIN,
|
|
|
|
SIZE_SHRINK_CENTER,
|
|
|
|
SIZE_SHRINK_END,
|
|
|
|
SIZE_FILL,
|
|
|
|
SIZE_EXPAND,
|
|
|
|
};
|
2021-11-08 21:53:41 +01:00
|
|
|
|
2022-07-23 00:28:05 +02:00
|
|
|
for (const KeyValue<Node *, Object *> &E : editor_selection->get_selection()) {
|
|
|
|
Control *control = Object::cast_to<Control>(E.key);
|
2021-11-08 21:53:41 +01:00
|
|
|
if (!control) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-07-23 00:28:05 +02:00
|
|
|
has_controls = true;
|
2021-11-08 21:53:41 +01:00
|
|
|
|
2022-07-23 00:28:05 +02:00
|
|
|
if (Object::cast_to<Control>(control->get_parent())) {
|
|
|
|
has_control_parents = true;
|
|
|
|
}
|
2021-11-08 21:53:41 +01:00
|
|
|
if (Object::cast_to<Container>(control->get_parent())) {
|
2022-07-23 00:28:05 +02:00
|
|
|
has_container_parents = true;
|
|
|
|
|
|
|
|
Container *parent_container = Object::cast_to<Container>(control->get_parent());
|
|
|
|
|
|
|
|
Vector<int> container_h_flags = parent_container->get_allowed_size_flags_horizontal();
|
|
|
|
Vector<SizeFlags> tmp_flags = allowed_h_flags.duplicate();
|
|
|
|
for (int i = 0; i < allowed_h_flags.size(); i++) {
|
|
|
|
if (!container_h_flags.has((int)allowed_h_flags[i])) {
|
|
|
|
tmp_flags.erase(allowed_h_flags[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
allowed_h_flags = tmp_flags;
|
|
|
|
|
|
|
|
Vector<int> container_v_flags = parent_container->get_allowed_size_flags_vertical();
|
|
|
|
tmp_flags = allowed_v_flags.duplicate();
|
|
|
|
for (int i = 0; i < allowed_v_flags.size(); i++) {
|
|
|
|
if (!container_v_flags.has((int)allowed_v_flags[i])) {
|
|
|
|
tmp_flags.erase(allowed_v_flags[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
allowed_v_flags = tmp_flags;
|
2021-11-08 21:53:41 +01:00
|
|
|
}
|
2022-07-23 00:28:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set general toolbar visibility.
|
|
|
|
set_visible(has_controls);
|
|
|
|
|
|
|
|
// Set anchor tools visibility.
|
|
|
|
if (has_controls && (!has_control_parents || !has_container_parents)) {
|
|
|
|
anchors_button->set_visible(true);
|
|
|
|
anchor_mode_button->set_visible(true);
|
|
|
|
|
|
|
|
// Update anchor mode.
|
|
|
|
int nb_valid_controls = 0;
|
|
|
|
int nb_anchors_mode = 0;
|
2021-11-08 21:53:41 +01:00
|
|
|
|
2022-07-23 00:28:05 +02:00
|
|
|
List<Node *> selection = editor_selection->get_selected_node_list();
|
|
|
|
for (Node *E : selection) {
|
|
|
|
Control *control = Object::cast_to<Control>(E);
|
|
|
|
if (!control) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Object::cast_to<Container>(control->get_parent())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nb_valid_controls++;
|
|
|
|
if (control->get_meta("_edit_use_anchors_", false)) {
|
|
|
|
nb_anchors_mode++;
|
|
|
|
}
|
2021-11-08 21:53:41 +01:00
|
|
|
}
|
2022-07-23 00:28:05 +02:00
|
|
|
|
|
|
|
anchors_mode = (nb_valid_controls == nb_anchors_mode);
|
|
|
|
anchor_mode_button->set_pressed(anchors_mode);
|
|
|
|
} else {
|
|
|
|
anchors_button->set_visible(false);
|
|
|
|
anchor_mode_button->set_visible(false);
|
|
|
|
anchor_mode_button->set_pressed(false);
|
2021-11-08 21:53:41 +01:00
|
|
|
}
|
|
|
|
|
2022-07-23 00:28:05 +02:00
|
|
|
// Set container tools visibility.
|
|
|
|
if (has_controls && (!has_control_parents || has_container_parents)) {
|
|
|
|
containers_button->set_visible(true);
|
2021-11-08 21:53:41 +01:00
|
|
|
|
2022-07-23 00:28:05 +02:00
|
|
|
// Update allowed size flags.
|
|
|
|
if (has_container_parents) {
|
|
|
|
container_h_picker->set_allowed_flags(allowed_h_flags);
|
|
|
|
container_v_picker->set_allowed_flags(allowed_v_flags);
|
|
|
|
} else {
|
|
|
|
Vector<SizeFlags> allowed_all_flags = {
|
|
|
|
SIZE_SHRINK_BEGIN,
|
|
|
|
SIZE_SHRINK_CENTER,
|
|
|
|
SIZE_SHRINK_END,
|
|
|
|
SIZE_FILL,
|
|
|
|
SIZE_EXPAND,
|
|
|
|
};
|
|
|
|
|
|
|
|
container_h_picker->set_allowed_flags(allowed_all_flags);
|
|
|
|
container_v_picker->set_allowed_flags(allowed_all_flags);
|
|
|
|
}
|
2021-11-08 21:53:41 +01:00
|
|
|
} else {
|
2022-07-23 00:28:05 +02:00
|
|
|
containers_button->set_visible(false);
|
2021-11-08 21:53:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlEditorToolbar::_notification(int p_what) {
|
|
|
|
switch (p_what) {
|
2022-08-29 11:04:31 +02:00
|
|
|
case NOTIFICATION_ENTER_TREE:
|
2022-07-23 00:28:05 +02:00
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
2023-08-13 02:33:39 +02:00
|
|
|
anchors_button->set_icon(get_editor_theme_icon(SNAME("ControlLayout")));
|
|
|
|
anchor_mode_button->set_icon(get_editor_theme_icon(SNAME("Anchor")));
|
|
|
|
containers_button->set_icon(get_editor_theme_icon(SNAME("ContainerLayout")));
|
2021-11-08 21:53:41 +01:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 10:36:51 +01:00
|
|
|
ControlEditorToolbar::ControlEditorToolbar() {
|
2022-07-23 00:28:05 +02:00
|
|
|
add_child(memnew(VSeparator));
|
2021-11-08 21:53:41 +01:00
|
|
|
|
2022-07-23 00:28:05 +02:00
|
|
|
// Anchor and offset tools.
|
|
|
|
anchors_button = memnew(ControlEditorPopupButton);
|
2022-08-25 12:42:17 +02:00
|
|
|
anchors_button->set_tooltip_text(TTR("Presets for the anchor and offset values of a Control node."));
|
2022-07-23 00:28:05 +02:00
|
|
|
add_child(anchors_button);
|
2021-11-08 21:53:41 +01:00
|
|
|
|
2022-07-23 00:28:05 +02:00
|
|
|
Label *anchors_label = memnew(Label);
|
|
|
|
anchors_label->set_text(TTR("Anchor preset"));
|
|
|
|
anchors_button->get_popup_hbox()->add_child(anchors_label);
|
|
|
|
AnchorPresetPicker *anchors_picker = memnew(AnchorPresetPicker);
|
|
|
|
anchors_picker->set_h_size_flags(SIZE_SHRINK_CENTER);
|
|
|
|
anchors_button->get_popup_hbox()->add_child(anchors_picker);
|
|
|
|
anchors_picker->connect("anchors_preset_selected", callable_mp(this, &ControlEditorToolbar::_anchors_preset_selected));
|
|
|
|
|
|
|
|
anchors_button->get_popup_hbox()->add_child(memnew(HSeparator));
|
|
|
|
|
|
|
|
Button *keep_ratio_button = memnew(Button);
|
|
|
|
keep_ratio_button->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
|
|
|
|
keep_ratio_button->set_text(TTR("Set to Current Ratio"));
|
2022-08-25 12:42:17 +02:00
|
|
|
keep_ratio_button->set_tooltip_text(TTR("Adjust anchors and offsets to match the current rect size."));
|
2022-07-23 00:28:05 +02:00
|
|
|
anchors_button->get_popup_hbox()->add_child(keep_ratio_button);
|
|
|
|
keep_ratio_button->connect("pressed", callable_mp(this, &ControlEditorToolbar::_anchors_to_current_ratio));
|
2021-11-08 21:53:41 +01:00
|
|
|
|
|
|
|
anchor_mode_button = memnew(Button);
|
|
|
|
anchor_mode_button->set_flat(true);
|
|
|
|
anchor_mode_button->set_toggle_mode(true);
|
2022-08-25 12:42:17 +02:00
|
|
|
anchor_mode_button->set_tooltip_text(TTR("When active, moving Control nodes changes their anchors instead of their offsets."));
|
2021-11-08 21:53:41 +01:00
|
|
|
add_child(anchor_mode_button);
|
2022-07-23 00:28:05 +02:00
|
|
|
anchor_mode_button->connect("toggled", callable_mp(this, &ControlEditorToolbar::_anchor_mode_toggled));
|
|
|
|
|
|
|
|
// Container tools.
|
|
|
|
containers_button = memnew(ControlEditorPopupButton);
|
2022-08-25 12:42:17 +02:00
|
|
|
containers_button->set_tooltip_text(TTR("Sizing settings for children of a Container node."));
|
2022-07-23 00:28:05 +02:00
|
|
|
add_child(containers_button);
|
|
|
|
|
|
|
|
Label *container_h_label = memnew(Label);
|
|
|
|
container_h_label->set_text(TTR("Horizontal alignment"));
|
|
|
|
containers_button->get_popup_hbox()->add_child(container_h_label);
|
|
|
|
container_h_picker = memnew(SizeFlagPresetPicker(false));
|
|
|
|
containers_button->get_popup_hbox()->add_child(container_h_picker);
|
|
|
|
container_h_picker->connect("size_flags_selected", callable_mp(this, &ControlEditorToolbar::_container_flags_selected).bind(false));
|
|
|
|
|
|
|
|
containers_button->get_popup_hbox()->add_child(memnew(HSeparator));
|
|
|
|
|
|
|
|
Label *container_v_label = memnew(Label);
|
|
|
|
container_v_label->set_text(TTR("Vertical alignment"));
|
|
|
|
containers_button->get_popup_hbox()->add_child(container_v_label);
|
|
|
|
container_v_picker = memnew(SizeFlagPresetPicker(true));
|
|
|
|
containers_button->get_popup_hbox()->add_child(container_v_picker);
|
|
|
|
container_v_picker->connect("size_flags_selected", callable_mp(this, &ControlEditorToolbar::_container_flags_selected).bind(true));
|
|
|
|
|
|
|
|
// Editor connections.
|
2022-01-27 10:36:51 +01:00
|
|
|
editor_selection = EditorNode::get_singleton()->get_editor_selection();
|
2021-11-08 21:53:41 +01:00
|
|
|
editor_selection->add_editor_plugin(this);
|
|
|
|
editor_selection->connect("selection_changed", callable_mp(this, &ControlEditorToolbar::_selection_changed));
|
|
|
|
|
|
|
|
singleton = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ControlEditorToolbar *ControlEditorToolbar::singleton = nullptr;
|
|
|
|
|
2022-07-23 00:28:05 +02:00
|
|
|
// Editor plugin.
|
|
|
|
|
2022-01-27 10:36:51 +01:00
|
|
|
ControlEditorPlugin::ControlEditorPlugin() {
|
|
|
|
toolbar = memnew(ControlEditorToolbar);
|
2021-11-08 21:53:41 +01:00
|
|
|
toolbar->hide();
|
|
|
|
add_control_to_container(CONTAINER_CANVAS_EDITOR_MENU, toolbar);
|
|
|
|
|
|
|
|
Ref<EditorInspectorPluginControl> plugin;
|
|
|
|
plugin.instantiate();
|
|
|
|
add_inspector_plugin(plugin);
|
|
|
|
}
|