CatStation/Content.Client/UserInterface/Controls/ListContainer.cs
Dmitry 25c5e59248
Upstream (#3172)
Signed-off-by: Prole <172158352+Prole0@users.noreply.github.com>
Co-authored-by: PJBot <pieterjan.briers+bot@gmail.com>
Co-authored-by: ScarKy0 <106310278+ScarKy0@users.noreply.github.com>
Co-authored-by: Samuka-C <47865393+Samuka-C@users.noreply.github.com>
Co-authored-by: ArtisticRoomba <145879011+ArtisticRoomba@users.noreply.github.com>
Co-authored-by: Partmedia <kevinz5000@gmail.com>
Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
Co-authored-by: themias <89101928+themias@users.noreply.github.com>
Co-authored-by: Victor Shen <71985089+Vexerot@users.noreply.github.com>
Co-authored-by: Ed <96445749+TheShuEd@users.noreply.github.com>
Co-authored-by: Milon <milonpl.git@proton.me>
Co-authored-by: Kirus59 <145689588+Kirus59@users.noreply.github.com>
Co-authored-by: Nemanja <98561806+EmoGarbage404@users.noreply.github.com>
Co-authored-by: Stomf <5dorkydorks@gmail.com>
Co-authored-by: drakewill-CRL <46307022+drakewill-CRL@users.noreply.github.com>
Co-authored-by: PraxisMapper <praxismapper@gmail.com>
Co-authored-by: EmoGarbage404 <retron404@gmail.com>
Co-authored-by: lzk <124214523+lzk228@users.noreply.github.com>
Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com>
Co-authored-by: IProduceWidgets <107586145+IProduceWidgets@users.noreply.github.com>
Co-authored-by: TytosB <54259736+TytosB@users.noreply.github.com>
Co-authored-by: abadaba695 <spacestation13thingy@gmail.com>
Co-authored-by: kosticia <kosticia46@gmail.com>
Co-authored-by: Thinbug <101073555+Thinbug0@users.noreply.github.com>
Co-authored-by: pathetic meowmeow <uhhadd@gmail.com>
Co-authored-by: SlamBamActionman <83650252+SlamBamActionman@users.noreply.github.com>
Co-authored-by: Boaz1111 <149967078+Boaz1111@users.noreply.github.com>
Co-authored-by: ActiveMammmoth <140334666+ActiveMammmoth@users.noreply.github.com>
Co-authored-by: Myra <vasilis@pikachu.systems>
Co-authored-by: Whatstone <166147148+whatston3@users.noreply.github.com>
Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
Co-authored-by: K-Dynamic <20566341+K-Dynamic@users.noreply.github.com>
Co-authored-by: Gentleman-Bird <dcgreen406@gmail.com>
Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Co-authored-by: BIGZi0348 <svalker0348@gmail.com>
Co-authored-by: LaCumbiaDelCoronavirus <90893484+LaCumbiaDelCoronavirus@users.noreply.github.com>
Co-authored-by: imatsoup <93290208+imatsoup@users.noreply.github.com>
Co-authored-by: Matthew Herber <32679887+happyrobot33@users.noreply.github.com>
Co-authored-by: Ertanic <36124833+Ertanic@users.noreply.github.com>
Co-authored-by: MissKay1994 <15877268+MissKay1994@users.noreply.github.com>
Co-authored-by: Errant <35878406+Errant-4@users.noreply.github.com>
Co-authored-by: eoineoineoin <helloworld@eoinrul.es>
Co-authored-by: Tiniest Shark <head.rebel@yahoo.com>
Co-authored-by: nikitosych <boriszyn@gmail.com>
Co-authored-by: Tayrtahn <tayrtahn@gmail.com>
Co-authored-by: Perry Fraser <perryprog@users.noreply.github.com>
Co-authored-by: YoungThug <ramialanbagy@gmail.com>
Co-authored-by: beck-thompson <107373427+beck-thompson@users.noreply.github.com>
Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
Co-authored-by: Southbridge <7013162+southbridge-fur@users.noreply.github.com>
Co-authored-by: Vladislav Suchkov <20380250+murolem@users.noreply.github.com>
Co-authored-by: Prole <172158352+Prole0@users.noreply.github.com>
Co-authored-by: Unkn0wn_Gh0st <shadowstalkermll@gmail.com>
Co-authored-by: 3nderall <101940324+3nderall@users.noreply.github.com>
Co-authored-by: Radezolid <snappednexus@gmail.com>
Co-authored-by: J <billsmith116@gmail.com>
Co-authored-by: Ghagliiarghii <68826635+Ghagliiarghii@users.noreply.github.com>
Co-authored-by: chromiumboy <50505512+chromiumboy@users.noreply.github.com>
Co-authored-by: youtissoum <51883137+youtissoum@users.noreply.github.com>
Co-authored-by: Minemoder5000 <minemoder50000@gmail.com>
Co-authored-by: Spanky <scott@wearejacob.com>
Co-authored-by: Spessmann <156740760+Spessmann@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: brainfood1183 <113240905+brainfood1183@users.noreply.github.com>
Co-authored-by: Deerstop <edainturner@gmail.com>
Co-authored-by: B_Kirill <153602297+B-Kirill@users.noreply.github.com>
Co-authored-by: archee1 <archee3@hotmail.co.uk>
Co-authored-by: Cojoke <83733158+Cojoke-dot@users.noreply.github.com>
Co-authored-by: Quantum-cross <7065792+Quantum-cross@users.noreply.github.com>
Co-authored-by: poklj <compgeek223@gmail.com>
Co-authored-by: Krunklehorn <42424291+Krunklehorn@users.noreply.github.com>
Co-authored-by: OnyxTheBrave <131422822+OnyxTheBrave@users.noreply.github.com>
Co-authored-by: UpAndLeaves <92269094+Alpha-Two@users.noreply.github.com>
Co-authored-by: Flareguy <78941145+Flareguy@users.noreply.github.com>
Co-authored-by: Zalycon <84675130+Zalycon@users.noreply.github.com>
Co-authored-by: deltanedas <39013340+deltanedas@users.noreply.github.com>
Co-authored-by: Verm <32827189+Vermidia@users.noreply.github.com>
Co-authored-by: nikthechampiongr <32041239+nikthechampiongr@users.noreply.github.com>
Co-authored-by: ScarKy0 <scarky0@onet.eu>
Co-authored-by: Dmitry <57028746+dimm00n@users.noreply.github.com>
2025-05-24 11:00:17 +03:00

403 lines
12 KiB
C#

using System.Linq;
using System.Numerics;
using JetBrains.Annotations;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.Input;
using Robust.Shared.Map;
namespace Content.Client.UserInterface.Controls;
[Virtual]
public class ListContainer : Control
{
public const string StylePropertySeparation = "separation";
public const string StyleClassListContainerButton = "list-container-button";
public int? SeparationOverride { get; set; }
public bool Group
{
get => _buttonGroup != null;
set => _buttonGroup = value ? new ButtonGroup() : null;
}
public bool Toggle { get; set; }
/// <summary>
/// Called when creating a button on the UI.
/// The provided <see cref="ListContainerButton"/> is the generated button that Controls should be parented to.
/// </summary>
public Action<ListData, ListContainerButton>? GenerateItem;
/// <inheritdoc cref="BaseButton.OnPressed"/>
public Action<BaseButton.ButtonEventArgs, ListData>? ItemPressed;
/// <summary>
/// Invoked when a KeyBind is pressed on a ListContainerButton.
/// </summary>
public Action<GUIBoundKeyEventArgs, ListData>? ItemKeyBindDown;
/// <summary>
/// Invoked when the selected item does not exist in the new data when PopulateList is called.
/// </summary>
public Action? NoItemSelected;
public IReadOnlyList<ListData> Data => _data;
private const int DefaultSeparation = 3;
private readonly VScrollBar _vScrollBar;
private readonly Dictionary<ListData, ListContainerButton> _buttons = new();
private List<ListData> _data = new();
private ListData? _selected;
private float _itemHeight = 0;
private float _totalHeight = 0;
private int _topIndex = 0;
private int _bottomIndex = 0;
private bool _updateChildren = false;
private bool _suppressScrollValueChanged;
private ButtonGroup? _buttonGroup;
public int ScrollSpeedY { get; set; } = 50;
private int ActualSeparation
{
get
{
if (TryGetStyleProperty(StylePropertySeparation, out int separation))
{
return separation;
}
return SeparationOverride ?? DefaultSeparation;
}
}
public ListContainer()
{
HorizontalExpand = true;
VerticalExpand = true;
RectClipContent = true;
MouseFilter = MouseFilterMode.Pass;
_vScrollBar = new VScrollBar
{
HorizontalExpand = false,
HorizontalAlignment = HAlignment.Right
};
AddChild(_vScrollBar);
_vScrollBar.OnValueChanged += ScrollValueChanged;
}
public virtual void PopulateList(IReadOnlyList<ListData> data)
{
if ((_itemHeight == 0 || _data is {Count: 0}) && data.Count > 0)
{
ListContainerButton control = new(data[0], 0);
GenerateItem?.Invoke(data[0], control);
// Yes this AddChild is necessary for reasons (get proper style or whatever?)
// without it the DesiredSize may be different to the final DesiredSize.
AddChild(control);
control.Measure(Vector2Helpers.Infinity);
_itemHeight = control.DesiredSize.Y;
control.Orphan();
}
// Ensure buttons are re-generated.
foreach (var button in _buttons.Values)
{
button.Dispose();
}
_buttons.Clear();
_data = data.ToList();
_updateChildren = true;
InvalidateArrange();
if (_selected != null && !data.Contains(_selected))
{
_selected = null;
NoItemSelected?.Invoke();
}
}
public void DirtyList()
{
_updateChildren = true;
InvalidateArrange();
}
#region Selection
public void Select(ListData data)
{
if (!_data.Contains(data))
return;
if (_buttons.TryGetValue(data, out var button) && Toggle)
button.Pressed = true;
_selected = data;
button ??= new ListContainerButton(data, _data.IndexOf(data));
OnItemPressed(new BaseButton.ButtonEventArgs(button,
new GUIBoundKeyEventArgs(EngineKeyFunctions.UIClick, BoundKeyState.Up,
new ScreenCoordinates(0, 0, WindowId.Main), true, Vector2.Zero, Vector2.Zero)));
}
/*
* Need to implement selecting the first item in code.
* Need to implement updating one entry without having to repopulate
*/
#endregion
private void OnItemPressed(BaseButton.ButtonEventArgs args)
{
if (args.Button is not ListContainerButton button)
return;
_selected = button.Data;
ItemPressed?.Invoke(args, button.Data);
}
private void OnItemKeyBindDown(ListContainerButton button, GUIBoundKeyEventArgs args)
{
ItemKeyBindDown?.Invoke(args, button.Data);
}
[Pure]
private Vector2 GetScrollValue()
{
var v = _vScrollBar.Value;
if (!_vScrollBar.Visible)
{
v = 0;
}
return new Vector2(0, v);
}
protected override Vector2 ArrangeOverride(Vector2 finalSize)
{
#region Scroll
var cHeight = _totalHeight;
var vBarSize = _vScrollBar.DesiredSize.X;
var (finalWidth, finalHeight) = finalSize;
try
{
// Suppress events to avoid weird recursion.
_suppressScrollValueChanged = true;
if (finalHeight < cHeight)
finalWidth -= vBarSize;
if (finalHeight < cHeight)
{
_vScrollBar.Visible = true;
_vScrollBar.Page = finalHeight;
_vScrollBar.MaxValue = cHeight;
}
else
_vScrollBar.Visible = false;
}
finally
{
_suppressScrollValueChanged = false;
}
if (_vScrollBar.Visible)
{
_vScrollBar.Arrange(UIBox2.FromDimensions(Vector2.Zero, finalSize));
}
#endregion
#region Rebuild Children
/*
* Example:
*
* var _itemHeight = 32;
* var separation = 3;
* 32 | 32 | Control.Size.Y 0
* 35 | 3 | Padding
* 67 | 32 | Control.Size.Y 1
* 70 | 3 | Padding
* 102 | 32 | Control.Size.Y 2
* 105 | 3 | Padding
* 137 | 32 | Control.Size.Y 3
*
* If viewport height is 60
* visible should be 2 items (start = 0, end = 1)
*
* scroll.Y = 11
* visible should be 3 items (start = 0, end = 2)
*
* start expected: 11 (item: 0)
* var start = (int) (scroll.Y
*
* if (scroll == 32) then { start = 1 }
* var start = (int) (scroll.Y + separation / (_itemHeight + separation));
* var start = (int) (32 + 3 / (32 + 3));
* var start = (int) (35 / 35);
* var start = (int) (1);
*
* scroll = 0, height = 36
* if (scroll + height == 36) then { end = 2 }
* var end = (int) Math.Ceiling(scroll.Y + height / (_itemHeight + separation));
* var end = (int) Math.Ceiling(0 + 36 / (32 + 3));
* var end = (int) Math.Ceiling(36 / 35);
* var end = (int) Math.Ceiling(1.02857);
* var end = (int) 2;
*
*/
var scroll = GetScrollValue();
var oldTopIndex = _topIndex;
_topIndex = (int) ((scroll.Y + ActualSeparation) / (_itemHeight + ActualSeparation));
if (_topIndex != oldTopIndex)
_updateChildren = true;
var oldBottomIndex = _bottomIndex;
_bottomIndex = (int) Math.Ceiling((scroll.Y + finalHeight) / (_itemHeight + ActualSeparation));
_bottomIndex = Math.Min(_bottomIndex, _data.Count);
if (_bottomIndex != oldBottomIndex)
_updateChildren = true;
// When scrolling only rebuild visible list when a new item should be visible
if (_updateChildren)
{
_updateChildren = false;
var toRemove = new Dictionary<ListData, ListContainerButton>(_buttons);
if (_data.Count > 0)
{
for (var i = _topIndex; i < _bottomIndex; i++)
{
var data = _data[i];
if (_buttons.TryGetValue(data, out var button))
toRemove.Remove(data);
else
{
button = new ListContainerButton(data, i);
button.OnPressed += OnItemPressed;
button.OnKeyBindDown += args => OnItemKeyBindDown(button, args);
button.ToggleMode = Toggle;
button.Group = _buttonGroup;
GenerateItem?.Invoke(data, button);
_buttons.Add(data, button);
if (Toggle && data == _selected)
button.Pressed = true;
AddChild(button);
}
button.SetPositionInParent(i - _topIndex);
button.Measure(finalSize);
}
}
foreach (var (data, button) in toRemove)
{
_buttons.Remove(data);
button.Dispose();
}
_vScrollBar.SetPositionLast();
}
#endregion
#region Layout Children
// Use pixel position
var pixelWidth = (int)(finalWidth * UIScale);
var pixelSeparation = (int) (ActualSeparation * UIScale);
var pixelOffset = (int) -((scroll.Y - _topIndex * (_itemHeight + ActualSeparation)) * UIScale);
var first = true;
foreach (var child in Children)
{
if (child == _vScrollBar)
continue;
if (!first)
pixelOffset += pixelSeparation;
first = false;
var pixelSize = child.DesiredPixelSize.Y;
var targetBox = new UIBox2i(0, pixelOffset, pixelWidth, pixelOffset + pixelSize);
child.ArrangePixel(targetBox);
pixelOffset += pixelSize;
}
#endregion
return finalSize;
}
protected override Vector2 MeasureOverride(Vector2 availableSize)
{
_vScrollBar.Measure(availableSize);
availableSize.X -= _vScrollBar.DesiredSize.X;
var constraint = new Vector2(availableSize.X, float.PositiveInfinity);
var childSize = Vector2.Zero;
foreach (var child in Children)
{
child.Measure(constraint);
if (child == _vScrollBar)
continue;
childSize = Vector2.Max(childSize, child.DesiredSize);
}
if (_itemHeight == 0 && childSize.Y != 0)
_itemHeight = childSize.Y;
_totalHeight = _itemHeight * _data.Count + ActualSeparation * (_data.Count - 1);
return new Vector2(childSize.X, 0);
}
private void ScrollValueChanged(Robust.Client.UserInterface.Controls.Range _)
{
if (_suppressScrollValueChanged)
{
return;
}
InvalidateArrange();
}
protected override void MouseWheel(GUIMouseWheelEventArgs args)
{
base.MouseWheel(args);
_vScrollBar.ValueTarget -= args.Delta.Y * ScrollSpeedY;
args.Handle();
}
}
public sealed class ListContainerButton : ContainerButton, IEntityControl
{
public readonly ListData Data;
public readonly int Index;
// public PanelContainer Background;
public ListContainerButton(ListData data, int index)
{
AddStyleClass(StyleClassButton);
Data = data;
Index = index;
// AddChild(Background = new PanelContainer
// {
// HorizontalExpand = true,
// VerticalExpand = true,
// PanelOverride = new StyleBoxFlat {BackgroundColor = new Color(55, 55, 68)}
// });
}
public EntityUid? UiEntity => (Data as EntityListData)?.Uid;
}
#region Data
public abstract record ListData;
public record EntityListData(EntityUid Uid) : ListData;
#endregion