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
HealthBarWidget.cs
1using System.Drawing;
2using System.Numerics;
6
8
16public sealed class HealthBarWidget : WidgetBase
17{
18 private const int InnerPadding = 2;
19
20 private readonly DirectRectangle _track;
21 private readonly DirectRectangle _fill;
22
23 private bool _disposed;
24 private float _maximum;
25 private float _value;
26 private Size _size;
27 private Point _offsetPx;
28
39 RenderSurfaceHostBase renderSurfaceHost,
40 Sprite target,
41 float maximum,
42 Size? size = null,
43 Point? offsetPx = null,
44 string? nickname = null)
45 : base(
46 renderSurfaceHost,
48 nickname: nickname)
49 {
50 Target = target ?? throw new ArgumentNullException(nameof(target));
51
52 if (maximum <= 0f)
53 throw new ArgumentOutOfRangeException(nameof(maximum), "Maximum health must be greater than zero.");
54
55 _maximum = maximum;
56 _value = maximum;
57 _size = ValidateSize(size ?? new Size(64, 9));
58 _offsetPx = offsetPx ?? Point.Empty;
59
60 IsInputEnabled = false;
62
63 _track = new DirectRectangle(
64 Color.FromArgb(
65 220,
66 20,
67 24,
68 31),
69 renderSurfaceHost,
70 Target.SceneLayer,
71 new Rectangle(Point.Empty, _size),
72 $"{nickname ?? Target.Nickname ?? "sprite"}-health-track")
73 .SetFilled(true)
75 Color.FromArgb(
76 235,
77 235,
78 241,
79 247))
82 .SetCornerRadius(2f);
83
84 _fill = new DirectRectangle(
85 Color.FromArgb(
86 245,
87 55,
88 210,
89 105),
90 renderSurfaceHost,
91 Target.SceneLayer,
92 GetFillBounds(Point.Empty),
93 $"{nickname ?? Target.Nickname ?? "sprite"}-health-fill")
94 .SetFilled(true)
96 .SetCornerRadius(1f);
97
98 Add(
99 _track,
100 keepCurrentOffset: false,
101 explicitLocalOffsetPx: Vector2.Zero);
102
103 Add(
104 _fill,
105 keepCurrentOffset: false,
106 explicitLocalOffsetPx: new Vector2(InnerPadding, InnerPadding));
107
108 Target.SpriteMoved += OnTargetMoved;
109 Target.Disposing += OnTargetDisposing;
110
112 UpdateFillBounds();
113 }
114
118 public Sprite Target { get; }
119
123 public float Maximum
124 {
125 get => _maximum;
126 set
127 {
128 if (value <= 0f)
129 throw new ArgumentOutOfRangeException(nameof(value), "Maximum health must be greater than zero.");
130
131 _maximum = value;
132 _value = Math.Clamp(_value, 0f, _maximum);
133 UpdateFillBounds();
134 }
135 }
136
140 public float Value
141 {
142 get => _value;
143 set
144 {
145 float clamped = Math.Clamp(value, 0f, Maximum);
146
147 if (_value.Equals(clamped))
148 return;
149
150 _value = clamped;
151 UpdateFillBounds();
152 }
153 }
154
158 public float Fraction => Value / Maximum;
159
163 public Size Size
164 {
165 get => _size;
166 set
167 {
168 Size validated = ValidateSize(value);
169
170 if (_size == validated)
171 return;
172
173 _size = validated;
174 _track.WorldBounds = new Rectangle(_track.WorldBounds.Location, _size);
176 UpdateFillBounds();
177 }
178 }
179
183 public Point OffsetPx
184 {
185 get => _offsetPx;
186 set
187 {
188 _offsetPx = value;
190 }
191 }
192
196 public Rectangle TrackBoundsWorld => _track.WorldBounds;
197
201 public Rectangle FillBoundsWorld => _fill.WorldBounds;
202
208 public HealthBarWidget SetValue(float value)
209 {
210 Value = value;
211 return this;
212 }
213
219 public HealthBarWidget SetFillColor(Color color)
220 {
221 _fill.SetColor(color);
222 return this;
223 }
224
228 public void RefreshPosition()
229 {
230 Rectangle targetBounds = Target.DrawLocationWorld;
231 int x = targetBounds.Left + (targetBounds.Width - Size.Width) / 2 + OffsetPx.X;
232 int y = targetBounds.Top - Size.Height - 6 + OffsetPx.Y;
233
234 SetPosition(x, y);
235 }
236
238 public override void Dispose()
239 {
240 if (_disposed)
241 return;
242
243 _disposed = true;
244 Target.SpriteMoved -= OnTargetMoved;
245 Target.Disposing -= OnTargetDisposing;
246 base.Dispose();
247 }
248
250 protected override void ProcessShown()
251 {
252 base.ProcessShown();
253 _fill.Visible = Value > 0f;
254 }
255
256 private void OnTargetMoved(SpriteMovedEventArgs args)
257 {
259 }
260
261 private void OnTargetDisposing(Sprite sprite)
262 {
263 Dispose();
264 }
265
266 private void UpdateFillBounds()
267 {
268 Point location = _fill.WorldBounds.Location;
269 Rectangle fillBounds = GetFillBounds(location);
270
271 _fill.WorldBounds = fillBounds;
272 _fill.Visible = Value > 0f && _track.Visible;
273 }
274
275 private Rectangle GetFillBounds(Point location)
276 {
277 int availableWidth = Math.Max(0, Size.Width - InnerPadding * 2);
278 int availableHeight = Math.Max(1, Size.Height - InnerPadding * 2);
279 int fillWidth = (int)MathF.Round(availableWidth * Fraction);
280
281 return new Rectangle(location, new Size(fillWidth, availableHeight));
282 }
283
284 private static Size ValidateSize(Size value)
285 {
286 if (value.Width < InnerPadding * 2 + 1)
287 throw new ArgumentOutOfRangeException(nameof(value), "Health-bar width is too small.");
288
289 if (value.Height < InnerPadding * 2 + 1)
290 throw new ArgumentOutOfRangeException(nameof(value), "Health-bar height is too small.");
291
292 return value;
293 }
294}
SceneLayer? SceneLayer
Gets the scene layer shared by all children in scene-layer mode.
virtual DirectComposite Add(IDirectCompositeChild child, bool keepCurrentOffset=true, Vector2? explicitLocalOffsetPx=null)
Adds a composite-compatible direct drawable as a child.
void SetPosition(Vector2 position)
Sets the composite anchor and repositions all children from their stored offsets.
Renders a configurable rectangle with support for fill, border, rounded corners, dash patterns,...
DirectRectangle SetFilled(bool isFilled)
Sets whether the rectangle interior is filled with the base color (or pattern).
DirectRectangle SetBorderColor(Color color)
Sets a distinct border (stroke) color, independent of the fill color.
StrokeAlign
Defines how the stroke (outline) is positioned relative to the rectangle's boundary.
DirectRectangle SetStrokeWidth(float width)
Sets the width (thickness) of the rectangle's stroke (outline) in pixels.
DirectRectangle SetCornerRadius(float radius)
Sets the radius for rounded corners in pixels.
DirectRectangle SetStrokeAlign(StrokeAlign align)
Sets how the rectangle's stroke is positioned relative to its bounds.
Provides data for sprite movement events.
Represents a movable visual element on a scene layer.
Definition Sprite.cs:21
Represents a base class for hosting a render surface, providing functionality for managing rendering ...
float Maximum
Gets or sets the maximum health value.
float Fraction
Gets the filled fraction in the range zero through one.
Point OffsetPx
Gets or sets an additional world-pixel offset from the centered position above the target.
Size Size
Gets or sets the outer bar size in world pixels.
Rectangle FillBoundsWorld
Gets the current world bounds of the filled portion.
override void ProcessShown()
Performs processing when the widget is shown before notification callbacks are invoked.
HealthBarWidget(RenderSurfaceHostBase renderSurfaceHost, Sprite target, float maximum, Size? size=null, Point? offsetPx=null, string? nickname=null)
Initializes a health bar that follows target .
Sprite Target
Gets the sprite followed by this health bar.
HealthBarWidget SetFillColor(Color color)
Sets the fill color and returns this widget.
Rectangle TrackBoundsWorld
Gets the current world bounds of the health-bar track.
override void Dispose()
Disposes the composite and every child it owns.
HealthBarWidget SetValue(float value)
Sets the current health and returns this widget for fluent setup.
float Value
Gets or sets the current health value. Values are clamped to zero through Maximum.
void RefreshPosition()
Repositions the bar from the target's current render bounds.
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 IsInputEnabled
Gets or sets a value indicating whether the widget can participate in input routing.
DirectDrawingMode
Specifies where direct drawing operations are applied within the rendering pipeline.