Fix inconsistent state of Controls when editing and running scenes

This commit is contained in:
Yuri Sizov 2023-01-03 19:21:26 +03:00
parent e84bab8e4f
commit 00b5222f66
4 changed files with 42 additions and 27 deletions

View file

@ -171,7 +171,9 @@ Node *SceneCreateDialog::create_scene_root() {
break; break;
case ROOT_USER_INTERFACE: { case ROOT_USER_INTERFACE: {
Control *gui_ctl = memnew(Control); Control *gui_ctl = memnew(Control);
// Making the root control full rect by default is more useful for resizable UIs.
gui_ctl->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); gui_ctl->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
gui_ctl->set_grow_direction_preset(Control::PRESET_FULL_RECT);
root = gui_ctl; root = gui_ctl;
} break; } break;
case ROOT_OTHER: case ROOT_OTHER:

View file

@ -1184,7 +1184,9 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
break; break;
case TOOL_CREATE_USER_INTERFACE: { case TOOL_CREATE_USER_INTERFACE: {
Control *node = memnew(Control); Control *node = memnew(Control);
node->set_anchors_and_offsets_preset(PRESET_FULL_RECT); //more useful for resizable UIs. // Making the root control full rect by default is more useful for resizable UIs.
node->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
node->set_grow_direction_preset(PRESET_FULL_RECT);
new_node = node; new_node = node;
} break; } break;

View file

