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
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
31 RenderSurfaceHostBase renderSurfaceHost,
32 Sprite target,
33 string text,
34 Size? size = null,
35 Point? offsetPx = null,
36 string? nickname = null)
37 : base(renderSurfaceHost, DirectDrawingMode.SceneLayer, nickname: nickname)
38 {
39 Target = target ?? throw new ArgumentNullException(nameof(target));
40 _text = text ?? string.Empty;
41 _size = ValidateSize(size ?? new Size(128, 26));
42 _offsetPx = offsetPx ?? Point.Empty;
43
45 Color.FromArgb(
46 185,
47 20,
48 22,
49 28),
50 renderSurfaceHost,
51 Target.SceneLayer,
52 new Rectangle(Point.Empty, _size),
53 $"{Nickname}-name-tag-background")
54 .SetFilled(true)
56 Color.FromArgb(
57 200,
58 220,
59 220,
60 230))
62 .SetCornerRadius(4f);
63
64 TextBlock = new TextBlock(
65 renderSurfaceHost,
66 Target.SceneLayer,
67 view: null,
68 worldBounds: new Rectangle(Point.Empty, _size),
69 nickname: $"{Nickname}-name-tag-text")
70 .SetText(_text)
71 .SetFont(SKTypeface.Default, 15f, minSize: 9f)
72 .SetColors(SKColors.White, SKColors.Transparent)
73 .SetAlignment(SKTextAlign.Center, TextBlock.VerticalAlign.Center)
74 .SetPadding(5f, 2f)
75 .EnableWrapping(false);
76
77 Add(Background, keepCurrentOffset: false, explicitLocalOffsetPx: Vector2.Zero);
78 Add(TextBlock, keepCurrentOffset: false, explicitLocalOffsetPx: Vector2.Zero);
79
80 IsInputEnabled = false;
83 CanReceiveFocus = false;
84
86
87 Target.SpriteMoved += OnTargetMoved;
88 Target.VisualBoundsChanged += OnTargetVisualBoundsChanged;
89 Target.Disposing += OnTargetDisposing;
90
92 }
93
97 public Sprite Target { get; }
98
103
107 public TextBlock TextBlock { get; }
108
112 public string Text => _text;
113
117 public Size Size
118 {
119 get => _size;
120 set
121 {
122 Size validated = ValidateSize(value);
123
124 if (_size == validated)
125 return;
126
127 _size = validated;
128 Background.WorldBounds = new Rectangle(Background.WorldBounds.Location, _size);
129 TextBlock.SetSize(_size);
131 }
132 }
133
137 public Point OffsetPx
138 {
139 get => _offsetPx;
140 set
141 {
142 _offsetPx = value;
144 }
145 }
146
150 public Rectangle BoundsWorld => Background.WorldBounds;
151
157 public NameTagWidget SetText(string text)
158 {
159 _text = text ?? string.Empty;
160 TextBlock.SetText(_text);
161 return this;
162 }
163
169 public NameTagWidget SetTextColor(SKColor color)
170 {
171 TextBlock.SetColors(color, SKColors.Transparent);
172 return this;
173 }
174
181 {
182 Background.SetColor(color);
183 Background.SetPosition(Background.GetPosition());
184 return this;
185 }
186
192 public NameTagWidget ShowBackground(bool visible = true)
193 {
194 _backgroundVisible = visible;
195 Background.Visible = visible;
196 return this;
197 }
198
205 {
206 Background.ZOrder = zOrder;
207 TextBlock.ZOrder = zOrder + 1;
208 return this;
209 }
210
214 public void RefreshPosition()
215 {
216 Rectangle targetBounds = Target.DrawLocationWorld;
217 int x = targetBounds.Left + (targetBounds.Width - Size.Width) / 2 + OffsetPx.X;
218 int y = targetBounds.Top - Size.Height - 6 + OffsetPx.Y;
219 SetPosition(x, y);
220 }
221
223 public override void Dispose()
224 {
225 if (_disposed)
226 return;
227
228 _disposed = true;
229 Target.SpriteMoved -= OnTargetMoved;
230 Target.VisualBoundsChanged -= OnTargetVisualBoundsChanged;
231 Target.Disposing -= OnTargetDisposing;
232 base.Dispose();
233 }
234
236 protected override void ProcessShown()
237 {
238 base.ProcessShown();
239 Background.Visible = _backgroundVisible;
240 }
241
242 private void OnTargetMoved(SpriteMovedEventArgs args)
243 {
245 }
246
247 private void OnTargetVisualBoundsChanged(Sprite sprite)
248 {
250 }
251
252 private void OnTargetDisposing(Sprite sprite)
253 {
254 Dispose();
255 }
256
257 private static Size ValidateSize(Size size)
258 {
259 if (size.Width <= 0 || size.Height <= 0)
260 throw new ArgumentOutOfRangeException(nameof(size), size, "Name-tag size must be positive.");
261
262 return size;
263 }
264}
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:414
TextBlock EnableWrapping(bool enable=true)
Enables or disables word wrapping when laying out paragraphs.
Definition TextBlock.cs:527
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:453
TextBlock SetPadding(float horizontal, float vertical)
Sets symmetric horizontal and vertical padding and invalidates cached layout.
Definition TextBlock.cs:336
TextBlock SetColors(SKColor fg, SKColor bg)
Sets the foreground (text) and background colors using Skia colors.
Definition TextBlock.cs:430
TextBlock SetText(string text)
Sets the current text content and rebuilds layout as needed. If no reveal animation is active,...
Definition TextBlock.cs:378
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 ...
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.