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
Sprite.Resize.cs
1using System.Drawing;
3
5
9public partial class Sprite
10{
14 public event Action? ResizeComplete;
15
16 private bool _isResizing;
17 private float _resizeElapsedSeconds;
18 private float _resizeDurationSeconds;
19 private Size _resizeStart;
20 private Size _resizeTarget;
21
22 private bool _resizeReturnToStart;
23 private float _resizeReturnDurationSeconds;
24 private Size _resizeOriginalStart;
25 private Size _resizePeak;
26
27 private bool _isPulseMode;
28 private bool _pulseLoop;
29 private bool _pulseForward;
30 private float _pulseGrowDurationSeconds;
31 private float _pulseShrinkDurationSeconds;
32
33 private CollisionAdjust _resizeStartCollisionAdjust;
34 private Size _resizeStartSizeForCollision;
35
36 internal void AdvanceResize(float deltaSeconds)
37 {
38 if (!_isResizing)
39 return;
40
41 if (_resizeDurationSeconds <= 0f)
42 {
43 RenderSize = _resizeTarget;
44 ApplyScaledCollisionAdjust(RenderSize);
45 CompleteResizeLeg();
46 return;
47 }
48
49 _resizeElapsedSeconds += deltaSeconds;
50
51 float t = _resizeElapsedSeconds / _resizeDurationSeconds;
52
53 if (t >= 1f)
54 t = 1f;
55
56 int w = (int)MathF.Round(
57 _resizeStart.Width + ((_resizeTarget.Width - _resizeStart.Width) * t));
58
59 int h = (int)MathF.Round(
60 _resizeStart.Height + ((_resizeTarget.Height - _resizeStart.Height) * t));
61
62 var next = new Size(Math.Max(1, w), Math.Max(1, h));
63
64 if (next != _renderSize)
65 {
66 RenderSize = next;
67 ApplyScaledCollisionAdjust(next);
68 }
69
70 if (t >= 1f)
71 CompleteResizeLeg();
72 }
73
74 private void CompleteResizeLeg()
75 {
76 RenderSize = _resizeTarget;
77 ApplyScaledCollisionAdjust(RenderSize);
78
79 if (_isPulseMode)
80 {
81 if (_pulseForward)
82 {
83 _pulseForward = false;
84
85 StartResizeLeg(
86 start: _resizePeak,
87 target: _resizeOriginalStart,
88 durationSeconds: _pulseShrinkDurationSeconds,
89 returnToStart: false,
90 returnDurationSeconds: 0f);
91
92 return;
93 }
94
95 if (_pulseLoop)
96 {
97 _pulseForward = true;
98
99 StartResizeLeg(
100 start: _resizeOriginalStart,
101 target: _resizePeak,
102 durationSeconds: _pulseGrowDurationSeconds,
103 returnToStart: false,
104 returnDurationSeconds: 0f);
105
106 return;
107 }
108
109 _isPulseMode = false;
110 _isResizing = false;
111 ResizeComplete?.Invoke();
112 return;
113 }
114
115 if (_resizeReturnToStart)
116 {
117 StartResizeLeg(
118 start: RenderSize,
119 target: _resizeOriginalStart,
120 durationSeconds: _resizeReturnDurationSeconds,
121 returnToStart: false,
122 returnDurationSeconds: 0f);
123 }
124 else
125 {
126 _isResizing = false;
127 ResizeComplete?.Invoke();
128 }
129 }
130
131 private void StartResizeLeg(
132 Size start,
133 Size target,
134 float durationSeconds,
135 bool returnToStart,
136 float returnDurationSeconds)
137 {
138 _resizeStart = start;
139 _resizeTarget = target;
140 _resizeDurationSeconds = Math.Max(0f, durationSeconds);
141 _resizeElapsedSeconds = 0f;
142 _isResizing = true;
143
144 _resizeReturnToStart = returnToStart;
145 _resizeReturnDurationSeconds = Math.Max(0f, returnDurationSeconds);
146
147 _resizeStartCollisionAdjust = AdjustCollisionArea;
148 _resizeStartSizeForCollision = start;
149 }
150
151 private void ApplyScaledCollisionAdjust(Size currentSize)
152 {
153 if (_resizeStartSizeForCollision.Width <= 0 ||
154 _resizeStartSizeForCollision.Height <= 0)
155 {
156 return;
157 }
158
159 float sx = (float)currentSize.Width / _resizeStartSizeForCollision.Width;
160 float sy = (float)currentSize.Height / _resizeStartSizeForCollision.Height;
161
162 AdjustCollisionArea = new CollisionAdjust(
163 top: (int)MathF.Round(_resizeStartCollisionAdjust.Top * sy),
164 bottom: (int)MathF.Round(_resizeStartCollisionAdjust.Bottom * sy),
165 left: (int)MathF.Round(_resizeStartCollisionAdjust.Left * sx),
166 right: (int)MathF.Round(_resizeStartCollisionAdjust.Right * sx));
167 }
168
174 public void ResizeTo(Size targetSize, float durationSeconds)
175 {
176 targetSize = new Size(
177 Math.Max(1, targetSize.Width),
178 Math.Max(1, targetSize.Height));
179
180 _isPulseMode = false;
181 _pulseLoop = false;
182 _resizeOriginalStart = RenderSize;
183
184 StartResizeLeg(
185 start: RenderSize,
186 target: targetSize,
187 durationSeconds: durationSeconds,
188 returnToStart: false,
189 returnDurationSeconds: 0f);
190 }
191
197 public void ScaleBy(float factor, float durationSeconds)
198 {
199 factor = MathF.Max(0.01f, factor);
200
201 int w = (int)MathF.Round(RenderSize.Width * factor);
202 int h = (int)MathF.Round(RenderSize.Height * factor);
203
204 ResizeTo(
205 new Size(Math.Max(1, w), Math.Max(1, h)),
206 durationSeconds);
207 }
208
216 public void PulseTo(
217 Size targetSize,
218 float growDurationSeconds,
219 float shrinkDurationSeconds,
220 bool loop = false)
221 {
222 targetSize = new Size(
223 Math.Max(1, targetSize.Width),
224 Math.Max(1, targetSize.Height));
225
226 _resizeOriginalStart = RenderSize;
227 _resizePeak = targetSize;
228
229 _isPulseMode = true;
230 _pulseLoop = loop;
231 _pulseForward = true;
232 _pulseGrowDurationSeconds = Math.Max(0f, growDurationSeconds);
233 _pulseShrinkDurationSeconds = Math.Max(0f, shrinkDurationSeconds);
234
235 StartResizeLeg(
236 start: _resizeOriginalStart,
237 target: _resizePeak,
238 durationSeconds: _pulseGrowDurationSeconds,
239 returnToStart: false,
240 returnDurationSeconds: 0f);
241 }
242
250 public void PulseBy(
251 float factor,
252 float growDurationSeconds,
253 float shrinkDurationSeconds,
254 bool loop = false)
255 {
256 factor = MathF.Max(0.01f, factor);
257
258 int w = (int)MathF.Round(RenderSize.Width * factor);
259 int h = (int)MathF.Round(RenderSize.Height * factor);
260
261 PulseTo(
262 new Size(Math.Max(1, w), Math.Max(1, h)),
263 growDurationSeconds,
264 shrinkDurationSeconds,
265 loop);
266 }
267
273 public void StopPulse(bool snapBack = true, float returnDuration = 0f)
274 {
275 Size originalSize = _resizeOriginalStart;
276 var originalCollisionAdjust = _resizeStartCollisionAdjust;
277
278 CancelResize();
279
280 if (!snapBack)
281 return;
282
283 if (returnDuration <= 0f)
284 {
285 RenderSize = originalSize;
286 AdjustCollisionArea = originalCollisionAdjust;
287 }
288 else
289 {
290 ResizeTo(originalSize, returnDuration);
291 }
292 }
293
297 public void CancelResize()
298 {
299 _isResizing = false;
300 _resizeReturnToStart = false;
301 _isPulseMode = false;
302 _pulseLoop = false;
303 _pulseForward = false;
304
305 ResizeComplete?.Invoke();
306 }
307}
Represents a movable visual element on a scene layer.
Definition Sprite.cs:21
void PulseBy(float factor, float growDurationSeconds, float shrinkDurationSeconds, bool loop=false)
Pulses by a factor relative to the current render size.
void ResizeTo(Size targetSize, float durationSeconds)
Smoothly resizes the sprite to an absolute pixel size.
Size RenderSize
Gets or sets the render size.
Definition Sprite.cs:572
void ScaleBy(float factor, float durationSeconds)
Scales the sprite by a factor relative to its current render size.
void PulseTo(Size targetSize, float growDurationSeconds, float shrinkDurationSeconds, bool loop=false)
Resizes to an absolute target size and then returns to the original size.
void CancelResize()
Cancels any in-progress resize or pulse.
void StopPulse(bool snapBack=true, float returnDuration=0f)
Stops the current pulse, optionally returning to the original size.
Action? ResizeComplete
Raised when a resize or pulse animation completes its current leg, or when cancelled.
virtual CollisionAdjust AdjustCollisionArea
Gets or sets the collision adjustment used to derive CollisionArea.
Definition Tile.cs:295
Per-edge inset amounts applied to visual bounds to produce a collision rectangle.