Gondwana Game Engine Development (master) - 2.6.1
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
30 public ProgressBarWidget(RenderSurfaceHostBase renderSurfaceHost,
31 View view,
32 Rectangle bounds,
33 float value = 0f,
34 WidgetOrientation orientation = WidgetOrientation.Horizontal,
35 string? nickname = null)
36 : base(renderSurfaceHost, DirectDrawingMode.View, ValidateBounds(bounds).Location, nickname)
37 {
38 ArgumentNullException.ThrowIfNull(view);
39
40 _size = bounds.Size;
41 _orientation = orientation;
42
43 Track = CreateTrack(renderSurfaceHost, view, bounds);
44 Fill = CreateFill(renderSurfaceHost, view, bounds);
45 CompleteInitialization(value);
46 }
47
57 public ProgressBarWidget(RenderSurfaceHostBase renderSurfaceHost,
58 SceneLayer sceneLayer,
59 Rectangle bounds,
60 float value = 0f,
61 WidgetOrientation orientation = WidgetOrientation.Horizontal,
62 string? nickname = null)
63 : base(renderSurfaceHost, DirectDrawingMode.SceneLayer, ValidateBounds(bounds).Location, nickname)
64 {
65 ArgumentNullException.ThrowIfNull(sceneLayer);
66
67 _size = bounds.Size;
68 _orientation = orientation;
69
70 Track = CreateTrack(renderSurfaceHost, sceneLayer, bounds);
71 Fill = CreateFill(renderSurfaceHost, sceneLayer, bounds);
72 CompleteInitialization(value);
73 }
74
78 public DirectRectangle Track { get; }
79
83 public DirectRectangle Fill { get; }
84
88 public float Value
89 {
90 get => _value;
91 set
92 {
93 if (!float.IsFinite(value))
94 throw new ArgumentOutOfRangeException(nameof(value));
95
96 float clamped = Math.Clamp(value, 0f, 1f);
97 if (_value.Equals(clamped))
98 return;
99
100 _value = clamped;
101 UpdateFillBounds();
102 }
103 }
104
109 {
110 get => _orientation;
111 set
112 {
113 if (_orientation == value)
114 return;
115
116 _orientation = value;
117 UpdateFillBounds();
118 }
119 }
120
124 public int Padding
125 {
126 get => _padding;
127 set
128 {
129 if (value < 0)
130 throw new ArgumentOutOfRangeException(nameof(value));
131
132 _padding = value;
133 UpdateFillBounds();
134 }
135 }
136
140 public Size Size
141 {
142 get => _size;
143 set
144 {
145 ValidateSize(value);
146 _size = value;
147
148 if (Mode == DirectDrawingMode.View)
149 Track.ScreenBounds = new Rectangle(Track.ScreenBounds.Location, value);
150 else
151 Track.WorldBounds = new Rectangle(Track.WorldBounds.Location, value);
152
153 UpdateFillBounds();
154 }
155 }
156
161 ? Track.ScreenBounds
162 : Track.WorldBounds;
163
168 ? Fill.ScreenBounds
169 : Fill.WorldBounds;
170
176 public ProgressBarWidget SetValue(float value)
177 {
178 Value = value;
179 return this;
180 }
181
187 public ProgressBarWidget SetFillColor(Color color)
188 {
189 Fill.SetColor(color);
190 Fill.SetPosition(Fill.GetPosition());
191 return this;
192 }
193
200 {
201 Track.SetColor(color);
202 Track.SetPosition(Track.GetPosition());
203 return this;
204 }
205
212 {
213 Track.ZOrder = zOrder;
214 Fill.ZOrder = zOrder + 1;
215 return this;
216 }
217
219 protected override void ProcessShown()
220 {
221 base.ProcessShown();
222 Fill.Visible = Value > 0f;
223 }
224
225 private void CompleteInitialization(float value)
226 {
227 Add(Track, keepCurrentOffset: false, explicitLocalOffsetPx: Vector2.Zero);
228 Add(Fill, keepCurrentOffset: false, explicitLocalOffsetPx: new Vector2(_padding, _padding));
229
230 IsInputEnabled = false;
231 IsPointerInputEnabled = false;
233 CanReceiveFocus = false;
234
236 _value = Math.Clamp(float.IsFinite(value) ? value : throw new ArgumentOutOfRangeException(nameof(value)), 0f, 1f);
237 UpdateFillBounds();
238 }
239
240 private void UpdateFillBounds()
241 {
242 int innerWidth = Math.Max(0, _size.Width - _padding * 2);
243 int innerHeight = Math.Max(0, _size.Height - _padding * 2);
244
245 Size fillSize;
246 Vector2 offset;
247
248 if (_orientation == WidgetOrientation.Horizontal)
249 {
250 int fillWidth = (int)MathF.Round(innerWidth * _value);
251 fillSize = new Size(fillWidth, innerHeight);
252 offset = new Vector2(_padding, _padding);
253 }
254 else
255 {
256 int fillHeight = (int)MathF.Round(innerHeight * _value);
257 fillSize = new Size(innerWidth, fillHeight);
258 offset = new Vector2(_padding, _padding + innerHeight - fillHeight);
259 }
260
261 Rectangle current = FillBounds;
262 Rectangle resized = new(current.Location, fillSize);
263
264 if (Mode == DirectDrawingMode.View)
265 Fill.ScreenBounds = resized;
266 else
267 Fill.WorldBounds = resized;
268
269 SetLocalOffset(Fill, offset);
270 Fill.Visible = _value > 0f && Track.Visible;
271 }
272
273 private DirectRectangle CreateTrack(RenderSurfaceHostBase host, View view, Rectangle bounds)
274 {
275 return new DirectRectangle(Color.FromArgb(220, 28, 30, 36), host, view, bounds, $"{Nickname}.track")
276 .SetFilled(true)
277 .SetBorderColor(Color.FromArgb(235, 225, 228, 234))
278 .SetStrokeWidth(1f)
279 .SetStrokeAlign(DirectRectangle.StrokeAlign.Inside)
280 .SetCornerRadius(2f);
281 }
282
283 private DirectRectangle CreateTrack(RenderSurfaceHostBase host, SceneLayer layer, Rectangle bounds)
284 {
285 return new DirectRectangle(Color.FromArgb(220, 28, 30, 36), host, layer, bounds, $"{Nickname}.track")
286 .SetFilled(true)
287 .SetBorderColor(Color.FromArgb(235, 225, 228, 234))
288 .SetStrokeWidth(1f)
289 .SetStrokeAlign(DirectRectangle.StrokeAlign.Inside)
290 .SetCornerRadius(2f);
291 }
292
293 private DirectRectangle CreateFill(RenderSurfaceHostBase host, View view, Rectangle bounds)
294 {
295 return new DirectRectangle(Color.FromArgb(245, 55, 210, 105), host, view, bounds, $"{Nickname}.fill")
296 .SetFilled(true)
297 .SetStrokeWidth(0f)
298 .SetCornerRadius(1f);
299 }
300
301 private DirectRectangle CreateFill(RenderSurfaceHostBase host, SceneLayer layer, Rectangle bounds)
302 {
303 return new DirectRectangle(Color.FromArgb(245, 55, 210, 105), host, layer, bounds, $"{Nickname}.fill")
304 .SetFilled(true)
305 .SetStrokeWidth(0f)
306 .SetCornerRadius(1f);
307 }
308
309 private static Rectangle ValidateBounds(Rectangle bounds)
310 {
311 ValidateSize(bounds.Size);
312 return bounds;
313 }
314
315 private static void ValidateSize(Size size)
316 {
317 if (size.Width <= 0 || size.Height <= 0)
318 throw new ArgumentOutOfRangeException(nameof(size), size, "Progress-bar size must be positive.");
319 }
320}
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.