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
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(renderSurfaceHost, DirectDrawingMode.View, PointF.Empty, nickname)
37 {
38 ArgumentNullException.ThrowIfNull(view);
39 ValidateSize(size);
40
41 _view = view;
42 _size = size;
43 Rectangle bounds = new(Point.Empty, size);
44
46 Color.FromArgb(238, 30, 30, 36),
47 renderSurfaceHost,
48 view,
49 bounds,
50 $"{Nickname}.background")
51 .SetFilled(true)
52 .SetBorderColor(Color.FromArgb(255, 168, 168, 180))
55 .SetCornerRadius(4f);
56
57 Label = new TextBlock(
58 renderSurfaceHost,
59 view,
60 bounds,
61 $"{Nickname}.label")
62 .SetFont(SKTypeface.Default, 14f, minSize: 11f)
63 .SetColors(SKColors.White, SKColors.Transparent)
64 .SetAlignment(SKTextAlign.Left, TextBlock.VerticalAlign.Top)
65 .SetPadding(10f, 8f)
66 .EnableWrapping(true);
67
68 Add(Background, keepCurrentOffset: false, explicitLocalOffsetPx: Vector2.Zero);
69 Add(Label, keepCurrentOffset: false, explicitLocalOffsetPx: Vector2.Zero);
70
71 IsInputEnabled = false;
74 CanReceiveFocus = false;
75
76 SetTooltipZOrder(int.MaxValue - 64);
77 SetIsVisible(false);
78 }
79
82
84 public TextBlock Label { get; }
85
87 public Rectangle Bounds
88 {
89 get
90 {
91 Vector2 position = GetPosition();
92 return new Rectangle(
93 (int)MathF.Round(position.X),
94 (int)MathF.Round(position.Y),
95 _size.Width,
96 _size.Height);
97 }
98 }
99
101 public Size Size
102 {
103 get => _size;
104 set
105 {
106 ValidateSize(value);
107 _size = value;
108 ApplyBounds(new Rectangle(Bounds.Location, value));
109 }
110 }
111
114 {
115 get => _pointerOffsetPx;
116 set => _pointerOffsetPx = value >= 0
117 ? value
118 : throw new ArgumentOutOfRangeException(nameof(value));
119 }
120
127 public TooltipWidget ShowTooltip(string text, Point screenPositionPx)
128 {
129 ArgumentNullException.ThrowIfNull(text);
130
131 Label.SetText(text);
132 ApplyBounds(ResolveBounds(screenPositionPx));
133
134 if (!Visible)
135 Show();
136
137 return this;
138 }
139
143 {
144 if (Visible)
145 Hide();
146
147 return this;
148 }
149
154 {
155 Background.ZOrder = zOrder;
156 Label.ZOrder = zOrder + 1;
157 return this;
158 }
159
160 private Rectangle ResolveBounds(Point pointer)
161 {
162 Rectangle viewport = _view.Viewport.TargetRectPx;
163
164 int x = pointer.X + PointerOffsetPx;
165 int y = pointer.Y + PointerOffsetPx;
166
167 if (x + _size.Width > viewport.Right)
168 x = pointer.X - PointerOffsetPx - _size.Width;
169 if (y + _size.Height > viewport.Bottom)
170 y = pointer.Y - PointerOffsetPx - _size.Height;
171
172 x = Math.Clamp(
173 x,
174 viewport.Left,
175 Math.Max(viewport.Left, viewport.Right - _size.Width));
176 y = Math.Clamp(
177 y,
178 viewport.Top,
179 Math.Max(viewport.Top, viewport.Bottom - _size.Height));
180
181 return new Rectangle(x, y, _size.Width, _size.Height);
182 }
183
184 private void ApplyBounds(Rectangle bounds)
185 {
186 SetPosition(bounds.X, bounds.Y);
187 Background.ScreenBounds = bounds;
188 Label.ScreenBounds = bounds;
189 }
190
191 private static void ValidateSize(Size size)
192 {
193 if (size.Width <= 0 || size.Height <= 0)
194 throw new ArgumentOutOfRangeException(nameof(size), size, "Tooltip size must be positive.");
195 }
196}
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: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
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:410
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.