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
DialogBox.cs
1using System.Drawing;
2using System.Numerics;
9using SkiaSharp;
10
12
17public abstract class DialogBox : DraggableContainerWidget
18{
22 protected const int DefaultTitleBarHeight = 36;
23
27 protected const int DefaultCloseButtonSize = 28;
28
32 protected readonly static Color DefaultPanelColor = Color.FromArgb(
33 245,
34 36,
35 36,
36 44);
37
41 protected readonly static Color DefaultPanelBorderColor = Color.FromArgb(
42 255,
43 140,
44 140,
45 155);
46
50 protected readonly static Color DefaultTitleBarColor = Color.FromArgb(
51 255,
52 57,
53 57,
54 72);
55
56 private const int DialogZOrderStart = 100_000;
57 private const int DialogZOrderGap = 100;
58
59 private static readonly object _zOrderSyncRoot = new();
60 private static int _nextDialogZOrder = DialogZOrderStart;
61
62 private bool _disposed;
63
67 public event Action<DialogResult>? Closed;
68
69 #region constructors
70
80 protected DialogBox(
81 RenderSurfaceHostBase renderSurfaceHost,
82 View view,
83 Rectangle bounds,
84 string title,
85 bool showCloseButton = true,
86 string? nickname = null)
87 : base(
88 renderSurfaceHost,
90 bounds.Location,
91 nickname)
92 {
93 ValidateBounds(bounds);
94
95 DialogSize = bounds.Size;
96
97 Panel = CreatePanel(renderSurfaceHost, view, bounds);
98 TitleBar = CreateTitleBar(renderSurfaceHost, view, bounds);
99
100 TitleText = CreateTitleText(
101 renderSurfaceHost,
102 view,
103 bounds,
104 title);
105
106 Add(Panel);
107 Add(TitleBar);
108 Add(TitleText);
109
110 if (showCloseButton)
111 {
112 CloseButton = CreateCloseButton(renderSurfaceHost, view, bounds);
113 Add(CloseButton, GetCloseButtonOffset(bounds.Size));
114
115 CloseButton.Clicked += OnCloseButtonClicked;
116 }
117
118 CompleteInitialization();
119 }
120
130 protected DialogBox(
131 RenderSurfaceHostBase renderSurfaceHost,
132 SceneLayer sceneLayer,
133 Rectangle bounds,
134 string title,
135 bool showCloseButton = true,
136 string? nickname = null)
137 : base(
138 renderSurfaceHost,
140 bounds.Location,
141 nickname)
142 {
143 ValidateBounds(bounds);
144
145 DialogSize = bounds.Size;
146
147 Panel = CreatePanel(renderSurfaceHost, sceneLayer, bounds);
148 TitleBar = CreateTitleBar(renderSurfaceHost, sceneLayer, bounds);
149
150 TitleText = CreateTitleText(
151 renderSurfaceHost,
152 sceneLayer,
153 bounds,
154 title);
155
156 Add(Panel);
157 Add(TitleBar);
158 Add(TitleText);
159
160 if (showCloseButton)
161 {
162 CloseButton = CreateCloseButton(renderSurfaceHost, sceneLayer, bounds);
163 Add(CloseButton, GetCloseButtonOffset(bounds.Size));
164
165 CloseButton.Clicked += OnCloseButtonClicked;
166 }
167
168 CompleteInitialization();
169 }
170
171 #endregion constructors
172
173 #region public properties
174
178 public DirectRectangle Panel { get; }
179
183 public DirectRectangle TitleBar { get; }
184
188 public TextBlock TitleText { get; }
189
193 public ButtonWidget? CloseButton { get; }
194
198 public Size DialogSize { get; }
199
203 public bool IsClosed { get; private set; }
204
208 public DialogResult Result { get; private set; }
209
213 public bool DisposeOnClose { get; set; } = true;
214
221 public int AcceptKey { get; set; } = 13;
222
229 public int CancelKey { get; set; } = 27;
230
231 #endregion public properties
232
233 #region exposed methods and hooks
234
239 public void Close(DialogResult result = DialogResult.Close)
240 {
241 if (IsClosed)
242 return;
243
244 IsClosed = true;
245 Result = result;
246
247 Hide();
248
249 OnClosed(result);
250 Closed?.Invoke(result);
251
252 if (DisposeOnClose)
253 Dispose();
254 }
255
260 protected virtual void OnClosed(DialogResult result)
261 {
262 }
263
267 protected virtual void OnAcceptRequested()
268 {
270 }
271
273 protected override void ProcessActivated()
274 {
275 BringVisualTreeToFront();
276 base.ProcessActivated();
277 }
278
279 private void BringVisualTreeToFront()
280 {
281 DirectDrawingBase[] visuals = GetVisualDescendants(this).ToArray();
282
283 if (visuals.Length == 0)
284 return;
285
286 int minimumZOrder = visuals.Min(static visual => visual.ZOrder);
287 int maximumZOrder = visuals.Max(static visual => visual.ZOrder);
288 int span = maximumZOrder - minimumZOrder;
289
290 int targetMinimum;
291
292 lock (_zOrderSyncRoot)
293 {
294 targetMinimum = _nextDialogZOrder;
295 _nextDialogZOrder = checked(targetMinimum + span + DialogZOrderGap);
296 }
297
298 int delta = targetMinimum - minimumZOrder;
299
300 foreach (DirectDrawingBase visual in visuals)
301 visual.ZOrder += delta;
302 }
303
304 private static IEnumerable<DirectDrawingBase> GetVisualDescendants(IDirectCompositeContainer container)
305 {
306 foreach (IDirectCompositeChild child in container.Children)
307 {
308 if (child is DirectDrawingBase visual)
309 yield return visual;
310
311 if (child is not IDirectCompositeContainer childContainer)
312 continue;
313
314 foreach (DirectDrawingBase descendant in GetVisualDescendants(childContainer))
315 yield return descendant;
316 }
317 }
318
320 protected override bool CanStartDrag(WidgetPointerEventArgs args)
321 {
322 if (!base.CanStartDrag(args))
323 return false;
324
325 RectangleF titleBarBounds = TitleBar.GetDrawLocationScreen(args.View);
326
327 titleBarBounds.Offset(
329 args.WrappedOffsetWorldPx.Y * args.View.Viewport.Zoom);
330
331 return titleBarBounds.Contains(args.ScreenPositionPx.X, args.ScreenPositionPx.Y);
332 }
333
335 protected override void OnKeyboardInput(WidgetKeyboardEventArgs args)
336 {
337 base.OnKeyboardInput(args);
338
339 if (args.Handled || args.KeyAction != KeyAction.Pressed)
340 return;
341
342 if (args.Key == CancelKey)
343 {
344 args.Handled = true;
345 Close(DialogResult.Cancel);
346 return;
347 }
348
349 if (args.Key == AcceptKey)
350 {
351 args.Handled = true;
353 }
354 }
355
357 protected override void OnCancelled()
358 {
359 base.OnCancelled();
360 Close(DialogResult.Cancel);
361 }
362
364 public override void Dispose()
365 {
366 if (_disposed)
367 return;
368
369 _disposed = true;
370
371 if (CloseButton is not null)
372 {
373 CloseButton.Clicked -= OnCloseButtonClicked;
374 }
375
376 base.Dispose();
377 GC.SuppressFinalize(this);
378 }
379
380 #endregion exposed methods and hooks
381
382 #region private methods
383
384 private void CompleteInitialization()
385 {
386 CanReceiveFocus = true;
389 }
390
391 private void OnCloseButtonClicked()
392 {
393 Close(DialogResult.Cancel);
394 }
395
396 private static void ValidateBounds(Rectangle bounds)
397 {
398 if (bounds.Width <= 0 || bounds.Height <= DefaultTitleBarHeight)
399 {
400 throw new ArgumentOutOfRangeException(
401 nameof(bounds),
402 bounds,
403 "Dialog bounds must have positive width and enough height for the title bar.");
404 }
405 }
406
407 private static Vector2 GetCloseButtonOffset(Size size)
408 {
409 return new Vector2(size.Width - DefaultCloseButtonSize - 4, 4);
410 }
411
412 private static DirectRectangle CreatePanel(
413 RenderSurfaceHostBase host,
414 View view,
415 Rectangle bounds)
416 {
417 return ConfigurePanel(
418 new DirectRectangle(
420 host,
421 view,
422 bounds));
423 }
424
425 private static DirectRectangle CreatePanel(
426 RenderSurfaceHostBase host,
427 SceneLayer layer,
428 Rectangle bounds)
429 {
430 return ConfigurePanel(
431 new DirectRectangle(
433 host,
434 layer,
435 bounds));
436 }
437
438 private static DirectRectangle ConfigurePanel(DirectRectangle panel)
439 {
440 panel.SetFilled(true)
442 .SetStrokeWidth(2f)
443 .SetCornerRadius(8f);
444
445 panel.ZOrder = 10_000;
446
447 return panel;
448 }
449
450 private static DirectRectangle CreateTitleBar(
451 RenderSurfaceHostBase host,
452 View view,
453 Rectangle bounds)
454 {
455 return ConfigureTitleBar(
456 new DirectRectangle(
458 host,
459 view,
460 GetTitleBarBounds(bounds)));
461 }
462
463 private static DirectRectangle CreateTitleBar(
464 RenderSurfaceHostBase host,
465 SceneLayer layer,
466 Rectangle bounds)
467 {
468 return ConfigureTitleBar(
469 new DirectRectangle(
471 host,
472 layer,
473 GetTitleBarBounds(bounds)));
474 }
475
476 private static DirectRectangle ConfigureTitleBar(DirectRectangle titleBar)
477 {
478 titleBar.SetFilled(true)
479 .SetCornerRadius(8f);
480
481 titleBar.ZOrder = 10_001;
482
483 return titleBar;
484 }
485
486 private static TextBlock CreateTitleText(
487 RenderSurfaceHostBase host,
488 View view,
489 Rectangle bounds,
490 string title)
491 {
492 return ConfigureTitleText(new TextBlock(host, view, GetTitleTextBounds(bounds)), title);
493 }
494
495 private static TextBlock CreateTitleText(
496 RenderSurfaceHostBase host,
497 SceneLayer layer,
498 Rectangle bounds,
499 string title)
500 {
501 return ConfigureTitleText(
502 new TextBlock(
503 host,
504 layer,
505 view: null,
506 worldBounds: GetTitleTextBounds(bounds)),
507 title);
508 }
509
510 private static TextBlock ConfigureTitleText(TextBlock titleText, string title)
511 {
512 titleText.SetText(title)
513 .SetFont(SKTypeface.Default, 18f, minSize: 12f)
514 .SetColors(SKColors.White, SKColors.Transparent)
515 .SetAlignment(SKTextAlign.Left, TextBlock.VerticalAlign.Center)
516 .EnableWrapping(false);
517
518 titleText.HorizontalPadding = 12f;
519 titleText.ZOrder = 10_002;
520
521 return titleText;
522 }
523
524 private static ButtonWidget CreateCloseButton(
525 RenderSurfaceHostBase host,
526 View view,
527 Rectangle bounds)
528 {
529 return ConfigureCloseButton(
530 new ButtonWidget(
531 host,
532 view,
533 GetCloseButtonBounds(bounds),
534 "×"));
535 }
536
537 private static ButtonWidget CreateCloseButton(
538 RenderSurfaceHostBase host,
539 SceneLayer layer,
540 Rectangle bounds)
541 {
542 return ConfigureCloseButton(
543 new ButtonWidget(
544 host,
545 layer,
546 GetCloseButtonBounds(bounds),
547 "×"));
548 }
549
550 private static ButtonWidget ConfigureCloseButton(ButtonWidget closeButton)
551 {
552 closeButton.SetBackgroundColors(
553 Color.FromArgb(
554 0,
555 0,
556 0,
557 0),
558 Color.FromArgb(
559 255,
560 110,
561 55,
562 62),
563 Color.FromArgb(
564 255,
565 145,
566 45,
567 55))
568 .SetTextColor(Color.White)
569 .SetButtonZOrder(10_003);
570
571 return closeButton;
572 }
573
574 private static Rectangle GetTitleBarBounds(Rectangle bounds)
575 {
576 return new Rectangle(
577 bounds.X,
578 bounds.Y,
579 bounds.Width,
581 }
582
583 private static Rectangle GetTitleTextBounds(
584 Rectangle bounds)
585 {
586 return new Rectangle(
587 bounds.X,
588 bounds.Y,
589 bounds.Width - DefaultCloseButtonSize - 12,
591 }
592
593 private static Rectangle GetCloseButtonBounds(
594 Rectangle bounds)
595 {
596 return new Rectangle(
597 bounds.Right - DefaultCloseButtonSize - 4,
598 bounds.Y + 4,
601 }
602
603 #endregion private methods
604}
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.
Provides the base implementation for direct drawing objects that can be rendered to the backbuffer wi...
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.
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.
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 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 ...
Viewport Viewport
Gets the viewport that defines the screen-space rectangle, zoom level, and other rendering parameters...
Definition View.cs:44
float Zoom
Zoom factor applied to the world when rendering this view. Values greater than 1 zoom in,...
Definition Viewport.cs:77
override DirectComposite Add(IDirectCompositeChild child, bool keepCurrentOffset=true, Vector2? explicitLocalOffsetPx=null)
Adds a composite-compatible direct drawable as a child.The current composite.
Represents a basic text button composed from a rectangle and text block.
ButtonWidget SetTextColor(Color color)
Sets the button text color.
ButtonWidget SetBackgroundColors(Color normal, Color hover, Color pressed)
Sets the normal, hover, and pressed background colors.
ButtonWidget SetButtonZOrder(int zOrder)
Sets the background Z-order and places the label immediately above it.
const int DefaultTitleBarHeight
The default title bar height.
Definition DialogBox.cs:22
DialogResult Result
Gets the result selected when the dialog closed.
Definition DialogBox.cs:208
override void OnKeyboardInput(WidgetKeyboardEventArgs args)
Called when keyboard input is routed to the widget.
Definition DialogBox.cs:335
override void ProcessActivated()
Performs processing when the widget is activated before notification callbacks are invoked.
Definition DialogBox.cs:273
static readonly Color DefaultPanelBorderColor
The default panel border color.
Definition DialogBox.cs:41
ButtonWidget? CloseButton
Gets the optional close button.
Definition DialogBox.cs:193
DialogBox(RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle bounds, string title, bool showCloseButton=true, string? nickname=null)
Initializes a scene-layer dialog.
Definition DialogBox.cs:130
DialogBox(RenderSurfaceHostBase renderSurfaceHost, View view, Rectangle bounds, string title, bool showCloseButton=true, string? nickname=null)
Initializes a view-level dialog.
Definition DialogBox.cs:80
static readonly Color DefaultTitleBarColor
The default title bar color.
Definition DialogBox.cs:50
virtual void OnAcceptRequested()
Called when the accept key is pressed.
Definition DialogBox.cs:267
Size DialogSize
Gets the dialog size.
Definition DialogBox.cs:198
static readonly Color DefaultPanelColor
The default panel color.
Definition DialogBox.cs:32
int AcceptKey
Gets or sets the key value used to accept the dialog.
Definition DialogBox.cs:221
const int DefaultCloseButtonSize
The default close button size.
Definition DialogBox.cs:27
bool DisposeOnClose
Gets or sets whether closing the dialog disposes it.
Definition DialogBox.cs:213
void Close(DialogResult result=DialogResult.Close)
Closes the dialog with the specified result.
Definition DialogBox.cs:239
virtual void OnClosed(DialogResult result)
Called after the dialog closes.
Definition DialogBox.cs:260
override bool CanStartDrag(WidgetPointerEventArgs args)
Determines whether a pointer-down event may begin drag tracking.true if the pointer event can begin a...
Definition DialogBox.cs:320
override void Dispose()
Disposes the composite and every child it owns.
Definition DialogBox.cs:364
bool IsClosed
Gets whether the dialog has closed.
Definition DialogBox.cs:203
DirectRectangle Panel
Gets the dialog panel.
Definition DialogBox.cs:178
Action< DialogResult >? Closed
Occurs when the dialog closes.
Definition DialogBox.cs:67
TextBlock TitleText
Gets the title text block.
Definition DialogBox.cs:188
DirectRectangle TitleBar
Gets the title-bar rectangle.
Definition DialogBox.cs:183
override void OnCancelled()
Called when the widget is cancelled.
Definition DialogBox.cs:357
int CancelKey
Gets or sets the key value used to cancel the dialog.
Definition DialogBox.cs:229
DraggableContainerWidget(RenderSurfaceHostBase renderSurfaceHost, DirectDrawingMode mode, PointF anchor=default, string? nickname=null)
Initializes a new instance of the DraggableContainerWidget class.
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 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 Handled
Gets or sets whether this widget interaction was handled.
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.
PointF WrappedOffsetWorldPx
World-space translation of the selected periodic widget instance. Retained throughout pointer capture...
View View
Gets the view through which the pointer interaction was routed.
PointF ScreenPositionPx
Gets the pointer position in screen pixels.
Defines a movable direct drawable that may be owned by a DirectComposite.
Defines a container that can hold and manage a collection of direct composite children.
IReadOnlyCollection< IDirectCompositeChild > Children
Gets the read-only collection of child elements in this container.
@ Rectangle
Particles are spawned uniformly within a rectangular area. This is the default distribution.
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
DialogResult
Represents the result produced when a dialog closes.
@ Close
The dialog was closed without a more specific result.