@ -105,7 +105,7 @@ void Control::_edit_set_state(const Dictionary &p_state) {
} }
_set_layout_mode(_layout); _set_layout_mode(_layout);
if (_layout == LayoutMode::LAYOUT_MODE_ANCHORS) { if (_layout == LayoutMode::LAYOUT_MODE_ANCHORS || _layout == LayoutMode::LAYOUT_MODE_UNCONTROLLED) {
_set_anchors_layout_preset((int)state["anchors_layout_preset"]); _set_anchors_layout_preset((int)state["anchors_layout_preset"]);
} }
@ -125,7 +125,7 @@ void Control::_edit_set_state(const Dictionary &p_state) {
void Control::_edit_set_position(const Point2 &p_position) { void Control::_edit_set_position(const Point2 &p_position) {
ERR_FAIL_COND_MSG(!Engine::get_singleton()->is_editor_hint(), "This function can only be used from editor plugins."); ERR_FAIL_COND_MSG(!Engine::get_singleton()->is_editor_hint(), "This function can only be used from editor plugins.");
set_position(p_position, ControlEditorToolbar::get_singleton()->is_anchors_mode_enabled() && Object::cast_to<Control>(data.parent)); set_position(p_position, ControlEditorToolbar::get_singleton()->is_anchors_mode_enabled() && get_parent_control());
}; };
Point2 Control::_edit_get_position() const { Point2 Control::_edit_get_position() const {
@ -553,7 +553,8 @@ void Control::_validate_property(PropertyInfo &p_property) const {
} }
// Use the layout mode to display or hide advanced anchoring properties. // Use the layout mode to display or hide advanced anchoring properties.
bool use_anchors = _get_layout_mode() == LayoutMode::LAYOUT_MODE_ANCHORS; LayoutMode _layout = _get_layout_mode();
bool use_anchors = (_layout == LayoutMode::LAYOUT_MODE_ANCHORS || _layout == LayoutMode::LAYOUT_MODE_UNCONTROLLED);
if (!use_anchors && p_property.name == "anchors_preset") { if (!use_anchors && p_property.name == "anchors_preset") {
p_property.usage ^= PROPERTY_USAGE_EDITOR; p_property.usage ^= PROPERTY_USAGE_EDITOR;
} }
@ -606,7 +607,7 @@ bool Control::is_top_level_control() const {
} }
Control *Control::get_parent_control() const { Control *Control::get_parent_control() const {
return data.parent; return data.parent_control;
} }
Window *Control::get_parent_window() const { Window *Control::get_parent_window() const {
@ -895,11 +896,9 @@ Control::LayoutMode Control::_get_default_layout_mode() const {
} }
void Control::_set_anchors_layout_preset(int p_preset) { void Control::_set_anchors_layout_preset(int p_preset) {
bool list_changed = false; if (data.stored_layout_mode != LayoutMode::LAYOUT_MODE_UNCONTROLLED && data.stored_layout_mode != LayoutMode::LAYOUT_MODE_ANCHORS) {
// In other modes the anchor preset is non-operational and shouldn't be set to anything.
if (data.stored_layout_mode != LayoutMode::LAYOUT_MODE_ANCHORS) { return;
list_changed = true;
data.stored_layout_mode = LayoutMode::LAYOUT_MODE_ANCHORS;
} }
if (p_preset == -1) { if (p_preset == -1) {
@ -910,6 +909,8 @@ void Control::_set_anchors_layout_preset(int p_preset) {
return; // Keep settings as is. return; // Keep settings as is.
} }
bool list_changed = false;
if (data.stored_use_custom_anchors) { if (data.stored_use_custom_anchors) {
list_changed = true; list_changed = true;
data.stored_use_custom_anchors = false; data.stored_use_custom_anchors = false;
@ -952,6 +953,11 @@ void Control::_set_anchors_layout_preset(int p_preset) {
} }
int Control::_get_anchors_layout_preset() const { int Control::_get_anchors_layout_preset() const {
// If this is a layout mode that doesn't rely on anchors, avoid excessive checks.
if (data.stored_layout_mode != LayoutMode::LAYOUT_MODE_UNCONTROLLED && data.stored_layout_mode != LayoutMode::LAYOUT_MODE_ANCHORS) {
return LayoutPreset::PRESET_TOP_LEFT;
}
// If the custom preset was selected by user, use it. // If the custom preset was selected by user, use it.
if (data.stored_use_custom_anchors) { if (data.stored_use_custom_anchors) {
return -1; return -1;
@ -1533,19 +1539,20 @@ void Control::update_minimum_size() {
Control *invalidate = this; Control *invalidate = this;
//invalidate cache upwards // Invalidate cache upwards.
while (invalidate && invalidate->data.minimum_size_valid) { while (invalidate && invalidate->data.minimum_size_valid) {
invalidate->data.minimum_size_valid = false; invalidate->data.minimum_size_valid = false;
if (invalidate->is_set_as_top_level()) { if (invalidate->is_set_as_top_level()) {
break; // do not go further up break; // Do not go further up.
} }
if (!invalidate->data.parent && get_parent()) {
Window *parent_window = Object::cast_to<Window>(get_parent()); Window *parent_window = invalidate->get_parent_window();
if (parent_window && parent_window->is_wrapping_controls()) { if (parent_window && parent_window->is_wrapping_controls()) {
parent_window->child_controls_changed(); parent_window->child_controls_changed();
} break; // Stop on a window as well.
} }
invalidate = invalidate->data.parent;
invalidate = invalidate->get_parent_control();
} }
if (!is_visible_in_tree()) { if (!is_visible_in_tree()) {
@ -2858,10 +2865,17 @@ void Control::_notification(int p_notification) {
} break; } break;
case NOTIFICATION_PARENTED: { case NOTIFICATION_PARENTED: {
Node *parent_node = get_parent();
data.parent_control = Object::cast_to<Control>(parent_node);
data.parent_window = Object::cast_to<Window>(parent_node);
data.theme_owner->assign_theme_on_parented(this); data.theme_owner->assign_theme_on_parented(this);
} break; } break;
case NOTIFICATION_UNPARENTED: { case NOTIFICATION_UNPARENTED: {
data.parent_control = nullptr;
data.parent_window = nullptr;
data.theme_owner->clear_theme_on_unparented(this); data.theme_owner->clear_theme_on_unparented(this);
} break; } break;
@ -2887,8 +2901,6 @@ void Control::_notification(int p_notification) {
} break; } break;
case NOTIFICATION_ENTER_CANVAS: { case NOTIFICATION_ENTER_CANVAS: {
data.parent = Object::cast_to<Control>(get_parent());
data.parent_window = Object::cast_to<Window>(get_parent());
data.is_rtl_dirty = true; data.is_rtl_dirty = true;
CanvasItem *node = this; CanvasItem *node = this;
@ -2946,17 +2958,16 @@ void Control::_notification(int p_notification) {
data.RI = nullptr; data.RI = nullptr;
} }
data.parent = nullptr;
data.parent_canvas_item = nullptr; data.parent_canvas_item = nullptr;
data.parent_window = nullptr;
data.is_rtl_dirty = true; data.is_rtl_dirty = true;
} break; } break;
case NOTIFICATION_MOVED_IN_PARENT: { case NOTIFICATION_MOVED_IN_PARENT: {
// some parents need to know the order of the children to draw (like TabContainer) // Some parents need to know the order of the children to draw (like TabContainer),
// update if necessary // so we update them just in case.
if (data.parent) { Control *parent_control = get_parent_control();
data.parent->queue_redraw(); if (parent_control) {
parent_control->queue_redraw();
} }
queue_redraw(); queue_redraw();

View file

@ -165,7 +165,7 @@ private:
List<Control *>::Element *RI = nullptr; List<Control *>::Element *RI = nullptr;
Control *parent = nullptr; Control *parent_control = nullptr;
Window *parent_window = nullptr; Window *parent_window = nullptr;
CanvasItem *parent_canvas_item = nullptr; CanvasItem *parent_canvas_item = nullptr;
ObjectID drag_owner; ObjectID drag_owner;