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
View.cs
1using System.Drawing;
4
6
13public sealed class View
14{
15 private SceneLayer? _zoomAnchorLayer;
16 private PointF _zoomAnchorScreenPoint;
17 private PointF _zoomAnchorWorldPoint;
18
25 public Guid Id { get; } = Guid.NewGuid();
26
34 public Camera Camera { get; }
35
44 public Viewport Viewport { get; }
45
50 public int ZOrder { get; set; } = 0;
51
55 public float MinZoom { get; set; } = 0.1f;
56
60 public float MaxZoom { get; set; } = 8f;
61
62 // Presentation-only state owned by EffectsManager. These values never alter
63 // camera, viewport, world, or collision state.
64 internal float EffectOpacity { get; set; } = 1f;
65 internal float EffectReveal { get; set; } = 1f;
66 internal EffectDirection EffectRevealDirection { get; set; } = EffectDirection.FromLeftToRight;
67 internal PointF EffectOffsetFactor { get; set; } = PointF.Empty;
68 internal PointF EffectOffsetPx { get; set; } = PointF.Empty;
69
70 internal bool HasPresentationEffect =>
71 EffectOpacity < 0.9999f
72 || EffectReveal < 0.9999f
73 || Math.Abs(EffectOffsetFactor.X) > 0.0001f
74 || Math.Abs(EffectOffsetFactor.Y) > 0.0001f
75 || Math.Abs(EffectOffsetPx.X) > 0.0001f
76 || Math.Abs(EffectOffsetPx.Y) > 0.0001f;
77
78 internal bool BlocksViewsBelow => !HasPresentationEffect;
79
80 internal View(Camera cam, Viewport vp)
81 {
82 Camera = cam;
83 Viewport = vp;
84 // Let camera clamp against THIS viewport's visible world size.
85 Camera.GetVisibleWorldSizePx = () => Viewport.VisibleWorldSizePx;
86 }
87
111 SceneLayer layer,
112 PointF screenPoint,
113 float targetZoom,
114 float durationSeconds)
115 {
116 ArgumentNullException.ThrowIfNull(layer);
117
118 targetZoom = Math.Clamp(targetZoom, MinZoom, MaxZoom);
119
120 PointF worldUnderCursor = ScreenPxToWorldPx(layer, screenPoint);
121
122 // Anchored zoom owns camera positioning until the zoom completes. Cancel an
123 // unrelated explicit pan, but leave follow configuration intact so it can
124 // resume on the next update after the anchored zoom finishes.
125 Camera.CancelPan();
126
127 if (durationSeconds <= 0f)
128 {
129 ClearZoomAnchor();
130 Viewport.SnapZoom(targetZoom);
131 SnapCameraToZoomAnchor(layer, screenPoint, worldUnderCursor);
132 return;
133 }
134
135 _zoomAnchorLayer = layer;
136 _zoomAnchorScreenPoint = screenPoint;
137 _zoomAnchorWorldPoint = worldUnderCursor;
138
139 Viewport.ZoomToOverDuration(targetZoom, durationSeconds);
140 }
141
146 internal void Update(float dtSeconds)
147 {
148 if (_zoomAnchorLayer is { } anchorLayer)
149 {
150 // Update zoom first, then derive the one camera position that keeps
151 // the selected world point at the selected screen point.
152 Viewport.UpdateZoom(dtSeconds);
153
154 SnapCameraToZoomAnchor(
155 anchorLayer,
156 _zoomAnchorScreenPoint,
157 _zoomAnchorWorldPoint);
158
159 if (!Viewport.IsZoomAnimating)
160 ClearZoomAnchor();
161
162 return;
163 }
164
165 Camera.Update(dtSeconds);
166 Viewport.UpdateZoom(dtSeconds);
167 }
168
169 private void SnapCameraToZoomAnchor(
170 SceneLayer layer,
171 PointF screenPoint,
172 PointF worldPoint)
173 {
174 float zoom = Viewport.Zoom > 0f
175 ? Viewport.Zoom
176 : 1f;
177
178 PointF effectOffset = GetEffectOffsetPx(layer);
179
180 float offsetX =
181 Viewport.TargetRectPx.Left
182 + Viewport.ScreenOffsetPx.X
183 + effectOffset.X;
184
185 float offsetY =
186 Viewport.TargetRectPx.Top
187 + Viewport.ScreenOffsetPx.Y
188 + effectOffset.Y;
189
190 float parallax = layer.Parallax;
191
192 if (Math.Abs(parallax) < 1e-6f)
193 parallax = 1f;
194
195 float localX = screenPoint.X - offsetX;
196 float localY = screenPoint.Y - offsetY;
197
198 // screen = offset + (world - camera*p) * zoom
199 // camera = (world - local/zoom) / p
200 float cameraX =
201 (worldPoint.X - localX / zoom)
202 / parallax;
203
204 float cameraY =
205 (worldPoint.Y - localY / zoom)
206 / parallax;
207
208 Camera.SnapTo(new PointF(cameraX, cameraY));
209 }
210
211 private void ClearZoomAnchor()
212 {
213 _zoomAnchorLayer = null;
214 _zoomAnchorScreenPoint = PointF.Empty;
215 _zoomAnchorWorldPoint = PointF.Empty;
216 }
217
218 #region Coordinate conversion methods
219
239 public PointF ScreenPxToWorldPx(SceneLayer layer, PointF screenPx)
240 {
241 RenderContext? context = GetCurrentRenderContext();
242
243 float zoom = context?.ViewportZoom
244 ?? (Viewport.Zoom <= 0f ? 1f : Viewport.Zoom);
245
246 Rectangle targetRect = context?.ViewportTargetRectPx
247 ?? Viewport.TargetRectPx;
248
249 PointF screenOffset = context?.ViewportScreenOffsetPx
250 ?? Viewport.ScreenOffsetPx;
251
252 PointF cameraPosition = context?.CameraPositionPx
253 ?? Camera.PositionPx;
254
255 PointF effectOffset = GetEffectOffsetPx(layer);
256 float offsetX = targetRect.Left + screenOffset.X + effectOffset.X;
257 float offsetY = targetRect.Top + screenOffset.Y + effectOffset.Y;
258 float parallax = layer.Parallax;
259
260 float worldX = cameraPosition.X * parallax
261 + (screenPx.X - offsetX) / zoom;
262
263 float worldY = cameraPosition.Y * parallax
264 + (screenPx.Y - offsetY) / zoom;
265
266 return new PointF(worldX, worldY);
267 }
268
289 public PointF WorldPxToScreenPx(SceneLayer layer, PointF worldPx)
290 {
291 RenderContext? context = GetCurrentRenderContext();
292
293 float zoom = context?.ViewportZoom
294 ?? (Viewport.Zoom <= 0f ? 1f : Viewport.Zoom);
295
296 Rectangle targetRect = context?.ViewportTargetRectPx
297 ?? Viewport.TargetRectPx;
298
299 PointF screenOffset = context?.ViewportScreenOffsetPx
300 ?? Viewport.ScreenOffsetPx;
301
302 PointF cameraPosition = context?.CameraPositionPx
303 ?? Camera.PositionPx;
304
305 PointF effectOffset = GetEffectOffsetPx(layer);
306 float offsetX = targetRect.Left + screenOffset.X + effectOffset.X;
307 float offsetY = targetRect.Top + screenOffset.Y + effectOffset.Y;
308 float parallax = layer.Parallax;
309
310 float screenX = offsetX + (worldPx.X - cameraPosition.X * parallax) * zoom;
311 float screenY = offsetY + (worldPx.Y - cameraPosition.Y * parallax) * zoom;
312
313 return new PointF(screenX, screenY);
314 }
315
324 public PointF ScreenPxToGrid(SceneLayer layer, PointF screenPx)
325 {
326 var worldPx = ScreenPxToWorldPx(layer, screenPx);
327 return layer.CoordinateSystem.GetSceneLayerCoordinatesAtPixel(layer, worldPx);
328 }
329
340 public RectangleF WorldRectToScreenRect(SceneLayer layer, RectangleF worldRect)
341 {
342 if (layer is null)
343 throw new ArgumentNullException(nameof(layer));
344
345 RenderContext? context = GetCurrentRenderContext();
346
347 float zoom = context?.ViewportZoom
348 ?? (Viewport.Zoom <= 0f ? 1f : Viewport.Zoom);
349
350 Rectangle targetRect = context?.ViewportTargetRectPx
351 ?? Viewport.TargetRectPx;
352
353 PointF screenOffset = context?.ViewportScreenOffsetPx
354 ?? Viewport.ScreenOffsetPx;
355
356 PointF cameraPosition = context?.CameraPositionPx
357 ?? Camera.PositionPx;
358
359 PointF effectOffset = GetEffectOffsetPx(layer);
360 float offsetX = targetRect.Left + screenOffset.X + effectOffset.X;
361 float offsetY = targetRect.Top + screenOffset.Y + effectOffset.Y;
362
363 float parallax = layer.Parallax;
364
365 // screen = offset + (world - camera * p) * zoom
366 float localLeft = worldRect.Left - cameraPosition.X * parallax;
367 float localTop = worldRect.Top - cameraPosition.Y * parallax;
368
369 float scaledLeft = localLeft * zoom;
370 float scaledTop = localTop * zoom;
371 float scaledWidth = worldRect.Width * zoom;
372 float scaledHeight = worldRect.Height * zoom;
373
374 float screenLeft = offsetX + scaledLeft;
375 float screenTop = offsetY + scaledTop;
376
377 return new RectangleF(
378 screenLeft,
379 screenTop,
380 scaledWidth,
381 scaledHeight);
382 }
383
395 public RectangleF ScreenRectToWorldRect(SceneLayer layer, RectangleF screenRect)
396 {
397 if (layer is null)
398 throw new ArgumentNullException(nameof(layer));
399
400 RenderContext? context = GetCurrentRenderContext();
401
402 float zoom = context?.ViewportZoom
403 ?? (Viewport.Zoom <= 0f ? 1f : Viewport.Zoom);
404
405 Rectangle targetRect = context?.ViewportTargetRectPx
406 ?? Viewport.TargetRectPx;
407
408 PointF screenOffset = context?.ViewportScreenOffsetPx
409 ?? Viewport.ScreenOffsetPx;
410
411 PointF cameraPosition = context?.CameraPositionPx
412 ?? Camera.PositionPx;
413
414 PointF effectOffset = GetEffectOffsetPx(layer);
415 float offsetX = targetRect.Left + screenOffset.X + effectOffset.X;
416 float offsetY = targetRect.Top + screenOffset.Y + effectOffset.Y;
417
418 float parallax = layer.Parallax;
419
420 // (screen - offset)
421 float localLeft = screenRect.Left - offsetX;
422 float localTop = screenRect.Top - offsetY;
423
424 // world = camera*parallax + local / zoom
425 float worldLeft = cameraPosition.X * parallax + localLeft / zoom;
426 float worldTop = cameraPosition.Y * parallax + localTop / zoom;
427
428 float worldWidth = screenRect.Width / zoom;
429 float worldHeight = screenRect.Height / zoom;
430
431 return new RectangleF(
432 worldLeft,
433 worldTop,
434 worldWidth,
435 worldHeight);
436 }
437
438 internal PointF GetEffectOffsetPx(SceneLayer? layer)
439 {
440 RenderContext? context = GetCurrentRenderContext();
441
442 PointF factor = context?.ViewEffectOffsetFactor
443 ?? EffectOffsetFactor;
444
445 PointF pixels = context?.ViewEffectOffsetPx
446 ?? EffectOffsetPx;
447
448 Rectangle targetRect = context?.ViewportTargetRectPx
450
451 if (layer is not null)
452 {
453 factor = new PointF(
454 factor.X + layer.EffectOffsetFactor.X,
455 factor.Y + layer.EffectOffsetFactor.Y);
456
457 pixels = new PointF(
458 pixels.X + layer.EffectOffsetPx.X,
459 pixels.Y + layer.EffectOffsetPx.Y);
460 }
461
462 var wrappedOffset = layer is null ? PointF.Empty : Gondwana.Drawing.WrappedDrawable.ScreenOffset(this, layer);
463
464 return new PointF(
465 pixels.X + factor.X * targetRect.Width + wrappedOffset.X,
466 pixels.Y + factor.Y * targetRect.Height + wrappedOffset.Y);
467 }
468
469 internal RectangleF GetPresentationBoundsPx()
470 {
471 PointF offset = GetEffectOffsetPx(layer: null);
472 RectangleF viewport = GetRenderViewportTargetRectPx();
473
474 return new RectangleF(
475 viewport.Left + offset.X,
476 viewport.Top + offset.Y,
477 viewport.Width,
478 viewport.Height);
479 }
480
481 internal Rectangle GetRenderViewportTargetRectPx() =>
482 GetCurrentRenderContext()?.ViewportTargetRectPx
484
485 private RenderContext? GetCurrentRenderContext()
486 {
487 RenderContext? context = RenderContext.Current;
488
489 return context is not null && ReferenceEquals(context.View, this)
490 ? context
491 : null;
492 }
493
494 #endregion Coordinate conversion methods
495}
void SnapTo(PointF worldUpperLeftPx)
Instantly moves the camera to the specified world-space position, interpreted as the upper-left corne...
Definition Camera.cs:92
float MaxZoom
Gets or sets the maximum zoom level allowed for this View.
Definition View.cs:60
PointF ScreenPxToWorldPx(SceneLayer layer, PointF screenPx)
Converts a screen-space pixel position into world-space coordinates for the specified scene layer,...
Definition View.cs:239
PointF WorldPxToScreenPx(SceneLayer layer, PointF worldPx)
Converts a world-space pixel position into screen-space coordinates for this view,...
Definition View.cs:289
float MinZoom
Gets or sets the minimum zoom level allowed for this View.
Definition View.cs:55
PointF ScreenPxToGrid(SceneLayer layer, PointF screenPx)
Converts a point in screen-space into the grid coordinate on the specified SceneLayer by first mappin...
Definition View.cs:324
Guid Id
Gets the unique identifier for this view instance.
Definition View.cs:25
int ZOrder
Controls the draw order of this view relative to other views. Lower values are drawn first (behind); ...
Definition View.cs:50
RectangleF WorldRectToScreenRect(SceneLayer layer, RectangleF worldRect)
Converts a world-space pixel rectangle into a screen-space rectangle for this View,...
Definition View.cs:340
RectangleF ScreenRectToWorldRect(SceneLayer layer, RectangleF screenRect)
Converts a screen-space rectangle (on the logical Backbuffer) into a world-space rectangle for the gi...
Definition View.cs:395
Camera Camera
Gets the camera that controls the world-space position and following behavior for this view....
Definition View.cs:34
Viewport Viewport
Gets the viewport that defines the screen-space rectangle, zoom level, and other rendering parameters...
Definition View.cs:44
void ZoomAroundScreenPoint(SceneLayer layer, PointF screenPoint, float targetZoom, float durationSeconds)
Zooms the view around a screen-space point while keeping the world point beneath that location fixed ...
Definition View.cs:110
A rectangular window on the render target with its own zoom and placement. Viewport never moves the w...
Definition Viewport.cs:10
Rectangle TargetRectPx
Screen-space rectangle (in SCREEN / RenderSurface pixels) where this view is drawn....
Definition Viewport.cs:40
float Zoom
Zoom factor applied to the world when rendering this view. Values greater than 1 zoom in,...
Definition Viewport.cs:77
Represents a single layer within a Scene, containing a grid of tiles and supporting various coordinat...
Definition SceneLayer.cs:40
float Parallax
Gets or sets the parallax scrolling factor for this layer.
EffectDirection
Specifies the direction in which a directional display effect travels.