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
ComboBoxWidget.cs
1using System.Drawing;
2using System.Numerics;
8using SkiaSharp;
9
11
15public sealed class ComboBoxWidget : ContainerWidget
16{
17 private const int DefaultDropDownHeight = 144;
18
19 private readonly DropDownChevronDrawing _chevron;
20 private bool _isDropDownOpen;
21 private bool _disposed;
22 private string _placeholder = "Select...";
23
27 public event Action<int>? SelectedIndexChanged;
28
39 RenderSurfaceHostBase renderSurfaceHost,
40 View view,
41 Rectangle bounds,
42 IEnumerable<string>? items = null,
43 int dropDownHeight = DefaultDropDownHeight,
44 string? nickname = null)
45 : base(
46 renderSurfaceHost,
48 ValidateBounds(bounds).Location,
49 nickname)
50 {
51 ArgumentNullException.ThrowIfNull(view);
52 ValidateDropDownHeight(dropDownHeight);
53
54 Header = new ButtonWidget(
55 renderSurfaceHost,
56 view,
57 bounds,
58 string.Empty,
59 $"{Nickname}.header");
60
62 renderSurfaceHost,
63 view,
64 new Rectangle(
65 bounds.X,
66 bounds.Bottom,
67 bounds.Width,
68 dropDownHeight),
69 items,
70 $"{Nickname}.list");
71
72 _chevron = new DropDownChevronDrawing(
73 renderSurfaceHost,
74 view,
75 ResolveChevronBounds(bounds),
76 $"{Nickname}.chevron");
77
78 CompleteInitialization(bounds);
79 }
80
91 RenderSurfaceHostBase renderSurfaceHost,
92 SceneLayer sceneLayer,
93 Rectangle bounds,
94 IEnumerable<string>? items = null,
95 int dropDownHeight = DefaultDropDownHeight,
96 string? nickname = null)
97 : base(
98 renderSurfaceHost,
100 ValidateBounds(bounds).Location,
101 nickname)
102 {
103 ArgumentNullException.ThrowIfNull(sceneLayer);
104 ValidateDropDownHeight(dropDownHeight);
105
106 Header = new ButtonWidget(
107 renderSurfaceHost,
108 sceneLayer,
109 bounds,
110 string.Empty,
111 $"{Nickname}.header");
112
114 renderSurfaceHost,
115 sceneLayer,
116 new Rectangle(
117 bounds.X,
118 bounds.Bottom,
119 bounds.Width,
120 dropDownHeight),
121 items,
122 $"{Nickname}.list");
123
124 _chevron = new DropDownChevronDrawing(
125 renderSurfaceHost,
126 sceneLayer,
127 ResolveChevronBounds(bounds),
128 $"{Nickname}.chevron");
129
130 CompleteInitialization(bounds);
131 }
132
136 public ButtonWidget Header { get; }
137
141 public ListBoxWidget DropDown { get; }
142
146 public IReadOnlyList<string> Items => DropDown.Items;
147
151 public bool IsDropDownOpen => _isDropDownOpen;
152
156 public int SelectedIndex
157 {
158 get => DropDown.SelectedIndex;
159 set => DropDown.SelectedIndex = value;
160 }
161
165 public string? SelectedItem => DropDown.SelectedItem;
166
170 public string Placeholder
171 {
172 get => _placeholder;
173 set
174 {
175 _placeholder = value ?? string.Empty;
176 RefreshHeaderText();
177 }
178 }
179
185 public ComboBoxWidget SetItems(IEnumerable<string> items)
186 {
187 DropDown.SetItems(items);
188 RefreshHeaderText();
189 return this;
190 }
191
197 public ComboBoxWidget AddItem(string item)
198 {
199 DropDown.AddItem(item);
200 return this;
201 }
202
208 {
209 if (_isDropDownOpen || Items.Count == 0)
210 return this;
211
212 _isDropDownOpen = true;
213 DropDown.Show();
214 DropDown.Activate();
215 WidgetInputRouterRegistry.TryFocus(DropDown);
216 return this;
217 }
218
224 {
225 CollapseDropDown(restoreHeaderFocus: true);
226 return this;
227 }
228
237
244 {
245 Header.SetButtonZOrder(zOrder);
246 _chevron.ZOrder = zOrder + 2;
247 DropDown.SetListBoxZOrder(zOrder + 10);
248 return this;
249 }
250
257 {
258 Header.SetTextColor(color);
259 _chevron.SetColor(color);
260 return this;
261 }
262
264 public override void Dispose()
265 {
266 if (_disposed)
267 return;
268
269 _disposed = true;
270 Header.Clicked -= OnHeaderClicked;
271 DropDown.SelectedIndexChanged -= OnSelectedIndexChanged;
272 DropDown.SelectionCommitted -= OnDropDownSelectionCommitted;
273 DropDown.FocusLost -= OnDropDownFocusLost;
274 base.Dispose();
275 }
276
278 protected override void ProcessShown()
279 {
280 base.ProcessShown();
281
282 if (!_isDropDownOpen)
283 DropDown.Hide();
284 }
285
287 protected override void ProcessHidden()
288 {
289 _isDropDownOpen = false;
290 base.ProcessHidden();
291 }
292
293 private void CompleteInitialization(Rectangle bounds)
294 {
295 ConfigureHeaderContentBounds(bounds);
296
297 Add(Header, Vector2.Zero);
298 Add(_chevron);
299 Add(DropDown, new Vector2(0f, bounds.Height));
300 Header.Clicked += OnHeaderClicked;
301 DropDown.SelectedIndexChanged += OnSelectedIndexChanged;
302 DropDown.SelectionCommitted += OnDropDownSelectionCommitted;
303 DropDown.FocusLost += OnDropDownFocusLost;
304
305 IsInputEnabled = false;
306 IsPointerInputEnabled = false;
308 CanReceiveFocus = false;
309
310 DropDown.Hide();
312 RefreshHeaderText();
313 }
314
315 private void OnHeaderClicked()
316 {
318 }
319
320 private void OnSelectedIndexChanged(int index)
321 {
322 RefreshHeaderText();
323 SelectedIndexChanged?.Invoke(index);
324 }
325
326 private void OnDropDownSelectionCommitted(int index)
327 {
328 if (index >= 0)
330 }
331
332 private void OnDropDownFocusLost()
333 {
334 if (!_isDropDownOpen)
335 return;
336
337 // Pointer-down moves focus before the header click is dispatched. If focus
338 // moved back to this combo's own header, leave the list open long enough
339 // for that click to toggle it closed normally. Any other focus target means
340 // the user has moved on, so collapse without stealing focus back.
341 WidgetBase? focusedWidget = WidgetInputRouterRegistry.GetFocusedWidget(DropDown);
342
343 if (ReferenceEquals(focusedWidget, Header) || ReferenceEquals(focusedWidget, DropDown))
344 return;
345
346 CollapseDropDown(restoreHeaderFocus: false);
347 }
348
349 private void CollapseDropDown(bool restoreHeaderFocus)
350 {
351 if (!_isDropDownOpen)
352 return;
353
354 _isDropDownOpen = false;
355 DropDown.Hide();
356
357 if (!restoreHeaderFocus)
358 return;
359
360 Header.Activate();
361 WidgetInputRouterRegistry.TryFocus(Header);
362 }
363
364 private void RefreshHeaderText()
365 {
366 Header.SetText(SelectedItem ?? Placeholder);
367 }
368
369 private void ConfigureHeaderContentBounds(Rectangle bounds)
370 {
371 Rectangle labelBounds = new(
372 bounds.Left + 4,
373 bounds.Top,
374 Math.Max(1, bounds.Width - 28),
375 bounds.Height);
376
377 if (Mode == DirectDrawingMode.View)
378 Header.Label.ScreenBounds = labelBounds;
379 else
380 Header.Label.WorldBounds = labelBounds;
381 }
382
383 private static Rectangle ResolveChevronBounds(Rectangle bounds)
384 {
385 const int width = 18;
386
387 return new Rectangle(
388 Math.Max(bounds.Left, bounds.Right - width - 4),
389 bounds.Top,
390 Math.Min(width, bounds.Width),
391 bounds.Height);
392 }
393
394 private static Rectangle ValidateBounds(Rectangle bounds)
395 {
396 if (bounds.Width < 60 || bounds.Height < 20)
397 throw new ArgumentOutOfRangeException(nameof(bounds), bounds, "Combo-box bounds are too small.");
398
399 return bounds;
400 }
401
402 private static void ValidateDropDownHeight(int height)
403 {
404 if (height < 28)
405 throw new ArgumentOutOfRangeException(nameof(height), "Drop-down height must be at least 28 pixels.");
406 }
407
408 private sealed class DropDownChevronDrawing : DirectDrawingMovableBase
409 {
410 private SKColor _color = SKColors.White;
411
412 internal DropDownChevronDrawing(
413 RenderSurfaceHostBase host,
414 View view,
415 Rectangle bounds,
416 string nickname)
417 : base(
418 host,
420 null,
421 view,
422 bounds,
423 null,
424 nickname)
425 {
426 }
427
428 internal DropDownChevronDrawing(
429 RenderSurfaceHostBase host,
430 SceneLayer sceneLayer,
431 Rectangle bounds,
432 string nickname)
433 : base(
434 host,
436 sceneLayer,
437 null,
438 null,
439 bounds,
440 nickname)
441 {
442 }
443
444 internal void SetColor(Color color)
445 {
446 _color = new SKColor(
447 color.R,
448 color.G,
449 color.B,
450 color.A);
451
452 ForceRefresh();
453 }
454
456 protected override void OnDraw(BackbufferBase backbuffer, RectangleF destRectScreen)
457 {
458 float width = Math.Min(8f, destRectScreen.Width - 4f);
459 float centerX = destRectScreen.Left + destRectScreen.Width / 2f;
460 float centerY = destRectScreen.Top + destRectScreen.Height / 2f;
461
462 using var paint = new SKPaint
463 {
464 Color = _color,
465 IsAntialias = true,
466 Style = SKPaintStyle.Stroke,
467 StrokeWidth = 1.75f,
468 StrokeCap = SKStrokeCap.Round,
469 StrokeJoin = SKStrokeJoin.Round
470 };
471
472 using var path = new SKPath();
473
474 path.MoveTo(centerX - width / 2f, centerY - 2f);
475 path.LineTo(centerX, centerY + 2f);
476 path.LineTo(centerX + width / 2f, centerY - 2f);
477
478 backbuffer.Canvas.DrawPath(path, paint);
479 }
480 }
481}
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.
DirectDrawingMode Mode
Gets the coordinate mode shared by the composite and its children.
View? View
Gets the view to which this direct drawing is attached, or null if the drawing is in scene-layer mode...
SceneLayer? SceneLayer
Gets the scene layer to which this direct drawing is attached, or null if the drawing is in view mode...
SKCanvas Canvas
Gets the SkiaSharp canvas used for drawing operations on this backbuffer.
Represents a base class for hosting a render surface, providing functionality for managing rendering ...
override DirectComposite Add(IDirectCompositeChild child, bool keepCurrentOffset=true, Vector2? explicitLocalOffsetPx=null)
Adds a composite-compatible direct drawable as a child.The current composite.
ContainerWidget(RenderSurfaceHostBase renderSurfaceHost, DirectDrawingMode mode, PointF anchor=default, string? nickname=null)
Initializes a new instance of the ContainerWidget class.
Represents a basic text button composed from a rectangle and text block.
string Placeholder
Gets or sets the text displayed while no item is selected.
ComboBoxWidget SetHeaderTextColor(Color color)
Sets the collapsed header text and chevron color.
string? SelectedItem
Gets the selected item text, or null.
override void ProcessHidden()
Performs processing when the widget is hidden before notification callbacks are invoked.
ComboBoxWidget AddItem(string item)
Adds an item to the end of the combo box.
ComboBoxWidget SetItems(IEnumerable< string > items)
Replaces all available items and clears the current selection.
ButtonWidget Header
Gets the collapsed button portion of the combo box.
bool IsDropDownOpen
Gets whether the drop-down list is currently open.
ListBoxWidget DropDown
Gets the list displayed while the combo box is expanded.
IReadOnlyList< string > Items
Gets the available items.
ComboBoxWidget(RenderSurfaceHostBase renderSurfaceHost, View view, Rectangle bounds, IEnumerable< string >? items=null, int dropDownHeight=DefaultDropDownHeight, string? nickname=null)
Creates a view-level combo box.
override void ProcessShown()
Performs processing when the widget is shown before notification callbacks are invoked.
ComboBoxWidget CloseDropDown()
Closes the drop-down list.
ComboBoxWidget ToggleDropDown()
Toggles the drop-down list.
override void Dispose()
Disposes the composite and every child it owns.
Action< int >? SelectedIndexChanged
Occurs when SelectedIndex changes.
ComboBoxWidget SetComboBoxZOrder(int zOrder)
Sets the base Z-order used by the combo-box visuals.
ComboBoxWidget OpenDropDown()
Opens the drop-down list when items are available.
ComboBoxWidget(RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle bounds, IEnumerable< string >? items=null, int dropDownHeight=DefaultDropDownHeight, string? nickname=null)
Creates a scene-layer combo box.
int SelectedIndex
Gets or sets the selected item index. Use -1 to clear selection.
Provides a selectable, vertically scrolling list of text items.
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(RenderSurfaceHostBase renderSurfaceHost, DirectDrawingMode mode, PointF anchor=default, string? nickname=null)
Initializes a new instance of the WidgetBase class.
Definition WidgetBase.cs:95
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 IsInputEnabled
Gets or sets a value indicating whether the widget can participate in input routing.
@ 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.