diff --git a/engine/Sandbox.Tools/Scene/EditorScene.cs b/engine/Sandbox.Tools/Scene/EditorScene.cs index aa0fd4432..d56d8bcf8 100644 --- a/engine/Sandbox.Tools/Scene/EditorScene.cs +++ b/engine/Sandbox.Tools/Scene/EditorScene.cs @@ -161,6 +161,9 @@ public static void Discard() static SceneEditorSession FindPlayableSession() { + if ( SceneEditorSession.Active is not PrefabEditorSession ) + return SceneEditorSession.Active; + // Current scene is good if ( SceneEditorSession.Active?.Scene is not PrefabScene ) return SceneEditorSession.Active; @@ -238,8 +241,9 @@ public static void Play( bool playMode, SceneEditorSession playableSession = nul { playableSession ??= FindPlayableSession(); if ( playableSession is null ) return; - + OnPlayStore(); + playableSession.MakeActive(); Game.IsPlaying = true; diff --git a/engine/Sandbox.Tools/Scene/Session/SceneEditorSession.cs b/engine/Sandbox.Tools/Scene/Session/SceneEditorSession.cs index f3f240dae..51a7477b8 100644 --- a/engine/Sandbox.Tools/Scene/Session/SceneEditorSession.cs +++ b/engine/Sandbox.Tools/Scene/Session/SceneEditorSession.cs @@ -167,8 +167,12 @@ public virtual void Destroy() /// public void MakeActive( bool bringToFront = true ) { - Active = this; + if (Active == this) + { + return; + } + Active = this; if ( bringToFront && EditorWindow is not null ) { BringToFront(); @@ -185,6 +189,10 @@ public void BringToFront() EditorWindow.DockManager.RaiseDock( SceneDock ); } + if ( SceneDock.IsValid() ) + SceneDock.Focus(); + + UpdateEditorTitle(); } diff --git a/game/addons/tools/Code/Scene/SceneView/SceneDock.cs b/game/addons/tools/Code/Scene/SceneView/SceneDock.cs index 5414440ae..b4dae7279 100644 --- a/game/addons/tools/Code/Scene/SceneView/SceneDock.cs +++ b/game/addons/tools/Code/Scene/SceneView/SceneDock.cs @@ -49,7 +49,6 @@ public override void OnDestroyed() protected override void OnVisibilityChanged( bool visible ) { base.OnVisibilityChanged( visible ); - if ( visible ) { Session.MakeActive(); diff --git a/game/addons/tools/Code/Scene/SceneView/SceneViewWidget.cs b/game/addons/tools/Code/Scene/SceneView/SceneViewWidget.cs index 66d9445c0..d1b7fac1d 100644 --- a/game/addons/tools/Code/Scene/SceneView/SceneViewWidget.cs +++ b/game/addons/tools/Code/Scene/SceneView/SceneViewWidget.cs @@ -82,13 +82,21 @@ protected override void OnPaint() public void Frame() { var session = Session; - if ( session is null ) return; - if ( !session.Scene.IsValid() ) return; + if ( session is null ) + { + Log.Info($"SceneViewWidget#Frame.L91 is null {session is null}"); + return; + } + if ( !session.Scene.IsValid() ) + { + return; + } using var scope = session.Scene.Push(); bool isActive = session == SceneEditorSession.Active; - + if (!isActive) + Log.Info($"SceneViewWidget#Frame.L91 {session.Scene.Name} isActive = {isActive}"); // Update inspector with current selection, if changed if ( isActive && selectionHash != session.Selection.GetHashCode() ) { @@ -112,6 +120,7 @@ public void Frame() if ( isActive ) { + // Log.Info("SceneViewWidget#Frame.L115 isActive"); Current = this; } @@ -338,15 +347,9 @@ void OnToolChanged() scroller.Canvas = toolWidget; _sidebar.Add( scroller ); + Log.Info($"SceneViewWidget.ViewportToolBar#OnToolChanged.L351 {toolWidget.Name}"); toolWidget.Focus(); } - else - { - if ( SceneViewWidget.Current?.LastSelectedViewportWidget?.IsValid() ?? false ) - { - SceneViewWidget.Current.LastSelectedViewportWidget.Focus(); - } - } // Update footer var footerWidget = subTool?.CreateToolFooter() ?? rootTool?.CreateToolFooter(); @@ -370,6 +373,8 @@ public void Frame() if ( _activeTool != tool || (selectionHash != _selectionHash && (tool?.RebuildSidebarOnSelectionChange ?? false)) ) { + Log.Info($"_activeTool {_activeTool?.GetType().ToString()} {SceneViewWidget.Current?.Tools.CurrentSession.Scene.Name}"); + Log.Info($"tool {tool?.GetType().ToString()}"); _activeTool = tool; _selectionHash = selectionHash; OnToolChanged(); diff --git a/game/addons/tools/Code/Scene/SceneView/SceneViewportWidget.Sizing.cs b/game/addons/tools/Code/Scene/SceneView/SceneViewportWidget.Sizing.cs index 3e266012f..691932b8e 100644 --- a/game/addons/tools/Code/Scene/SceneView/SceneViewportWidget.Sizing.cs +++ b/game/addons/tools/Code/Scene/SceneView/SceneViewportWidget.Sizing.cs @@ -15,6 +15,7 @@ public partial class SceneViewportWidget /// public void SetDefaultSize() { + Log.Info("SetDefaultSize"); ForcedSize = null; ForcedAspectRatio = null; @@ -24,13 +25,25 @@ public void SetDefaultSize() SetSizeMode( SizeMode.CanGrow, SizeMode.CanGrow ); } - + /// /// Set the viewport to a specific aspect ratio /// /// public void SetAspectRatio( float aspectRatio ) { + Log.Info("SetAspectRatio"); + + MinimumSize = Vector2.Zero; + MaximumSize = QT_MAX_SIZE; + FixedSize = QT_MAX_SIZE; + + // Don't update if the aspect ratio hasn't changed + if ( ForcedAspectRatio.HasValue && ForcedAspectRatio.Value.AlmostEqual( aspectRatio ) && !ForcedSize.HasValue ) + { + return; + } + ForcedAspectRatio = aspectRatio; ForcedSize = null; @@ -51,58 +64,90 @@ public void SetResolution( Vector2 resolution ) private void UpdateSizeConstraints() { + Log.Info("UpdateSizeConstraints"); if ( ForcedSize.HasValue ) { var size = ForcedSize.Value; - // For fixed resolution, use exact size constraints MaximumSize = size; FixedSize = size; } + else if ( ForcedAspectRatio.HasValue ) + { + if ( Parent != null ) + { + Log.Info($"CurrentSize: x{Size.x} y{Size.y}"); + var contentRect = Parent.ContentRect; + var parentSize = new Vector2( contentRect.Width, contentRect.Height ); + MaximumSize = parentSize; + Log.Info($"MaximumSize: x{MaximumSize.x} y{MaximumSize.y}"); + } + else + { + MaximumSize = new Vector2( QT_MAX_SIZE, QT_MAX_SIZE ); + } + FixedSize = QT_MAX_SIZE; + Log.Info($"FixedSize: x{FixedSize.x} y{FixedSize.y}"); + } else { - // For aspect ratio mode or free, don't lock the size - let it be dynamic MaximumSize = new Vector2( QT_MAX_SIZE, QT_MAX_SIZE ); FixedSize = QT_MAX_SIZE; } Layout.SizeConstraint = SizeConstraint.SetDefaultConstraint; SetSizeMode( SizeMode.Expand, SizeMode.Expand ); - - UpdateGeometry(); - AdjustSize(); } + Vector2? defaultSize; + protected override Vector2 SizeHint() { - // Exact size, easy if ( ForcedSize.HasValue ) { return ForcedSize.Value; } - // Free if ( !ForcedAspectRatio.HasValue ) { return base.SizeHint(); } - // Dynamically calculate size based on current parent size - var parentSize = Parent?.Size ?? base.SizeHint(); - var parentAspect = parentSize.x / parentSize.y; + if ( defaultSize == null ) + { + defaultSize = Size; + } + + var availableSize = defaultSize.Value; + if ( availableSize.x <= 0 || availableSize.y <= 0 ) + { + return base.SizeHint(); + } + var aspectRatio = ForcedAspectRatio.Value; + Log.Info( $"Aspect ratio: {aspectRatio}" ); + + if ( aspectRatio <= 0 || float.IsNaN( aspectRatio ) || float.IsInfinity( aspectRatio ) ) + { + return availableSize; + } - if ( aspectRatio > parentAspect ) + var availableAspect = availableSize.x / availableSize.y; + Vector2 result; + if ( aspectRatio > availableAspect ) { - // Fit to width - return new Vector2( parentSize.x, parentSize.x / aspectRatio ); + result = new Vector2( availableSize.x, availableSize.x / aspectRatio ); } else { - // Fit to height - return new Vector2( parentSize.y * aspectRatio, parentSize.y ); + result = new Vector2( availableSize.y * aspectRatio, availableSize.y ); } + if ( MaximumSize.x > 0 && MaximumSize.y > 0 ) + { + result = new Vector2( MathF.Min( result.x, MaximumSize.x ), MathF.Min( result.y, MaximumSize.y ) ); + } + return result; } } diff --git a/game/addons/tools/Code/Scene/SceneView/SceneViewportWidget.cs b/game/addons/tools/Code/Scene/SceneView/SceneViewportWidget.cs index b19b0332b..10f3b1669 100644 --- a/game/addons/tools/Code/Scene/SceneView/SceneViewportWidget.cs +++ b/game/addons/tools/Code/Scene/SceneView/SceneViewportWidget.cs @@ -446,7 +446,7 @@ void OnEditorPreFrame() // var hasMouseFocus = hasMouseInput; - if ( IsFocused && SceneViewWidget.Current.IsValid() ) + if ( IsFocused && SceneViewWidget.Current.LastSelectedViewportWidget != this && SceneViewWidget.Current.IsValid() ) { SceneViewWidget.Current.LastSelectedViewportWidget = this; }