Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NativeEngine;
using Sandbox.UI;
using System.Collections.Concurrent;
using System.IO;

Expand Down Expand Up @@ -91,6 +92,8 @@ public static void TakeHighResScreenshot( Scene scene, int width, int height )

const int MaxDimension = 16384;

var requestedSize = new Vector2( width, height );

if ( width <= 0 || height <= 0 )
{
Log.Warning( "screenshot_highres requires width and height greater than zero." );
Expand All @@ -112,10 +115,20 @@ public static void TakeHighResScreenshot( Scene scene, int width, int height )
Bitmap captureBitmap = null;
RenderTarget renderTarget = null;
var previousCustomSize = camera.CustomSize;
var previousScreenSize = Screen.Size;
var screenSizeChanged = previousScreenSize != requestedSize;

try
{
camera.CustomSize = new Vector2( width, height );
if ( screenSizeChanged )
{
Screen.Size = requestedSize;
RenderTarget.Flush();
}

camera.CustomSize = requestedSize;

ResizeUI( camera, requestedSize );

renderTarget = RenderTarget.GetTemporary( width, height, ImageFormat.Default, ImageFormat.Default, MultisampleAmount.Multisample16x, 1, "HighResScreenshot" );
if ( renderTarget is null )
Expand Down Expand Up @@ -162,10 +175,54 @@ public static void TakeHighResScreenshot( Scene scene, int width, int height )

captureBitmap?.Dispose();

if ( screenSizeChanged )
{
Screen.Size = previousScreenSize;
RenderTarget.Flush();
ResizeUI( camera, previousScreenSize );
}

if ( camera.IsValid() )
{
camera.InitializeRendering();
}
}
}

private static void ResizeUI( CameraComponent camera, Vector2 size )
{
if ( !camera.IsValid() )
return;

if ( !camera.Scene.IsValid() )
return;

foreach ( var panel in camera.Scene.GetAll<ScreenPanel>() )
{
if ( !panel.IsValid() || !panel.Active )
continue;

var target = panel.TargetCamera ?? (camera.IsMainCamera ? camera : null);
if ( target != camera )
continue;

if ( camera.RenderExcludeTags.HasAny( panel.GameObject.Tags ) )
continue;

if ( panel.GetPanel() is not RootPanel rootPanel )
continue;

if ( !rootPanel.IsValid() )
continue;

if ( rootPanel.PanelBounds.Width == size.x && rootPanel.PanelBounds.Height == size.y )
continue;

var screenRect = new Rect( 0, 0, size.x, size.y );

rootPanel.PreLayout( screenRect );
rootPanel.CalculateLayout();
rootPanel.PostLayout();
}
}
}