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
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 IExternalUriLauncher uriLauncher,
48 RenderSurfaceHostBase renderSurfaceHost,
49 View view,
50 Rectangle bounds,
51 string text,
52 Uri navigateUri,
53 string? nickname = null)
54 : base(
55 renderSurfaceHost,
57 bounds.Location,
58 nickname)
59 {
60 ArgumentNullException.ThrowIfNull(uriLauncher);
61 ArgumentNullException.ThrowIfNull(view);
62 ArgumentNullException.ThrowIfNull(navigateUri);
63
64 if (!navigateUri.IsAbsoluteUri)
65 throw new ArgumentException("The URI must be absolute.", nameof(navigateUri));
66
67 _uriLauncher = uriLauncher;
68 NavigateUri = navigateUri;
69
70 Label = CreateLabel(
71 renderSurfaceHost,
72 view,
73 bounds,
74 text);
75
76 Add(Label);
77 CompleteInitialization();
78 }
79
91 IExternalUriLauncher uriLauncher,
92 RenderSurfaceHostBase renderSurfaceHost,
93 SceneLayer sceneLayer,
94 Rectangle bounds,
95 string text,
96 Uri navigateUri,
97 string? nickname = null)
98 : base(
99 renderSurfaceHost,
101 bounds.Location,
102 nickname)
103 {
104 ArgumentNullException.ThrowIfNull(uriLauncher);
105 ArgumentNullException.ThrowIfNull(sceneLayer);
106 ArgumentNullException.ThrowIfNull(navigateUri);
107
108 _uriLauncher = uriLauncher;
109 NavigateUri = navigateUri;
110
111 Label = CreateLabel(
112 renderSurfaceHost,
113 sceneLayer,
114 bounds,
115 text);
116
117 Add(Label);
118 CompleteInitialization();
119 }
120
121 #endregion constructors
122
123 #region public properties
124
128 public Uri NavigateUri { get; set; }
129
133 public TextBlock Label { get; }
134
135 #endregion public properties
136
137 #region public methods
138
144 public async ValueTask OpenAsync(CancellationToken cancellationToken = default)
145 {
146 if (!IsInputEnabled)
147 return;
148
149 Uri uri = NavigateUri;
150
151 try
152 {
153 await _uriLauncher.OpenAsync(uri, cancellationToken);
154 LinkOpened?.Invoke(uri);
155 }
156 catch (Exception ex)
157 {
158 LinkOpenFailed?.Invoke(uri, ex);
159 }
160 }
161
162 #endregion public methods
163
164 #region protected methods
165
167 protected override void OnPointerEnter(WidgetPointerEventArgs args)
168 {
169 base.OnPointerEnter(args);
170 Label.SetColors(_hoverColor, SKColors.Transparent);
171 }
172
174 protected override void OnPointerLeave(WidgetPointerEventArgs args)
175 {
176 base.OnPointerLeave(args);
177 Label.SetColors(_normalColor, SKColors.Transparent);
178 }
179
181 protected override void OnPointerClick(WidgetPointerEventArgs args)
182 {
183 base.OnPointerClick(args);
184
185 if (!args.IsPrimaryButton)
186 return;
187
188 args.Handled = true;
189 _ = OpenAsync();
190 }
191
193 protected override void OnKeyboardInput(WidgetKeyboardEventArgs args)
194 {
195 base.OnKeyboardInput(args);
196
197 if (args.KeyAction != KeyAction.Pressed)
198 return;
199
200 // Standard Enter and Space virtual-key values.
201 if (args.Key is not 13 and not 32)
202 return;
203
204 args.Handled = true;
205 _ = OpenAsync();
206 }
207
208 #endregion protected methods
209
210 #region private methods
211
212 private void CompleteInitialization()
213 {
214 CanReceiveFocus = true;
216 }
217
218 private TextBlock CreateLabel(
220 View view,
221 Rectangle bounds,
222 string text)
223 {
224 return new TextBlock(host, view, bounds)
225 .SetText(text)
226 .SetFont(SKTypeface.Default, 16f, minSize: 10f)
227 .SetColors(_normalColor, SKColors.Transparent)
228 .SetAlignment(SKTextAlign.Left, TextBlock.VerticalAlign.Center)
229 .EnableWrapping(false);
230 }
231
232 private TextBlock CreateLabel(
234 SceneLayer layer,
235 Rectangle bounds,
236 string text)
237 {
238 return new TextBlock(
239 host,
240 layer,
241 view: null,
242 worldBounds: bounds)
243 .SetText(text)
244 .SetFont(SKTypeface.Default, 16f, minSize: 10f)
245 .SetColors(_normalColor, SKColors.Transparent)
246 .SetAlignment(SKTextAlign.Left, TextBlock.VerticalAlign.Center)
247 .EnableWrapping(false);
248 }
249
250 #endregion private methods
251}
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: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
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
TextBlock SetText(string text)
Sets the current text content and rebuilds layout as needed. If no reveal animation is active,...
Definition TextBlock.cs:378
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