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
CheckBoxWidget.cs
1using System.Drawing;
7using SkiaSharp;
8
10
14public sealed class CheckBoxWidget : WidgetBase
15{
16 private const int DefaultIndicatorSize = 20;
17 private const int DefaultLabelGap = 8;
18 private const int MarkInset = 5;
19
20 private Color _normalColor = Color.FromArgb(
21 255,
22 48,
23 48,
24 56);
25
26 private Color _hoverColor = Color.FromArgb(
27 255,
28 66,
29 66,
30 76);
31
32 private Color _markColor = Color.FromArgb(
33 255,
34 235,
35 235,
36 242);
37
38 private bool _isChecked;
39
43 public event Action<bool>? CheckedChanged;
44
55 RenderSurfaceHostBase renderSurfaceHost,
56 View view,
57 Rectangle bounds,
58 string text,
59 bool isChecked = false,
60 string? nickname = null)
61 : base(
62 renderSurfaceHost,
64 ValidateBounds(bounds).Location,
65 nickname)
66 {
67 ArgumentNullException.ThrowIfNull(view);
68
69 Rectangle indicatorBounds = GetIndicatorBounds(bounds);
70 Rectangle markBounds = GetMarkBounds(indicatorBounds);
71 Rectangle labelBounds = GetLabelBounds(bounds, indicatorBounds.Width);
72
73 Box = CreateBox(renderSurfaceHost, view, indicatorBounds);
74 Mark = CreateMark(renderSurfaceHost, view, markBounds);
75
76 Label = CreateLabel(
77 renderSurfaceHost,
78 view,
79 labelBounds,
80 text);
81
82 Add(Box);
83 Add(Mark);
84 Add(Label);
85
86 CompleteInitialization(isChecked);
87 }
88
99 RenderSurfaceHostBase renderSurfaceHost,
100 SceneLayer sceneLayer,
101 Rectangle bounds,
102 string text,
103 bool isChecked = false,
104 string? nickname = null)
105 : base(
106 renderSurfaceHost,
108 ValidateBounds(bounds).Location,
109 nickname)
110 {
111 ArgumentNullException.ThrowIfNull(sceneLayer);
112
113 Rectangle indicatorBounds = GetIndicatorBounds(bounds);
114 Rectangle markBounds = GetMarkBounds(indicatorBounds);
115 Rectangle labelBounds = GetLabelBounds(bounds, indicatorBounds.Width);
116
117 Box = CreateBox(renderSurfaceHost, sceneLayer, indicatorBounds);
118 Mark = CreateMark(renderSurfaceHost, sceneLayer, markBounds);
119
120 Label = CreateLabel(
121 renderSurfaceHost,
122 sceneLayer,
123 labelBounds,
124 text);
125
126 Add(Box);
127 Add(Mark);
128 Add(Label);
129
130 CompleteInitialization(isChecked);
131 }
132
136 public DirectRectangle Box { get; }
137
141 public DirectRectangle Mark { get; }
142
146 public TextBlock Label { get; }
147
151 public bool IsChecked
152 {
153 get => _isChecked;
154 set => SetChecked(value);
155 }
156
160 public Rectangle Bounds
161 {
162 get
163 {
164 Rectangle boxBounds = Mode == DirectDrawingMode.View ? Box.ScreenBounds : Box.WorldBounds;
165 Rectangle labelBounds = Mode == DirectDrawingMode.View ? Label.ScreenBounds : Label.WorldBounds;
166 return Rectangle.Union(boxBounds, labelBounds);
167 }
168 }
169
175 public CheckBoxWidget SetChecked(bool isChecked)
176 {
177 if (_isChecked == isChecked)
178 return this;
179
180 _isChecked = isChecked;
181 Mark.Visible = isChecked;
182 CheckedChanged?.Invoke(isChecked);
183 return this;
184 }
185
191 {
192 return SetChecked(!IsChecked);
193 }
194
200 public CheckBoxWidget SetText(string text)
201 {
202 Label.SetText(text ?? string.Empty);
203 return this;
204 }
205
214 Color normal,
215 Color hover,
216 Color mark)
217 {
218 _normalColor = normal;
219 _hoverColor = hover;
220 _markColor = mark;
221
222 UpdateBoxColor(IsFocused ? _hoverColor : _normalColor);
223 Mark.SetColor(_markColor);
224 Refresh(Mark);
225 return this;
226 }
227
233 public CheckBoxWidget SetTextColor(SKColor color)
234 {
235 Label.SetColors(color, SKColors.Transparent);
236 return this;
237 }
238
245 {
246 Box.ZOrder = zOrder;
247 Mark.ZOrder = zOrder + 1;
248 Label.ZOrder = zOrder + 1;
249 return this;
250 }
251
253 protected override void ProcessShown()
254 {
255 base.ProcessShown();
256 Mark.Visible = IsChecked;
257 }
258
260 protected override void OnPointerEnter(WidgetPointerEventArgs args)
261 {
262 base.OnPointerEnter(args);
263 UpdateBoxColor(_hoverColor);
264 }
265
267 protected override void OnPointerLeave(WidgetPointerEventArgs args)
268 {
269 base.OnPointerLeave(args);
270 UpdateBoxColor(_normalColor);
271 }
272
274 protected override void OnPointerClick(WidgetPointerEventArgs args)
275 {
276 base.OnPointerClick(args);
277
278 if (!args.IsPrimaryButton)
279 return;
280
281 args.Handled = true;
282 Toggle();
283 }
284
286 protected override void OnKeyboardInput(WidgetKeyboardEventArgs args)
287 {
288 base.OnKeyboardInput(args);
289
290 if (args.KeyAction != KeyAction.Pressed || args.Key != 32)
291 return;
292
293 args.Handled = true;
294 Toggle();
295 }
296
297 private void CompleteInitialization(bool isChecked)
298 {
299 _isChecked = isChecked;
300 Mark.Visible = isChecked;
302
303 CanReceiveFocus = true;
305 }
306
307 private void UpdateBoxColor(Color color)
308 {
309 Box.SetColor(color);
310 Refresh(Box);
311 }
312
313 private static void Refresh(DirectRectangle rectangle)
314 {
315 rectangle.SetPosition(rectangle.GetPosition());
316 }
317
318 private DirectRectangle CreateBox(
320 View view,
321 Rectangle bounds)
322 {
323 return ConfigureBox(
324 new DirectRectangle(
325 _normalColor,
326 host,
327 view,
328 bounds,
329 $"{Nickname}.box"));
330 }
331
332 private DirectRectangle CreateBox(
333 RenderSurfaceHostBase host,
334 SceneLayer layer,
335 Rectangle bounds)
336 {
337 return ConfigureBox(
338 new DirectRectangle(
339 _normalColor,
340 host,
341 layer,
342 bounds,
343 $"{Nickname}.box"));
344 }
345
346 private static DirectRectangle ConfigureBox(DirectRectangle box)
347 {
348 return box.SetFilled(true)
350 Color.FromArgb(
351 255,
352 150,
353 150,
354 160))
355 .SetStrokeWidth(1.5f)
356 .SetCornerRadius(3f);
357 }
358
359 private DirectRectangle CreateMark(
360 RenderSurfaceHostBase host,
361 View view,
362 Rectangle bounds)
363 {
364 return ConfigureMark(
365 new DirectRectangle(
366 _markColor,
367 host,
368 view,
369 bounds,
370 $"{Nickname}.mark"));
371 }
372
373 private DirectRectangle CreateMark(
374 RenderSurfaceHostBase host,
375 SceneLayer layer,
376 Rectangle bounds)
377 {
378 return ConfigureMark(
379 new DirectRectangle(
380 _markColor,
381 host,
382 layer,
383 bounds,
384 $"{Nickname}.mark"));
385 }
386
387 private static DirectRectangle ConfigureMark(DirectRectangle mark)
388 {
389 return mark.SetFilled(true)
390 .SetStrokeWidth(0f)
391 .SetCornerRadius(2f);
392 }
393
394 private static TextBlock CreateLabel(
395 RenderSurfaceHostBase host,
396 View view,
397 Rectangle bounds,
398 string text)
399 {
400 return ConfigureLabel(new TextBlock(host, view, bounds), text);
401 }
402
403 private static TextBlock CreateLabel(
404 RenderSurfaceHostBase host,
405 SceneLayer layer,
406 Rectangle bounds,
407 string text)
408 {
409 return ConfigureLabel(
410 new TextBlock(
411 host,
412 layer,
413 view: null,
414 worldBounds: bounds),
415 text);
416 }
417
418 private static TextBlock ConfigureLabel(TextBlock label, string text)
419 {
420 return label.SetText(text ?? string.Empty)
421 .SetFont(SKTypeface.Default, 16f, minSize: 10f)
422 .SetColors(SKColors.White, SKColors.Transparent)
423 .SetAlignment(SKTextAlign.Left, TextBlock.VerticalAlign.Center)
424 .EnableWrapping(false);
425 }
426
427 private static Rectangle GetIndicatorBounds(Rectangle bounds)
428 {
429 int size = Math.Min(DefaultIndicatorSize, Math.Max(10, bounds.Height - 2));
430
431 return new Rectangle(
432 bounds.X,
433 bounds.Y + (bounds.Height - size) / 2,
434 size,
435 size);
436 }
437
438 private static Rectangle GetMarkBounds(Rectangle indicatorBounds)
439 {
440 int inset = Math.Min(MarkInset, Math.Max(2, indicatorBounds.Width / 4));
441 return Rectangle.Inflate(indicatorBounds, -inset, -inset);
442 }
443
444 private static Rectangle GetLabelBounds(Rectangle bounds, int indicatorWidth)
445 {
446 int left = bounds.X + indicatorWidth + DefaultLabelGap;
447
448 return new Rectangle(
449 left,
450 bounds.Y,
451 Math.Max(1, bounds.Right - left),
452 bounds.Height);
453 }
454
455 private static Rectangle ValidateBounds(Rectangle bounds)
456 {
457 if (bounds.Width < 40 || bounds.Height < 12)
458 throw new ArgumentOutOfRangeException(nameof(bounds), bounds, "Checkbox bounds are too small.");
459
460 return bounds;
461 }
462}
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.
DirectDrawingMode Mode
Gets the coordinate mode shared by the composite and its children.
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 ...
Represents a rendered view of a scene, combining a camera position with a viewport configuration to c...
Definition View.cs:14
override void OnPointerLeave(WidgetPointerEventArgs args)
Called when a pointer leaves the widget bounds.
CheckBoxWidget SetColors(Color normal, Color hover, Color mark)
Sets the normal, hover, and checked-mark colors.
CheckBoxWidget SetCheckBoxZOrder(int zOrder)
Sets the base Z-order used by the checkbox visuals.
CheckBoxWidget Toggle()
Toggles the checkbox state.
DirectRectangle Box
Gets the outer checkbox indicator.
CheckBoxWidget SetText(string text)
Changes the checkbox label text.
CheckBoxWidget(RenderSurfaceHostBase renderSurfaceHost, View view, Rectangle bounds, string text, bool isChecked=false, string? nickname=null)
Creates a view-level checkbox.
override void ProcessShown()
Performs processing when the widget is shown before notification callbacks are invoked.
override void OnPointerEnter(WidgetPointerEventArgs args)
Called when a pointer enters the widget bounds.
CheckBoxWidget SetChecked(bool isChecked)
Sets the checkbox state.
bool IsChecked
Gets or sets whether the checkbox is checked.
CheckBoxWidget(RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle bounds, string text, bool isChecked=false, string? nickname=null)
Creates a scene-layer checkbox.
CheckBoxWidget SetTextColor(SKColor color)
Sets the label text color.
override void OnKeyboardInput(WidgetKeyboardEventArgs args)
Called when keyboard input is routed to the widget.
DirectRectangle Mark
Gets the filled mark displayed while the checkbox is checked.
Action< bool >? CheckedChanged
Occurs when IsChecked changes.
TextBlock Label
Gets the text label displayed beside the checkbox.
Rectangle Bounds
Gets the checkbox bounds in its native coordinate space.
override void OnPointerClick(WidgetPointerEventArgs args)
Called when a pointer click is dispatched to the 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 IsFocused
Gets a value indicating whether the widget currently has keyboard focus.
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.
@ View
Performs direct drawing at the view level using screen-space coordinates.
KeyAction
Represents the type of action performed on a keyboard key, indicating whether the key was pressed,...
Definition KeyAction.cs:10