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
NameTagWidget.cs
1using System.Drawing;
2using System.Numerics;
6using SkiaSharp;
7
9
13public sealed class NameTagWidget : WidgetBase
14{
15 private bool _disposed;
16 private bool _backgroundVisible = true;
17 private Size _size;
18 private Point _offsetPx;
19 private string _text;
20
30 public NameTagWidget(RenderSurfaceHostBase renderSurfaceHost,
31 Sprite target,
32 string text,
33 Size? size = null,
34 Point? offsetPx = null,
35 string? nickname = null)
36 : base(renderSurfaceHost, DirectDrawingMode.SceneLayer, nickname: nickname)
37 {
38 Target = target ?? throw new ArgumentNullException(nameof(target));
39 _text = text ?? string.Empty;
40 _size = ValidateSize(size ?? new Size(128, 26));
41 _offsetPx = offsetPx ?? Point.Empty;
42
44 Color.FromArgb(185, 20, 22, 28),
45 renderSurfaceHost,
46 Target.SceneLayer,
47 new Rectangle(Point.Empty, _size),
48 $"{Nickname}-name-tag-background")
49 .SetFilled(true)
50 .SetBorderColor(Color.FromArgb(200, 220, 220, 230))
52 .SetCornerRadius(4f);
53
54 TextBlock = new TextBlock(
55 renderSurfaceHost,
56 Target.SceneLayer,
57 view: null,
58 worldBounds: new Rectangle(Point.Empty, _size),
59 nickname: $"{Nickname}-name-tag-text")
60 .SetText(_text)
61 .SetFont(SKTypeface.Default, 15f, minSize: 9f)
62 .SetColors(SKColors.White, SKColors.Transparent)
63 .SetAlignment(SKTextAlign.Center, TextBlock.VerticalAlign.Center)
64 .SetPadding(5f, 2f)
65 .EnableWrapping(false);
66
67 Add(Background, keepCurrentOffset: false, explicitLocalOffsetPx: Vector2.Zero);
68 Add(TextBlock, keepCurrentOffset: false, explicitLocalOffsetPx: Vector2.Zero);
69
70 IsInputEnabled = false;
73 CanReceiveFocus = false;
74
76
77 Target.SpriteMoved += OnTargetMoved;
78 Target.VisualBoundsChanged += OnTargetVisualBoundsChanged;
79 Target.Disposing += OnTargetDisposing;
80
82 }
83
87 public Sprite Target { get; }
88
93
97 public TextBlock TextBlock { get; }
98
102 public string Text => _text;
103
107 public Size Size
108 {
109 get => _size;
110 set
111 {
112 Size validated = ValidateSize(value);
113 if (_size == validated)
114 return;
115
116 _size = validated;
117 Background.WorldBounds = new Rectangle(Background.WorldBounds.Location, _size);
118 TextBlock.SetSize(_size);
120 }
121 }
122
126 public Point OffsetPx
127 {
128 get => _offsetPx;
129 set
130 {
131 _offsetPx = value;
133 }
134 }
135
139 public Rectangle BoundsWorld => Background.WorldBounds;
140
146 public NameTagWidget SetText(string text)
147 {
148 _text = text ?? string.Empty;
149 TextBlock.SetText(_text);
150 return this;
151 }
152
158 public NameTagWidget SetTextColor(SKColor color)
159 {
160 TextBlock.SetColors(color, SKColors.Transparent);
161 return this;
162 }
163
170 {
171 Background.SetColor(color);
172 Background.SetPosition(Background.GetPosition());
173 return this;
174 }
175
181 public NameTagWidget ShowBackground(bool visible = true)
182 {
183 _backgroundVisible = visible;
184 Background.Visible = visible;
185 return this;
186 }
187
194 {
195 Background.ZOrder = zOrder;
196 TextBlock.ZOrder = zOrder + 1;
197 return this;
198 }
199
203 public void RefreshPosition()
204 {
205 Rectangle targetBounds = Target.DrawLocationWorld;
206 int x = targetBounds.Left + (targetBounds.Width - Size.Width) / 2 + OffsetPx.X;
207 int y = targetBounds.Top - Size.Height - 6 + OffsetPx.Y;
208 SetPosition(x, y);
209 }
210
212 public override void Dispose()
213 {
214 if (_disposed)
215 return;
216
217 _disposed = true;
218 Target.SpriteMoved -= OnTargetMoved;
219 Target.VisualBoundsChanged -= OnTargetVisualBoundsChanged;
220 Target.Disposing -= OnTargetDisposing;
221 base.Dispose();
222 }
223
225 protected override void ProcessShown()
226 {
227 base.ProcessShown();
228 Background.Visible = _backgroundVisible;
229 }
230
231 private void OnTargetMoved(SpriteMovedEventArgs args)
232 {
234 }
235
236 private void OnTargetVisualBoundsChanged(Sprite sprite)
237 {
239 }
240
241 private void OnTargetDisposing(Sprite sprite)
242 {
243 Dispose();
244 }
245
246 private static Size ValidateSize(Size size)
247 {
248 if (size.Width <= 0 || size.Height <= 0)
249 throw new ArgumentOutOfRangeException(nameof(size), size, "Name-tag size must be positive.");
250
251 return size;
252 }
253}
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.
int ZOrder
Gets or sets the Z-order (depth) used for sorting this direct drawing relative to other drawables.
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.
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.
TextBlock SetFont(SKTypeface typeface, float size, float? minSize=null)
Sets the font face and size (and optional minimum size for auto-shrink).
Definition TextBlock.cs:394
TextBlock EnableWrapping(bool enable=true)
Enables or disables word wrapping when laying out paragraphs.
Definition TextBlock.cs:503
TextBlock SetAlignment(SKTextAlign h, VerticalAlign v)
Sets the horizontal and vertical alignment used when drawing the laid-out lines within the bounds.
Definition TextBlock.cs:433
TextBlock SetPadding(float horizontal, float vertical)
Sets symmetric horizontal and vertical padding and invalidates cached layout.
Definition TextBlock.cs:317
TextBlock SetColors(SKColor fg, SKColor bg)
Sets the foreground (text) and background colors using Skia colors.
Definition TextBlock.cs:410
TextBlock SetText(string text)
Sets the current text content and rebuilds layout as needed. If no reveal animation is active,...
Definition TextBlock.cs:358
Provides data for sprite movement events.
Represents a movable visual element on a scene layer.
Definition Sprite.cs:20
Represents a base class for hosting a render surface, providing functionality for managing rendering ...
NameTagWidget SetNameTagZOrder(int zOrder)
Sets the base Z-order used by the name-tag visuals.
override void ProcessShown()
Performs processing when the widget is shown before notification callbacks are invoked.
Rectangle BoundsWorld
Gets the current world bounds of the name tag.
NameTagWidget SetBackgroundColor(Color color)
Sets the background color.
NameTagWidget(RenderSurfaceHostBase renderSurfaceHost, Sprite target, string text, Size? size=null, Point? offsetPx=null, string? nickname=null)
Creates a name tag that follows target .
NameTagWidget SetText(string text)
Changes the displayed name.
Size Size
Gets or sets the name-tag size in world pixels.
override void Dispose()
Disposes the composite and every child it owns.
Sprite Target
Gets the sprite followed by the tag.
NameTagWidget SetTextColor(SKColor color)
Sets the name-tag text color.
TextBlock TextBlock
Gets the text drawing.
string Text
Gets the displayed name text.
DirectRectangle Background
Gets the background drawing.
void RefreshPosition()
Repositions the tag from the target's current render bounds.
Point OffsetPx
Gets or sets an additional world-pixel offset from the centered position above the sprite.
NameTagWidget ShowBackground(bool visible=true)
Shows or hides the background while preserving the text.
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.
DirectDrawingMode
Specifies where direct drawing operations are applied within the rendering pipeline.