18 private const int InnerPadding = 2;
23 private bool _disposed;
24 private float _maximum;
27 private Point _offsetPx;
43 Point? offsetPx =
null,
44 string? nickname =
null)
50 Target = target ??
throw new ArgumentNullException(nameof(target));
53 throw new ArgumentOutOfRangeException(nameof(maximum),
"Maximum health must be greater than zero.");
57 _size = ValidateSize(size ??
new Size(64, 9));
58 _offsetPx = offsetPx ?? Point.Empty;
71 new Rectangle(Point.Empty, _size),
72 $
"{nickname ?? Target.Nickname ?? "sprite
"}-health-track")
92 GetFillBounds(Point.Empty),
93 $
"{nickname ?? Target.Nickname ?? "sprite
"}-health-fill")
100 keepCurrentOffset:
false,
101 explicitLocalOffsetPx: Vector2.Zero);
105 keepCurrentOffset:
false,
106 explicitLocalOffsetPx:
new Vector2(InnerPadding, InnerPadding));
108 Target.SpriteMoved += OnTargetMoved;
109 Target.Disposing += OnTargetDisposing;
129 throw new ArgumentOutOfRangeException(nameof(value),
"Maximum health must be greater than zero.");
132 _value = Math.Clamp(_value, 0f, _maximum);
145 float clamped = Math.Clamp(value, 0f,
Maximum);
147 if (_value.Equals(clamped))
168 Size validated = ValidateSize(value);
170 if (_size == validated)
174 _track.WorldBounds =
new Rectangle(_track.WorldBounds.Location, _size);
221 _fill.SetColor(color);
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;
244 Target.SpriteMoved -= OnTargetMoved;
245 Target.Disposing -= OnTargetDisposing;
253 _fill.Visible =
Value > 0f;
261 private void OnTargetDisposing(
Sprite sprite)
266 private void UpdateFillBounds()
268 Point location = _fill.WorldBounds.Location;
269 Rectangle fillBounds = GetFillBounds(location);
271 _fill.WorldBounds = fillBounds;
272 _fill.Visible =
Value > 0f && _track.Visible;
275 private Rectangle GetFillBounds(Point location)
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);
281 return new Rectangle(location,
new Size(fillWidth, availableHeight));
284 private static Size ValidateSize(
Size value)
286 if (value.Width < InnerPadding * 2 + 1)
287 throw new ArgumentOutOfRangeException(nameof(value),
"Health-bar width is too small.");
289 if (value.Height < InnerPadding * 2 + 1)
290 throw new ArgumentOutOfRangeException(nameof(value),
"Health-bar height is too small.");
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.
Represents a base class for hosting a render surface, providing functionality for managing rendering ...
DirectDrawingMode
Specifies where direct drawing operations are applied within the rendering pipeline.