96 lines
3.4 KiB
C#
96 lines
3.4 KiB
C#
using Content.Shared._Sunrise.PrinterDoc;
|
|
using Robust.Client.AutoGenerated;
|
|
using Robust.Client.UserInterface.CustomControls;
|
|
using Robust.Client.UserInterface.XAML;
|
|
using Robust.Shared.Prototypes;
|
|
using System.Linq;
|
|
using Robust.Client.UserInterface.Controls;
|
|
|
|
namespace Content.Client._Sunrise.PrinterDoc;
|
|
|
|
[GenerateTypedNameReferences]
|
|
public sealed partial class PrinterDocMenu : DefaultWindow
|
|
{
|
|
[Dependency] private readonly IPrototypeManager _protoManager = default!;
|
|
|
|
public event Action<string?>? OnPrintPressed;
|
|
public event Action? OnCopyPressed;
|
|
|
|
private string? _currentTemplate;
|
|
private bool _canCopy;
|
|
private float _incVolume;
|
|
private string _searchText = string.Empty;
|
|
private List<string> _allTemplates = new();
|
|
|
|
public PrinterDocMenu()
|
|
{
|
|
RobustXamlLoader.Load(this);
|
|
IoCManager.InjectDependencies(this);
|
|
|
|
PrintButton.OnPressed += _ => OnPrintPressed?.Invoke(_currentTemplate);
|
|
CopyButton.OnPressed += _ => OnCopyPressed?.Invoke();
|
|
TemplateList.OnItemSelected += args =>
|
|
{
|
|
var id = args.ItemIndex >= 0 && args.ItemIndex < TemplateList.Count
|
|
? TemplateList[args.ItemIndex].Metadata as string
|
|
: null;
|
|
if (id != null)
|
|
_currentTemplate = id;
|
|
};
|
|
TemplateList.OnItemSelected += TemplateListOnOnItemSelected;
|
|
TemplateList.OnItemDeselected += TemplateListOnOnItemDeselected;
|
|
SearchBar.OnTextChanged += _ =>
|
|
{
|
|
_searchText = SearchBar.Text.Trim();
|
|
PopulateTemplates();
|
|
};
|
|
}
|
|
|
|
private void TemplateListOnOnItemSelected(ItemList.ItemListSelectedEventArgs obj)
|
|
{
|
|
_currentTemplate = (string) obj.ItemList[obj.ItemIndex].Metadata!;
|
|
PrintButton.Disabled = _incVolume <= 0.0f;
|
|
}
|
|
|
|
private void TemplateListOnOnItemDeselected(ItemList.ItemListDeselectedEventArgs obj)
|
|
{
|
|
_currentTemplate = null;
|
|
PrintButton.Disabled = true;
|
|
}
|
|
|
|
public void UpdateState(PrinterDocBoundUserInterfaceState state)
|
|
{
|
|
_canCopy = state.CanCopy;
|
|
_incVolume = state.InkAmount;
|
|
PaperCountLabel.Text = state.PaperCount.ToString();
|
|
InkAmountLabel.Text = _incVolume.ToString("F1");
|
|
_allTemplates = state.Templates;
|
|
|
|
CopyStatusLabel.Text = _canCopy ? Loc.GetString("printerdoc-menu-copy-available") : Loc.GetString("printerdoc-menu-copy-unavailable");
|
|
|
|
PrintButton.Disabled = _currentTemplate == null || _incVolume <= 0.0f;
|
|
CopyButton.Disabled = !_canCopy;
|
|
|
|
PopulateTemplates();
|
|
}
|
|
|
|
private void PopulateTemplates()
|
|
{
|
|
TemplateList.Clear();
|
|
var filtered = string.IsNullOrWhiteSpace(_searchText)
|
|
? _allTemplates
|
|
: _allTemplates.Where(id =>
|
|
(_protoManager.TryIndex<DocTemplatePrototype>(id, out var proto) &&
|
|
(Loc.GetString(proto.Name).ToLowerInvariant().Contains(_searchText.ToLowerInvariant()) ||
|
|
id.ToLowerInvariant().Contains(_searchText.ToLowerInvariant())))
|
|
).ToList();
|
|
for (int i = 0; i < filtered.Count; i++)
|
|
{
|
|
var template = filtered[i];
|
|
if (_protoManager.TryIndex<DocTemplatePrototype>(template, out var templateProto))
|
|
{
|
|
TemplateList.AddItem(Loc.GetString(templateProto.Name), metadata: template);
|
|
}
|
|
}
|
|
}
|
|
}
|