Skip to content

Commit f32fbc1

Browse files
committed
Fix UI not being resized to screenshot_highres dimensions
1 parent c25d0aa commit f32fbc1

1 file changed

Lines changed: 55 additions & 1 deletion

File tree

engine/Sandbox.Engine/Systems/Render/Multimedia/ScreenshotService.cs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using NativeEngine;
2+
using Sandbox.UI;
23
using System.Collections.Concurrent;
34
using System.IO;
45

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

9293
const int MaxDimension = 16384;
9394

95+
var requestedSize = new Vector2( width, height );
96+
9497
if ( width <= 0 || height <= 0 )
9598
{
9699
Log.Warning( "screenshot_highres requires width and height greater than zero." );
@@ -112,10 +115,20 @@ public static void TakeHighResScreenshot( Scene scene, int width, int height )
112115
Bitmap captureBitmap = null;
113116
RenderTarget renderTarget = null;
114117
var previousCustomSize = camera.CustomSize;
118+
var previousScreenSize = Screen.Size;
119+
var screenSizeChanged = previousScreenSize != requestedSize;
115120

116121
try
117122
{
118-
camera.CustomSize = new Vector2( width, height );
123+
if ( screenSizeChanged )
124+
{
125+
Screen.Size = requestedSize;
126+
RenderTarget.Flush();
127+
}
128+
129+
camera.CustomSize = requestedSize;
130+
131+
EnsureUiLayoutForSize( camera, requestedSize );
119132

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

163176
captureBitmap?.Dispose();
164177

178+
if ( screenSizeChanged )
179+
{
180+
Screen.Size = previousScreenSize;
181+
RenderTarget.Flush();
182+
EnsureUiLayoutForSize( camera, previousScreenSize );
183+
}
184+
165185
if ( camera.IsValid() )
166186
{
167187
camera.InitializeRendering();
168188
}
169189
}
170190
}
191+
192+
private static void EnsureUiLayoutForSize( CameraComponent camera, Vector2 size )
193+
{
194+
if ( camera?.Scene is null )
195+
return;
196+
197+
foreach ( var panel in camera.Scene.GetAll<ScreenPanel>() )
198+
{
199+
if ( panel is null || !panel.Active )
200+
continue;
201+
202+
var target = panel.TargetCamera ?? (camera.IsMainCamera ? camera : null);
203+
if ( target != camera )
204+
continue;
205+
206+
if ( camera.RenderExcludeTags.HasAny( panel.GameObject.Tags ) )
207+
continue;
208+
209+
if ( panel.GetPanel() is not RootPanel rootPanel )
210+
continue;
211+
212+
if ( !rootPanel.IsValid() )
213+
continue;
214+
215+
if ( rootPanel.PanelBounds.Width == size.x && rootPanel.PanelBounds.Height == size.y )
216+
continue;
217+
218+
var screenRect = new Rect( 0, 0, size.x, size.y );
219+
220+
rootPanel.PreLayout( screenRect );
221+
rootPanel.CalculateLayout();
222+
rootPanel.PostLayout();
223+
}
224+
}
171225
}

0 commit comments

Comments
 (0)