1using System.Collections.ObjectModel;
13public sealed class ViewManager
16 private readonly List<View> _views =
new();
28 private ViewManager() { }
32 _renderSurfaceHost = renderSurfaceHost;
42 public ReadOnlyCollection<View>
Views => _views.AsReadOnly();
71 Rectangle targetRectPx,
74 RectangleF? worldBoundsPx =
null)
76 if (worldBoundsPx is
null)
77 worldBoundsPx = RectangleF.Empty;
79 var cam =
new Camera(_renderSurfaceHost.Scene)
82 WorldBoundsPx = worldBoundsPx.Value,
83 FollowLerpPerSecond = 0f
86 cam.
SnapTo(
new PointF(0, 0));
90 TargetRectPx = targetRectPx,
94 var view =
new View(cam, vp) { ZOrder = zOrder };
95 view.Viewport.TargetRectChanged += OnViewportTargetRectChanged;
96 view.Viewport.ZoomChanged += OnViewportZoomChanged;
99 _renderSurfaceHost.Scene.FullRefreshNeeded =
true;
114 if (_renderSurfaceHost.RenderSurfaceAdapter is
null)
115 throw new InvalidOperationException(
"RenderSurfaceAdapter is not available.");
119 var backbuffer = _renderSurfaceHost.Backbuffer;
121 var bounds =
new Rectangle(
127 RectangleF worldBoundsPx = RectangleF.Empty;
129 if (_renderSurfaceHost.Scene is not
null)
130 worldBoundsPx = _renderSurfaceHost.Scene.GetWorldBoundsPx();
147 if (_renderSurfaceHost.RenderSurfaceAdapter is
null)
148 throw new InvalidOperationException(
"RenderSurfaceAdapter is not available.");
152 var backbuffer = _renderSurfaceHost.Backbuffer;
153 int width = backbuffer.Width;
154 int height = backbuffer.Height;
156 int halfWidth = width / 2;
158 RectangleF worldBoundsPx = RectangleF.Empty;
160 if (_renderSurfaceHost.Scene is not
null)
161 worldBoundsPx = _renderSurfaceHost.Scene.GetWorldBoundsPx();
172 worldBoundsPx: worldBoundsPx);
183 worldBoundsPx: worldBoundsPx);
194 if (_renderSurfaceHost.RenderSurfaceAdapter is
null)
195 throw new InvalidOperationException(
"RenderSurfaceAdapter is not available.");
199 var backbuffer = _renderSurfaceHost.Backbuffer;
200 int width = backbuffer.Width;
201 int height = backbuffer.Height;
203 int halfHeight = height / 2;
205 RectangleF worldBoundsPx = RectangleF.Empty;
207 if (_renderSurfaceHost.Scene is not
null)
208 worldBoundsPx = _renderSurfaceHost.Scene.GetWorldBoundsPx();
219 worldBoundsPx: worldBoundsPx);
227 height - halfHeight),
230 worldBoundsPx: worldBoundsPx);
244 foreach (var view
in _views!)
246 view.Viewport.TargetRectChanged -= OnViewportTargetRectChanged;
247 view.Viewport.ZoomChanged -= OnViewportZoomChanged;
253 if (_renderSurfaceHost.Scene is not
null)
254 _renderSurfaceHost.Scene.FullRefreshNeeded =
true;
271 int idx = _views.IndexOf(view);
274 return Array.Empty<
View>();
277 return _views.GetRange(0, idx);
294 int idx = _views.IndexOf(view);
296 if (idx < 0 || idx >= _views.Count - 1)
297 return Array.Empty<
View>();
300 return _views.GetRange(idx + 1, _views.Count - (idx + 1));
303 #region internal methods
305 internal void UpdateCameras(
float dtSeconds)
307 foreach (var view
in _views)
308 view.Update(dtSeconds);
311 internal void BindToScene(
Scene scene,
bool limitCameraToWorldBoundPx)
313 RectangleF worldBoundsPx = limitCameraToWorldBoundPx ? scene.
GetWorldBoundsPx() : RectangleF.Empty;
322 _renderSurfaceHost.Backbuffer.Width,
323 _renderSurfaceHost.Backbuffer.Height),
330 foreach (var view
in _views)
332 view.Camera.WorldBoundsPx = worldBoundsPx;
337 #endregion internal methods
339 #region private methods
341 private void OnViewportZoomChanged(ViewportZoomChangedEventArgs args)
343 if (_renderSurfaceHost.Scene is not
null)
344 _renderSurfaceHost.Scene.FullRefreshNeeded =
true;
347 private void OnViewportTargetRectChanged(ViewportResizedEventArgs args)
349 if (_renderSurfaceHost.Scene is not
null)
350 _renderSurfaceHost.Scene.FullRefreshNeeded =
true;
353 private void SortViews()
355 _views.Sort(
static (a, b) =>
357 int cmp = a.ZOrder.CompareTo(b.ZOrder);
363 return a.Id.CompareTo(b.Id);
367 #endregion private methods
Represents a base class for hosting a render surface, providing functionality for managing rendering ...
Represents a world-space camera for a Scene. The camera tracks a world-space upper-left position in p...
void SnapTo(PointF worldUpperLeftPx)
Instantly moves the camera to the specified world-space position, interpreted as the upper-left corne...
Manages a collection of views for a render surface, handling view creation, removal,...
void ConfigureVerticalSplit(float leftZoom=1f, float rightZoom=1f)
Removes all existing views and creates a vertical split-screen layout: left and right views sharing t...
void ClearViews()
Removes all views from the manager, unsubscribing from their events and marking the scene as needing ...
IReadOnlyList< View > GetViewsAbove(View view)
Returns all views that are rendered in front of (above) the specified view, based on Z-order....
Action< View >? ViewRemoved
Occurs after a View is removed.
ReadOnlyCollection< View > Views
Gets a read-only collection of all views currently managed by this view manager. Views are sorted by ...
void ConfigureHorizontalSplit(float topZoom=1f, float bottomZoom=1f)
Removes all existing views and creates a horizontal split-screen layout: top and bottom views sharing...
void ConfigureSingleFullView(float zoom=1f, int zOrder=0)
Removes all existing views and creates a single full-screen view that occupies the entire logical Bac...
IReadOnlyList< View > GetViewsBelow(View view)
Returns all views that are rendered behind (below) the specified view, based on Z-order....
void AddView(Rectangle targetRectPx, float zoom=1f, int zOrder=0, RectangleF? worldBoundsPx=null)
Creates and adds a new view with the specified screen rectangle, zoom level, and Z-order....
Action< View >? ViewAdded
Occurs after a new View is added.
Represents a rendered view of a scene, combining a camera position with a viewport configuration to c...
A rectangular window on the render target with its own zoom and placement. Viewport never moves the w...
Represents a game scene containing multiple layers of tiles, sprites, and game objects....
RectangleF GetWorldBoundsPx()
Computes a world-space pixel bounding rectangle that encloses all layers in the scene.