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
HyperlinkWidget.cs
1using System.Drawing;
7using SkiaSharp;
8
10
14public sealed class HyperlinkWidget : WidgetBase
15{
16 private readonly IExternalUriLauncher _uriLauncher;
17 private readonly SKColor _normalColor = new(80, 150, 255);
18 private readonly SKColor _hoverColor = new(125, 185, 255);
19
20 #region events
21
25 public event Action<Uri>? LinkOpened;
26
30 public event Action<Uri, Exception>? LinkOpenFailed;
31
32 #endregion
33
34 #region constructors
35
47 RenderSurfaceHostBase renderSurfaceHost,
48 View view,
49 Rectangle bounds,
50 string text,
51 Uri navigateUri,
52 string? nickname = null)
53 : base(renderSurfaceHost, DirectDrawingMode.View, bounds.Location, nickname)
54 {
55 ArgumentNullException.ThrowIfNull(uriLauncher);
56 ArgumentNullException.ThrowIfNull(view);
57 ArgumentNullException.ThrowIfNull(navigateUri);
58
59 if (!navigateUri.IsAbsoluteUri)
60 throw new ArgumentException("The URI must be absolute.", nameof(navigateUri));
61
62 _uriLauncher = uriLauncher;
63 NavigateUri = navigateUri;
64 Label = CreateLabel(renderSurfaceHost, view, bounds, text);
65
66 Add(Label);
67 CompleteInitialization();
68 }
69
81 RenderSurfaceHostBase renderSurfaceHost,
82 SceneLayer sceneLayer,
83 Rectangle bounds,
84 string text,
85 Uri navigateUri,
86 string? nickname = null)
87 : base(renderSurfaceHost, DirectDrawingMode.SceneLayer, bounds.Location, nickname)
88 {
89 ArgumentNullException.ThrowIfNull(uriLauncher);
90 ArgumentNullException.ThrowIfNull(sceneLayer);
91 ArgumentNullException.ThrowIfNull(navigateUri);
92
93 _uriLauncher = uriLauncher;
94 NavigateUri = navigateUri;
95
96 Label = CreateLabel(renderSurfaceHost, sceneLayer, bounds, text);
97
98 Add(Label);
99 CompleteInitialization();
100 }
101
102 #endregion constructors
103
104 #region public properties
105
109 public Uri NavigateUri { get; set; }
110
114 public TextBlock Label { get; }
115
116 #endregion public properties
117
118 #region public methods
119
125 public async ValueTask OpenAsync(CancellationToken cancellationToken = default)
126 {
127 if (!IsInputEnabled)
128 return;
129
130 Uri uri = NavigateUri;
131
132 try
133 {
134 await _uriLauncher.OpenAsync(uri, cancellationToken);
135 LinkOpened?.Invoke(uri);
136 }
137 catch (Exception ex)
138 {
139 LinkOpenFailed?.Invoke(uri, ex);
140 }
141 }
142
143 #endregion public methods
144
145 #region protected methods
146
148 protected override void OnPointerEnter(WidgetPointerEventArgs args)
149 {
150 base.OnPointerEnter(args);
151 Label.SetColors(_hoverColor, SKColors.Transparent);
152 }
153
155 protected override void OnPointerLeave(WidgetPointerEventArgs args)
156 {
157 base.OnPointerLeave(args);
158 Label.SetColors(_normalColor, SKColors.Transparent);
159 }
160
162 protected override void OnPointerClick(WidgetPointerEventArgs args)
163 {
164 base.OnPointerClick(args);
165
166 if (!args.IsPrimaryButton)
167 return;
168
169 args.Handled = true;
170 _ = OpenAsync();
171 }
172
174 protected override void OnKeyboardInput(WidgetKeyboardEventArgs args)
175 {
176 base.OnKeyboardInput(args);
177
178 if (args.KeyAction != KeyAction.Pressed)
179 return;
180
181 // Standard Enter and Space virtual-key values.
182 if (args.Key is not 13 and not 32)
183 return;
184
185 args.Handled = true;
186 _ = OpenAsync();
187 }
188
189 #endregion protected methods
190
191 #region private methods
192
193 private void CompleteInitialization()
194 {
195 CanReceiveFocus = true;
197 }
198
199 private TextBlock CreateLabel(RenderSurfaceHostBase host,
200 View view,
201 Rectangle bounds,
202 string text)
203 {
204 return new TextBlock(host, view, bounds)
205 .SetText(text)
206 .SetFont(SKTypeface.Default, 16f, minSize: 10f)
207 .SetColors(_normalColor, SKColors.Transparent)
208 .SetAlignment(SKTextAlign.Left, TextBlock.VerticalAlign.Center)
209 .EnableWrapping(false);
210 }
211
212 private TextBlock CreateLabel(RenderSurfaceHostBase host,
213 SceneLayer layer,
214 Rectangle bounds,
215 string text)
216 {
217 return new TextBlock(host, layer, view: null, worldBounds: bounds)
218 .SetText(text)
219 .SetFont(SKTypeface.Default, 16f, minSize: 10f)
220 .SetColors(_normalColor, SKColors.Transparent)
221 .SetAlignment(SKTextAlign.Left, TextBlock.VerticalAlign.Center)
222 .EnableWrapping(false);
223 }
224
225 #endregion private methods
226}
SceneLayer? SceneLayer
Gets the scene layer shared by all children in scene-layer mode.
View? View
Gets the view shared by all children in view mode.
virtual DirectComposite Add(IDirectCompositeChild child, bool keepCurrentOffset=true, Vector2? explicitLocalOffsetPx=null)
Adds a composite-compatible direct drawable as a child.
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
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
TextBlock SetText(string text)
Sets the current text content and rebuilds layout as needed. If no reveal animation is active,...
Definition TextBlock.cs:358
Represents a base class for hosting a render surface, providing functionality for managing rendering ...
Represents a rendered view of a scene, combining a camera position with a viewport configuration to c...
Definition View.cs:14
Represents a single layer within a Scene, containing a grid of tiles and supporting various coordinat...
Definition SceneLayer.cs:40
override void OnPointerLeave(WidgetPointerEventArgs args)
Called when a pointer leaves the widget bounds.
override void OnPointerClick(WidgetPointerEventArgs args)
Called when a pointer click is dispatched to the widget.
HyperlinkWidget(IExternalUriLauncher uriLauncher, RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle bounds, string text, Uri navigateUri, string? nickname=null)
Initializes a scene-layer hyperlink widget.
async ValueTask OpenAsync(CancellationToken cancellationToken=default)
Opens the configured URI.
override void OnKeyboardInput(WidgetKeyboardEventArgs args)
Called when keyboard input is routed to the widget.
override void OnPointerEnter(WidgetPointerEventArgs args)
Called when a pointer enters the widget bounds.
TextBlock Label
Gets the text block used to display the link.
Action< Uri >? LinkOpened
Occurs after the link has been opened successfully.
Action< Uri, Exception >? LinkOpenFailed
Occurs when an attempt to open the link fails.
Uri NavigateUri
Gets or sets the URI opened when the widget is activated.
HyperlinkWidget(IExternalUriLauncher uriLauncher, RenderSurfaceHostBase renderSurfaceHost, View view, Rectangle bounds, string text, Uri navigateUri, string? nickname=null)
Initializes a view-level hyperlink widget.
bool CanReceiveFocus
Gets or sets a value indicating whether the widget can receive keyboard focus.
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.
Provides data for widget keyboard input events routed by WidgetInputRouter.
int Key
Gets the integer key identifier.
KeyAction KeyAction
Gets the action associated with this key event.
Event arguments for pointer-based widget callbacks.
bool IsPrimaryButton
Gets whether the pointer event represents a primary-button interaction.
Defines a platform-specific service for opening external URIs.
DirectDrawingMode
Specifies where direct drawing operations are applied within the rendering pipeline.
KeyAction
Represents the type of action performed on a keyboard key, indicating whether the key was pressed,...
Definition KeyAction.cs:10