11 private Rectangle _targetRectPx =
new Rectangle(
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;
33 public event Action<ViewportZoomChangedEventArgs>?
ZoomChanged;
44 if (value == _targetRectPx)
47 var oldRect = _targetRectPx;
48 _targetRectPx = value;
60 public void Resize(
int width,
int height)
81 if (Math.Abs(value - _zoom) < 1e-6f)
97 internal bool IsZoomAnimating => _zoomTarget is not
null;
108 _zoomLerpPerSecond = 0f;
109 _zoomDurationSeconds =
null;
110 _zoomElapsedSeconds = 0f;
122 public void ZoomTo(
float targetZoom,
float lerpPerSecond)
124 if (lerpPerSecond <= 0f)
131 _zoomTarget = targetZoom;
132 _zoomLerpPerSecond = lerpPerSecond;
133 _zoomDurationSeconds =
null;
134 _zoomElapsedSeconds = 0f;
145 if (durationSeconds <= 0f)
152 _zoomTarget = targetZoom;
154 _zoomDurationSeconds = durationSeconds;
155 _zoomElapsedSeconds = 0f;
158 _zoomLerpPerSecond = 0f;
166 internal void UpdateZoom(
float dtSeconds)
168 if (_zoomTarget is
null)
171 float target = _zoomTarget.Value;
172 float dt = Math.Max(0f, dtSeconds);
174 if (_zoomDurationSeconds is { } durationSeconds)
176 _zoomElapsedSeconds = Math.Min(_zoomElapsedSeconds + dt, durationSeconds);
178 float progress = durationSeconds > 0f ? _zoomElapsedSeconds / durationSeconds : 1f;
182 const float decay = 4.605170186f;
184 float normalization = 1f - MathF.Exp(-decay);
185 float easedProgress = (1f - MathF.Exp(-decay * progress)) / normalization;
187 Zoom = _zoomStart + (target - _zoomStart) * easedProgress;
189 if (_zoomElapsedSeconds >= durationSeconds)
198 if (_zoomLerpPerSecond <= 0f)
201 float t = 1f - MathF.Exp(-_zoomLerpPerSecond * dt);
203 float newZoom = _zoom + (target - _zoom) * t;
207 if (Math.Abs(newZoom - target) < 1e-4f)
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...
Rectangle TargetRectPx
Screen-space rectangle (in SCREEN / RenderSurface pixels) where this view is drawn....
PointF ScreenOffsetPx
Optional per-view HUD/safe-area offset in screen pixels.
void ZoomToOverDuration(float targetZoom, float durationSeconds)
Smoothly animates the zoom using exponential easing and snaps exactly to the target when the requeste...
Action< ViewportResizedEventArgs >? TargetRectChanged
Fired whenever TargetRectPx changes (viewport resized or moved). Provides the Viewport instance and t...
Action< ViewportZoomChangedEventArgs >? ZoomChanged
Occurs when the zoom level of the viewport changes.
void SnapZoom(float zoom)
Instantly sets the zoom level, raising the ZoomChanged event.
void ZoomTo(float targetZoom, float lerpPerSecond)
Smoothly animates the zoom toward a target level using an exponential lerp rate in "per second" units...
void Resize(int width, int height)
Updates the viewport size while preserving its current on-screen origin. This is a convenience around...
SizeF VisibleWorldSizePx
World size visible through this viewport (used for Camera clamping).
float Zoom
Zoom factor applied to the world when rendering this view. Values greater than 1 zoom in,...