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
ListBoxWidget.cs
1using System.Drawing;
2using System.Numerics;
8using SkiaSharp;
9
11
15public sealed class ListBoxWidget : WidgetBase
16{
17 private const int ContentPadding = 2;
18 private const int DefaultItemHeight = 24;
19 private const int ScrollBarWidth = 12;
20 private const int ScrollBarMargin = 3;
21 private const int MinimumScrollBarThumbHeight = 18;
22
23 private readonly List<string> _items = [];
24 private readonly List<TextBlock> _rowTextBlocks = [];
25
26 private int _selectedIndex = -1;
27 private int _topIndex;
28 private int _itemHeight = DefaultItemHeight;
29 private int _baseZOrder;
30 private int _mouseWheelScrollItems = 3;
31 private ScrollBarVisibility _verticalScrollBarVisibility = ScrollBarVisibility.Auto;
32 private bool _isDraggingScrollBarThumb;
33 private float _scrollBarDragOffset;
34 private int? _scrollBarPointerId;
35 private bool _suppressScrollBarClick;
36
40 public event Action<int>? SelectedIndexChanged;
41
45 public event Action<int>? SelectionCommitted;
46
56 RenderSurfaceHostBase renderSurfaceHost,
57 View view,
58 Rectangle bounds,
59 IEnumerable<string>? items = null,
60 string? nickname = null)
61 : base(
62 renderSurfaceHost,
64 ValidateBounds(bounds).Location,
65 nickname)
66 {
67 ArgumentNullException.ThrowIfNull(view);
68
69 Background = CreateBackground(renderSurfaceHost, view, bounds);
70 SelectionHighlight = CreateSelectionHighlight(renderSurfaceHost, view, GetInitialSelectionBounds(bounds));
71 VerticalScrollBarTrack = CreateScrollBarTrack(renderSurfaceHost, view, bounds);
72 VerticalScrollBarThumb = CreateScrollBarThumb(renderSurfaceHost, view, bounds);
73
78
79 CompleteInitialization(items);
80 }
81
91 RenderSurfaceHostBase renderSurfaceHost,
92 SceneLayer sceneLayer,
93 Rectangle bounds,
94 IEnumerable<string>? items = null,
95 string? nickname = null)
96 : base(
97 renderSurfaceHost,
99 ValidateBounds(bounds).Location,
100 nickname)
101 {
102 ArgumentNullException.ThrowIfNull(sceneLayer);
103
104 Background = CreateBackground(renderSurfaceHost, sceneLayer, bounds);
105 SelectionHighlight = CreateSelectionHighlight(renderSurfaceHost, sceneLayer, GetInitialSelectionBounds(bounds));
106 VerticalScrollBarTrack = CreateScrollBarTrack(renderSurfaceHost, sceneLayer, bounds);
107 VerticalScrollBarThumb = CreateScrollBarThumb(renderSurfaceHost, sceneLayer, bounds);
108
113
114 CompleteInitialization(items);
115 }
116
121
126
129
132
136 public IReadOnlyList<string> Items => _items;
137
141 public Rectangle Bounds => Mode == DirectDrawingMode.View
142 ? Background.ScreenBounds
143 : Background.WorldBounds;
144
148 public int ItemHeight
149 {
150 get => _itemHeight;
151 set
152 {
153 if (value < 12)
154 throw new ArgumentOutOfRangeException(nameof(value), "Item height must be at least 12 pixels.");
155
156 if (_itemHeight == value)
157 return;
158
159 _itemHeight = value;
160 ClampTopIndex();
161 EnsureSelectionVisible();
162 RefreshRows();
163 }
164 }
165
169 public int VisibleItemCount => Math.Max(1, (Bounds.Height - ContentPadding * 2) / ItemHeight);
170
173 {
174 get => _verticalScrollBarVisibility;
175 set
176 {
177 if (!Enum.IsDefined(value))
178 throw new ArgumentOutOfRangeException(nameof(value));
179
180 if (_verticalScrollBarVisibility == value)
181 return;
182
183 _verticalScrollBarVisibility = value;
184 RefreshRows();
185 }
186 }
187
189 public bool IsVerticalScrollBarVisible => Visible && NeedsVerticalScrollBar;
190
191 private bool NeedsVerticalScrollBar => VerticalScrollBarVisibility != ScrollBarVisibility.Never
193 _items.Count > VisibleItemCount);
194
197 {
198 get => _mouseWheelScrollItems;
199 set => _mouseWheelScrollItems = value > 0
200 ? value
201 : throw new ArgumentOutOfRangeException(
202 nameof(value),
203 "Mouse-wheel scrolling must advance at least one item.");
204 }
205
207 public int VerticalScrollBarMinimumThumbHeight => MinimumScrollBarThumbHeight;
208
212 public int TopIndex
213 {
214 get => _topIndex;
215 set
216 {
217 int clamped = Math.Clamp(value, 0, GetMaximumTopIndex());
218
219 if (_topIndex == clamped)
220 return;
221
222 _topIndex = clamped;
223 RefreshRows();
224 }
225 }
226
230 public int SelectedIndex
231 {
232 get => _selectedIndex;
233 set => SetSelectedIndex(value);
234 }
235
239 public string? SelectedItem => _selectedIndex >= 0 && _selectedIndex < _items.Count
240 ? _items[_selectedIndex]
241 : null;
242
248 public ListBoxWidget SetItems(IEnumerable<string> items)
249 {
250 ArgumentNullException.ThrowIfNull(items);
251
252 bool hadSelection = _selectedIndex >= 0;
253 string[] replacement = items.Select(static item => item ?? string.Empty).ToArray();
254 _items.Clear();
255 _items.AddRange(replacement);
256 _selectedIndex = -1;
257 _topIndex = 0;
258 RefreshRows();
259
260 if (hadSelection)
261 SelectedIndexChanged?.Invoke(-1);
262
263 return this;
264 }
265
271 public ListBoxWidget AddItem(string item)
272 {
273 _items.Add(item ?? string.Empty);
274 ClampTopIndex();
275 RefreshRows();
276 return this;
277 }
278
284 public ListBoxWidget RemoveAt(int index)
285 {
286 if (index < 0 || index >= _items.Count)
287 throw new ArgumentOutOfRangeException(nameof(index));
288
289 int oldSelectedIndex = _selectedIndex;
290 bool selectedItemWasRemoved = index == oldSelectedIndex;
291
292 _items.RemoveAt(index);
293
294 if (_items.Count == 0)
295 {
296 _selectedIndex = -1;
297 }
298 else if (index < oldSelectedIndex)
299 {
300 _selectedIndex = oldSelectedIndex - 1;
301 }
302 else if (selectedItemWasRemoved)
303 {
304 _selectedIndex = Math.Min(index, _items.Count - 1);
305 }
306
307 ClampTopIndex();
308 EnsureSelectionVisible();
309 RefreshRows();
310
311 if (_selectedIndex != oldSelectedIndex || selectedItemWasRemoved)
312 SelectedIndexChanged?.Invoke(_selectedIndex);
313
314 return this;
315 }
316
322 {
323 bool hadSelection = _selectedIndex >= 0;
324 _items.Clear();
325 _selectedIndex = -1;
326 _topIndex = 0;
327 RefreshRows();
328
329 if (hadSelection)
330 SelectedIndexChanged?.Invoke(-1);
331
332 return this;
333 }
334
341 {
342 if (index < -1 || index >= _items.Count)
343 throw new ArgumentOutOfRangeException(nameof(index));
344
345 if (_selectedIndex == index)
346 {
347 EnsureSelectionVisible();
348 return this;
349 }
350
351 _selectedIndex = index;
352 EnsureSelectionVisible();
353 RefreshSelectionHighlight();
354 SelectedIndexChanged?.Invoke(index);
355 return this;
356 }
357
363 {
364 if (_items.Count == 0)
365 return this;
366
367 int next = _selectedIndex < 0 ? 0 : Math.Min(_selectedIndex + 1, _items.Count - 1);
368 return SetSelectedIndex(next);
369 }
370
376 {
377 if (_items.Count == 0)
378 return this;
379
380 int previous = _selectedIndex < 0 ? 0 : Math.Max(_selectedIndex - 1, 0);
381 return SetSelectedIndex(previous);
382 }
383
390 {
391 _baseZOrder = zOrder;
392 Background.ZOrder = zOrder;
393 SelectionHighlight.ZOrder = zOrder + 1;
394
395 foreach (TextBlock row in _rowTextBlocks)
396 row.ZOrder = zOrder + 2;
397
398 VerticalScrollBarTrack.ZOrder = zOrder + 3;
399 VerticalScrollBarThumb.ZOrder = zOrder + 4;
400
401 return this;
402 }
403
410 {
411 SelectionHighlight.SetColor(color);
412 Refresh(SelectionHighlight);
413 return this;
414 }
415
417 protected override void ProcessShown()
418 {
419 base.ProcessShown();
420 RefreshScrollBar();
421 RefreshSelectionHighlight();
422 }
423
425 protected override void ProcessHidden()
426 {
427 _isDraggingScrollBarThumb = false;
428 _scrollBarPointerId = null;
429 _suppressScrollBarClick = false;
430 base.ProcessHidden();
431 }
432
434 protected override void OnPointerClick(WidgetPointerEventArgs args)
435 {
436 base.OnPointerClick(args);
437
438 if (!args.IsPrimaryButton || _items.Count == 0)
439 return;
440
441 if (_suppressScrollBarClick || IsPointerOverScrollBar(args))
442 {
443 _suppressScrollBarClick = false;
444 args.Handled = true;
445 return;
446 }
447
448 RectangleF screenBounds = Background.GetDrawLocationScreen(args.View);
449
450 screenBounds.Offset(
452 args.WrappedOffsetWorldPx.Y * args.View.Viewport.Zoom);
453
454 if (screenBounds.Height <= 0f || Bounds.Height <= 0)
455 return;
456
457 float scaleY = screenBounds.Height / Bounds.Height;
458 float localY = args.ScreenPositionPx.Y - screenBounds.Top - ContentPadding * scaleY;
459 float rowHeightScreen = ItemHeight * scaleY;
460
461 if (localY < 0f || rowHeightScreen <= 0f)
462 return;
463
464 int row = (int)(localY / rowHeightScreen);
465 int index = TopIndex + row;
466
467 if (row < 0 || row >= VisibleItemCount || index >= _items.Count)
468 return;
469
470 args.Handled = true;
471 SetSelectedIndex(index);
472 SelectionCommitted?.Invoke(index);
473 }
474
476 protected override void OnPointerDown(WidgetPointerEventArgs args)
477 {
478 base.OnPointerDown(args);
479
480 if (_scrollBarPointerId.HasValue)
481 return;
482
483 _suppressScrollBarClick = false;
484
486 return;
487
488 PointF local = GetLocalPointerPosition(args);
489 Rectangle thumbBounds = GetScrollBarThumbBounds(Bounds);
490 Rectangle trackBounds = GetScrollBarTrackBounds(Bounds);
491
492 if (!trackBounds.Contains(Point.Round(local)))
493 return;
494
495 args.Handled = true;
496 _scrollBarPointerId = args.PointerId;
497 _suppressScrollBarClick = true;
498
499 if (thumbBounds.Contains(Point.Round(local)))
500 {
501 _isDraggingScrollBarThumb = true;
502 _scrollBarDragOffset = local.Y - thumbBounds.Top;
503 return;
504 }
505
506 TopIndex += local.Y < thumbBounds.Top ? -VisibleItemCount : VisibleItemCount;
507 }
508
510 protected override void OnPointerMove(WidgetPointerEventArgs args)
511 {
512 base.OnPointerMove(args);
513
514 if (!_isDraggingScrollBarThumb || _scrollBarPointerId != args.PointerId || !IsVerticalScrollBarVisible)
515 return;
516
517 Rectangle trackBounds = GetScrollBarTrackBounds(Bounds);
518 Rectangle thumbBounds = GetScrollBarThumbBounds(Bounds);
519 float travel = trackBounds.Height - thumbBounds.Height;
520
521 if (travel <= 0f || GetMaximumTopIndex() == 0)
522 return;
523
524 float desiredTop = GetLocalPointerPosition(args).Y - _scrollBarDragOffset;
525 float fraction = Math.Clamp((desiredTop - trackBounds.Top) / travel, 0f, 1f);
526 TopIndex = (int)MathF.Round(fraction * GetMaximumTopIndex());
527 args.Handled = true;
528 }
529
531 protected override void OnPointerUp(WidgetPointerEventArgs args)
532 {
533 base.OnPointerUp(args);
534
535 if (_scrollBarPointerId == args.PointerId)
536 {
537 _isDraggingScrollBarThumb = false;
538 _scrollBarPointerId = null;
539 args.Handled = true;
540 }
541 }
542
544 protected override void OnMouseWheel(WidgetMouseWheelEventArgs args)
545 {
546 base.OnMouseWheel(args);
547
548 if (args.Delta == 0 || GetMaximumTopIndex() == 0)
549 return;
550
551 long notches = Math.Max(1, Math.Abs((long)args.Delta) / 120);
552 long change = notches * MouseWheelScrollItems * (args.Delta > 0 ? -1 : 1);
553 TopIndex = (int)Math.Clamp(TopIndex + change, 0, GetMaximumTopIndex());
554 args.Handled = true;
555 }
556
558 protected override void OnKeyboardInput(WidgetKeyboardEventArgs args)
559 {
560 base.OnKeyboardInput(args);
561
562 if (_items.Count == 0)
563 return;
564
565 if (args.Key == 13)
566 {
567 if (args.KeyAction != KeyAction.Pressed || _selectedIndex < 0)
568 return;
569
570 args.Handled = true;
571 SelectionCommitted?.Invoke(_selectedIndex);
572 return;
573 }
574
575 if (args.KeyAction is not KeyAction.Pressed and not KeyAction.Repeated)
576 return;
577
578 switch (args.Key)
579 {
580 case 38: // Up
582 break;
583 case 40: // Down
584 SelectNext();
585 break;
586 case 36: // Home
588 break;
589 case 35: // End
590 SetSelectedIndex(_items.Count - 1);
591 break;
592 case 33: // Page Up
593 SetSelectedIndex(Math.Max(0, (_selectedIndex < 0 ? 0 : _selectedIndex) - VisibleItemCount));
594 break;
595 case 34: // Page Down
597 Math.Min(_items.Count - 1, (_selectedIndex < 0 ? 0 : _selectedIndex) + VisibleItemCount));
598
599 break;
600 default:
601 return;
602 }
603
604 args.Handled = true;
605 }
606
607 private void CompleteInitialization(IEnumerable<string>? items)
608 {
609 SelectionHighlight.Visible = false;
610 VerticalScrollBarTrack.Visible = false;
611 VerticalScrollBarThumb.Visible = false;
612 CanReceiveFocus = true;
615
616 if (items is not null)
617 _items.AddRange(items.Select(static item => item ?? string.Empty));
618
619 RefreshRows();
620 }
621
622 private void EnsureSelectionVisible()
623 {
624 if (_selectedIndex < 0)
625 return;
626
627 if (_selectedIndex < _topIndex)
628 {
629 _topIndex = _selectedIndex;
630 RefreshRows();
631 return;
632 }
633
634 int lastVisible = _topIndex + VisibleItemCount - 1;
635
636 if (_selectedIndex > lastVisible)
637 {
638 _topIndex = Math.Min(GetMaximumTopIndex(), _selectedIndex - VisibleItemCount + 1);
639 RefreshRows();
640 }
641 }
642
643 private void ClampTopIndex()
644 {
645 _topIndex = Math.Clamp(_topIndex, 0, GetMaximumTopIndex());
646 }
647
648 private int GetMaximumTopIndex()
649 {
650 return Math.Max(0, _items.Count - VisibleItemCount);
651 }
652
653 private void RefreshRows()
654 {
655 bool wasVisible = Visible;
656
657 RefreshScrollBar();
658
659 foreach (TextBlock row in _rowTextBlocks)
660 {
661 Remove(row);
662 row.Dispose();
663 }
664
665 _rowTextBlocks.Clear();
666
667 Rectangle bounds = Bounds;
668 int count = Math.Min(VisibleItemCount, Math.Max(0, _items.Count - TopIndex));
669
670 for (int rowIndex = 0; rowIndex < count; rowIndex++)
671 {
672 int itemIndex = TopIndex + rowIndex;
673 Rectangle rowBounds = GetRowBounds(bounds, rowIndex);
674 TextBlock row = CreateRowText(rowBounds, _items[itemIndex]);
675 row.ZOrder = _baseZOrder + 2;
676 Add(row);
677 _rowTextBlocks.Add(row);
678 }
679
680 RefreshSelectionHighlight();
681
682 if (!wasVisible)
683 SetIsVisible(false);
684 }
685
686 private TextBlock CreateRowText(Rectangle bounds, string text)
687 {
688 TextBlock row = Mode == DirectDrawingMode.View
689 ? new TextBlock(RenderSurfaceHost, View!, bounds)
690 : new TextBlock(
692 SceneLayer!,
693 view: null,
694 worldBounds: bounds);
695
696 return row.SetText(text)
697 .SetFont(SKTypeface.Default, 15f, minSize: 9f)
698 .SetColors(SKColors.White, SKColors.Transparent)
699 .SetAlignment(SKTextAlign.Left, TextBlock.VerticalAlign.Center)
700 .SetPadding(6f, 0f)
701 .EnableWrapping(false);
702 }
703
704 private void RefreshSelectionHighlight()
705 {
706 if (!Visible || _selectedIndex < TopIndex || _selectedIndex >= TopIndex + VisibleItemCount ||
707 _selectedIndex >= _items.Count)
708 {
709 SelectionHighlight.Visible = false;
710 return;
711 }
712
713 int rowIndex = _selectedIndex - TopIndex;
714 Rectangle selectionBounds = GetRowBounds(Bounds, rowIndex);
715
716 Rectangle currentBounds = Mode == DirectDrawingMode.View
717 ? SelectionHighlight.ScreenBounds
718 : SelectionHighlight.WorldBounds;
719
720 Rectangle resizedBounds = new(currentBounds.Location, selectionBounds.Size);
721
722 if (Mode == DirectDrawingMode.View)
723 SelectionHighlight.ScreenBounds = resizedBounds;
724 else
725 SelectionHighlight.WorldBounds = resizedBounds;
726
729 new Vector2(ContentPadding, ContentPadding + rowIndex * ItemHeight));
730
731 SelectionHighlight.Visible = true;
732 }
733
734 private Rectangle GetRowBounds(Rectangle bounds, int rowIndex)
735 {
736 int y = bounds.Y + ContentPadding + rowIndex * ItemHeight;
737
738 return new Rectangle(
739 bounds.X + ContentPadding,
740 y,
741 Math.Max(
742 1,
743 bounds.Width - ContentPadding * 2 -
744 (NeedsVerticalScrollBar ? ScrollBarWidth + ScrollBarMargin * 2 : 0)),
745 Math.Min(ItemHeight, Math.Max(1, bounds.Bottom - ContentPadding - y)));
746 }
747
748 private void RefreshScrollBar()
749 {
750 bool visible = IsVerticalScrollBarVisible;
751 VerticalScrollBarTrack.Visible = visible;
752 VerticalScrollBarThumb.Visible = visible;
753
754 if (!visible)
755 return;
756
757 Rectangle trackBounds = GetScrollBarTrackBounds(Bounds);
758 Rectangle thumbBounds = GetScrollBarThumbBounds(Bounds);
759 SetRectangleBounds(VerticalScrollBarTrack, trackBounds);
760 SetRectangleBounds(VerticalScrollBarThumb, thumbBounds);
761 }
762
763 private Rectangle GetScrollBarTrackBounds(Rectangle bounds)
764 {
765 return new Rectangle(
766 bounds.Right - ScrollBarMargin - ScrollBarWidth,
767 bounds.Top + ContentPadding + ScrollBarMargin,
768 ScrollBarWidth,
769 Math.Max(1, bounds.Height - (ContentPadding + ScrollBarMargin) * 2));
770 }
771
772 private Rectangle GetScrollBarThumbBounds(Rectangle bounds)
773 {
774 Rectangle trackBounds = GetScrollBarTrackBounds(bounds);
775
776 int thumbHeight = _items.Count == 0
777 ? trackBounds.Height
778 : Math.Clamp(
779 (int)MathF.Round(trackBounds.Height * Math.Min(1f, (float)VisibleItemCount / _items.Count)),
780 Math.Min(MinimumScrollBarThumbHeight, trackBounds.Height),
781 trackBounds.Height);
782
783 int travel = trackBounds.Height - thumbHeight;
784 int maximumTopIndex = GetMaximumTopIndex();
785 int offset = maximumTopIndex == 0 ? 0 : (int)MathF.Round(travel * ((float)TopIndex / maximumTopIndex));
786
787 return new Rectangle(
788 trackBounds.Left,
789 trackBounds.Top + offset,
790 trackBounds.Width,
791 thumbHeight);
792 }
793
794 private PointF GetLocalPointerPosition(WidgetPointerEventArgs args)
795 {
796 RectangleF screenBounds = Background.GetDrawLocationScreen(args.View);
797
798 screenBounds.Offset(
799 args.WrappedOffsetWorldPx.X * args.View.Viewport.Zoom,
800 args.WrappedOffsetWorldPx.Y * args.View.Viewport.Zoom);
801
802 float scaleX = screenBounds.Width / Bounds.Width;
803 float scaleY = screenBounds.Height / Bounds.Height;
804
805 return new PointF(
806 Bounds.Left + (args.ScreenPositionPx.X - screenBounds.Left) / scaleX,
807 Bounds.Top + (args.ScreenPositionPx.Y - screenBounds.Top) / scaleY);
808 }
809
810 private bool IsPointerOverScrollBar(WidgetPointerEventArgs args)
811 {
812 // The entire reserved gutter belongs to the scrollbar, including its margins.
813 return IsVerticalScrollBarVisible && GetLocalPointerPosition(args).X >= GetRowBounds(Bounds, 0).Right;
814 }
815
816 private void SetRectangleBounds(DirectRectangle rectangle, Rectangle bounds)
817 {
818 if (Mode == DirectDrawingMode.View)
819 rectangle.ScreenBounds = bounds;
820 else
821 rectangle.WorldBounds = bounds;
822
823 SetLocalOffset(rectangle, new Vector2(bounds.X, bounds.Y) - GetPosition());
824 }
825
826 private static DirectRectangle CreateBackground(
827 RenderSurfaceHostBase host,
828 View view,
829 Rectangle bounds)
830 {
831 return ConfigureBackground(
832 new DirectRectangle(
833 Color.FromArgb(
834 255,
835 36,
836 36,
837 44),
838 host,
839 view,
840 bounds));
841 }
842
843 private static DirectRectangle CreateBackground(
844 RenderSurfaceHostBase host,
845 SceneLayer layer,
846 Rectangle bounds)
847 {
848 return ConfigureBackground(
849 new DirectRectangle(
850 Color.FromArgb(
851 255,
852 36,
853 36,
854 44),
855 host,
856 layer,
857 bounds));
858 }
859
860 private static DirectRectangle ConfigureBackground(DirectRectangle background)
861 {
862 return background.SetFilled(true)
864 Color.FromArgb(
865 255,
866 140,
867 140,
868 155))
869 .SetStrokeWidth(1.5f)
870 .SetCornerRadius(4f);
871 }
872
873 private static DirectRectangle CreateSelectionHighlight(
874 RenderSurfaceHostBase host,
875 View view,
876 Rectangle bounds)
877 {
878 return ConfigureSelectionHighlight(
879 new DirectRectangle(
880 Color.FromArgb(
881 220,
882 63,
883 91,
884 138),
885 host,
886 view,
887 bounds));
888 }
889
890 private static DirectRectangle CreateSelectionHighlight(
891 RenderSurfaceHostBase host,
892 SceneLayer layer,
893 Rectangle bounds)
894 {
895 return ConfigureSelectionHighlight(
896 new DirectRectangle(
897 Color.FromArgb(
898 220,
899 63,
900 91,
901 138),
902 host,
903 layer,
904 bounds));
905 }
906
907 private static DirectRectangle ConfigureSelectionHighlight(DirectRectangle highlight)
908 {
909 return highlight.SetFilled(true)
910 .SetStrokeWidth(0f)
911 .SetCornerRadius(2f);
912 }
913
914 private static DirectRectangle CreateScrollBarTrack(RenderSurfaceHostBase host, View view, Rectangle bounds)
915 {
916 return ConfigureScrollBarTrack(
917 new DirectRectangle(
918 Color.FromArgb(
919 255,
920 26,
921 26,
922 32),
923 host,
924 view,
925 bounds));
926 }
927
928 private static DirectRectangle CreateScrollBarTrack(RenderSurfaceHostBase host, SceneLayer layer, Rectangle bounds)
929 {
930 return ConfigureScrollBarTrack(
931 new DirectRectangle(
932 Color.FromArgb(
933 255,
934 26,
935 26,
936 32),
937 host,
938 layer,
939 bounds));
940 }
941
942 private static DirectRectangle ConfigureScrollBarTrack(DirectRectangle track)
943 {
944 return track.SetFilled(true).SetStrokeWidth(0f).SetCornerRadius(3f);
945 }
946
947 private static DirectRectangle CreateScrollBarThumb(RenderSurfaceHostBase host, View view, Rectangle bounds)
948 {
949 return ConfigureScrollBarThumb(
950 new DirectRectangle(
951 Color.FromArgb(
952 255,
953 126,
954 126,
955 142),
956 host,
957 view,
958 bounds));
959 }
960
961 private static DirectRectangle CreateScrollBarThumb(RenderSurfaceHostBase host, SceneLayer layer, Rectangle bounds)
962 {
963 return ConfigureScrollBarThumb(
964 new DirectRectangle(
965 Color.FromArgb(
966 255,
967 126,
968 126,
969 142),
970 host,
971 layer,
972 bounds));
973 }
974
975 private static DirectRectangle ConfigureScrollBarThumb(DirectRectangle thumb)
976 {
977 return thumb.SetFilled(true).SetStrokeWidth(0f).SetCornerRadius(3f);
978 }
979
980 private static Rectangle GetInitialSelectionBounds(Rectangle bounds)
981 {
982 return new Rectangle(
983 bounds.X + ContentPadding,
984 bounds.Y + ContentPadding,
985 Math.Max(1, bounds.Width - ContentPadding * 2),
986 DefaultItemHeight);
987 }
988
989 private static void Refresh(DirectRectangle rectangle)
990 {
991 rectangle.SetPosition(rectangle.GetPosition());
992 }
993
994 private static Rectangle ValidateBounds(Rectangle bounds)
995 {
996 if (bounds.Width < 24 || bounds.Height < DefaultItemHeight + ContentPadding * 2)
997 throw new ArgumentOutOfRangeException(nameof(bounds), bounds, "List-box bounds are too small.");
998
999 return bounds;
1000 }
1001}
bool Visible
Gets or sets the visibility of all descendants.
virtual DirectComposite Remove(IDirectCompositeChild child)
Removes a child without disposing it.
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.
DirectComposite SetLocalOffset(IDirectCompositeChild child, Vector2 newLocalOffsetPx)
Changes one child's local offset and reapplies its position.
DirectComposite SetIsVisible(bool visible)
Applies one visibility value to every descendant.
virtual DirectComposite Clear()
Removes every child without disposing them.
View? View
Gets the view shared by all children in view mode.
Vector2 GetPosition()
Gets the current composite anchor.
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 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 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 SetPadding(float horizontal, float vertical)
Sets symmetric horizontal and vertical padding and invalidates cached layout.
Definition TextBlock.cs:336
TextBlock SetColors(SKColor fg, SKColor bg)
Sets the foreground (text) and background colors using Skia colors.
Definition TextBlock.cs:430
override void Dispose(bool disposing)
Releases managed resources used by the text block.
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
ListBoxWidget SetSelectedIndex(int index)
Selects the requested item index. Use -1 to clear selection.
override void ProcessHidden()
Performs processing when the widget is hidden before notification callbacks are invoked.
override void OnKeyboardInput(WidgetKeyboardEventArgs args)
Called when keyboard input is routed to the widget.
int VisibleItemCount
Gets the number of complete rows that can currently be displayed.
ListBoxWidget SetItems(IEnumerable< string > items)
Replaces all list items and clears selection.
ScrollBarVisibility VerticalScrollBarVisibility
Gets or sets when the vertical scrollbar is displayed.
Action< int >? SelectionCommitted
Occurs when the current selection is explicitly committed.
IReadOnlyList< string > Items
Gets the current items.
override void OnPointerClick(WidgetPointerEventArgs args)
Called when a pointer click is dispatched to the widget.
Action< int >? SelectedIndexChanged
Occurs when SelectedIndex changes.
DirectRectangle VerticalScrollBarThumb
Gets the vertical scrollbar thumb.
override void OnPointerDown(WidgetPointerEventArgs args)
Called when a pointer press begins on the widget.
Rectangle Bounds
Gets the list-box bounds in its native coordinate space.
ListBoxWidget SelectNext()
Selects the next item when possible.
DirectRectangle VerticalScrollBarTrack
Gets the vertical scrollbar track.
int SelectedIndex
Gets or sets the selected item index. Use -1 to clear selection.
string? SelectedItem
Gets the selected item text, or null when no item is selected.
DirectRectangle Background
Gets the list background.
override void OnPointerUp(WidgetPointerEventArgs args)
Called when a pointer press is released for the widget.
ListBoxWidget SelectPrevious()
Selects the previous item when possible.
DirectRectangle SelectionHighlight
Gets the rectangle used to highlight the selected row.
ListBoxWidget ClearItems()
Removes all items and clears selection.
ListBoxWidget SetSelectionColor(Color color)
Sets the selected-row highlight color.
ListBoxWidget SetListBoxZOrder(int zOrder)
Sets the base Z-order used by the list-box visuals.
int ItemHeight
Gets or sets the height of each row in native pixels.
ListBoxWidget RemoveAt(int index)
Removes an item by index.
ListBoxWidget(RenderSurfaceHostBase renderSurfaceHost, View view, Rectangle bounds, IEnumerable< string >? items=null, string? nickname=null)
Creates a view-level list box.
bool IsVerticalScrollBarVisible
Gets whether the vertical scrollbar is currently displayed.
int TopIndex
Gets or sets the index of the first visible item.
int VerticalScrollBarMinimumThumbHeight
Gets the minimum height used for a visible scrollbar thumb.
override void OnMouseWheel(WidgetMouseWheelEventArgs args)
Called when mouse-wheel input is routed to the widget.
override void ProcessShown()
Performs processing when the widget is shown before notification callbacks are invoked.
override void OnPointerMove(WidgetPointerEventArgs args)
Called when pointer movement is routed to the widget.
ListBoxWidget(RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle bounds, IEnumerable< string >? items=null, string? nickname=null)
Creates a scene-layer list box.
int MouseWheelScrollItems
Gets or sets the number of items scrolled for one wheel notch.
ListBoxWidget AddItem(string item)
Adds an item to the end of the list.
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 mouse-wheel input routed to a widget.
int Delta
Gets the wheel delta. Positive values scroll upward; negative values scroll downward.
Event arguments for pointer-based widget callbacks.
PointF WrappedOffsetWorldPx
World-space translation of the selected periodic widget instance. Retained throughout pointer capture...
bool IsPrimaryButton
Gets whether the pointer event represents a primary-button interaction.
View View
Gets the view through which the pointer interaction was routed.
int PointerId
Gets the identifier of the pointer that produced the event.
@ 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
ScrollBarVisibility
Specifies when a scrollable widget displays its vertical scrollbar.
@ Always
Always displays the scrollbar while the widget is visible.