Gondwana Game Engine Development (master) - 2.7.0
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
ViewManager.cs
1using System.Collections.ObjectModel;
2using System.Drawing;
4
6
13public sealed class ViewManager
14{
15 private readonly RenderSurfaceHostBase _renderSurfaceHost;
16 private readonly List<View> _views = new();
17
21 public event Action<View>? ViewAdded;
22
26 public event Action<View>? ViewRemoved;
27
28 private ViewManager() { }
29
30 internal ViewManager(RenderSurfaceHostBase renderSurfaceHost)
31 {
32 _renderSurfaceHost = renderSurfaceHost;
33 }
34
42 public ReadOnlyCollection<View> Views => _views.AsReadOnly();
43
70 public void AddView(
71 Rectangle targetRectPx,
72 float zoom = 1f,
73 int zOrder = 0,
74 RectangleF? worldBoundsPx = null)
75 {
76 if (worldBoundsPx is null)
77 worldBoundsPx = RectangleF.Empty;
78
79 var cam = new Camera(_renderSurfaceHost.Scene)
80 {
81 // clamp camera to Scene pixel bounds
82 WorldBoundsPx = worldBoundsPx.Value,
83 FollowLerpPerSecond = 0f // snap by default
84 };
85
86 cam.SnapTo(new PointF(0, 0));
87
88 var vp = new Viewport
89 {
90 TargetRectPx = targetRectPx,
91 Zoom = zoom
92 };
93
94 var view = new View(cam, vp) { ZOrder = zOrder };
95 view.Viewport.TargetRectChanged += OnViewportTargetRectChanged;
96 view.Viewport.ZoomChanged += OnViewportZoomChanged;
97
98 _views.Add(view);
99 _renderSurfaceHost.Scene.FullRefreshNeeded = true;
100
101 SortViews();
102
103 ViewAdded?.Invoke(view);
104 }
105
112 public void ConfigureSingleFullView(float zoom = 1f, int zOrder = 0)
113 {
114 if (_renderSurfaceHost.RenderSurfaceAdapter is null)
115 throw new InvalidOperationException("RenderSurfaceAdapter is not available.");
116
117 ClearViews();
118
119 var backbuffer = _renderSurfaceHost.Backbuffer;
120
121 var bounds = new Rectangle(
122 0,
123 0,
124 backbuffer.Width,
125 backbuffer.Height);
126
127 RectangleF worldBoundsPx = RectangleF.Empty;
128
129 if (_renderSurfaceHost.Scene is not null)
130 worldBoundsPx = _renderSurfaceHost.Scene.GetWorldBoundsPx();
131
132 AddView(
133 bounds,
134 zoom,
135 zOrder,
136 worldBoundsPx);
137 }
138
145 public void ConfigureVerticalSplit(float leftZoom = 1f, float rightZoom = 1f)
146 {
147 if (_renderSurfaceHost.RenderSurfaceAdapter is null)
148 throw new InvalidOperationException("RenderSurfaceAdapter is not available.");
149
150 ClearViews();
151
152 var backbuffer = _renderSurfaceHost.Backbuffer;
153 int width = backbuffer.Width;
154 int height = backbuffer.Height;
155
156 int halfWidth = width / 2;
157
158 RectangleF worldBoundsPx = RectangleF.Empty;
159
160 if (_renderSurfaceHost.Scene is not null)
161 worldBoundsPx = _renderSurfaceHost.Scene.GetWorldBoundsPx();
162
163 // Left view
164 AddView(
165 new Rectangle(
166 0,
167 0,
168 halfWidth,
169 height),
170 leftZoom,
171 zOrder: 0,
172 worldBoundsPx: worldBoundsPx);
173
174 // Right view
175 AddView(
176 new Rectangle(
177 halfWidth,
178 0,
179 width - halfWidth,
180 height),
181 rightZoom,
182 zOrder: 1,
183 worldBoundsPx: worldBoundsPx);
184 }
185
192 public void ConfigureHorizontalSplit(float topZoom = 1f, float bottomZoom = 1f)
193 {
194 if (_renderSurfaceHost.RenderSurfaceAdapter is null)
195 throw new InvalidOperationException("RenderSurfaceAdapter is not available.");
196
197 ClearViews();
198
199 var backbuffer = _renderSurfaceHost.Backbuffer;
200 int width = backbuffer.Width;
201 int height = backbuffer.Height;
202
203 int halfHeight = height / 2;
204
205 RectangleF worldBoundsPx = RectangleF.Empty;
206
207 if (_renderSurfaceHost.Scene is not null)
208 worldBoundsPx = _renderSurfaceHost.Scene.GetWorldBoundsPx();
209
210 // Top view
211 AddView(
212 new Rectangle(
213 0,
214 0,
215 width,
216 halfHeight),
217 topZoom,
218 zOrder: 0,
219 worldBoundsPx: worldBoundsPx);
220
221 // Bottom view
222 AddView(
223 new Rectangle(
224 0,
225 halfHeight,
226 width,
227 height - halfHeight),
228 bottomZoom,
229 zOrder: 1,
230 worldBoundsPx: worldBoundsPx);
231 }
232
242 public void ClearViews()
243 {
244 foreach (var view in _views!)
245 {
246 view.Viewport.TargetRectChanged -= OnViewportTargetRectChanged;
247 view.Viewport.ZoomChanged -= OnViewportZoomChanged;
248 ViewRemoved?.Invoke(view);
249 }
250
251 _views.Clear();
252
253 if (_renderSurfaceHost.Scene is not null)
254 _renderSurfaceHost.Scene.FullRefreshNeeded = true;
255 }
256
269 public IReadOnlyList<View> GetViewsBelow(View view)
270 {
271 int idx = _views.IndexOf(view);
272
273 if (idx <= 0)
274 return Array.Empty<View>();
275
276 // views with lower Z (earlier in sorted list)
277 return _views.GetRange(0, idx);
278 }
279
292 public IReadOnlyList<View> GetViewsAbove(View view)
293 {
294 int idx = _views.IndexOf(view);
295
296 if (idx < 0 || idx >= _views.Count - 1)
297 return Array.Empty<View>();
298
299 // views with higher Z (later in sorted list)
300 return _views.GetRange(idx + 1, _views.Count - (idx + 1));
301 }
302
303 #region internal methods
304
305 internal void UpdateCameras(float dtSeconds)
306 {
307 foreach (var view in _views)
308 view.Update(dtSeconds);
309 }
310
311 internal void BindToScene(Scene scene, bool limitCameraToWorldBoundPx)
312 {
313 RectangleF worldBoundsPx = limitCameraToWorldBoundPx ? scene.GetWorldBoundsPx() : RectangleF.Empty;
314
315 // if no views exist, create a default one
316 if (!_views.Any())
317 {
318 AddView(
319 new Rectangle(
320 0,
321 0,
322 _renderSurfaceHost.Backbuffer.Width,
323 _renderSurfaceHost.Backbuffer.Height),
324 1,
325 0,
326 worldBoundsPx);
327 }
328 else
329 {
330 foreach (var view in _views)
331 {
332 view.Camera.WorldBoundsPx = worldBoundsPx;
333 }
334 }
335 }
336
337 #endregion internal methods
338
339 #region private methods
340
341 private void OnViewportZoomChanged(ViewportZoomChangedEventArgs args)
342 {
343 if (_renderSurfaceHost.Scene is not null)
344 _renderSurfaceHost.Scene.FullRefreshNeeded = true;
345 }
346
347 private void OnViewportTargetRectChanged(ViewportResizedEventArgs args)
348 {
349 if (_renderSurfaceHost.Scene is not null)
350 _renderSurfaceHost.Scene.FullRefreshNeeded = true;
351 }
352
353 private void SortViews()
354 {
355 _views.Sort(static (a, b) =>
356 {
357 int cmp = a.ZOrder.CompareTo(b.ZOrder);
358
359 if (cmp != 0)
360 return cmp;
361
362 // deterministic tie-breaker
363 return a.Id.CompareTo(b.Id);
364 });
365 }
366
367 #endregion private methods
368}
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...
Definition Camera.cs:16
void SnapTo(PointF worldUpperLeftPx)
Instantly moves the camera to the specified world-space position, interpreted as the upper-left corne...
Definition Camera.cs:92
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...
Definition View.cs:14
A rectangular window on the render target with its own zoom and placement. Viewport never moves the w...
Definition Viewport.cs:10
Represents a game scene containing multiple layers of tiles, sprites, and game objects....
Definition Scene.cs:35
RectangleF GetWorldBoundsPx()
Computes a world-space pixel bounding rectangle that encloses all layers in the scene.
Definition Scene.cs:597