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
Viewport.cs
1using System.Drawing;
2
4
9public sealed class Viewport
10{
11 private Rectangle _targetRectPx = new Rectangle(
12 0,
13 0,
14 1280,
15 720);
16
17 private float _zoom = 1f;
18 private float? _zoomTarget;
19 private float _zoomLerpPerSecond;
20 private float? _zoomDurationSeconds;
21 private float _zoomElapsedSeconds;
22 private float _zoomStart;
23
28 public event Action<ViewportResizedEventArgs>? TargetRectChanged;
29
33 public event Action<ViewportZoomChangedEventArgs>? ZoomChanged;
34
39 public Rectangle TargetRectPx
40 {
41 get => _targetRectPx;
42 set
43 {
44 if (value == _targetRectPx)
45 return;
46
47 var oldRect = _targetRectPx;
48 _targetRectPx = value;
49 TargetRectChanged?.Invoke(new ViewportResizedEventArgs(this, oldRect, value));
50 }
51 }
52
60 public void Resize(int width, int height)
61 {
62 var rect = TargetRectPx;
63
64 TargetRectPx = new Rectangle(
65 rect.Left,
66 rect.Top,
67 width,
68 height);
69 }
70
76 public float Zoom
77 {
78 get => _zoom;
79 set
80 {
81 if (Math.Abs(value - _zoom) < 1e-6f)
82 return;
83
84 var oldZoom = _zoom;
85 _zoom = value;
86 ZoomChanged?.Invoke(new ViewportZoomChangedEventArgs(this, oldZoom, value));
87 }
88 }
89
91 public PointF ScreenOffsetPx { get; set; } = PointF.Empty;
92
94 public SizeF VisibleWorldSizePx => new SizeF(TargetRectPx.Width / Zoom, TargetRectPx.Height / Zoom);
95
97 internal bool IsZoomAnimating => _zoomTarget is not null;
98
99 #region zoom zoom
100
105 public void SnapZoom(float zoom)
106 {
107 _zoomTarget = null;
108 _zoomLerpPerSecond = 0f;
109 _zoomDurationSeconds = null;
110 _zoomElapsedSeconds = 0f;
111 _zoomStart = zoom;
112
113 Zoom = zoom;
114 }
115
122 public void ZoomTo(float targetZoom, float lerpPerSecond)
123 {
124 if (lerpPerSecond <= 0f)
125 {
126 SnapZoom(targetZoom);
127 return;
128 }
129
130 _zoomStart = _zoom;
131 _zoomTarget = targetZoom;
132 _zoomLerpPerSecond = lerpPerSecond;
133 _zoomDurationSeconds = null;
134 _zoomElapsedSeconds = 0f;
135 }
136
143 public void ZoomToOverDuration(float targetZoom, float durationSeconds)
144 {
145 if (durationSeconds <= 0f)
146 {
147 SnapZoom(targetZoom);
148 return;
149 }
150
151 _zoomStart = _zoom;
152 _zoomTarget = targetZoom;
153
154 _zoomDurationSeconds = durationSeconds;
155 _zoomElapsedSeconds = 0f;
156
157 // The fixed-duration path calculates progress directly.
158 _zoomLerpPerSecond = 0f;
159 }
160
166 internal void UpdateZoom(float dtSeconds)
167 {
168 if (_zoomTarget is null)
169 return;
170
171 float target = _zoomTarget.Value;
172 float dt = Math.Max(0f, dtSeconds);
173
174 if (_zoomDurationSeconds is { } durationSeconds)
175 {
176 _zoomElapsedSeconds = Math.Min(_zoomElapsedSeconds + dt, durationSeconds);
177
178 float progress = durationSeconds > 0f ? _zoomElapsedSeconds / durationSeconds : 1f;
179
180 // Preserve the existing exponential ease-out character, but normalize
181 // it so progress reaches exactly 1 at the requested duration.
182 const float decay = 4.605170186f; // -ln(0.01)
183
184 float normalization = 1f - MathF.Exp(-decay);
185 float easedProgress = (1f - MathF.Exp(-decay * progress)) / normalization;
186
187 Zoom = _zoomStart + (target - _zoomStart) * easedProgress;
188
189 if (_zoomElapsedSeconds >= durationSeconds)
190 {
191 // This no longer causes a visible jump: easedProgress is already 1.
192 SnapZoom(target);
193 }
194
195 return;
196 }
197
198 if (_zoomLerpPerSecond <= 0f)
199 return;
200
201 float t = 1f - MathF.Exp(-_zoomLerpPerSecond * dt);
202
203 float newZoom = _zoom + (target - _zoom) * t;
204
205 Zoom = newZoom;
206
207 if (Math.Abs(newZoom - target) < 1e-4f)
208 SnapZoom(target);
209 }
210
211 #endregion zoom zoom
212}
Provides data for the Viewport.TargetRectChanged event, which is raised when a viewport's target rect...
Provides data for the Viewport.ZoomChanged event, which is raised when a viewport's zoom level change...
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
PointF ScreenOffsetPx
Optional per-view HUD/safe-area offset in screen pixels.
Definition Viewport.cs:91
void ZoomToOverDuration(float targetZoom, float durationSeconds)
Smoothly animates the zoom using exponential easing and snaps exactly to the target when the requeste...
Definition Viewport.cs:143
Action< ViewportResizedEventArgs >? TargetRectChanged
Fired whenever TargetRectPx changes (viewport resized or moved). Provides the Viewport instance and t...
Definition Viewport.cs:28
Action< ViewportZoomChangedEventArgs >? ZoomChanged
Occurs when the zoom level of the viewport changes.
Definition Viewport.cs:33
void SnapZoom(float zoom)
Instantly sets the zoom level, raising the ZoomChanged event.
Definition Viewport.cs:105
void ZoomTo(float targetZoom, float lerpPerSecond)
Smoothly animates the zoom toward a target level using an exponential lerp rate in "per second" units...
Definition Viewport.cs:122
void Resize(int width, int height)
Updates the viewport size while preserving its current on-screen origin. This is a convenience around...
Definition Viewport.cs:60
SizeF VisibleWorldSizePx
World size visible through this viewport (used for Camera clamping).
Definition Viewport.cs:94
float Zoom
Zoom factor applied to the world when rendering this view. Values greater than 1 zoom in,...
Definition Viewport.cs:77