ListContainer.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. using System.Linq;
  2. using System.Numerics;
  3. using JetBrains.Annotations;
  4. using Robust.Client.UserInterface;
  5. using Robust.Client.UserInterface.Controls;
  6. using Robust.Shared.Input;
  7. using Robust.Shared.Map;
  8. namespace Content.Client.UserInterface.Controls;
  9. [Virtual]
  10. public class ListContainer : Control
  11. {
  12. public const string StylePropertySeparation = "separation";
  13. public const string StyleClassListContainerButton = "list-container-button";
  14. public int? SeparationOverride { get; set; }
  15. public bool Group
  16. {
  17. get => _buttonGroup != null;
  18. set => _buttonGroup = value ? new ButtonGroup() : null;
  19. }
  20. public bool Toggle { get; set; }
  21. /// <summary>
  22. /// Called when creating a button on the UI.
  23. /// The provided <see cref="ListContainerButton"/> is the generated button that Controls should be parented to.
  24. /// </summary>
  25. public Action<ListData, ListContainerButton>? GenerateItem;
  26. /// <inheritdoc cref="BaseButton.OnPressed"/>
  27. public Action<BaseButton.ButtonEventArgs, ListData>? ItemPressed;
  28. /// <summary>
  29. /// Invoked when a KeyBind is pressed on a ListContainerButton.
  30. /// </summary>
  31. public Action<GUIBoundKeyEventArgs, ListData>? ItemKeyBindDown;
  32. /// <summary>
  33. /// Invoked when the selected item does not exist in the new data when PopulateList is called.
  34. /// </summary>
  35. public Action? NoItemSelected;
  36. public IReadOnlyList<ListData> Data => _data;
  37. private const int DefaultSeparation = 3;
  38. private readonly VScrollBar _vScrollBar;
  39. private readonly Dictionary<ListData, ListContainerButton> _buttons = new();
  40. private List<ListData> _data = new();
  41. private ListData? _selected;
  42. private float _itemHeight = 0;
  43. private float _totalHeight = 0;
  44. private int _topIndex = 0;
  45. private int _bottomIndex = 0;
  46. private bool _updateChildren = false;
  47. private bool _suppressScrollValueChanged;
  48. private ButtonGroup? _buttonGroup;
  49. public int ScrollSpeedY { get; set; } = 50;
  50. private int ActualSeparation
  51. {
  52. get
  53. {
  54. if (TryGetStyleProperty(StylePropertySeparation, out int separation))
  55. {
  56. return separation;
  57. }
  58. return SeparationOverride ?? DefaultSeparation;
  59. }
  60. }
  61. public ListContainer()
  62. {
  63. HorizontalExpand = true;
  64. VerticalExpand = true;
  65. RectClipContent = true;
  66. MouseFilter = MouseFilterMode.Pass;
  67. _vScrollBar = new VScrollBar
  68. {
  69. HorizontalExpand = false,
  70. HorizontalAlignment = HAlignment.Right
  71. };
  72. AddChild(_vScrollBar);
  73. _vScrollBar.OnValueChanged += ScrollValueChanged;
  74. }
  75. public virtual void PopulateList(IReadOnlyList<ListData> data)
  76. {
  77. if ((_itemHeight == 0 || _data is {Count: 0}) && data.Count > 0)
  78. {
  79. ListContainerButton control = new(data[0], 0);
  80. GenerateItem?.Invoke(data[0], control);
  81. // Yes this AddChild is necessary for reasons (get proper style or whatever?)
  82. // without it the DesiredSize may be different to the final DesiredSize.
  83. AddChild(control);
  84. control.Measure(Vector2Helpers.Infinity);
  85. _itemHeight = control.DesiredSize.Y;
  86. control.Orphan();
  87. }
  88. // Ensure buttons are re-generated.
  89. foreach (var button in _buttons.Values)
  90. {
  91. button.Dispose();
  92. }
  93. _buttons.Clear();
  94. _data = data.ToList();
  95. _updateChildren = true;
  96. InvalidateArrange();
  97. if (_selected != null && !data.Contains(_selected))
  98. {
  99. _selected = null;
  100. NoItemSelected?.Invoke();
  101. }
  102. }
  103. public void DirtyList()
  104. {
  105. _updateChildren = true;
  106. InvalidateArrange();
  107. }
  108. #region Selection
  109. public void Select(ListData data)
  110. {
  111. if (!_data.Contains(data))
  112. return;
  113. if (_buttons.TryGetValue(data, out var button) && Toggle)
  114. button.Pressed = true;
  115. _selected = data;
  116. button ??= new ListContainerButton(data, _data.IndexOf(data));
  117. OnItemPressed(new BaseButton.ButtonEventArgs(button,
  118. new GUIBoundKeyEventArgs(EngineKeyFunctions.UIClick, BoundKeyState.Up,
  119. new ScreenCoordinates(0, 0, WindowId.Main), true, Vector2.Zero, Vector2.Zero)));
  120. }
  121. /*
  122. * Need to implement selecting the first item in code.
  123. * Need to implement updating one entry without having to repopulate
  124. */
  125. #endregion
  126. private void OnItemPressed(BaseButton.ButtonEventArgs args)
  127. {
  128. if (args.Button is not ListContainerButton button)
  129. return;
  130. _selected = button.Data;
  131. ItemPressed?.Invoke(args, button.Data);
  132. }
  133. private void OnItemKeyBindDown(ListContainerButton button, GUIBoundKeyEventArgs args)
  134. {
  135. ItemKeyBindDown?.Invoke(args, button.Data);
  136. }
  137. [Pure]
  138. private Vector2 GetScrollValue()
  139. {
  140. var v = _vScrollBar.Value;
  141. if (!_vScrollBar.Visible)
  142. {
  143. v = 0;
  144. }
  145. return new Vector2(0, v);
  146. }
  147. protected override Vector2 ArrangeOverride(Vector2 finalSize)
  148. {
  149. #region Scroll
  150. var cHeight = _totalHeight;
  151. var vBarSize = _vScrollBar.DesiredSize.X;
  152. var (finalWidth, finalHeight) = finalSize;
  153. try
  154. {
  155. // Suppress events to avoid weird recursion.
  156. _suppressScrollValueChanged = true;
  157. if (finalHeight < cHeight)
  158. finalWidth -= vBarSize;
  159. if (finalHeight < cHeight)
  160. {
  161. _vScrollBar.Visible = true;
  162. _vScrollBar.Page = finalHeight;
  163. _vScrollBar.MaxValue = cHeight;
  164. }
  165. else
  166. _vScrollBar.Visible = false;
  167. }
  168. finally
  169. {
  170. _suppressScrollValueChanged = false;
  171. }
  172. if (_vScrollBar.Visible)
  173. {
  174. _vScrollBar.Arrange(UIBox2.FromDimensions(Vector2.Zero, finalSize));
  175. }
  176. #endregion
  177. #region Rebuild Children
  178. /*
  179. * Example:
  180. *
  181. * var _itemHeight = 32;
  182. * var separation = 3;
  183. * 32 | 32 | Control.Size.Y 0
  184. * 35 | 3 | Padding
  185. * 67 | 32 | Control.Size.Y 1
  186. * 70 | 3 | Padding
  187. * 102 | 32 | Control.Size.Y 2
  188. * 105 | 3 | Padding
  189. * 137 | 32 | Control.Size.Y 3
  190. *
  191. * If viewport height is 60
  192. * visible should be 2 items (start = 0, end = 1)
  193. *
  194. * scroll.Y = 11
  195. * visible should be 3 items (start = 0, end = 2)
  196. *
  197. * start expected: 11 (item: 0)
  198. * var start = (int) (scroll.Y
  199. *
  200. * if (scroll == 32) then { start = 1 }
  201. * var start = (int) (scroll.Y + separation / (_itemHeight + separation));
  202. * var start = (int) (32 + 3 / (32 + 3));
  203. * var start = (int) (35 / 35);
  204. * var start = (int) (1);
  205. *
  206. * scroll = 0, height = 36
  207. * if (scroll + height == 36) then { end = 2 }
  208. * var end = (int) Math.Ceiling(scroll.Y + height / (_itemHeight + separation));
  209. * var end = (int) Math.Ceiling(0 + 36 / (32 + 3));
  210. * var end = (int) Math.Ceiling(36 / 35);
  211. * var end = (int) Math.Ceiling(1.02857);
  212. * var end = (int) 2;
  213. *
  214. */
  215. var scroll = GetScrollValue();
  216. var oldTopIndex = _topIndex;
  217. _topIndex = (int) ((scroll.Y + ActualSeparation) / (_itemHeight + ActualSeparation));
  218. if (_topIndex != oldTopIndex)
  219. _updateChildren = true;
  220. var oldBottomIndex = _bottomIndex;
  221. _bottomIndex = (int) Math.Ceiling((scroll.Y + finalHeight) / (_itemHeight + ActualSeparation));
  222. _bottomIndex = Math.Min(_bottomIndex, _data.Count);
  223. if (_bottomIndex != oldBottomIndex)
  224. _updateChildren = true;
  225. // When scrolling only rebuild visible list when a new item should be visible
  226. if (_updateChildren)
  227. {
  228. _updateChildren = false;
  229. var toRemove = new Dictionary<ListData, ListContainerButton>(_buttons);
  230. foreach (var child in Children.ToArray())
  231. {
  232. if (child == _vScrollBar)
  233. continue;
  234. RemoveChild(child);
  235. }
  236. if (_data.Count > 0)
  237. {
  238. for (var i = _topIndex; i < _bottomIndex; i++)
  239. {
  240. var data = _data[i];
  241. if (_buttons.TryGetValue(data, out var button))
  242. toRemove.Remove(data);
  243. else
  244. {
  245. button = new ListContainerButton(data, i);
  246. button.OnPressed += OnItemPressed;
  247. button.OnKeyBindDown += args => OnItemKeyBindDown(button, args);
  248. button.ToggleMode = Toggle;
  249. button.Group = _buttonGroup;
  250. GenerateItem?.Invoke(data, button);
  251. _buttons.Add(data, button);
  252. if (Toggle && data == _selected)
  253. button.Pressed = true;
  254. }
  255. AddChild(button);
  256. button.Measure(finalSize);
  257. }
  258. }
  259. foreach (var (data, button) in toRemove)
  260. {
  261. _buttons.Remove(data);
  262. button.Dispose();
  263. }
  264. _vScrollBar.SetPositionLast();
  265. }
  266. #endregion
  267. #region Layout Children
  268. // Use pixel position
  269. var pixelWidth = (int)(finalWidth * UIScale);
  270. var pixelSeparation = (int) (ActualSeparation * UIScale);
  271. var pixelOffset = (int) -((scroll.Y - _topIndex * (_itemHeight + ActualSeparation)) * UIScale);
  272. var first = true;
  273. foreach (var child in Children)
  274. {
  275. if (child == _vScrollBar)
  276. continue;
  277. if (!first)
  278. pixelOffset += pixelSeparation;
  279. first = false;
  280. var pixelSize = child.DesiredPixelSize.Y;
  281. var targetBox = new UIBox2i(0, pixelOffset, pixelWidth, pixelOffset + pixelSize);
  282. child.ArrangePixel(targetBox);
  283. pixelOffset += pixelSize;
  284. }
  285. #endregion
  286. return finalSize;
  287. }
  288. protected override Vector2 MeasureOverride(Vector2 availableSize)
  289. {
  290. _vScrollBar.Measure(availableSize);
  291. availableSize.X -= _vScrollBar.DesiredSize.X;
  292. var constraint = new Vector2(availableSize.X, float.PositiveInfinity);
  293. var childSize = Vector2.Zero;
  294. foreach (var child in Children)
  295. {
  296. child.Measure(constraint);
  297. if (child == _vScrollBar)
  298. continue;
  299. childSize = Vector2.Max(childSize, child.DesiredSize);
  300. }
  301. if (_itemHeight == 0 && childSize.Y != 0)
  302. _itemHeight = childSize.Y;
  303. _totalHeight = _itemHeight * _data.Count + ActualSeparation * (_data.Count - 1);
  304. return new Vector2(childSize.X, 0);
  305. }
  306. private void ScrollValueChanged(Robust.Client.UserInterface.Controls.Range _)
  307. {
  308. if (_suppressScrollValueChanged)
  309. {
  310. return;
  311. }
  312. InvalidateArrange();
  313. }
  314. protected override void MouseWheel(GUIMouseWheelEventArgs args)
  315. {
  316. base.MouseWheel(args);
  317. _vScrollBar.ValueTarget -= args.Delta.Y * ScrollSpeedY;
  318. args.Handle();
  319. }
  320. }
  321. public sealed class ListContainerButton : ContainerButton, IEntityControl
  322. {
  323. public readonly ListData Data;
  324. public readonly int Index;
  325. // public PanelContainer Background;
  326. public ListContainerButton(ListData data, int index)
  327. {
  328. AddStyleClass(StyleClassButton);
  329. Data = data;
  330. Index = index;
  331. // AddChild(Background = new PanelContainer
  332. // {
  333. // HorizontalExpand = true,
  334. // VerticalExpand = true,
  335. // PanelOverride = new StyleBoxFlat {BackgroundColor = new Color(55, 55, 68)}
  336. // });
  337. }
  338. public EntityUid? UiEntity => (Data as EntityListData)?.Uid;
  339. }
  340. #region Data
  341. public abstract record ListData;
  342. public record EntityListData(EntityUid Uid) : ListData;
  343. #endregion