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
TabControlWidget.cs
1using System.Drawing;
2using System.Numerics;
8using SkiaSharp;
9
11
23public sealed class TabControlWidget : ContainerWidget, IWidgetKeyboardFallback
24{
25 private const int DefaultTabHeight = 32;
26 private const int DefaultPreferredTabWidth = 132;
27 private const float DefaultReorderDragThresholdPx = 6f;
28
29 private readonly List<TabPageWidget> _tabs = [];
30 private readonly Dictionary<TabPageWidget, TabHeaderWidget> _headers = [];
31
32 private readonly View? _view;
33 private readonly SceneLayer? _sceneLayer;
34
35 private Size _size;
36 private int _selectedIndex = -1;
37 private int _tabRowCount = 1;
38 private int _displayedRowCount = 1;
39 private int _displayedColumnCount = 1;
40 private int _tabHeight = DefaultTabHeight;
41 private int _preferredTabWidth = DefaultPreferredTabWidth;
42 private int _baseZOrder;
43 private float _tabReorderDragThresholdPx = DefaultReorderDragThresholdPx;
44 private bool _autoExpandRows;
45 private bool _isTabReorderingEnabled;
46 private bool _isShown;
47 private bool _disposed;
48
49 private Color _normalTabColor = Color.FromArgb(
50 255,
51 60,
52 60,
53 68);
54
55 private Color _hoverTabColor = Color.FromArgb(
56 255,
57 78,
58 78,
59 88);
60
61 private Color _pressedTabColor = Color.FromArgb(
62 255,
63 42,
64 42,
65 48);
66
67 private Color _selectedTabColor = Color.FromArgb(
68 255,
69 48,
70 89,
71 142);
72
73 private Color _tabTextColor = Color.White;
74
78 public event Action<TabPageWidget>? SelectedTabChanged;
79
83 public event Action<TabPageWidget, int, int>? TabReordered;
84
93 RenderSurfaceHostBase renderSurfaceHost,
94 View view,
95 Rectangle bounds,
96 string? nickname = null)
97 : base(
98 renderSurfaceHost,
100 ValidateBounds(bounds).Location,
101 nickname)
102 {
103 ArgumentNullException.ThrowIfNull(view);
104
105 _view = view;
106 _size = bounds.Size;
107
108 InitializeInput();
109 }
110
119 RenderSurfaceHostBase renderSurfaceHost,
120 SceneLayer sceneLayer,
121 Rectangle bounds,
122 string? nickname = null)
123 : base(
124 renderSurfaceHost,
126 ValidateBounds(bounds).Location,
127 nickname)
128 {
129 ArgumentNullException.ThrowIfNull(sceneLayer);
130
131 _sceneLayer = sceneLayer;
132 _size = bounds.Size;
133
134 InitializeInput();
135 }
136
140 public Rectangle Bounds
141 {
142 get
143 {
144 Vector2 position = GetPosition();
145
146 return new Rectangle(
147 (int)MathF.Round(position.X),
148 (int)MathF.Round(position.Y),
149 _size.Width,
150 _size.Height);
151 }
152 }
153
157 public Size Size
158 {
159 get => _size;
160 set
161 {
162 ValidateSize(value);
163
164 if (_size == value)
165 return;
166
167 _size = value;
168 Relayout();
169 }
170 }
171
175 public IReadOnlyList<TabPageWidget> Tabs => _tabs.AsReadOnly();
176
180 public int SelectedIndex => _selectedIndex;
181
185 public TabPageWidget? SelectedTab => _selectedIndex >= 0 && _selectedIndex < _tabs.Count
186 ? _tabs[_selectedIndex]
187 : null;
188
192 public int TabHeight
193 {
194 get => _tabHeight;
195 set
196 {
197 if (value < 20)
198 throw new ArgumentOutOfRangeException(nameof(value), "The tab height must be at least 20 pixels.");
199
200 if (_tabHeight == value)
201 return;
202
203 _tabHeight = value;
204 Relayout();
205 }
206 }
207
215 public int TabRowCount
216 {
217 get => _tabRowCount;
218 set
219 {
220 if (value < 1)
221 throw new ArgumentOutOfRangeException(nameof(value), "At least one tab row is required.");
222
223 if (_tabRowCount == value)
224 return;
225
226 _tabRowCount = value;
227 Relayout();
228 }
229 }
230
239 public bool AutoExpandRows
240 {
241 get => _autoExpandRows;
242 set
243 {
244 if (_autoExpandRows == value)
245 return;
246
247 _autoExpandRows = value;
248 Relayout();
249 }
250 }
251
256 {
257 get => _preferredTabWidth;
258 set
259 {
260 if (value < 48)
261 throw new ArgumentOutOfRangeException(
262 nameof(value),
263 "The preferred tab width must be at least 48 pixels.");
264
265 if (_preferredTabWidth == value)
266 return;
267
268 _preferredTabWidth = value;
269 Relayout();
270 }
271 }
272
276 public int DisplayedTabRowCount => _displayedRowCount;
277
281 public int DisplayedTabColumnCount => _displayedColumnCount;
282
287 {
288 get => _isTabReorderingEnabled;
289 set
290 {
291 if (_isTabReorderingEnabled == value)
292 return;
293
294 _isTabReorderingEnabled = value;
295
296 foreach (TabHeaderWidget header in _headers.Values)
297 header.IsDragEnabled = value;
298 }
299 }
300
305 {
306 get => _tabReorderDragThresholdPx;
307 set
308 {
309 if (value < 0f)
310 throw new ArgumentOutOfRangeException(
311 nameof(value),
312 "The tab reordering threshold cannot be negative.");
313
314 if (Math.Abs(_tabReorderDragThresholdPx - value) < float.Epsilon)
315 return;
316
317 _tabReorderDragThresholdPx = value;
318
319 foreach (TabHeaderWidget header in _headers.Values)
320 header.DragThresholdPx = value;
321 }
322 }
323
332 string title,
333 char? mnemonic = null,
334 string? nickname = null)
335 {
336 ObjectDisposedException.ThrowIf(_disposed, this);
337 ValidateTitle(title);
338 ValidateMnemonic(mnemonic);
339
340 Rectangle initialHeaderBounds = new(Bounds.Location, new Size(1, TabHeight));
341
342 Rectangle initialContentBounds = new(
343 Bounds.Left,
344 Bounds.Top + TabHeight,
345 Bounds.Width,
346 Math.Max(1, Bounds.Height - TabHeight));
347
349 ? new TabPageWidget(
351 _view!,
352 initialContentBounds,
353 title,
354 mnemonic,
355 nickname)
356 : new TabPageWidget(
358 _sceneLayer!,
359 initialContentBounds,
360 title,
361 mnemonic,
362 nickname);
363
364 TabHeaderWidget header = Mode == DirectDrawingMode.View
365 ? new TabHeaderWidget(
367 _view!,
368 initialHeaderBounds,
369 title,
370 $"{page.Nickname}.header")
371 : new TabHeaderWidget(
373 _sceneLayer!,
374 initialHeaderBounds,
375 title,
376 $"{page.Nickname}.header");
377
378 page.TitleChanged += OnPageTitleChanged;
379 page.Disposing += OnPageDisposing;
380 header.Invoked += OnHeaderInvoked;
381 header.ReorderDragged += OnHeaderDragged;
382 header.ReorderDragEnded += OnHeaderDragEnded;
383 header.Disposing += OnHeaderDisposing;
384 header.IsDragEnabled = IsTabReorderingEnabled;
385 header.DragThresholdPx = TabReorderDragThresholdPx;
386
387 _tabs.Add(page);
388 _headers.Add(page, header);
389
390 Add(page, Vector2.Zero);
391 Add(header, Vector2.Zero);
392
393 if (_selectedIndex < 0)
394 _selectedIndex = 0;
395
396 Relayout();
397 UpdateSelectedTabVisuals();
398 UpdatePageVisibility();
399
400 return page;
401 }
402
409 public bool RemoveTab(
410 TabPageWidget page,
411 bool dispose = false)
412 {
413 ArgumentNullException.ThrowIfNull(page);
414 return RemoveTabCore(page, dispose, raiseSelectionChanged: true);
415 }
416
422 public TabControlWidget SelectTab(int index)
423 {
424 ObjectDisposedException.ThrowIf(_disposed, this);
425
426 if (index < 0 || index >= _tabs.Count)
427 throw new ArgumentOutOfRangeException(nameof(index));
428
429 if (_selectedIndex == index)
430 return this;
431
432 _selectedIndex = index;
433 UpdateSelectedTabVisuals();
434 UpdatePageVisibility();
435
436 SelectedTabChanged?.Invoke(_tabs[index]);
437 return this;
438 }
439
446 {
447 ArgumentNullException.ThrowIfNull(page);
448
449 int index = _tabs.IndexOf(page);
450
451 if (index < 0)
452 throw new ArgumentException("The page is not owned by this tab control.", nameof(page));
453
454 return SelectTab(index);
455 }
456
464 int fromIndex,
465 int toIndex)
466 {
467 ObjectDisposedException.ThrowIf(_disposed, this);
468
469 if (fromIndex < 0 || fromIndex >= _tabs.Count)
470 throw new ArgumentOutOfRangeException(nameof(fromIndex));
471
472 if (toIndex < 0 || toIndex >= _tabs.Count)
473 throw new ArgumentOutOfRangeException(nameof(toIndex));
474
475 if (fromIndex == toIndex)
476 return this;
477
478 TabPageWidget page = _tabs[fromIndex];
479 TabPageWidget? selectedPage = SelectedTab;
480
481 _tabs.RemoveAt(fromIndex);
482 _tabs.Insert(toIndex, page);
483 _selectedIndex = selectedPage is null ? -1 : _tabs.IndexOf(selectedPage);
484
485 Relayout();
486 TabReordered?.Invoke(page, fromIndex, toIndex);
487
488 return this;
489 }
490
498 TabPageWidget page,
499 int toIndex)
500 {
501 ArgumentNullException.ThrowIfNull(page);
502
503 int fromIndex = _tabs.IndexOf(page);
504
505 if (fromIndex < 0)
506 throw new ArgumentException("The page is not owned by this tab control.", nameof(page));
507
508 return MoveTab(fromIndex, toIndex);
509 }
510
516 {
517 ObjectDisposedException.ThrowIf(_disposed, this);
518
519 Rectangle bounds = Bounds;
520 (_displayedRowCount, _displayedColumnCount) = ResolveGrid(bounds);
521 Rectangle contentBounds = CreateContentBounds(bounds, _displayedRowCount);
522
523 for (int index = 0; index < _tabs.Count; index++)
524 {
525 TabPageWidget page = _tabs[index];
526 TabHeaderWidget header = _headers[page];
527 Rectangle headerBounds = CreateHeaderBounds(bounds, index, _displayedColumnCount);
528
529 header.SetBounds(headerBounds);
530 page.HeaderBounds = headerBounds;
531 page.SetContentBounds(contentBounds);
532
533 SetLocalOffset(header, new Vector2(headerBounds.Left - bounds.Left, headerBounds.Top - bounds.Top));
534 SetLocalOffset(page, new Vector2(contentBounds.Left - bounds.Left, contentBounds.Top - bounds.Top));
535 }
536
537 ApplyZOrder();
538 return this;
539 }
540
550 Color normal,
551 Color hover,
552 Color pressed,
553 Color selected)
554 {
555 _normalTabColor = normal;
556 _hoverTabColor = hover;
557 _pressedTabColor = pressed;
558 _selectedTabColor = selected;
559 UpdateSelectedTabVisuals();
560
561 return this;
562 }
563
570 {
571 _tabTextColor = color;
572
573 foreach (TabHeaderWidget header in _headers.Values)
574 header.SetTextColor(color);
575
576 return this;
577 }
578
585 {
586 _baseZOrder = zOrder;
587 ApplyZOrder();
588
589 return this;
590 }
591
593 public override void Dispose()
594 {
595 if (_disposed)
596 return;
597
598 _disposed = true;
599
600 foreach (TabPageWidget page in _tabs)
601 {
602 page.TitleChanged -= OnPageTitleChanged;
603 page.Disposing -= OnPageDisposing;
604
605 TabHeaderWidget header = _headers[page];
606 header.Invoked -= OnHeaderInvoked;
607 header.ReorderDragged -= OnHeaderDragged;
608 header.ReorderDragEnded -= OnHeaderDragEnded;
609 header.Disposing -= OnHeaderDisposing;
610 }
611
612 base.Dispose();
613
614 _tabs.Clear();
615 _headers.Clear();
616 SelectedTabChanged = null;
617 TabReordered = null;
618 }
619
621 protected override void ProcessShown()
622 {
623 base.ProcessShown();
624 _isShown = true;
625 UpdatePageVisibility();
626 }
627
629 protected override void ProcessHidden()
630 {
631 _isShown = false;
632 base.ProcessHidden();
633 }
634
636 protected override void ProcessActivated()
637 {
638 base.ProcessActivated();
639
640 SelectedTab?.Activate();
641 }
642
644 protected override void OnKeyboardInput(WidgetKeyboardEventArgs args)
645 {
646 base.OnKeyboardInput(args);
647 HandleKeyboardInput(args);
648 }
649
651 void IWidgetKeyboardFallback.HandleUnhandledKeyboardInput(WidgetKeyboardEventArgs args)
652 {
653 HandleKeyboardInput(args);
654 }
655
656 private void InitializeInput()
657 {
658 IsPointerInputEnabled = false;
660 CanReceiveFocus = false;
661 }
662
663 private bool RemoveTabCore(
664 TabPageWidget page,
665 bool dispose,
666 bool raiseSelectionChanged)
667 {
668 int removedIndex = _tabs.IndexOf(page);
669
670 if (removedIndex < 0)
671 return false;
672
673 TabPageWidget? previouslySelected = SelectedTab;
674 TabHeaderWidget header = _headers[page];
675
676 page.TitleChanged -= OnPageTitleChanged;
677 page.Disposing -= OnPageDisposing;
678 header.Invoked -= OnHeaderInvoked;
679 header.ReorderDragged -= OnHeaderDragged;
680 header.ReorderDragEnded -= OnHeaderDragEnded;
681 header.Disposing -= OnHeaderDisposing;
682
683 _tabs.RemoveAt(removedIndex);
684 _headers.Remove(page);
685
686 Remove(header);
687 Remove(page);
688
689 header.Dispose();
690
691 if (dispose)
692 page.Dispose();
693
694 if (_tabs.Count == 0)
695 {
696 _selectedIndex = -1;
697 }
698 else if (ReferenceEquals(previouslySelected, page))
699 {
700 _selectedIndex = Math.Min(removedIndex, _tabs.Count - 1);
701 }
702 else
703 {
704 _selectedIndex = previouslySelected is null ? 0 : _tabs.IndexOf(previouslySelected);
705 }
706
707 Relayout();
708 UpdateSelectedTabVisuals();
709 UpdatePageVisibility();
710
711 if (raiseSelectionChanged && SelectedTab is not null && !ReferenceEquals(previouslySelected, SelectedTab))
713
714 return true;
715 }
716
717 private void OnPageTitleChanged(TabPageWidget page)
718 {
719 if (_headers.TryGetValue(page, out TabHeaderWidget? header))
720 header.SetText(page.Title);
721 }
722
723 private void OnPageDisposing(
724 object? sender,
725 IDirectDrawable _)
726 {
727 if (!_disposed && sender is TabPageWidget page)
728 RemoveTabCore(page, dispose: true, raiseSelectionChanged: true);
729 }
730
731 private void OnHeaderDisposing(
732 object? sender,
733 IDirectDrawable _)
734 {
735 if (_disposed || sender is not TabHeaderWidget header)
736 return;
737
738 TabPageWidget? page = _headers.FirstOrDefault(pair => ReferenceEquals(pair.Value, header)).Key;
739
740 if (page is not null)
741 RemoveTabCore(page, dispose: true, raiseSelectionChanged: true);
742 }
743
744 private void OnHeaderInvoked(TabHeaderWidget header)
745 {
746 TabPageWidget? page = _headers.FirstOrDefault(pair => ReferenceEquals(pair.Value, header)).Key;
747
748 if (page is not null)
749 SelectTab(page);
750 }
751
752 private void OnHeaderDragged(
753 TabHeaderWidget header,
754 WidgetDragEventArgs args,
755 View view)
756 {
758 return;
759
760 TabPageWidget? page = _headers.FirstOrDefault(pair => ReferenceEquals(pair.Value, header)).Key;
761
762 if (page is null)
763 return;
764
765 int fromIndex = _tabs.IndexOf(page);
766 int targetIndex = GetHeaderIndexAt(args.CurrentScreenPositionPx, view);
767
768 if (targetIndex >= 0 && targetIndex != fromIndex)
769 MoveTab(fromIndex, targetIndex);
770 }
771
772 private void OnHeaderDragEnded(TabHeaderWidget _)
773 {
774 Relayout();
775 }
776
777 private int GetHeaderIndexAt(
778 PointF screenPositionPx,
779 View view)
780 {
781 Point screenPosition = Point.Round(screenPositionPx);
782
783 for (int index = 0; index < _tabs.Count; index++)
784 {
785 TabHeaderWidget header = _headers[_tabs[index]];
786
787 if (header.HitTest(view, screenPosition))
788 {
789 return index;
790 }
791 }
792
793 return -1;
794 }
795
796 private void HandleKeyboardInput(WidgetKeyboardEventArgs args)
797 {
798 if (args.Handled || args.KeyAction != KeyAction.Pressed || args.Modifiers != KeyboardModifierState.Alt)
799 return;
800
801 for (int index = 0; index < _tabs.Count; index++)
802 {
803 if (!MatchesMnemonic(_tabs[index].Mnemonic, args.Key))
804 continue;
805
806 args.Handled = true;
807 SelectTab(index);
808 return;
809 }
810 }
811
812 private void UpdateSelectedTabVisuals()
813 {
814 for (int index = 0; index < _tabs.Count; index++)
815 {
816 TabHeaderWidget header = _headers[_tabs[index]];
817 bool selected = index == _selectedIndex;
818
819 header.SetColors(
820 selected ? _selectedTabColor : _normalTabColor,
821 selected ? _selectedTabColor : _hoverTabColor,
822 selected ? _selectedTabColor : _pressedTabColor);
823
824 header.SetTextColor(_tabTextColor);
825 }
826 }
827
828 private void UpdatePageVisibility()
829 {
830 for (int index = 0; index < _tabs.Count; index++)
831 {
832 TabPageWidget page = _tabs[index];
833 bool isSelected = index == _selectedIndex;
834
835 if (_isShown && isSelected)
836 {
837 page.Show();
838 page.Activate();
839 }
840 else if (!isSelected || _isShown)
841 {
842 page.Hide();
843 }
844 }
845 }
846
847 private void ApplyZOrder()
848 {
849 foreach (TabPageWidget page in _tabs)
850 {
851 page.SetPageZOrder(_baseZOrder);
852 _headers[page].SetHeaderZOrder(_baseZOrder + 100);
853 }
854 }
855
856 private (int Rows, int Columns) ResolveGrid(Rectangle bounds)
857 {
858 if (!AutoExpandRows)
859 {
860 int columns = Math.Max(1, (int)Math.Ceiling(_tabs.Count / (double)TabRowCount));
861 return (TabRowCount, columns);
862 }
863
864 int autoColumns = _tabs.Count == 0
865 ? 1
866 : Math.Min(_tabs.Count, Math.Max(1, bounds.Width / PreferredTabWidth));
867
868 int autoRows = Math.Max(1, (int)Math.Ceiling(_tabs.Count / (double)autoColumns));
869
870 return (autoRows, autoColumns);
871 }
872
873 private Rectangle CreateHeaderBounds(
874 Rectangle bounds,
875 int index,
876 int columns)
877 {
878 int row = index / columns;
879 int column = index % columns;
880 int left = bounds.Left + column * bounds.Width / columns;
881 int right = bounds.Left + (column + 1) * bounds.Width / columns;
882
883 return Rectangle.FromLTRB(
884 left,
885 bounds.Top + row * TabHeight,
886 right,
887 bounds.Top + (row + 1) * TabHeight);
888 }
889
890 private Rectangle CreateContentBounds(
891 Rectangle bounds,
892 int rowCount)
893 {
894 int contentTop = bounds.Top + rowCount * TabHeight;
895
896 return new Rectangle(
897 bounds.Left,
898 contentTop,
899 bounds.Width,
900 Math.Max(1, bounds.Bottom - contentTop));
901 }
902
903 private static Rectangle ValidateBounds(Rectangle bounds)
904 {
905 ValidateSize(bounds.Size);
906 return bounds;
907 }
908
909 private static void ValidateSize(Size size)
910 {
911 if (size.Width <= 0 || size.Height <= DefaultTabHeight)
912 {
913 throw new ArgumentOutOfRangeException(
914 nameof(size),
915 size,
916 "Tab-control bounds must be positive and taller than one tab row.");
917 }
918 }
919
920 private void ValidateMnemonic(char? mnemonic)
921 {
922 if (mnemonic is not char value)
923 return;
924
925 if (_tabs.Any(tab => tab.Mnemonic is char existing &&
926 char.ToUpperInvariant(existing) == char.ToUpperInvariant(value)))
927 {
928 throw new ArgumentException($"Duplicate tab mnemonic '{value}'.", nameof(mnemonic));
929 }
930 }
931
932 private static void ValidateTitle(string title)
933 {
934 ArgumentException.ThrowIfNullOrWhiteSpace(title);
935 }
936
937 private static bool MatchesMnemonic(
938 char? mnemonic,
939 int key)
940 {
941 return mnemonic is char character &&
942 key >= 0 &&
943 key <= char.MaxValue &&
944 char.ToUpperInvariant(character) == char.ToUpperInvariant((char)key);
945 }
946
947 private sealed class TabHeaderWidget : DraggableWidgetBase
948 {
949 private Color _normalColor = Color.FromArgb(
950 255,
951 60,
952 60,
953 68);
954
955 private Color _hoverColor = Color.FromArgb(
956 255,
957 78,
958 78,
959 88);
960
961 private Color _pressedColor = Color.FromArgb(
962 255,
963 42,
964 42,
965 48);
966
970 internal event Action<TabHeaderWidget>? Invoked;
971
975 internal event Action<TabHeaderWidget, WidgetDragEventArgs, View>? ReorderDragged;
976
980 internal event Action<TabHeaderWidget>? ReorderDragEnded;
981
982 private View? _dragView;
983 private bool _disposed;
984
993 internal TabHeaderWidget(
994 RenderSurfaceHostBase renderSurfaceHost,
995 View view,
996 Rectangle bounds,
997 string text,
998 string nickname)
999 : base(
1000 renderSurfaceHost,
1002 bounds.Location,
1003 nickname)
1004 {
1005 ArgumentNullException.ThrowIfNull(view);
1006
1007 Background = CreateBackground(
1008 renderSurfaceHost,
1009 view,
1010 bounds,
1011 nickname);
1012
1013 Label = CreateLabel(
1014 renderSurfaceHost,
1015 view,
1016 bounds,
1017 text);
1018
1019 CompleteInitialization();
1020 }
1021
1030 internal TabHeaderWidget(
1031 RenderSurfaceHostBase renderSurfaceHost,
1032 SceneLayer sceneLayer,
1033 Rectangle bounds,
1034 string text,
1035 string nickname)
1036 : base(
1037 renderSurfaceHost,
1039 bounds.Location,
1040 nickname)
1041 {
1042 ArgumentNullException.ThrowIfNull(sceneLayer);
1043
1044 Background = CreateBackground(
1045 renderSurfaceHost,
1046 sceneLayer,
1047 bounds,
1048 nickname);
1049
1050 Label = CreateLabel(
1051 renderSurfaceHost,
1052 sceneLayer,
1053 bounds,
1054 text);
1055
1056 CompleteInitialization();
1057 }
1058
1062 internal DirectRectangle Background { get; }
1063
1067 internal TextBlock Label { get; }
1068
1072 internal Rectangle Bounds => Mode == DirectDrawingMode.View
1073 ? Background.ScreenBounds
1074 : Background.WorldBounds;
1075
1080 internal void SetBounds(Rectangle bounds)
1081 {
1082 if (bounds.Width <= 0 || bounds.Height <= 0)
1083 throw new ArgumentOutOfRangeException(nameof(bounds));
1084
1085 if (Mode == DirectDrawingMode.View)
1086 {
1087 Background.ScreenBounds = bounds;
1088 Label.ScreenBounds = bounds;
1089 }
1090 else
1091 {
1092 Background.WorldBounds = bounds;
1093 Label.WorldBounds = bounds;
1094 }
1095
1096 SetPosition(bounds.Location.X, bounds.Location.Y);
1097 }
1098
1103 internal void SetText(string text)
1104 {
1105 Label.SetText(text);
1106 }
1107
1114 internal void SetColors(
1115 Color normal,
1116 Color hover,
1117 Color pressed)
1118 {
1119 _normalColor = normal;
1120 _hoverColor = hover;
1121 _pressedColor = pressed;
1122 UpdateBackgroundColor(normal);
1123 }
1124
1129 internal void SetTextColor(Color color)
1130 {
1131 Label.SetColors(color, Color.Transparent);
1132 }
1133
1138 internal void SetHeaderZOrder(int zOrder)
1139 {
1140 Background.ZOrder = zOrder;
1141 Label.ZOrder = zOrder + 1;
1142 }
1143
1145 protected override Vector2 ConvertScreenDeltaToPositionDelta(
1146 Vector2 totalScreenDeltaPx,
1147 View view)
1148 {
1149 return Vector2.Zero;
1150 }
1151
1153 protected override void OnPointerEnter(WidgetPointerEventArgs args)
1154 {
1155 base.OnPointerEnter(args);
1156 UpdateBackgroundColor(_hoverColor);
1157 }
1158
1160 protected override void OnPointerLeave(WidgetPointerEventArgs args)
1161 {
1162 base.OnPointerLeave(args);
1163 UpdateBackgroundColor(_normalColor);
1164 }
1165
1167 protected override void OnPointerDown(WidgetPointerEventArgs args)
1168 {
1169 base.OnPointerDown(args);
1170
1171 if (args.IsPrimaryButton)
1172 {
1173 _dragView = args.View;
1174 UpdateBackgroundColor(_pressedColor);
1175 }
1176 }
1177
1179 protected override void OnPointerUp(WidgetPointerEventArgs args)
1180 {
1181 base.OnPointerUp(args);
1182 UpdateBackgroundColor(_hoverColor);
1183 }
1184
1186 protected override void OnPointerClick(WidgetPointerEventArgs args)
1187 {
1188 base.OnPointerClick(args);
1189
1190 if (!args.IsPrimaryButton)
1191 return;
1192
1193 args.Handled = true;
1194 Invoked?.Invoke(this);
1195 }
1196
1198 protected override void OnKeyboardInput(WidgetKeyboardEventArgs args)
1199 {
1200 base.OnKeyboardInput(args);
1201
1202 if (args.KeyAction != KeyAction.Pressed || args.Key is not 13 and not 32)
1203 return;
1204
1205 args.Handled = true;
1206 Invoked?.Invoke(this);
1207 }
1208
1210 protected override void OnDragged(WidgetDragEventArgs args)
1211 {
1212 base.OnDragged(args);
1213
1214 if (_dragView is not null)
1215 ReorderDragged?.Invoke(this, args, _dragView);
1216 }
1217
1219 protected override void OnDragEnded(WidgetDragEventArgs args)
1220 {
1221 base.OnDragEnded(args);
1222 ReorderDragEnded?.Invoke(this);
1223 _dragView = null;
1224 }
1225
1227 public override void Dispose()
1228 {
1229 if (_disposed)
1230 return;
1231
1232 _disposed = true;
1233 base.Dispose();
1234 Invoked = null;
1235 ReorderDragged = null;
1236 ReorderDragEnded = null;
1237 }
1238
1239 private void CompleteInitialization()
1240 {
1241 Add(Background);
1242 Add(Label);
1243
1244 SetHeaderZOrder(0);
1245 CanReceiveFocus = true;
1247 IsDragEnabled = false;
1248 }
1249
1250 private void UpdateBackgroundColor(Color color)
1251 {
1252 Background.SetColor(color);
1253 Background.SetPosition(Background.GetPosition());
1254 }
1255
1256 private static DirectRectangle CreateBackground(
1257 RenderSurfaceHostBase renderSurfaceHost,
1258 View view,
1259 Rectangle bounds,
1260 string nickname)
1261 {
1262 return new DirectRectangle(
1263 Color.FromArgb(
1264 255,
1265 60,
1266 60,
1267 68),
1268 renderSurfaceHost,
1269 view,
1270 bounds,
1271 $"{nickname}.background")
1272 .SetFilled(true)
1273 .SetBorderColor(
1274 Color.FromArgb(
1275 255,
1276 150,
1277 150,
1278 160))
1279 .SetStrokeWidth(1.5f)
1280 .SetCornerRadius(5f);
1281 }
1282
1283 private static DirectRectangle CreateBackground(
1284 RenderSurfaceHostBase renderSurfaceHost,
1285 SceneLayer sceneLayer,
1286 Rectangle bounds,
1287 string nickname)
1288 {
1289 return new DirectRectangle(
1290 Color.FromArgb(
1291 255,
1292 60,
1293 60,
1294 68),
1295 renderSurfaceHost,
1296 sceneLayer,
1297 bounds,
1298 $"{nickname}.background")
1299 .SetFilled(true)
1300 .SetBorderColor(
1301 Color.FromArgb(
1302 255,
1303 150,
1304 150,
1305 160))
1306 .SetStrokeWidth(1.5f)
1307 .SetCornerRadius(5f);
1308 }
1309
1310 private static TextBlock CreateLabel(
1311 RenderSurfaceHostBase renderSurfaceHost,
1312 View view,
1313 Rectangle bounds,
1314 string text)
1315 {
1316 return new TextBlock(renderSurfaceHost, view, bounds)
1317 .SetText(text)
1318 .SetFont(SKTypeface.Default, 16f, minSize: 10f)
1319 .SetColors(SKColors.White, SKColors.Transparent)
1320 .SetAlignment(SKTextAlign.Center, TextBlock.VerticalAlign.Center)
1321 .EnableWrapping(false);
1322 }
1323
1324 private static TextBlock CreateLabel(
1325 RenderSurfaceHostBase renderSurfaceHost,
1326 SceneLayer sceneLayer,
1327 Rectangle bounds,
1328 string text)
1329 {
1330 return new TextBlock(
1331 renderSurfaceHost,
1332 sceneLayer,
1333 view: null,
1334 worldBounds: bounds)
1335 .SetText(text)
1336 .SetFont(SKTypeface.Default, 16f, minSize: 10f)
1337 .SetColors(SKColors.White, SKColors.Transparent)
1338 .SetAlignment(SKTextAlign.Center, TextBlock.VerticalAlign.Center)
1339 .EnableWrapping(false);
1340 }
1341 }
1342}
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.
View? View
Gets the view shared by all children in view mode.
Vector2 GetPosition()
Gets the current composite anchor.
void SetPosition(Vector2 position)
Sets the composite anchor and repositions all children from their stored offsets.
DirectDrawingMode Mode
Gets the coordinate mode shared by the composite and its children.
Represents a base class for hosting a render surface, providing functionality for managing rendering ...
override DirectComposite Remove(IDirectCompositeChild child)
Removes a child without disposing it.The current composite.
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.
TabControlWidget Relayout()
Recomputes header and page layout after changing a size or tab-layout setting.
override void Dispose()
Disposes the composite and every child it owns.
TabControlWidget SelectTab(int index)
Selects the page at the supplied tab index.
bool AutoExpandRows
Gets or sets whether the control adds rows as more tabs are added.
override void OnKeyboardInput(WidgetKeyboardEventArgs args)
Called when keyboard input is routed to the widget.
TabPageWidget AddTab(string title, char? mnemonic=null, string? nickname=null)
Adds a page to the end of the tab strip.
TabControlWidget SetTabControlZOrder(int zOrder)
Sets the base Z-order for all page and header visuals.
int TabRowCount
Gets or sets the number of header rows reserved when AutoExpandRows is disabled.
TabPageWidget? SelectedTab
Gets the selected page, or null when the control has no pages.
TabControlWidget SetTabColors(Color normal, Color hover, Color pressed, Color selected)
Sets the tab header colors.
bool IsTabReorderingEnabled
Gets or sets whether headers can be drag-reordered with a primary pointer.
bool RemoveTab(TabPageWidget page, bool dispose=false)
Removes a page from the tab control.
int PreferredTabWidth
Gets or sets the preferred header width used when rows expand automatically.
Action< TabPageWidget, int, int >? TabReordered
Occurs after a page moves to a different tab index.
Rectangle Bounds
Gets the current control bounds in its native coordinate space.
override void ProcessShown()
Performs processing when the widget is shown before notification callbacks are invoked.
int SelectedIndex
Gets the selected tab index, or -1 when the control has no pages.
override void ProcessHidden()
Performs processing when the widget is hidden before notification callbacks are invoked.
TabControlWidget MoveTab(int fromIndex, int toIndex)
Moves one page to a different tab index.
TabControlWidget SetTabTextColor(Color color)
Sets the color used by tab-header labels.
IReadOnlyList< TabPageWidget > Tabs
Gets the pages in their visible tab order.
int DisplayedTabRowCount
Gets the number of header rows currently occupied or reserved by the control.
TabControlWidget SelectTab(TabPageWidget page)
Selects the supplied page.
int DisplayedTabColumnCount
Gets the number of tab cells displayed across each header row.
TabControlWidget MoveTab(TabPageWidget page, int toIndex)
Moves the supplied page to a different tab index.
TabControlWidget(RenderSurfaceHostBase renderSurfaceHost, View view, Rectangle bounds, string? nickname=null)
Creates a view-level tab control.
float TabReorderDragThresholdPx
Gets or sets the pointer movement required to begin a tab reordering drag.
int TabHeight
Gets or sets the height of one tab header row in native pixels.
TabControlWidget(RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle bounds, string? nickname=null)
Creates a scene-layer tab control.
Action< TabPageWidget >? SelectedTabChanged
Occurs after the selected page changes.
override void ProcessActivated()
Performs processing when the widget is activated before notification callbacks are invoked.
Size Size
Gets or sets the overall control size while preserving its anchor position.
Represents one selectable page owned by a TabControlWidget.
bool IsDragEnabled
Gets or sets whether new drag operations may begin.
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.
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.
@ 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.
KeyboardModifierState
Represents the state of keyboard modifier keys (Shift, Ctrl, Alt) that can be pressed simultaneously ...
KeyAction
Represents the type of action performed on a keyboard key, indicating whether the key was pressed,...
Definition KeyAction.cs:10