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
RadioButtonWidget.cs
1using System.Drawing;
7using SkiaSharp;
8
10
14public sealed class RadioButtonGroup
15{
16 private readonly HashSet<RadioButtonWidget> _buttons = [];
17
21 public RadioButtonWidget? SelectedButton => _buttons.FirstOrDefault(static button => button.IsSelected);
22
23 internal void Register(RadioButtonWidget button)
24 {
25 ArgumentNullException.ThrowIfNull(button);
26 _buttons.Add(button);
27
28 if (button.IsSelected)
29 Select(button);
30 }
31
32 internal void Unregister(RadioButtonWidget button)
33 {
34 ArgumentNullException.ThrowIfNull(button);
35 _buttons.Remove(button);
36 }
37
38 internal void Select(RadioButtonWidget selected)
39 {
40 ArgumentNullException.ThrowIfNull(selected);
41
42 foreach (RadioButtonWidget button in _buttons.ToArray())
43 button.SetSelectedFromGroup(ReferenceEquals(button, selected));
44 }
45}
46
50public sealed class RadioButtonWidget : WidgetBase
51{
52 private const int DefaultIndicatorSize = 20;
53 private const int DefaultLabelGap = 8;
54 private const int DotInset = 6;
55
56 private Color _normalColor = Color.FromArgb(
57 255,
58 48,
59 48,
60 56);
61
62 private Color _hoverColor = Color.FromArgb(
63 255,
64 66,
65 66,
66 76);
67
68 private Color _dotColor = Color.FromArgb(
69 255,
70 235,
71 235,
72 242);
73
74 private RadioButtonGroup? _group;
75 private bool _isSelected;
76 private bool _disposed;
77
81 public event Action<bool>? SelectionChanged;
82
94 RenderSurfaceHostBase renderSurfaceHost,
95 View view,
96 Rectangle bounds,
97 string text,
98 RadioButtonGroup? group = null,
99 bool isSelected = false,
100 string? nickname = null)
101 : base(
102 renderSurfaceHost,
104 ValidateBounds(bounds).Location,
105 nickname)
106 {
107 ArgumentNullException.ThrowIfNull(view);
108
109 Rectangle indicatorBounds = GetIndicatorBounds(bounds);
110 Rectangle dotBounds = GetDotBounds(indicatorBounds);
111 Rectangle labelBounds = GetLabelBounds(bounds, indicatorBounds.Width);
112
113 Ring = CreateRing(renderSurfaceHost, view, indicatorBounds);
114 Dot = CreateDot(renderSurfaceHost, view, dotBounds);
115
116 Label = CreateLabel(
117 renderSurfaceHost,
118 view,
119 labelBounds,
120 text);
121
122 Add(Ring);
123 Add(Dot);
124 Add(Label);
125
126 CompleteInitialization(group, isSelected);
127 }
128
140 RenderSurfaceHostBase renderSurfaceHost,
141 SceneLayer sceneLayer,
142 Rectangle bounds,
143 string text,
144 RadioButtonGroup? group = null,
145 bool isSelected = false,
146 string? nickname = null)
147 : base(
148 renderSurfaceHost,
150 ValidateBounds(bounds).Location,
151 nickname)
152 {
153 ArgumentNullException.ThrowIfNull(sceneLayer);
154
155 Rectangle indicatorBounds = GetIndicatorBounds(bounds);
156 Rectangle dotBounds = GetDotBounds(indicatorBounds);
157 Rectangle labelBounds = GetLabelBounds(bounds, indicatorBounds.Width);
158
159 Ring = CreateRing(renderSurfaceHost, sceneLayer, indicatorBounds);
160 Dot = CreateDot(renderSurfaceHost, sceneLayer, dotBounds);
161
162 Label = CreateLabel(
163 renderSurfaceHost,
164 sceneLayer,
165 labelBounds,
166 text);
167
168 Add(Ring);
169 Add(Dot);
170 Add(Label);
171
172 CompleteInitialization(group, isSelected);
173 }
174
178 public DirectRectangle Ring { get; }
179
183 public DirectRectangle Dot { get; }
184
188 public TextBlock Label { get; }
189
194 {
195 get => _group;
196 set
197 {
198 if (ReferenceEquals(_group, value))
199 return;
200
201 _group?.Unregister(this);
202 _group = value;
203 _group?.Register(this);
204 }
205 }
206
210 public bool IsSelected
211 {
212 get => _isSelected;
213 set => SetSelected(value);
214 }
215
221 public RadioButtonWidget SetSelected(bool isSelected)
222 {
223 if (isSelected && Group is not null)
224 {
225 Group.Select(this);
226 return this;
227 }
228
229 SetSelectedFromGroup(isSelected);
230 return this;
231 }
232
238 {
239 return SetSelected(true);
240 }
241
247 public RadioButtonWidget SetText(string text)
248 {
249 Label.SetText(text ?? string.Empty);
250 return this;
251 }
252
261 Color normal,
262 Color hover,
263 Color dot)
264 {
265 _normalColor = normal;
266 _hoverColor = hover;
267 _dotColor = dot;
268
269 UpdateRingColor(_normalColor);
270 Dot.SetColor(_dotColor);
271 Refresh(Dot);
272 return this;
273 }
274
280 public RadioButtonWidget SetTextColor(SKColor color)
281 {
282 Label.SetColors(color, SKColors.Transparent);
283 return this;
284 }
285
292 {
293 Ring.ZOrder = zOrder;
294 Dot.ZOrder = zOrder + 1;
295 Label.ZOrder = zOrder + 1;
296 return this;
297 }
298
300 public override void Dispose()
301 {
302 if (_disposed)
303 return;
304
305 _disposed = true;
306 Group = null;
307 base.Dispose();
308 }
309
311 protected override void ProcessShown()
312 {
313 base.ProcessShown();
314 Dot.Visible = IsSelected;
315 }
316
318 protected override void OnPointerEnter(WidgetPointerEventArgs args)
319 {
320 base.OnPointerEnter(args);
321 UpdateRingColor(_hoverColor);
322 }
323
325 protected override void OnPointerLeave(WidgetPointerEventArgs args)
326 {
327 base.OnPointerLeave(args);
328 UpdateRingColor(_normalColor);
329 }
330
332 protected override void OnPointerClick(WidgetPointerEventArgs args)
333 {
334 base.OnPointerClick(args);
335
336 if (!args.IsPrimaryButton)
337 return;
338
339 args.Handled = true;
340 Select();
341 }
342
344 protected override void OnKeyboardInput(WidgetKeyboardEventArgs args)
345 {
346 base.OnKeyboardInput(args);
347
348 if (args.KeyAction != KeyAction.Pressed || args.Key != 32)
349 return;
350
351 args.Handled = true;
352 Select();
353 }
354
355 internal void SetSelectedFromGroup(bool isSelected)
356 {
357 if (_isSelected == isSelected)
358 return;
359
360 _isSelected = isSelected;
361 Dot.Visible = isSelected;
362 SelectionChanged?.Invoke(isSelected);
363 }
364
365 private void CompleteInitialization(RadioButtonGroup? group, bool isSelected)
366 {
367 _isSelected = isSelected;
368 Dot.Visible = isSelected;
370
371 CanReceiveFocus = true;
373
374 Group = group;
375 }
376
377 private void UpdateRingColor(Color color)
378 {
379 Ring.SetColor(color);
380 Refresh(Ring);
381 }
382
383 private static void Refresh(DirectRectangle rectangle)
384 {
385 rectangle.SetPosition(rectangle.GetPosition());
386 }
387
388 private DirectRectangle CreateRing(
389 RenderSurfaceHostBase host,
390 View view,
391 Rectangle bounds)
392 {
393 return ConfigureRing(
394 new DirectRectangle(
395 _normalColor,
396 host,
397 view,
398 bounds,
399 $"{Nickname}.ring"),
400 bounds.Width);
401 }
402
403 private DirectRectangle CreateRing(
404 RenderSurfaceHostBase host,
405 SceneLayer layer,
406 Rectangle bounds)
407 {
408 return ConfigureRing(
409 new DirectRectangle(
410 _normalColor,
411 host,
412 layer,
413 bounds,
414 $"{Nickname}.ring"),
415 bounds.Width);
416 }
417
418 private static DirectRectangle ConfigureRing(DirectRectangle ring, int size)
419 {
420 return ring.SetFilled(true)
422 Color.FromArgb(
423 255,
424 150,
425 150,
426 160))
427 .SetStrokeWidth(1.5f)
428 .SetCornerRadius(size / 2f);
429 }
430
431 private DirectRectangle CreateDot(
432 RenderSurfaceHostBase host,
433 View view,
434 Rectangle bounds)
435 {
436 return ConfigureDot(
437 new DirectRectangle(
438 _dotColor,
439 host,
440 view,
441 bounds,
442 $"{Nickname}.dot"),
443 bounds.Width);
444 }
445
446 private DirectRectangle CreateDot(
447 RenderSurfaceHostBase host,
448 SceneLayer layer,
449 Rectangle bounds)
450 {
451 return ConfigureDot(
452 new DirectRectangle(
453 _dotColor,
454 host,
455 layer,
456 bounds,
457 $"{Nickname}.dot"),
458 bounds.Width);
459 }
460
461 private static DirectRectangle ConfigureDot(DirectRectangle dot, int size)
462 {
463 return dot.SetFilled(true)
464 .SetStrokeWidth(0f)
465 .SetCornerRadius(size / 2f);
466 }
467
468 private static TextBlock CreateLabel(
469 RenderSurfaceHostBase host,
470 View view,
471 Rectangle bounds,
472 string text)
473 {
474 return ConfigureLabel(new TextBlock(host, view, bounds), text);
475 }
476
477 private static TextBlock CreateLabel(
478 RenderSurfaceHostBase host,
479 SceneLayer layer,
480 Rectangle bounds,
481 string text)
482 {
483 return ConfigureLabel(
484 new TextBlock(
485 host,
486 layer,
487 view: null,
488 worldBounds: bounds),
489 text);
490 }
491
492 private static TextBlock ConfigureLabel(TextBlock label, string text)
493 {
494 return label.SetText(text ?? string.Empty)
495 .SetFont(SKTypeface.Default, 16f, minSize: 10f)
496 .SetColors(SKColors.White, SKColors.Transparent)
497 .SetAlignment(SKTextAlign.Left, TextBlock.VerticalAlign.Center)
498 .EnableWrapping(false);
499 }
500
501 private static Rectangle GetIndicatorBounds(Rectangle bounds)
502 {
503 int size = Math.Min(DefaultIndicatorSize, Math.Max(10, bounds.Height - 2));
504
505 return new Rectangle(
506 bounds.X,
507 bounds.Y + (bounds.Height - size) / 2,
508 size,
509 size);
510 }
511
512 private static Rectangle GetDotBounds(Rectangle indicatorBounds)
513 {
514 int inset = Math.Min(DotInset, Math.Max(2, indicatorBounds.Width / 3));
515 return Rectangle.Inflate(indicatorBounds, -inset, -inset);
516 }
517
518 private static Rectangle GetLabelBounds(Rectangle bounds, int indicatorWidth)
519 {
520 int left = bounds.X + indicatorWidth + DefaultLabelGap;
521
522 return new Rectangle(
523 left,
524 bounds.Y,
525 Math.Max(1, bounds.Right - left),
526 bounds.Height);
527 }
528
529 private static Rectangle ValidateBounds(Rectangle bounds)
530 {
531 if (bounds.Width < 40 || bounds.Height < 12)
532 throw new ArgumentOutOfRangeException(nameof(bounds), bounds, "Radio-button bounds are too small.");
533
534 return bounds;
535 }
536}
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.
Vector2 GetPosition()
Gets the current position of this direct drawing in pixel coordinates.
void SetPosition(Vector2 p)
Sets the position of this direct drawing to the specified pixel coordinates.
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 SetColor(Color color)
Sets the base color used for both fill and stroke (unless a distinct border color is specified).
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 ...
Coordinates mutually exclusive RadioButtonWidget instances.
RadioButtonWidget? SelectedButton
Gets the currently selected radio button, or null.
Provides a radio-button control with optional mutually exclusive grouping.
RadioButtonWidget(RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle bounds, string text, RadioButtonGroup? group=null, bool isSelected=false, string? nickname=null)
Creates a scene-layer radio button.
RadioButtonWidget SetText(string text)
Changes the label text.
override void OnPointerClick(WidgetPointerEventArgs args)
Called when a pointer click is dispatched to the widget.
RadioButtonWidget(RenderSurfaceHostBase renderSurfaceHost, View view, Rectangle bounds, string text, RadioButtonGroup? group=null, bool isSelected=false, string? nickname=null)
Creates a view-level radio button.
override void OnPointerEnter(WidgetPointerEventArgs args)
Called when a pointer enters the widget bounds.
TextBlock Label
Gets the label displayed beside the radio button.
override void ProcessShown()
Performs processing when the widget is shown before notification callbacks are invoked.
RadioButtonWidget SetRadioButtonZOrder(int zOrder)
Sets the base Z-order used by the radio-button visuals.
override void OnKeyboardInput(WidgetKeyboardEventArgs args)
Called when keyboard input is routed to the widget.
Action< bool >? SelectionChanged
Occurs when IsSelected changes.
RadioButtonGroup? Group
Gets or sets the group that coordinates this radio button.
override void Dispose()
Disposes the composite and every child it owns.
bool IsSelected
Gets or sets whether the radio button is selected.
override void OnPointerLeave(WidgetPointerEventArgs args)
Called when a pointer leaves the widget bounds.
DirectRectangle Ring
Gets the outer radio-button ring.
RadioButtonWidget SetTextColor(SKColor color)
Sets the label text color.
DirectRectangle Dot
Gets the inner selection dot.
RadioButtonWidget SetColors(Color normal, Color hover, Color dot)
Sets the normal, hover, and selected-dot colors.
RadioButtonWidget SetSelected(bool isSelected)
Sets the selection state.
RadioButtonWidget Select()
Selects this button and clears any other selection in its group.
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.
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.
@ 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