Files
wwdpublic/Content.Client/Materials/UI/MaterialStorageControl.xaml.cs
Spatison 35dd522f28 Material Silo (#1488)
<!--
This is a semi-strict format, you can add/remove sections as needed but
the order/format should be kept the same
Remove these comments before submitting
-->

# Description

<!--
Explain this PR in as much detail as applicable

Some example prompts to consider:
How might this affect the game? The codebase?
What might be some alternatives to this?
How/Who does this benefit/hurt [the game/codebase]?
-->

Port from [Goob](https://github.com/Goob-Station/Goob-Station/pull/989)

---

# Changelog

<!--
You can add an author after the `🆑` to change the name that appears
in the changelog (ex: `🆑 Death`)
Leaving it blank will default to your GitHub display name
This includes all available types for the changelog
-->

🆑 Aviu00, Spatison
- add: Added material silo

---------

Signed-off-by: Spatison <137375981+Spatison@users.noreply.github.com>
Signed-off-by: VMSolidus <evilexecutive@gmail.com>
Co-authored-by: VMSolidus <evilexecutive@gmail.com>

(cherry picked from commit 9897094a3c873ddca79ece3a5cdbb7d30252edfd)
2025-01-14 02:15:25 +03:00

112 lines
3.3 KiB
C#

using System.Linq;
using Content.Shared.Materials;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Timing;
namespace Content.Client.Materials.UI;
/// <summary>
/// This widget is one row in the lathe eject menu.
/// </summary>
[GenerateTypedNameReferences]
public sealed partial class MaterialStorageControl : ScrollContainer
{
[Dependency] private readonly IEntityManager _entityManager = default!;
private readonly MaterialSiloSystem _materialSilo;
private EntityUid? _owner;
private Dictionary<string, int> _currentMaterials = new();
public MaterialStorageControl()
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
_materialSilo = _entityManager.System<MaterialSiloSystem>();
}
public void SetOwner(EntityUid owner)
{
_owner = owner;
}
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
if (_owner == null)
return;
if (_entityManager.Deleted(_owner) || !_entityManager.TryGetComponent<MaterialStorageComponent>(_owner, out var materialStorage))
{
_owner = null;
return;
}
var canEject = materialStorage.CanEjectStoredMaterials;
Dictionary<string, int> mats;
if (_entityManager.TryGetComponent<MaterialSiloUtilizerComponent>(_owner, out var utilizer) && utilizer.Silo.HasValue)
{
var silo = _materialSilo.GetSiloStorage(_owner.Value);
mats = silo != null
? silo.Value.Comp.Storage.Select(pair => (pair.Key.Id, pair.Value)).ToDictionary()
: materialStorage.Storage.Select(pair => (pair.Key.Id, pair.Value)).ToDictionary();
ConnectToSiloLabel.Visible = silo != null;
}
else
{
mats = materialStorage.Storage.Select(pair => (pair.Key.Id, pair.Value)).ToDictionary();
ConnectToSiloLabel.Visible = false;
}
if (_currentMaterials.Equals(mats))
return;
var missing = new List<string>();
var extra = new List<string>();
foreach (var (mat, amount) in mats)
{
if (!_currentMaterials.ContainsKey(mat) ||
_currentMaterials[mat] == 0 && _currentMaterials[mat] != amount)
missing.Add(mat);
}
foreach (var (mat, amount) in _currentMaterials)
{
if (!mats.ContainsKey(mat) || amount == 0)
extra.Add(mat);
}
var children = new List<MaterialDisplay>();
children.AddRange(MaterialList.Children.OfType<MaterialDisplay>());
foreach (var display in children)
{
var mat = display.Material;
if (extra.Contains(mat))
{
MaterialList.RemoveChild(display);
continue;
}
if (!mats.TryGetValue(mat, out var newAmount))
continue;
display.UpdateVolume(newAmount);
}
foreach (var mat in missing)
{
var volume = mats[mat];
MaterialList.AddChild(new MaterialDisplay(_owner.Value, mat, volume, canEject));
}
_currentMaterials = mats;
NoMatsLabel.Visible = ChildCount == 1;
}
}