Files
tbd-station-14/Content.Client/Crayon/UI/CrayonWindow.xaml.cs
metalgearsloth edb05e36bb Reapply "Remove some BUI boilerplate" (#30214) (#30219)
* Reapply "Remove some BUI boilerplate" (#30214)

This reverts commit cb0ba66be3.

* Fix gas tank

* Fix PA

* Fix microwave

* Comms console underwrap

* Fix rcd

* log wehs
2024-07-21 14:48:13 +10:00

121 lines
3.4 KiB
C#

using System.Collections.Generic;
using Content.Client.Stylesheets;
using Content.Shared.Crayon;
using Content.Shared.Decals;
using Robust.Client.AutoGenerated;
using Robust.Client.Graphics;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Client.Utility;
using Robust.Shared.Graphics;
using Robust.Shared.Maths;
using Robust.Shared.Utility;
using static Robust.Client.UserInterface.Controls.BaseButton;
namespace Content.Client.Crayon.UI
{
[GenerateTypedNameReferences]
public sealed partial class CrayonWindow : DefaultWindow
{
private Dictionary<string, Texture>? _decals;
private string? _selected;
private Color _color;
public event Action<Color>? OnColorSelected;
public event Action<string>? OnSelected;
public CrayonWindow()
{
RobustXamlLoader.Load(this);
Search.OnTextChanged += _ => RefreshList();
ColorSelector.OnColorChanged += SelectColor;
}
private void SelectColor(Color color)
{
_color = color;
OnColorSelected?.Invoke(color);
RefreshList();
}
private void RefreshList()
{
// Clear
Grid.DisposeAllChildren();
if (_decals == null)
return;
var filter = Search.Text;
foreach (var (decal, tex) in _decals)
{
if (!decal.Contains(filter))
continue;
var button = new TextureButton()
{
TextureNormal = tex,
Name = decal,
ToolTip = decal,
Modulate = _color,
};
button.OnPressed += ButtonOnPressed;
if (_selected == decal)
{
var panelContainer = new PanelContainer()
{
PanelOverride = new StyleBoxFlat()
{
BackgroundColor = StyleNano.ButtonColorDefault,
},
Children =
{
button,
},
};
Grid.AddChild(panelContainer);
}
else
{
Grid.AddChild(button);
}
}
}
private void ButtonOnPressed(ButtonEventArgs obj)
{
if (obj.Button.Name == null) return;
_selected = obj.Button.Name;
RefreshList();
}
public void UpdateState(CrayonBoundUserInterfaceState state)
{
_selected = state.Selected;
ColorSelector.Visible = state.SelectableColor;
_color = state.Color;
if (ColorSelector.Visible)
{
ColorSelector.Color = state.Color;
}
RefreshList();
}
public void Populate(IEnumerable<DecalPrototype> prototypes)
{
_decals = new Dictionary<string, Texture>();
foreach (var decalPrototype in prototypes)
{
_decals.Add(decalPrototype.ID, decalPrototype.Sprite.Frame0());
}
RefreshList();
}
}
}