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
ProgressBarWidget.cs
1using System.Drawing;
2using System.Numerics;
8
10
14public sealed class ProgressBarWidget : WidgetBase
15{
16 private float _value;
17 private WidgetOrientation _orientation;
18 private int _padding = 2;
19 private Size _size;
20
31 RenderSurfaceHostBase renderSurfaceHost,
32 View view,
33 Rectangle bounds,
34 float value = 0f,
35 WidgetOrientation orientation = WidgetOrientation.Horizontal,
36 string? nickname = null)
37 : base(
38 renderSurfaceHost,
40 ValidateBounds(bounds).Location,
41 nickname)
42 {
43 ArgumentNullException.ThrowIfNull(view);
44
45 _size = bounds.Size;
46 _orientation = orientation;
47
48 Track = CreateTrack(renderSurfaceHost, view, bounds);
49 Fill = CreateFill(renderSurfaceHost, view, bounds);
50 CompleteInitialization(value);
51 }
52
63 RenderSurfaceHostBase renderSurfaceHost,
64 SceneLayer sceneLayer,
65 Rectangle bounds,
66 float value = 0f,
67 WidgetOrientation orientation = WidgetOrientation.Horizontal,
68 string? nickname = null)
69 : base(
70 renderSurfaceHost,
72 ValidateBounds(bounds).Location,
73 nickname)
74 {
75 ArgumentNullException.ThrowIfNull(sceneLayer);
76
77 _size = bounds.Size;
78 _orientation = orientation;
79
80 Track = CreateTrack(renderSurfaceHost, sceneLayer, bounds);
81 Fill = CreateFill(renderSurfaceHost, sceneLayer, bounds);
82 CompleteInitialization(value);
83 }
84
88 public DirectRectangle Track { get; }
89
93 public DirectRectangle Fill { get; }
94
98 public float Value
99 {
100 get => _value;
101 set
102 {
103 if (!float.IsFinite(value))
104 throw new ArgumentOutOfRangeException(nameof(value));
105
106 float clamped = Math.Clamp(value, 0f, 1f);
107
108 if (_value.Equals(clamped))
109 return;
110
111 _value = clamped;
112 UpdateFillBounds();
113 }
114 }
115
120 {
121 get => _orientation;
122 set
123 {
124 if (_orientation == value)
125 return;
126
127 _orientation = value;
128 UpdateFillBounds();
129 }
130 }
131
135 public int Padding
136 {
137 get => _padding;
138 set
139 {
140 if (value < 0)
141 throw new ArgumentOutOfRangeException(nameof(value));
142
143 _padding = value;
144 UpdateFillBounds();
145 }
146 }
147
151 public Size Size
152 {
153 get => _size;
154 set
155 {
156 ValidateSize(value);
157 _size = value;
158
159 if (Mode == DirectDrawingMode.View)
160 Track.ScreenBounds = new Rectangle(Track.ScreenBounds.Location, value);
161 else
162 Track.WorldBounds = new Rectangle(Track.WorldBounds.Location, value);
163
164 UpdateFillBounds();
165 }
166 }
167
172 ? Track.ScreenBounds
173 : Track.WorldBounds;
174
179 ? Fill.ScreenBounds
180 : Fill.WorldBounds;
181
187 public ProgressBarWidget SetValue(float value)
188 {
189 Value = value;
190 return this;
191 }
192
198 public ProgressBarWidget SetFillColor(Color color)
199 {
200 Fill.SetColor(color);
201 Fill.SetPosition(Fill.GetPosition());
202 return this;
203 }
204
211 {
212 Track.SetColor(color);
213 Track.SetPosition(Track.GetPosition());
214 return this;
215 }
216
223 {
224 Track.ZOrder = zOrder;
225 Fill.ZOrder = zOrder + 1;
226 return this;
227 }
228
230 protected override void ProcessShown()
231 {
232 base.ProcessShown();
233 Fill.Visible = Value > 0f;
234 }
235
236 private void CompleteInitialization(float value)
237 {
238 Add(Track, keepCurrentOffset: false, explicitLocalOffsetPx: Vector2.Zero);
239 Add(Fill, keepCurrentOffset: false, explicitLocalOffsetPx: new Vector2(_padding, _padding));
240
241 IsInputEnabled = false;
242 IsPointerInputEnabled = false;
244 CanReceiveFocus = false;
245
247
248 _value = Math.Clamp(
249 float.IsFinite(value) ? value : throw new ArgumentOutOfRangeException(nameof(value)),
250 0f,
251 1f);
252
253 UpdateFillBounds();
254 }
255
256 private void UpdateFillBounds()
257 {
258 int innerWidth = Math.Max(0, _size.Width - _padding * 2);
259 int innerHeight = Math.Max(0, _size.Height - _padding * 2);
260
261 Size fillSize;
262 Vector2 offset;
263
264 if (_orientation == WidgetOrientation.Horizontal)
265 {
266 int fillWidth = (int)MathF.Round(innerWidth * _value);
267 fillSize = new Size(fillWidth, innerHeight);
268 offset = new Vector2(_padding, _padding);
269 }
270 else
271 {
272 int fillHeight = (int)MathF.Round(innerHeight * _value);
273 fillSize = new Size(innerWidth, fillHeight);
274 offset = new Vector2(_padding, _padding + innerHeight - fillHeight);
275 }
276
277 Rectangle current = FillBounds;
278 Rectangle resized = new(current.Location, fillSize);
279
280 if (Mode == DirectDrawingMode.View)
281 Fill.ScreenBounds = resized;
282 else
283 Fill.WorldBounds = resized;
284
285 SetLocalOffset(Fill, offset);
286 Fill.Visible = _value > 0f && Track.Visible;
287 }
288
289 private DirectRectangle CreateTrack(RenderSurfaceHostBase host, View view, Rectangle bounds)
290 {
291 return new DirectRectangle(
292 Color.FromArgb(
293 220,
294 28,
295 30,
296 36),
297 host,
298 view,
299 bounds,
300 $"{Nickname}.track")
301 .SetFilled(true)
302 .SetBorderColor(
303 Color.FromArgb(
304 235,
305 225,
306 228,
307 234))
308 .SetStrokeWidth(1f)
309 .SetStrokeAlign(DirectRectangle.StrokeAlign.Inside)
310 .SetCornerRadius(2f);
311 }
312
313 private DirectRectangle CreateTrack(RenderSurfaceHostBase host, SceneLayer layer, Rectangle bounds)
314 {
315 return new DirectRectangle(
316 Color.FromArgb(
317 220,
318 28,
319 30,
320 36),
321 host,
322 layer,
323 bounds,
324 $"{Nickname}.track")
325 .SetFilled(true)
326 .SetBorderColor(
327 Color.FromArgb(
328 235,
329 225,
330 228,
331 234))
332 .SetStrokeWidth(1f)
333 .SetStrokeAlign(DirectRectangle.StrokeAlign.Inside)
334 .SetCornerRadius(2f);
335 }
336
337 private DirectRectangle CreateFill(RenderSurfaceHostBase host, View view, Rectangle bounds)
338 {
339 return new DirectRectangle(
340 Color.FromArgb(
341 245,
342 55,
343 210,
344 105),
345 host,
346 view,
347 bounds,
348 $"{Nickname}.fill")
349 .SetFilled(true)
350 .SetStrokeWidth(0f)
351 .SetCornerRadius(1f);
352 }
353
354 private DirectRectangle CreateFill(RenderSurfaceHostBase host, SceneLayer layer, Rectangle bounds)
355 {
356 return new DirectRectangle(
357 Color.FromArgb(
358 245,
359 55,
360 210,
361 105),
362 host,
363 layer,
364 bounds,
365 $"{Nickname}.fill")
366 .SetFilled(true)
367 .SetStrokeWidth(0f)
368 .SetCornerRadius(1f);
369 }
370
371 private static Rectangle ValidateBounds(Rectangle bounds)
372 {
373 ValidateSize(bounds.Size);
374 return bounds;
375 }
376
377 private static void ValidateSize(Size size)
378 {
379 if (size.Width <= 0 || size.Height <= 0)
380 throw new ArgumentOutOfRangeException(nameof(size), size, "Progress-bar size must be positive.");
381 }
382}
SceneLayer? SceneLayer
Gets the scene layer shared by all children in scene-layer mode.
DirectComposite SetLocalOffset(IDirectCompositeChild child, Vector2 newLocalOffsetPx)
Changes one child's local offset and reapplies its position.
View? View
Gets the view shared by all children in view mode.
virtual DirectComposite Add(IDirectCompositeChild child, bool keepCurrentOffset=true, Vector2? explicitLocalOffsetPx=null)
Adds a composite-compatible direct drawable as a child.
DirectDrawingMode Mode
Gets the coordinate mode shared by the composite and its children.
bool Visible
Gets or sets a value indicating whether this direct drawing is visible and should be rendered.
Renders a configurable rectangle with support for fill, border, rounded corners, dash patterns,...
Represents a base class for hosting a render surface, providing functionality for managing rendering ...
ProgressBarWidget(RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle bounds, float value=0f, WidgetOrientation orientation=WidgetOrientation.Horizontal, string? nickname=null)
Creates a scene-layer progress bar.
DirectRectangle Track
Gets the background/track drawing.
float Value
Gets or sets the normalized progress value in the range 0 through 1.
ProgressBarWidget SetFillColor(Color color)
Sets the fill color.
Size Size
Gets or sets the outer progress-bar size.
ProgressBarWidget SetTrackColor(Color color)
Sets the track color.
int Padding
Gets or sets the inner padding between the track and fill.
Rectangle FillBounds
Gets the fill bounds in the widget's native coordinate space.
ProgressBarWidget(RenderSurfaceHostBase renderSurfaceHost, View view, Rectangle bounds, float value=0f, WidgetOrientation orientation=WidgetOrientation.Horizontal, string? nickname=null)
Creates a view-level progress bar.
ProgressBarWidget SetValue(float value)
Sets the progress value and returns this widget for fluent setup.
Rectangle TrackBounds
Gets the track bounds in the widget's native coordinate space.
WidgetOrientation Orientation
Gets or sets the fill orientation. Vertical bars fill from bottom to top.
DirectRectangle Fill
Gets the foreground/fill drawing.
override void ProcessShown()
Performs processing when the widget is shown before notification callbacks are invoked.
ProgressBarWidget SetProgressZOrder(int zOrder)
Places the track at the requested Z-order and the fill directly above it.
bool CanReceiveFocus
Gets or sets a value indicating whether the widget can receive keyboard focus.
bool IsPointerInputEnabled
Gets or sets a value indicating whether the widget can receive pointer input.
WidgetBase(RenderSurfaceHostBase renderSurfaceHost, DirectDrawingMode mode, PointF anchor=default, string? nickname=null)
Initializes a new instance of the WidgetBase class.
Definition WidgetBase.cs:95
bool IsKeyboardInputEnabled
Gets or sets a value indicating whether the widget can receive keyboard input.
bool IsInputEnabled
Gets or sets a value indicating whether the widget can participate in input routing.
@ Rectangle
Particles are spawned uniformly within a rectangular area. This is the default distribution.
DirectDrawingMode
Specifies where direct drawing operations are applied within the rendering pipeline.
@ View
Performs direct drawing at the view level using screen-space coordinates.
@ Fill
Stretches the video to fill the entire bounds, ignoring the aspect ratio.
WidgetOrientation
Defines the primary layout or fill direction used by widgets.