From fc029b018ef7f4452d8f4f0d3bd6103778599912 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Fri, 8 Mar 2024 14:56:34 +0100 Subject: [PATCH] Fix UI size changes when saving scenes or prefabs in Editor with different layout --- Source/Editor/Modules/SceneModule.cs | 42 ++++++++++++++++++++ Source/Editor/Windows/Assets/PrefabWindow.cs | 6 +++ 2 files changed, 48 insertions(+) diff --git a/Source/Editor/Modules/SceneModule.cs b/Source/Editor/Modules/SceneModule.cs index 302d1e02a..9aae0b72f 100644 --- a/Source/Editor/Modules/SceneModule.cs +++ b/Source/Editor/Modules/SceneModule.cs @@ -6,6 +6,7 @@ using System.IO; using FlaxEditor.SceneGraph; using FlaxEditor.SceneGraph.Actors; using FlaxEngine; +using FlaxEngine.GUI; using Object = FlaxEngine.Object; namespace FlaxEditor.Modules @@ -454,6 +455,41 @@ namespace FlaxEditor.Modules Profiler.EndEvent(); } + private Dictionary _uiRootSizes; + + internal void OnSaveStart(ContainerControl uiRoot) + { + // Force viewport UI to have fixed size during scene/prefabs saving to result in stable data (less mess in version control diffs) + if (_uiRootSizes == null) + _uiRootSizes = new Dictionary(); + _uiRootSizes[uiRoot] = uiRoot.Size; + uiRoot.Size = new Float2(1920, 1080); + } + + internal void OnSaveEnd(ContainerControl uiRoot) + { + // Restore cached size of the UI root container + if (_uiRootSizes != null && _uiRootSizes.Remove(uiRoot, out var size)) + { + uiRoot.Size = size; + } + } + + private void OnSceneSaving(Scene scene, Guid sceneId) + { + OnSaveStart(RootControl.GameRoot); + } + + private void OnSceneSaved(Scene scene, Guid sceneId) + { + OnSaveEnd(RootControl.GameRoot); + } + + private void OnSceneSaveError(Scene scene, Guid sceneId) + { + OnSaveEnd(RootControl.GameRoot); + } + private void OnSceneLoaded(Scene scene, Guid sceneId) { var startTime = DateTime.UtcNow; @@ -659,6 +695,9 @@ namespace FlaxEditor.Modules Root = new ScenesRootNode(); // Bind events + Level.SceneSaving += OnSceneSaving; + Level.SceneSaved += OnSceneSaved; + Level.SceneSaveError += OnSceneSaveError; Level.SceneLoaded += OnSceneLoaded; Level.SceneUnloading += OnSceneUnloading; Level.ActorSpawned += OnActorSpawned; @@ -673,6 +712,9 @@ namespace FlaxEditor.Modules public override void OnExit() { // Unbind events + Level.SceneSaving -= OnSceneSaving; + Level.SceneSaved -= OnSceneSaved; + Level.SceneSaveError -= OnSceneSaveError; Level.SceneLoaded -= OnSceneLoaded; Level.SceneUnloading -= OnSceneUnloading; Level.ActorSpawned -= OnActorSpawned; diff --git a/Source/Editor/Windows/Assets/PrefabWindow.cs b/Source/Editor/Windows/Assets/PrefabWindow.cs index 5aa1486ee..2e3e700a2 100644 --- a/Source/Editor/Windows/Assets/PrefabWindow.cs +++ b/Source/Editor/Windows/Assets/PrefabWindow.cs @@ -355,6 +355,8 @@ namespace FlaxEditor.Windows.Assets try { + Editor.Scene.OnSaveStart(_viewport); + // Simply update changes Editor.Prefabs.ApplyAll(_viewport.Instance); @@ -371,6 +373,10 @@ namespace FlaxEditor.Windows.Assets throw; } + finally + { + Editor.Scene.OnSaveEnd(_viewport); + } } ///