Files
tbd-station-14/Content.Client/UserInterface/Controls/FancyTree/FancyTree.xaml.cs
Leon Friedrich 22d72f56b5 Guidebook Revival (#13320)
* Fix some bugs in stations and do a little cleanup.

* Begin backporting the guidebook.

* wow that's a lot of work.

* More work, gives the monkey some more interactions.

* disco monkye.

* monky

* jobs entry.

* more writing.

* disco

* im being harassed

* fix spacing.

* i hate writing.

* Update Resources/Prototypes/Entities/Mobs/NPCs/animals.yml

Co-authored-by: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com>

* builds again

* a

* pilfer changes from AL

* fix and remove unused code

* pilfer actual guide changes from AL

* localization

* more error logs & safety checks

* replace controls button with command

* add test

* todos

* pidgin parsing

* remove old parser

* Move files and change tree sorting

* add localization and public methods.

* Add help component/verb

* rename ITag to IDocumentTag

* Fix yml and tweak tooltips

* autoclose tooltip

* Split container

* Fancier-tree

* Hover color

* txt to xml

* oops

* Curse you hidden merge conflicts

* Rename parsing manager

* Stricter arg parsing

tag args must now be of the form key="value"

* Change default args

* Moar tests

* nullable enable

* Even fancier tree

* extremely fancy trees

* better indent icons

* stricter xml and subheadings

* tweak embed margin

* Fix parsing bugs

* quick fixes.

* spain.

* ogh

* hn bmvdsyc

Co-authored-by: moonheart08 <moonheart08@users.noreply.github.com>
2023-01-16 02:42:22 -06:00

283 lines
7.9 KiB
C#

using Content.Client.Resources;
using Robust.Client.AutoGenerated;
using Robust.Client.Graphics;
using Robust.Client.ResourceManagement;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
namespace Content.Client.UserInterface.Controls.FancyTree;
/// <summary>
/// Functionally similar to <see cref="Tree"/>, but with collapsible sections,
/// </summary>
[GenerateTypedNameReferences]
public sealed partial class FancyTree : Control
{
[Dependency] private readonly IResourceCache _resCache = default!;
public const string StylePropertyLineWidth = "LineWidth";
public const string StylePropertyLineColor = "LineColor";
public const string StylePropertyIconColor = "IconColor";
public const string StylePropertyIconExpanded = "IconExpanded";
public const string StylePropertyIconCollapsed = "IconCollapsed";
public const string StylePropertyIconNoChildren = "IconNoChildren";
public readonly List<TreeItem> Items = new();
public event Action<TreeItem?>? OnSelectedItemChanged;
public int? SelectedIndex { get; private set; }
private bool _rowStyleUpdateQueued = true;
/// <summary>
/// Whether or not to draw the lines connecting parents & children.
/// </summary>
public bool DrawLines = true;
/// <summary>
/// Colour of the lines connecting parents & their child entries.
/// </summary>
public Color LineColor = Color.White;
/// <summary>
/// Color used to modulate the icon textures.
/// </summary>
public Color IconColor = Color.White;
/// <summary>
/// Width of the lines connecting parents & their child entries.
/// </summary>
public int LineWidth = 2;
// If people ever want to customize this, this should be a style parameter/
public const int Indentation = 16;
public const string DefaultIconExpanded = "/Textures/Interface/Nano/inverted_triangle.svg.png";
public const string DefaultIconCollapsed = "/Textures/Interface/Nano/triangle_right.png";
public const string DefaultIconNoChildren = "/Textures/Interface/Nano/triangle_right_hollow.svg.png";
public Texture? IconExpanded;
public Texture? IconCollapsed;
public Texture? IconNoChildren;
/// <summary>
/// If true, tree entries will hide their icon if the texture is set to null. If the icon is hidden then the
/// text of that entry will no longer be aligned with sibling entries that do have an icon.
/// </summary>
public bool HideEmptyIcon
{
get => _hideEmptyIcon;
set => SetHideEmptyIcon(value);
}
private bool _hideEmptyIcon;
public TreeItem? SelectedItem => SelectedIndex == null ? null : Items[SelectedIndex.Value];
/// <summary>
/// If true, a collapsed item will automatically expand when first selected. If false, it has to be manually expanded by
/// clicking on it a second time.
/// </summary>
public bool AutoExpand = true;
public FancyTree()
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
LoadIcons();
}
private void LoadIcons()
{
IconColor = TryGetStyleProperty(StylePropertyIconColor, out Color color) ? color : Color.White;
string? path;
if (!TryGetStyleProperty(StylePropertyIconExpanded, out IconExpanded))
IconExpanded = _resCache.GetTexture(DefaultIconExpanded);
if (!TryGetStyleProperty(StylePropertyIconCollapsed, out IconCollapsed))
IconCollapsed = _resCache.GetTexture(DefaultIconCollapsed);
if (!TryGetStyleProperty(StylePropertyIconNoChildren, out IconNoChildren))
IconNoChildren = _resCache.GetTexture(DefaultIconNoChildren);
foreach (var item in Body.Children)
{
RecursiveUpdateIcon((TreeItem) item);
}
}
public TreeItem AddItem(TreeItem? parent = null)
{
if (parent != null)
{
if (parent.Tree != this)
throw new ArgumentException("Parent must be owned by this tree.", nameof(parent));
DebugTools.Assert(Items[parent.Index] == parent);
}
var item = new TreeItem()
{
Tree = this,
Index = Items.Count,
};
Items.Add(item);
item.Icon.SetSize = (Indentation, Indentation);
item.Button.OnPressed += (_) => OnPressed(item);
if (parent == null)
Body.AddChild(item);
else
{
item.Padding.MinWidth = parent.Padding.MinWidth + Indentation;
parent.Body.AddChild(item);
}
item.UpdateIcon();
QueueRowStyleUpdate();
return item;
}
private void OnPressed(TreeItem item)
{
if (SelectedIndex == item.Index)
{
item.SetExpanded(!item.Expanded);
return;
}
SetSelectedIndex(item.Index);
}
public void SetSelectedIndex(int? value)
{
if (value == null || value < 0 || value >= Items.Count)
value = null;
if (SelectedIndex == value)
return;
SelectedItem?.SetSelected(false);
SelectedIndex = value;
var newSelection = SelectedItem;
if (newSelection != null)
{
newSelection.SetSelected(true);
if (AutoExpand && !newSelection.Expanded)
newSelection.SetExpanded(true);
}
OnSelectedItemChanged?.Invoke(newSelection);
}
public void SetAllExpanded(bool value)
{
foreach (var item in Body.Children)
{
RecursiveSetExpanded((TreeItem) item, value);
}
}
public void RecursiveSetExpanded(TreeItem item, bool value)
{
item.SetExpanded(value);
foreach (var child in item.Body.Children)
{
RecursiveSetExpanded((TreeItem) child, value);
}
}
public void Clear()
{
foreach (var item in Items)
{
item.Dispose();
}
Items.Clear();
Body.Children.Clear();
SelectedIndex = null;
}
public void QueueRowStyleUpdate()
{
_rowStyleUpdateQueued = true;
}
protected override void FrameUpdate(FrameEventArgs args)
{
if (!_rowStyleUpdateQueued)
return;
_rowStyleUpdateQueued = false;
int index = 0;
foreach (var item in Body.Children)
{
RecursivelyUpdateRowStyle((TreeItem) item, ref index);
}
}
private void RecursivelyUpdateRowStyle(TreeItem item, ref int index)
{
if (int.IsOddInteger(index))
{
item.Button.RemoveStyleClass(TreeItem.StyleClassEvenRow);
item.Button.AddStyleClass(TreeItem.StyleClassOddRow);
}
else
{
item.Button.AddStyleClass(TreeItem.StyleClassEvenRow);
item.Button.RemoveStyleClass(TreeItem.StyleClassOddRow);
}
index++;
if (!item.Expanded)
return;
foreach (var child in item.Body.Children)
{
RecursivelyUpdateRowStyle((TreeItem) child, ref index);
}
}
private void SetHideEmptyIcon(bool value)
{
if (value == _hideEmptyIcon)
return;
_hideEmptyIcon = value;
foreach (var item in Body.Children)
{
RecursiveUpdateIcon((TreeItem) item);
}
}
private void RecursiveUpdateIcon(TreeItem item)
{
item.UpdateIcon();
foreach (var child in item.Body.Children)
{
RecursiveUpdateIcon((TreeItem) child);
}
}
protected override void StylePropertiesChanged()
{
LoadIcons();
LineColor = TryGetStyleProperty(StylePropertyLineColor, out Color color) ? color: Color.White;
LineWidth = TryGetStyleProperty(StylePropertyLineWidth, out int width) ? width : 2;
base.StylePropertiesChanged();
}
}