Lathe Refactor and ECS (#11201)
* lathe and material storage refactor * materialStorage ECS it kinda sus tho * beginning the lathe shitcode dive * couple lathe visuals and lathe system * lathe changes and such * dynamic lathe databases * rewrote internal logic on to ui * da newI * material display clientside * misc ui changes * component state handling and various other things * moar * Update Content.Shared/Lathe/LatheComponent.cs Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> * first volley of sloth review * more fixes * losin' my mind * all da changes * test fix and other review Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
142
Content.Client/Lathe/UI/LatheMenu.xaml.cs
Normal file
142
Content.Client/Lathe/UI/LatheMenu.xaml.cs
Normal file
@@ -0,0 +1,142 @@
|
||||
using System.Text;
|
||||
using Content.Shared.Lathe;
|
||||
using Content.Shared.Materials;
|
||||
using Content.Shared.Research.Prototypes;
|
||||
using Robust.Client.AutoGenerated;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Client.UserInterface.Controls;
|
||||
using Robust.Client.UserInterface.CustomControls;
|
||||
using Robust.Client.UserInterface.XAML;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Client.Lathe.UI;
|
||||
|
||||
[GenerateTypedNameReferences]
|
||||
public sealed partial class LatheMenu : DefaultWindow
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
[Dependency] private readonly IEntityManager _entityManager = default!;
|
||||
private readonly SpriteSystem _spriteSystem;
|
||||
private readonly LatheSystem _lathe;
|
||||
|
||||
public event Action<BaseButton.ButtonEventArgs>? OnQueueButtonPressed;
|
||||
public event Action<BaseButton.ButtonEventArgs>? OnServerListButtonPressed;
|
||||
public event Action<BaseButton.ButtonEventArgs>? OnServerSyncButtonPressed;
|
||||
public event Action<string, int>? RecipeQueueAction;
|
||||
|
||||
public List<string> Recipes = new();
|
||||
private List<LatheRecipePrototype> _oldRecipesToShow = new();
|
||||
|
||||
public LatheMenu(LatheBoundUserInterface owner)
|
||||
{
|
||||
RobustXamlLoader.Load(this);
|
||||
IoCManager.InjectDependencies(this);
|
||||
|
||||
_spriteSystem = _entityManager.EntitySysManager.GetEntitySystem<SpriteSystem>();
|
||||
_lathe = _entityManager.EntitySysManager.GetEntitySystem<LatheSystem>();
|
||||
|
||||
Title = _entityManager.GetComponent<MetaDataComponent>(owner.Lathe).EntityName;
|
||||
|
||||
SearchBar.OnTextChanged += _ =>
|
||||
{
|
||||
PopulateRecipes(owner.Lathe);
|
||||
};
|
||||
AmountLineEdit.OnTextChanged += _ =>
|
||||
{
|
||||
PopulateRecipes(owner.Lathe);
|
||||
};
|
||||
|
||||
QueueButton.OnPressed += a => OnQueueButtonPressed?.Invoke(a);
|
||||
ServerListButton.OnPressed += a => OnServerListButtonPressed?.Invoke(a);
|
||||
|
||||
//refresh the bui state
|
||||
ServerSyncButton.OnPressed += a => OnServerSyncButtonPressed?.Invoke(a);
|
||||
|
||||
if (_entityManager.TryGetComponent<LatheComponent>(owner.Lathe, out var latheComponent))
|
||||
{
|
||||
if (latheComponent.DynamicRecipes == null)
|
||||
{
|
||||
ServerListButton.Visible = false;
|
||||
ServerSyncButton.Visible = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void PopulateMaterials(EntityUid lathe)
|
||||
{
|
||||
if (!_entityManager.TryGetComponent<MaterialStorageComponent>(lathe, out var materials))
|
||||
return;
|
||||
|
||||
Materials.Clear();
|
||||
foreach (var (id, amount) in materials.Storage)
|
||||
{
|
||||
if (!_prototypeManager.TryIndex(id, out MaterialPrototype? material))
|
||||
continue;
|
||||
var mat = Loc.GetString("lathe-menu-material-display",
|
||||
("material", material.Name), ("amount", amount));
|
||||
Materials.AddItem(mat, _spriteSystem.Frame0(material.Icon), false);
|
||||
}
|
||||
PopulateRecipes(lathe);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Populates the list of all the recipes
|
||||
/// </summary>
|
||||
/// <param name="lathe"></param>
|
||||
public void PopulateRecipes(EntityUid lathe)
|
||||
{
|
||||
var recipesToShow = new List<LatheRecipePrototype>();
|
||||
foreach (var recipe in Recipes)
|
||||
{
|
||||
if (!_prototypeManager.TryIndex<LatheRecipePrototype>(recipe, out var proto))
|
||||
continue;
|
||||
|
||||
if (SearchBar.Text.Trim().Length != 0)
|
||||
{
|
||||
if (proto.Name.ToLowerInvariant().Contains(SearchBar.Text.Trim().ToLowerInvariant()))
|
||||
recipesToShow.Add(proto);
|
||||
}
|
||||
else
|
||||
{
|
||||
recipesToShow.Add(proto);
|
||||
}
|
||||
}
|
||||
|
||||
if (!int.TryParse(AmountLineEdit.Text, out var quantity) || quantity <= 0)
|
||||
quantity = 1;
|
||||
|
||||
RecipeList.Children.Clear();
|
||||
_oldRecipesToShow = recipesToShow;
|
||||
foreach (var prototype in recipesToShow)
|
||||
{
|
||||
StringBuilder sb = new();
|
||||
var first = true;
|
||||
foreach (var (id, amount) in prototype.RequiredMaterials)
|
||||
{
|
||||
if (!_prototypeManager.TryIndex<MaterialPrototype>(id, out var proto))
|
||||
continue;
|
||||
|
||||
if (first)
|
||||
first = false;
|
||||
else
|
||||
sb.Append('\n');
|
||||
|
||||
sb.Append(amount);
|
||||
sb.Append(' ');
|
||||
sb.Append(proto.Name);
|
||||
}
|
||||
|
||||
var icon = _spriteSystem.Frame0(prototype.Icon);
|
||||
var canProduce = _lathe.CanProduce(lathe, prototype, quantity);
|
||||
|
||||
var control = new RecipeControl(prototype, sb.ToString(), canProduce, icon);
|
||||
control.OnButtonPressed += s =>
|
||||
{
|
||||
if (!int.TryParse(AmountLineEdit.Text, out var amount) || amount <= 0)
|
||||
amount = 1;
|
||||
RecipeQueueAction?.Invoke(s, amount);
|
||||
};
|
||||
RecipeList.AddChild(control);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user