Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion engine/Sandbox.Tools/Scene/EditorScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
10 changes: 9 additions & 1 deletion engine/Sandbox.Tools/Scene/Session/SceneEditorSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,12 @@ public virtual void Destroy()
/// </summary>
public void MakeActive( bool bringToFront = true )
{
Active = this;
if (Active == this)
{
return;
}

Active = this;
if ( bringToFront && EditorWindow is not null )
{
BringToFront();
Expand All @@ -185,6 +189,10 @@ public void BringToFront()
EditorWindow.DockManager.RaiseDock( SceneDock );
}

if ( SceneDock.IsValid() )
SceneDock.Focus();


UpdateEditorTitle();
}

Expand Down
1 change: 0 additions & 1 deletion game/addons/tools/Code/Scene/SceneView/SceneDock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public override void OnDestroyed()
protected override void OnVisibilityChanged( bool visible )
{
base.OnVisibilityChanged( visible );

if ( visible )
{
Session.MakeActive();
Expand Down
25 changes: 15 additions & 10 deletions game/addons/tools/Code/Scene/SceneView/SceneViewWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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() )
{
Expand All @@ -112,6 +120,7 @@ public void Frame()

if ( isActive )
{
// Log.Info("SceneViewWidget#Frame.L115 isActive");
Current = this;
}

Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public partial class SceneViewportWidget
/// </summary>
public void SetDefaultSize()
{
Log.Info("SetDefaultSize");
ForcedSize = null;
ForcedAspectRatio = null;

Expand All @@ -24,13 +25,25 @@ public void SetDefaultSize()

SetSizeMode( SizeMode.CanGrow, SizeMode.CanGrow );
}

/// <summary>
/// Set the viewport to a specific aspect ratio
/// </summary>
/// <param name="aspectRatio"></param>
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;

Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading