11using NativeEngine ;
2+ using Sandbox . UI ;
23using System . Collections . Concurrent ;
34using 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