Files
wwdpublic/Content.Client/Crayon/UI/CrayonWindow.xaml.cs
RedFoxIV 1239747c2d [add] кириллица (#641)
* ИГРА ПРИОСТАНОВЛЕНА

* [пивная кега]   : ?

[пивная кега]   : ?
[пивная кега]   : ?
2025-06-28 11:33:45 +03:00

294 lines
10 KiB
C#

using Content.Client.Stylesheets;
using Content.Shared.Crayon;
using Content.Shared.Decals;
using Robust.Client.AutoGenerated;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Client.ResourceManagement;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.RichText;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
using System.Linq;
using static Robust.Client.UserInterface.Controls.BaseButton;
namespace Content.Client.Crayon.UI
{
[GenerateTypedNameReferences]
public sealed partial class CrayonWindow : DefaultWindow
{
[Dependency] private readonly IEntitySystemManager _entitySystem = default!;
[Dependency] private readonly IResourceCache _cache = default!;
[Dependency] private readonly IPrototypeManager _proto = default!;
private readonly SpriteSystem _spriteSystem = default!;
private Dictionary<string, List<(string Name, Texture Texture)>>? _decals;
private List<string>? _allDecals;
private string? _autoSelected;
private bool _firstLetterUsed;
private string? _selected;
private Color _color;
private bool _textInput;
private readonly Dictionary<char, string> _textInputShorthands = new() // todo make this a field in decal prototype
{
{ '-', "minus"},
{ '+', "plus"},
{ '!', "emark"},
{ '?', "qmark"},
{ '=', "equals"},
{ '/', "slash"},
{ '$', "credit"},
{ '^', "chevron"},
{ '&', "ampersand"},
{ '#', "pound"},
{ '%', "percent"},
{ '|', "thinline"},
{ '.', "dot"},
{ ',', "comma"},
{ '*', "star"}
};
public event Action<Color>? OnColorSelected;
public event Action<string>? OnSelected;
public CrayonWindow()
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
_spriteSystem = _entitySystem.GetEntitySystem<SpriteSystem>();
Search.OnTextChanged += SearchChanged;
TextInputCheckBox.OnToggled += TextInputChanged;
ColorSelector.OnColorChanged += SelectColor;
ClearButton.OnPressed += (_) => Search.SetText(String.Empty, true);
}
private void SelectColor(Color color)
{
_color = color;
OnColorSelected?.Invoke(color);
RefreshList();
}
private void RefreshList()
{
// Clear
Grids.DisposeAllChildren();
_firstLetterUsed = false;
if (_decals == null || _allDecals == null)
return;
// WWDP EDIT START
var filter = Search.Text.Trim();
var firstcomma = filter.IndexOf(' ');
var first = (firstcomma == -1 ? filter : filter[..firstcomma]).Trim();
if (_textInput && first.Length > 0)
first = first[0].ToString();
var comma = filter.LastIndexOf(' ') + 1;
filter = filter.Substring(comma).Trim();
// WWDP EDIT END
var names = _decals.Keys.ToList();
names.Sort((a, b) => a == "random" ? 1 : b == "random" ? -1 : a.CompareTo(b));
// WWDP EDIT START
if (!TrySelectDecal(first) && first.Length > 0 && _textInputShorthands.TryGetValue(first[0], out var shorthand))
DebugTools.Assert(TrySelectDecal(shorthand, true), "Invalid decal shorthand");
// WWDP EDIT END
var hsl = Color.ToHsl(_color);
hsl.Z = MathF.Max(hsl.Z, 0.5f);
Color labelcolor;
labelcolor = Color.FromHsl(hsl);
if (_textInput)
filter = string.Empty; // disable search for text entry
foreach (var categoryName in names)
{
var locName = Loc.GetString("crayon-category-" + categoryName);
var category = _decals[categoryName].Where(d => locName.Contains(filter) || d.Name.Contains(filter)).ToList(); // WWDP EDIT
if (category.Count == 0)
continue;
var label = new Label
{
Text = locName
};
var grid = new GridContainer
{
Columns = 6,
Margin = new Thickness(0, 0, 0, 16)
};
Grids.AddChild(label);
Grids.AddChild(grid);
foreach (var (name, texture) in category)
{
// WWDP EDIT START
var button = new ContainerButton()
{
Name = name,
ToolTip = name
};
button.OnPressed += ButtonOnPressed;
var boxcont = new BoxContainer()
{
Orientation = BoxContainer.LayoutOrientation.Vertical,
MaxWidth = texture.Width * 2
};
var texturerect = new TextureRect()
{
Texture = texture,
Name = name,
ToolTip = name,
Modulate = _color,
HorizontalAlignment = HAlignment.Center,
VerticalAlignment = VAlignment.Center,
TextureScale = new System.Numerics.Vector2(2, 2)
};
var buttonlabel = new Label
{
Text = name,
HorizontalAlignment = HAlignment.Center,
Align = Label.AlignMode.Center,
FontOverride = new VectorFont(_cache.GetResource<FontResource>(_proto.Index<FontPrototype>("Default").Path), 8),
FontColorOverride = labelcolor
};
boxcont.AddChild(texturerect);
boxcont.AddChild(buttonlabel);
button.AddChild(boxcont);
// WWDP EDIT END
if (_selected == name)
{
var panelContainer = new PanelContainer()
{
PanelOverride = new StyleBoxFlat()
{
BackgroundColor = StyleNano.ButtonColorDefault,
},
Children =
{
button,
},
};
grid.AddChild(panelContainer);
}
else
{
grid.AddChild(button);
}
}
}
}
private bool TrySelectDecal(string decalId, bool firstLetterFallback = false)
{
if (_autoSelected != null && _allDecals!.Contains(decalId))
{
_firstLetterUsed = firstLetterFallback;
if (decalId == _autoSelected)
return true;
_selected = decalId;
_autoSelected = _selected;
OnSelected?.Invoke(_selected);
return true;
}
if (decalId.Length > 1)
return TrySelectDecal(decalId[0].ToString(), true);
return false;
}
private void TextInputChanged(ButtonEventArgs obj)
{
_autoSelected = "";
_textInput = TextInputCheckBox.Pressed;
RefreshList();
}
private void SearchChanged(LineEdit.LineEditEventArgs obj)
{
_autoSelected = ""; // Placeholder to kick off the auto-select in refreshlist()
RefreshList();
}
private void ButtonOnPressed(ButtonEventArgs obj)
{
if (obj.Button.Name == null) return;
_selected = obj.Button.Name;
_autoSelected = null;
OnSelected?.Invoke(_selected);
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 AdvanceState(string drawnDecal)
{
var filter = Search.Text;
if (_textInput || _firstLetterUsed)
{
Search.Text = filter.Substring(1).Trim();
RefreshList();
return;
}
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));
foreach (var decalPrototype in prototypes)
{
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);
}
RefreshList();
}
}
}