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
TooltipWidget.cs
1using System.Drawing;
2using System.Numerics;
6using SkiaSharp;
7
9
18public sealed class TooltipWidget : WidgetBase
19{
20 private readonly View _view;
21 private Size _size;
22 private int _pointerOffsetPx = 12;
23
32 RenderSurfaceHostBase renderSurfaceHost,
33 View view,
34 Size size,
35 string? nickname = null)
36 : base(
37 renderSurfaceHost,
39 PointF.Empty,
40 nickname)
41 {
42 ArgumentNullException.ThrowIfNull(view);
43 ValidateSize(size);
44
45 _view = view;
46 _size = size;
47 Rectangle bounds = new(Point.Empty, size);
48
50 Color.FromArgb(
51 238,
52 30,
53 30,
54 36),
55 renderSurfaceHost,
56 view,
57 bounds,
58 $"{Nickname}.background")
59 .SetFilled(true)
61 Color.FromArgb(
62 255,
63 168,
64 168,
65 180))
68 .SetCornerRadius(4f);
69
70 Label = new TextBlock(
71 renderSurfaceHost,
72 view,
73 bounds,
74 $"{Nickname}.label")
75 .SetFont(SKTypeface.Default, 14f, minSize: 11f)
76 .SetColors(SKColors.White, SKColors.Transparent)
77 .SetAlignment(SKTextAlign.Left, TextBlock.VerticalAlign.Top)
78 .SetPadding(10f, 8f)
79 .EnableWrapping(true);
80
81 Add(Background, keepCurrentOffset: false, explicitLocalOffsetPx: Vector2.Zero);
82 Add(Label, keepCurrentOffset: false, explicitLocalOffsetPx: Vector2.Zero);
83
84 IsInputEnabled = false;
87 CanReceiveFocus = false;
88
89 SetTooltipZOrder(int.MaxValue - 64);
90 SetIsVisible(false);
91 }
92
95
97 public TextBlock Label { get; }
98
100 public Rectangle Bounds
101 {
102 get
103 {
104 Vector2 position = GetPosition();
105
106 return new Rectangle(
107 (int)MathF.Round(position.X),
108 (int)MathF.Round(position.Y),
109 _size.Width,
110 _size.Height);
111 }
112 }
113
115 public Size Size
116 {
117 get => _size;
118 set
119 {
120 ValidateSize(value);
121 _size = value;
122 ApplyBounds(new Rectangle(Bounds.Location, value));
123 }
124 }
125
128 {
129 get => _pointerOffsetPx;
130 set => _pointerOffsetPx = value >= 0
131 ? value
132 : throw new ArgumentOutOfRangeException(nameof(value));
133 }
134
141 public TooltipWidget ShowTooltip(string text, Point screenPositionPx)
142 {
143 ArgumentNullException.ThrowIfNull(text);
144
145 Label.SetText(text);
146 ApplyBounds(ResolveBounds(screenPositionPx));
147
148 if (!Visible)
149 Show();
150
151 return this;
152 }
153
157 {
158 if (Visible)
159 Hide();
160
161 return this;
162 }
163
168 {
169 Background.ZOrder = zOrder;
170 Label.ZOrder = zOrder + 1;
171 return this;
172 }
173
174 private Rectangle ResolveBounds(Point pointer)
175 {
176 Rectangle viewport = _view.Viewport.TargetRectPx;
177
178 int x = pointer.X + PointerOffsetPx;
179 int y = pointer.Y + PointerOffsetPx;
180
181 if (x + _size.Width > viewport.Right)
182 x = pointer.X - PointerOffsetPx - _size.Width;
183
184 if (y + _size.Height > viewport.Bottom)
185 y = pointer.Y - PointerOffsetPx - _size.Height;
186
187 x = Math.Clamp(
188 x,
189 viewport.Left,
190 Math.Max(viewport.Left, viewport.Right - _size.Width));
191
192 y = Math.Clamp(
193 y,
194 viewport.Top,
195 Math.Max(viewport.Top, viewport.Bottom - _size.Height));
196
197 return new Rectangle(
198 x,
199 y,
200 _size.Width,
201 _size.Height);
202 }
203
204 private void ApplyBounds(Rectangle bounds)
205 {
206 SetPosition(bounds.X, bounds.Y);
207 Background.ScreenBounds = bounds;
208 Label.ScreenBounds = bounds;
209 }
210
211 private static void ValidateSize(Size size)
212 {
213 if (size.Width <= 0 || size.Height <= 0)
214 throw new ArgumentOutOfRangeException(nameof(size), size, "Tooltip size must be positive.");
215 }
216}
bool Visible
Gets or sets the visibility of all descendants.
DirectComposite SetIsVisible(bool visible)
Applies one visibility value to every descendant.
View? View
Gets the view shared by all children in view mode.
Vector2 GetPosition()
Gets the current composite anchor.
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.
A retained-mode text drawable with wrapping, multi-line ( ) support, horizontal/vertical alignment,...
Definition TextBlock.cs:44
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
VerticalAlign
Vertical alignment options for multi-line text within the control's bounds.
Definition TextBlock.cs:49
TextBlock SetColors(SKColor fg, SKColor bg)
Sets the foreground (text) and background colors using Skia colors.
Definition TextBlock.cs:430
Represents a base class for hosting a render surface, providing functionality for managing rendering ...
int PointerOffsetPx
Gets or sets the gap between the pointer and tooltip.
Size Size
Gets or sets the tooltip size in screen pixels.
DirectRectangle Background
Gets the tooltip background drawing.
TooltipWidget(RenderSurfaceHostBase renderSurfaceHost, View view, Size size, string? nickname=null)
Creates a hidden view-level tooltip.
TooltipWidget SetTooltipZOrder(int zOrder)
Sets the drawing Z-order for the tooltip.
TextBlock Label
Gets the tooltip text drawing.
TooltipWidget HideTooltip()
Hides the tooltip.
TooltipWidget ShowTooltip(string text, Point screenPositionPx)
Shows text near the supplied pointer position, clamped to the owning view.
Rectangle Bounds
Gets the current tooltip bounds in screen pixels.
WidgetBase Show()
Shows the widget and registers it for input routing.
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
WidgetBase Hide()
Hides the widget and releases any routed input state.
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.