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
WidgetBase.cs
1using System.Drawing;
5
6namespace Gondwana.Widgets;
7
11public abstract class WidgetBase : DirectComposite
12{
13 private bool _isInputEnabled = true;
14 private bool _isPointerInputEnabled = true;
15 private bool _isKeyboardInputEnabled;
16 private bool _canReceiveFocus;
17
21 public event Action? Shown;
22
26 public event Action? Hidden;
27
31 public event Action? Activated;
32
36 public event Action? Cancelled;
37
41 public event Action<WidgetPointerEventArgs>? PointerEnter;
42
46 public event Action<WidgetPointerEventArgs>? PointerLeave;
47
51 public event Action<WidgetPointerEventArgs>? PointerDown;
52
56 public event Action<WidgetPointerEventArgs>? PointerMove;
57
61 public event Action<WidgetPointerEventArgs>? PointerUp;
62
66 public event Action<WidgetPointerEventArgs>? PointerClick;
67
71 public event Action<WidgetMouseWheelEventArgs>? MouseWheel;
72
76 public event Action? FocusGained;
77
81 public event Action? FocusLost;
82
86 public event Action<WidgetKeyboardEventArgs>? KeyboardInput;
87
95 protected WidgetBase(
96 RenderSurfaceHostBase renderSurfaceHost,
98 PointF anchor = default,
99 string? nickname = null)
100 : base(
101 renderSurfaceHost,
102 mode,
103 anchor,
104 nickname)
105 {
106 }
107
114 public bool IsInputEnabled
115 {
116 get => _isInputEnabled;
117 set
118 {
119 if (_isInputEnabled == value)
120 return;
121
122 _isInputEnabled = value;
123
124 if (!value)
125 WidgetInputRouterRegistry.NotifyInputDisabled(this);
126 }
127 }
128
136 {
137 get => _isPointerInputEnabled;
138 set
139 {
140 if (_isPointerInputEnabled == value)
141 return;
142
143 _isPointerInputEnabled = value;
144
145 if (!value)
146 WidgetInputRouterRegistry.NotifyPointerInputDisabled(this);
147 }
148 }
149
157 {
158 get => _isKeyboardInputEnabled;
159 set
160 {
161 if (_isKeyboardInputEnabled == value)
162 return;
163
164 _isKeyboardInputEnabled = value;
165
166 if (!value)
167 WidgetInputRouterRegistry.NotifyKeyboardFocusDisabled(this);
168 }
169 }
170
177 public bool CanReceiveFocus
178 {
179 get => _canReceiveFocus;
180 set
181 {
182 if (_canReceiveFocus == value)
183 return;
184
185 _canReceiveFocus = value;
186
187 if (!value)
188 WidgetInputRouterRegistry.NotifyKeyboardFocusDisabled(this);
189 }
190 }
191
198 public bool IsFocused { get; internal set; }
199
208 public virtual bool HitTest(
209 View view,
210 Point screenPositionPx)
211 {
212 ArgumentNullException.ThrowIfNull(view);
213
214 if (!IsInputEnabled ||
216 !Visible)
217 {
218 return false;
219 }
220
221 if (Mode == DirectDrawingMode.View
222 && !ReferenceEquals(View, view))
223 {
224 return false;
225 }
226
227 if (Mode == DirectDrawingMode.SceneLayer && SceneLayer is { } layer &&
228 (layer.WrapHorizontally || layer.WrapVertically))
229 return GetWrappedHitOffset(view, screenPositionPx).HasValue;
230
231 RectangleF screenBounds = GetDrawLocationScreen(view);
232
233 return !screenBounds.IsEmpty
234 && screenBounds.Contains(screenPositionPx.X, screenPositionPx.Y);
235 }
236
237 internal PointF? GetWrappedHitOffset(View view, Point screenPositionPx)
238 {
239 if (Mode != DirectDrawingMode.SceneLayer || SceneLayer is not { } layer)
240 return PointF.Empty;
241
242 var world = view.ScreenPxToWorldPx(layer, screenPositionPx);
243
244 foreach (var offset in layer.GetWrappedOffsets(
246 new RectangleF(
247 world.X,
248 world.Y,
249 0.01f,
250 0.01f)))
251 {
252 RectangleF bounds = WorldBounds;
253 bounds.Offset(offset);
254
255 if (bounds.Contains(world))
256 return offset;
257 }
258
259 return null;
260 }
261
269 public virtual bool HitTest(Point screenPositionPx)
270 {
271 var views = RenderSurfaceHost.ViewManager.Views;
272
273 for (int index = views.Count - 1; index >= 0; index--)
274 {
275 View view = views[index];
276
277 if (!view.Viewport.TargetRectPx.Contains(screenPositionPx))
278 {
279 continue;
280 }
281
282 return HitTest(view, screenPositionPx);
283 }
284
285 return false;
286 }
287
293 {
294 WidgetInputRouterRegistry.TryRegister(this);
295
296 SetIsVisible(true);
297
298 ProcessShown();
299 OnShown();
300 Shown?.Invoke();
301
302 return this;
303 }
304
310 {
311 WidgetInputRouterRegistry.NotifyHidden(this);
312
313 SetIsVisible(false);
314
316 OnHidden();
317 Hidden?.Invoke();
318
319 return this;
320 }
321
327 {
328 WidgetInputRouterRegistry.TryBringToFront(this);
329
331 OnActivated();
332 Activated?.Invoke();
333
334 return this;
335 }
336
342 {
344 OnCancelled();
345 Cancelled?.Invoke();
346
347 return this;
348 }
349
354 protected internal void DispatchPointerEnter(WidgetPointerEventArgs args)
355 {
356 ArgumentNullException.ThrowIfNull(args);
357
359 OnPointerEnter(args);
360 PointerEnter?.Invoke(args);
361 }
362
367 protected internal void DispatchPointerLeave(WidgetPointerEventArgs args)
368 {
369 ArgumentNullException.ThrowIfNull(args);
370
372 OnPointerLeave(args);
373 PointerLeave?.Invoke(args);
374 }
375
380 protected internal void DispatchPointerDown(WidgetPointerEventArgs args)
381 {
382 ArgumentNullException.ThrowIfNull(args);
383
384 ProcessPointerDown(args);
385 OnPointerDown(args);
386 PointerDown?.Invoke(args);
387 }
388
393 protected internal void DispatchPointerMove(WidgetPointerEventArgs args)
394 {
395 ArgumentNullException.ThrowIfNull(args);
396
397 ProcessPointerMove(args);
398 OnPointerMove(args);
399 PointerMove?.Invoke(args);
400 }
401
406 protected internal void DispatchPointerUp(WidgetPointerEventArgs args)
407 {
408 ArgumentNullException.ThrowIfNull(args);
409
410 ProcessPointerUp(args);
411 OnPointerUp(args);
412 PointerUp?.Invoke(args);
413 }
414
419 protected internal void DispatchPointerClick(WidgetPointerEventArgs args)
420 {
421 ArgumentNullException.ThrowIfNull(args);
422
424 return;
425
426 OnPointerClick(args);
427 PointerClick?.Invoke(args);
428 }
429
434 protected internal void DispatchMouseWheel(WidgetMouseWheelEventArgs args)
435 {
436 ArgumentNullException.ThrowIfNull(args);
437
438 OnMouseWheel(args);
439 MouseWheel?.Invoke(args);
440 }
441
445 protected internal void DispatchFocusGained()
446 {
447 IsFocused = true;
448
451 FocusGained?.Invoke();
452 }
453
457 protected internal void DispatchFocusLost()
458 {
459 IsFocused = false;
460
462 OnFocusLost();
463 FocusLost?.Invoke();
464 }
465
470 protected internal void DispatchKeyboardInput(WidgetKeyboardEventArgs args)
471 {
472 ArgumentNullException.ThrowIfNull(args);
473
475 OnKeyboardInput(args);
476 KeyboardInput?.Invoke(args);
477 }
478
482 protected virtual void ProcessShown() { }
483
487 protected virtual void ProcessHidden() { }
488
492 protected virtual void ProcessActivated() { }
493
497 protected virtual void ProcessCancelled() { }
498
503 protected virtual void ProcessPointerEnter(WidgetPointerEventArgs args) { }
504
509 protected virtual void ProcessPointerLeave(WidgetPointerEventArgs args) { }
510
515 protected virtual void ProcessPointerDown(WidgetPointerEventArgs args) { }
516
521 protected virtual void ProcessPointerMove(WidgetPointerEventArgs args) { }
522
527 protected virtual void ProcessPointerUp(WidgetPointerEventArgs args) { }
528
532 protected virtual void ProcessFocusGained() { }
533
537 protected virtual void ProcessFocusLost() { }
538
543 protected virtual void ProcessKeyboardInput(WidgetKeyboardEventArgs args) { }
544
553 {
554 return true;
555 }
556
560 protected virtual void OnShown() { }
561
565 protected virtual void OnHidden() { }
566
570 protected virtual void OnActivated() { }
571
575 protected virtual void OnCancelled() { }
576
581 protected virtual void OnPointerEnter(WidgetPointerEventArgs args) { }
582
587 protected virtual void OnPointerLeave(WidgetPointerEventArgs args) { }
588
593 protected virtual void OnPointerDown(WidgetPointerEventArgs args) { }
594
599 protected virtual void OnPointerMove(WidgetPointerEventArgs args) { }
600
605 protected virtual void OnPointerUp(WidgetPointerEventArgs args) { }
606
611 protected virtual void OnMouseWheel(WidgetMouseWheelEventArgs args) { }
612
617 protected virtual void OnPointerClick(WidgetPointerEventArgs args) { }
618
622 protected virtual void OnFocusGained() { }
623
627 protected virtual void OnFocusLost() { }
628
633 protected virtual void OnKeyboardInput(WidgetKeyboardEventArgs args) { }
634}
bool Visible
Gets or sets the visibility of all descendants.
RenderSurfaceHostBase RenderSurfaceHost
Gets the render surface host shared by the composite and its children.
SceneLayer? SceneLayer
Gets the scene layer shared by all children in scene-layer mode.
RectangleF GetDrawLocationScreen(View view)
Gets the union of all visible descendants as projected through a view.
DirectComposite SetIsVisible(bool visible)
Applies one visibility value to every descendant.
Rectangle WorldBounds
Gets the union of all visible descendants' world-space bounds.
View? View
Gets the view shared by all children in view mode.
DirectComposite(RenderSurfaceHostBase renderSurfaceHost, DirectDrawingMode mode, PointF anchor=default, string? nickname=null)
Initializes a new instance of the DirectComposite class.
DirectDrawingMode Mode
Gets the coordinate mode shared by the composite and its children.
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
PointF ScreenPxToWorldPx(SceneLayer layer, PointF screenPx)
Converts a screen-space pixel position into world-space coordinates for the specified scene layer,...
Definition View.cs:239
Viewport Viewport
Gets the viewport that defines the screen-space rectangle, zoom level, and other rendering parameters...
Definition View.cs:44
Rectangle TargetRectPx
Screen-space rectangle (in SCREEN / RenderSurface pixels) where this view is drawn....
Definition Viewport.cs:40
virtual void ProcessCancelled()
Performs processing when the widget is cancelled before notification callbacks are invoked.
WidgetBase Show()
Shows the widget and registers it for input routing.
virtual void OnMouseWheel(WidgetMouseWheelEventArgs args)
Called when mouse-wheel input is routed to the widget.
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.
virtual void ProcessShown()
Performs processing when the widget is shown before notification callbacks are invoked.
WidgetBase(RenderSurfaceHostBase renderSurfaceHost, DirectDrawingMode mode, PointF anchor=default, string? nickname=null)
Initializes a new instance of the WidgetBase class.
Definition WidgetBase.cs:95
WidgetBase Activate()
Activates the widget and brings it to the front of the input order.
bool IsFocused
Gets a value indicating whether the widget currently has keyboard focus.
virtual void OnPointerDown(WidgetPointerEventArgs args)
Called when a pointer press begins on the widget.
Action< WidgetPointerEventArgs >? PointerMove
Occurs when a pointer moves while routed to the widget.
Definition WidgetBase.cs:56
Action< WidgetPointerEventArgs >? PointerEnter
Occurs when a pointer enters the widget bounds.
Definition WidgetBase.cs:41
virtual void OnPointerEnter(WidgetPointerEventArgs args)
Called when a pointer enters the widget bounds.
virtual void ProcessFocusGained()
Performs processing when the widget gains focus before notification callbacks are invoked.
virtual void ProcessKeyboardInput(WidgetKeyboardEventArgs args)
Performs processing when keyboard input is routed to the widget before notification callbacks are inv...
Action? Cancelled
Occurs when the widget is cancelled.
Definition WidgetBase.cs:36
Action? Shown
Occurs when the widget is shown.
Definition WidgetBase.cs:21
virtual void OnPointerMove(WidgetPointerEventArgs args)
Called when pointer movement is routed to the widget.
virtual void ProcessPointerEnter(WidgetPointerEventArgs args)
Performs processing when a pointer enters the widget before notification callbacks are invoked.
virtual void OnPointerClick(WidgetPointerEventArgs args)
Called when a pointer click is dispatched to the widget.
virtual void ProcessPointerDown(WidgetPointerEventArgs args)
Performs processing when a pointer press begins on the widget before notification callbacks are invok...
Action< WidgetPointerEventArgs >? PointerClick
Occurs when a pointer press and release completes as a click on the widget.
Definition WidgetBase.cs:66
WidgetBase Hide()
Hides the widget and releases any routed input state.
virtual void OnCancelled()
Called when the widget is cancelled.
virtual void OnHidden()
Called when the widget is hidden.
Action? FocusLost
Occurs when the widget loses keyboard focus.
Definition WidgetBase.cs:81
virtual void ProcessActivated()
Performs processing when the widget is activated before notification callbacks are invoked.
Action< WidgetKeyboardEventArgs >? KeyboardInput
Occurs when keyboard input is routed to the widget.
Definition WidgetBase.cs:86
virtual void OnActivated()
Called when the widget is activated.
Action? Hidden
Occurs when the widget is hidden.
Definition WidgetBase.cs:26
virtual void OnKeyboardInput(WidgetKeyboardEventArgs args)
Called when keyboard input is routed to the widget.
Action< WidgetPointerEventArgs >? PointerUp
Occurs when a pointer button or touch press is released for the widget.
Definition WidgetBase.cs:61
Action? FocusGained
Occurs when the widget gains keyboard focus.
Definition WidgetBase.cs:76
virtual bool HitTest(Point screenPositionPx)
Determines whether the widget can be hit at the specified screen position.
virtual void OnPointerLeave(WidgetPointerEventArgs args)
Called when a pointer leaves the widget bounds.
Action< WidgetMouseWheelEventArgs >? MouseWheel
Occurs when mouse-wheel input is routed to the widget while the pointer is over it.
Definition WidgetBase.cs:71
virtual void ProcessPointerLeave(WidgetPointerEventArgs args)
Performs processing when a pointer leaves the widget before notification callbacks are invoked.
WidgetBase Cancel()
Cancels the widget.
virtual bool ShouldDispatchPointerClick(WidgetPointerEventArgs args)
Determines whether a pointer-click event should be dispatched for the specified pointer event data.
virtual void OnFocusLost()
Called when the widget loses focus.
Action< WidgetPointerEventArgs >? PointerLeave
Occurs when a pointer leaves the widget bounds.
Definition WidgetBase.cs:46
bool IsKeyboardInputEnabled
Gets or sets a value indicating whether the widget can receive keyboard input.
virtual void ProcessPointerMove(WidgetPointerEventArgs args)
Performs processing when pointer movement is routed to the widget before notification callbacks are i...
Action< WidgetPointerEventArgs >? PointerDown
Occurs when a pointer button or touch press begins on the widget.
Definition WidgetBase.cs:51
Action? Activated
Occurs when the widget is activated.
Definition WidgetBase.cs:31
virtual void ProcessFocusLost()
Performs processing when the widget loses focus before notification callbacks are invoked.
virtual void OnShown()
Called when the widget is shown.
virtual void ProcessHidden()
Performs processing when the widget is hidden before notification callbacks are invoked.
virtual void ProcessPointerUp(WidgetPointerEventArgs args)
Performs processing when a pointer press is released for the widget before notification callbacks are...
bool IsInputEnabled
Gets or sets a value indicating whether the widget can participate in input routing.
virtual void OnPointerUp(WidgetPointerEventArgs args)
Called when a pointer press is released for the widget.
virtual void OnFocusGained()
Called when the widget gains focus.
virtual bool HitTest(View view, Point screenPositionPx)
Determines whether the widget can be hit at the specified screen position within the specified view.
Provides data for widget keyboard input events routed by WidgetInputRouter.
Event arguments for mouse-wheel input routed to a widget.
Event arguments for pointer-based widget callbacks.
DirectDrawingMode
Specifies where direct drawing operations are applied within the rendering pipeline.
@ SceneLayer
Performs direct drawing within a scene layer using world-space coordinates.