Files
tbd-station-14/Content.Client/Lathe/UI/LatheMaterialsEjectionMenu.xaml.cs
chromiumboy a39fa80d58 Add lathe material ejection (#19218)
This completes PilgrimViis' (now closed) PR 16398. It addresses issue 10896, by allowing materials to be ejected from most lathes (except the ore processor and sheet-meister 2000)

* - Refinements to the material ejection UI
- Made the lathe UI default to a slightly larger size
- Fixed an offset issue with the label of the item currently being printed in the build queue UI

* Allow the materiel reclamation UI to pop if there is material left in the lathe, but not enough to print any sheets

---------

Co-authored-by: Kevin Zheng <kevinz5000@gmail.com>
2023-08-17 09:22:01 -08:00

75 lines
2.4 KiB
C#

using Content.Shared.Materials;
using Robust.Client.AutoGenerated;
using Robust.Client.GameObjects;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Prototypes;
using System.Linq;
namespace Content.Client.Lathe.UI;
[GenerateTypedNameReferences]
public sealed partial class LatheMaterialsEjectionMenu : DefaultWindow
{
[Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
private readonly SpriteSystem _spriteSystem;
public event Action<string, int>? OnEjectPressed;
public LatheMaterialsEjectionMenu()
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
_spriteSystem = _entityManager.EntitySysManager.GetEntitySystem<SpriteSystem>();
}
public void PopulateMaterials(EntityUid lathe)
{
if (!_entityManager.TryGetComponent<MaterialStorageComponent>(lathe, out var materials))
return;
MaterialsList.DisposeAllChildren();
foreach (var (materialId, volume) in materials.Storage)
{
if (volume <= 0)
continue;
if (!_prototypeManager.TryIndex(materialId, out MaterialPrototype? material))
continue;
var name = Loc.GetString(material.Name);
int volumePerSheet = 0;
int maxEjectableSheets = 0;
if (material.StackEntity != null)
{
var proto = _prototypeManager.Index<EntityPrototype>(material.StackEntity);
name = proto.Name;
if (proto.TryGetComponent<PhysicalCompositionComponent>(out var composition))
{
volumePerSheet = composition.MaterialComposition.FirstOrDefault(kvp => kvp.Key == materialId).Value;
maxEjectableSheets = (int) MathF.Floor(volume / volumePerSheet);
}
}
var row = new LatheMaterialEjector(materialId, OnEjectPressed, volumePerSheet, maxEjectableSheets)
{
Icon = { Texture = _spriteSystem.Frame0(material.Icon) },
ProductName = { Text = name }
};
MaterialsList.AddChild(row);
}
if (MaterialsList.ChildCount == 0)
{
Close();
}
}
}