2021-06-21 02:13:54 +02:00
|
|
|
using System.Collections.Generic;
|
2024-11-16 10:25:06 +07:00
|
|
|
using System.Linq;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Client.Stylesheets;
|
|
|
|
|
using Content.Shared.Crayon;
|
2021-12-03 15:35:57 +01:00
|
|
|
using Content.Shared.Decals;
|
2021-10-27 05:35:46 +02:00
|
|
|
using Robust.Client.AutoGenerated;
|
2024-11-16 10:25:06 +07:00
|
|
|
using Robust.Client.GameObjects;
|
2020-10-13 13:40:05 +02:00
|
|
|
using Robust.Client.Graphics;
|
|
|
|
|
using Robust.Client.UserInterface.Controls;
|
|
|
|
|
using Robust.Client.UserInterface.CustomControls;
|
2021-10-27 05:35:46 +02:00
|
|
|
using Robust.Client.UserInterface.XAML;
|
2020-10-13 13:40:05 +02:00
|
|
|
using Robust.Client.Utility;
|
2023-09-11 19:18:06 +10:00
|
|
|
using Robust.Shared.Graphics;
|
2020-10-13 13:40:05 +02:00
|
|
|
using Robust.Shared.Maths;
|
|
|
|
|
using Robust.Shared.Utility;
|
2021-03-10 14:48:29 +01:00
|
|
|
using static Robust.Client.UserInterface.Controls.BaseButton;
|
2020-10-13 13:40:05 +02:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Client.Crayon.UI
|
2020-10-13 13:40:05 +02:00
|
|
|
{
|
2021-10-27 05:35:46 +02:00
|
|
|
[GenerateTypedNameReferences]
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed partial class CrayonWindow : DefaultWindow
|
2020-10-13 13:40:05 +02:00
|
|
|
{
|
2024-11-16 10:25:06 +07:00
|
|
|
[Dependency] private readonly IEntitySystemManager _entitySystem = default!;
|
|
|
|
|
private readonly SpriteSystem _spriteSystem = default!;
|
|
|
|
|
|
|
|
|
|
private Dictionary<string, List<(string Name, Texture Texture)>>? _decals;
|
|
|
|
|
private List<string>? _allDecals;
|
|
|
|
|
private string? _autoSelected;
|
2021-03-10 14:48:29 +01:00
|
|
|
private string? _selected;
|
2020-10-13 13:40:05 +02:00
|
|
|
private Color _color;
|
|
|
|
|
|
2024-07-21 14:48:13 +10:00
|
|
|
public event Action<Color>? OnColorSelected;
|
|
|
|
|
public event Action<string>? OnSelected;
|
|
|
|
|
|
|
|
|
|
public CrayonWindow()
|
2020-10-13 13:40:05 +02:00
|
|
|
{
|
2021-10-27 05:35:46 +02:00
|
|
|
RobustXamlLoader.Load(this);
|
2024-11-16 10:25:06 +07:00
|
|
|
IoCManager.InjectDependencies(this);
|
|
|
|
|
_spriteSystem = _entitySystem.GetEntitySystem<SpriteSystem>();
|
2020-10-13 13:40:05 +02:00
|
|
|
|
2024-11-16 10:25:06 +07:00
|
|
|
Search.OnTextChanged += SearchChanged;
|
2022-04-28 05:23:45 -07:00
|
|
|
ColorSelector.OnColorChanged += SelectColor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SelectColor(Color color)
|
|
|
|
|
{
|
|
|
|
|
_color = color;
|
|
|
|
|
|
2024-07-21 14:48:13 +10:00
|
|
|
OnColorSelected?.Invoke(color);
|
2022-04-28 05:23:45 -07:00
|
|
|
RefreshList();
|
2020-10-13 13:40:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RefreshList()
|
|
|
|
|
{
|
|
|
|
|
// Clear
|
2024-11-16 10:25:06 +07:00
|
|
|
Grids.DisposeAllChildren();
|
|
|
|
|
|
|
|
|
|
if (_decals == null || _allDecals == null)
|
2024-07-21 14:48:13 +10:00
|
|
|
return;
|
2020-10-13 13:40:05 +02:00
|
|
|
|
2021-10-27 05:35:46 +02:00
|
|
|
var filter = Search.Text;
|
2024-11-16 10:25:06 +07:00
|
|
|
var comma = filter.IndexOf(',');
|
|
|
|
|
var first = (comma == -1 ? filter : filter[..comma]).Trim();
|
|
|
|
|
|
|
|
|
|
var names = _decals.Keys.ToList();
|
|
|
|
|
names.Sort((a, b) => a == "random" ? 1 : b == "random" ? -1 : a.CompareTo(b));
|
|
|
|
|
|
|
|
|
|
if (_autoSelected != null && first != _autoSelected && _allDecals.Contains(first))
|
|
|
|
|
{
|
|
|
|
|
_selected = first;
|
|
|
|
|
_autoSelected = _selected;
|
|
|
|
|
OnSelected?.Invoke(_selected);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var categoryName in names)
|
2020-10-13 13:40:05 +02:00
|
|
|
{
|
2024-11-16 10:25:06 +07:00
|
|
|
var locName = Loc.GetString("crayon-category-" + categoryName);
|
|
|
|
|
var category = _decals[categoryName].Where(d => locName.Contains(first) || d.Name.Contains(first)).ToList();
|
|
|
|
|
|
|
|
|
|
if (category.Count == 0)
|
2020-10-13 13:40:05 +02:00
|
|
|
continue;
|
|
|
|
|
|
2024-11-16 10:25:06 +07:00
|
|
|
var label = new Label
|
2020-10-13 13:40:05 +02:00
|
|
|
{
|
2024-11-16 10:25:06 +07:00
|
|
|
Text = locName
|
2020-10-13 13:40:05 +02:00
|
|
|
};
|
2024-11-16 10:25:06 +07:00
|
|
|
|
|
|
|
|
var grid = new GridContainer
|
2020-10-13 13:40:05 +02:00
|
|
|
{
|
2024-11-16 10:25:06 +07:00
|
|
|
Columns = 6,
|
|
|
|
|
Margin = new Thickness(0, 0, 0, 16)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Grids.AddChild(label);
|
|
|
|
|
Grids.AddChild(grid);
|
|
|
|
|
|
|
|
|
|
foreach (var (name, texture) in category)
|
|
|
|
|
{
|
|
|
|
|
var button = new TextureButton()
|
2020-10-13 13:40:05 +02:00
|
|
|
{
|
2024-11-16 10:25:06 +07:00
|
|
|
TextureNormal = texture,
|
|
|
|
|
Name = name,
|
|
|
|
|
ToolTip = name,
|
|
|
|
|
Modulate = _color,
|
|
|
|
|
Scale = new System.Numerics.Vector2(2, 2)
|
2020-10-13 13:40:05 +02:00
|
|
|
};
|
2024-11-16 10:25:06 +07:00
|
|
|
button.OnPressed += ButtonOnPressed;
|
|
|
|
|
|
|
|
|
|
if (_selected == name)
|
|
|
|
|
{
|
|
|
|
|
var panelContainer = new PanelContainer()
|
|
|
|
|
{
|
|
|
|
|
PanelOverride = new StyleBoxFlat()
|
|
|
|
|
{
|
|
|
|
|
BackgroundColor = StyleNano.ButtonColorDefault,
|
|
|
|
|
},
|
|
|
|
|
Children =
|
|
|
|
|
{
|
|
|
|
|
button,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
grid.AddChild(panelContainer);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
grid.AddChild(button);
|
|
|
|
|
}
|
2020-10-13 13:40:05 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-16 10:25:06 +07:00
|
|
|
private void SearchChanged(LineEdit.LineEditEventArgs obj)
|
|
|
|
|
{
|
|
|
|
|
_autoSelected = ""; // Placeholder to kick off the auto-select in refreshlist()
|
|
|
|
|
RefreshList();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 14:48:29 +01:00
|
|
|
private void ButtonOnPressed(ButtonEventArgs obj)
|
2020-10-13 13:40:05 +02:00
|
|
|
{
|
2021-10-27 05:35:46 +02:00
|
|
|
if (obj.Button.Name == null) return;
|
|
|
|
|
|
|
|
|
|
_selected = obj.Button.Name;
|
2024-11-16 10:25:06 +07:00
|
|
|
_autoSelected = null;
|
2024-07-22 19:22:59 -07:00
|
|
|
OnSelected?.Invoke(_selected);
|
2021-10-27 05:35:46 +02:00
|
|
|
RefreshList();
|
2020-10-13 13:40:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateState(CrayonBoundUserInterfaceState state)
|
|
|
|
|
{
|
|
|
|
|
_selected = state.Selected;
|
2022-04-28 05:23:45 -07:00
|
|
|
ColorSelector.Visible = state.SelectableColor;
|
2020-10-13 13:40:05 +02:00
|
|
|
_color = state.Color;
|
2022-04-28 05:23:45 -07:00
|
|
|
|
|
|
|
|
if (ColorSelector.Visible)
|
|
|
|
|
{
|
|
|
|
|
ColorSelector.Color = state.Color;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-13 13:40:05 +02:00
|
|
|
RefreshList();
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-16 10:25:06 +07:00
|
|
|
public void AdvanceState(string drawnDecal)
|
2020-10-13 13:40:05 +02:00
|
|
|
{
|
2024-11-16 10:25:06 +07:00
|
|
|
var filter = Search.Text;
|
|
|
|
|
if (!filter.Contains(',') || !filter.Contains(drawnDecal))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var first = filter[..filter.IndexOf(',')].Trim();
|
|
|
|
|
|
|
|
|
|
if (first.Equals(drawnDecal, StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
Search.Text = filter[(filter.IndexOf(',') + 1)..].Trim();
|
|
|
|
|
_autoSelected = first;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RefreshList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Populate(List<DecalPrototype> prototypes)
|
|
|
|
|
{
|
|
|
|
|
_decals = [];
|
|
|
|
|
_allDecals = [];
|
|
|
|
|
|
|
|
|
|
prototypes.Sort((a, b) => a.ID.CompareTo(b.ID));
|
|
|
|
|
|
2021-12-03 15:35:57 +01:00
|
|
|
foreach (var decalPrototype in prototypes)
|
2020-10-13 13:40:05 +02:00
|
|
|
{
|
2024-11-16 10:25:06 +07:00
|
|
|
var category = "random";
|
|
|
|
|
if (decalPrototype.Tags.Count > 1 && decalPrototype.Tags[1].StartsWith("crayon-"))
|
|
|
|
|
category = decalPrototype.Tags[1].Replace("crayon-", "");
|
|
|
|
|
var list = _decals.GetOrNew(category);
|
|
|
|
|
list.Add((decalPrototype.ID, _spriteSystem.Frame0(decalPrototype.Sprite)));
|
|
|
|
|
_allDecals.Add(decalPrototype.ID);
|
2020-10-13 13:40:05 +02:00
|
|
|
}
|
2021-02-21 12:38:56 +01:00
|
|
|
|
2020-10-13 13:40:05 +02:00
|
|
|
RefreshList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|