From 17d2acd945cb03cbb1299b44b4e007a0b051d4e5 Mon Sep 17 00:00:00 2001 From: Ainsley Su Date: Fri, 28 Jun 2024 13:44:23 +0800 Subject: [PATCH] Update controls after project settings changed. In `canvas_item_editor_plugin`: Call `update_viewport()` in `_project_setting_changed` to redraw viewport after project settings changed. In `control`: Add a new private function `_project_setting_changed` to update the size and redraw the control. Connect/Disconnet this function to "settings_changed" of `ProjectSettings` when the control enters/exits the canvas. --- editor/plugins/canvas_item_editor_plugin.cpp | 1 + scene/gui/control.cpp | 15 +++++++++++++++ scene/gui/control.h | 1 + 3 files changed, 17 insertions(+) diff --git a/editor/plugins/canvas_item_editor_plugin.cpp b/editor/plugins/canvas_item_editor_plugin.cpp index ee5d90b1f9b6..8a081c9fbd25 100644 --- a/editor/plugins/canvas_item_editor_plugin.cpp +++ b/editor/plugins/canvas_item_editor_plugin.cpp @@ -3967,6 +3967,7 @@ void CanvasItemEditor::_update_editor_settings() { void CanvasItemEditor::_project_settings_changed() { EditorNode::get_singleton()->get_scene_root()->set_snap_controls_to_pixels(GLOBAL_GET("gui/common/snap_controls_to_pixels")); + update_viewport(); } void CanvasItemEditor::_notification(int p_what) { diff --git a/scene/gui/control.cpp b/scene/gui/control.cpp index 0682c11a9b27..44a96338c68b 100644 --- a/scene/gui/control.cpp +++ b/scene/gui/control.cpp @@ -1760,6 +1760,11 @@ void Control::_size_changed() { } } +void Control::_project_settings_changed() { + update_minimum_size(); + _size_changed(); +} + void Control::_clear_size_warning() { data.size_warning = false; } @@ -3259,6 +3264,10 @@ void Control::_notification(int p_notification) { data.RI = viewport->_gui_add_root_control(this); get_parent()->connect(SNAME("child_order_changed"), callable_mp(get_viewport(), &Viewport::gui_set_root_order_dirty), CONNECT_REFERENCE_COUNTED); + + if (Engine::get_singleton()->is_editor_hint()) { + ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &Control::_project_settings_changed)); + } } data.parent_canvas_item = get_parent_item(); @@ -3290,6 +3299,12 @@ void Control::_notification(int p_notification) { get_parent()->disconnect(SNAME("child_order_changed"), callable_mp(get_viewport(), &Viewport::gui_set_root_order_dirty)); } + if (Engine::get_singleton()->is_editor_hint()) { + if (!Object::cast_to(get_parent())) { + ProjectSettings::get_singleton()->disconnect("settings_changed", callable_mp(this, &Control::_project_settings_changed)); + } + } + data.parent_canvas_item = nullptr; data.is_rtl_dirty = true; } break; diff --git a/scene/gui/control.h b/scene/gui/control.h index c784d4330dac..081619ba68bb 100644 --- a/scene/gui/control.h +++ b/scene/gui/control.h @@ -297,6 +297,7 @@ class Control : public CanvasItem { void _update_minimum_size_cache(); void _update_minimum_size(); void _size_changed(); + void _project_settings_changed(); void _top_level_changed() override {} // Controls don't need to do anything, only other CanvasItems. void _top_level_changed_on_parent() override;