Make the material ejection control completely generic (#23308)
* Finish decoupling material ejection from lathes * commented
This commit is contained in:
24
Content.Client/Materials/UI/MaterialDisplay.xaml
Normal file
24
Content.Client/Materials/UI/MaterialDisplay.xaml
Normal file
@@ -0,0 +1,24 @@
|
||||
<PanelContainer xmlns="https://spacestation14.io"
|
||||
HorizontalExpand="True">
|
||||
<BoxContainer Orientation="Horizontal">
|
||||
<TextureRect Name="Icon"
|
||||
Access="Public"
|
||||
MinSize="32 32"
|
||||
RectClipContent="True" />
|
||||
<BoxContainer Orientation="Vertical"
|
||||
HorizontalExpand="True"
|
||||
VerticalExpand="False">
|
||||
<Label Name="ProductName"
|
||||
Access="Public"
|
||||
HorizontalExpand="True"
|
||||
ClipText="True"
|
||||
Margin="4 4 4 4"/>
|
||||
</BoxContainer>
|
||||
<BoxContainer Name="Content"
|
||||
Orientation="Horizontal"
|
||||
HorizontalExpand="True"
|
||||
HorizontalAlignment="Right">
|
||||
<!--Here go buttons which added in c#-->
|
||||
</BoxContainer>
|
||||
</BoxContainer>
|
||||
</PanelContainer>
|
||||
110
Content.Client/Materials/UI/MaterialDisplay.xaml.cs
Normal file
110
Content.Client/Materials/UI/MaterialDisplay.xaml.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
using Content.Client.Stylesheets;
|
||||
using Content.Shared.Materials;
|
||||
using Robust.Client.AutoGenerated;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Client.UserInterface;
|
||||
using Robust.Client.UserInterface.Controls;
|
||||
using Robust.Client.UserInterface.XAML;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Client.Materials.UI;
|
||||
|
||||
/// <summary>
|
||||
/// This widget is one row in the material storage control.
|
||||
/// </summary>
|
||||
[GenerateTypedNameReferences]
|
||||
public sealed partial class MaterialDisplay : PanelContainer
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
[Dependency] private readonly IEntityManager _entityManager = default!;
|
||||
|
||||
private readonly MaterialStorageSystem _materialStorage;
|
||||
|
||||
private readonly MaterialStorageUIController _materialUIController;
|
||||
|
||||
private int _volume;
|
||||
private readonly EntityUid _ent;
|
||||
public readonly string Material;
|
||||
private readonly bool _canEject;
|
||||
|
||||
public MaterialDisplay(EntityUid ent, string material, int volume, bool canEject)
|
||||
{
|
||||
RobustXamlLoader.Load(this);
|
||||
IoCManager.InjectDependencies(this);
|
||||
|
||||
_materialStorage = _entityManager.System<MaterialStorageSystem>();
|
||||
_materialUIController = UserInterfaceManager.GetUIController<MaterialStorageUIController>();
|
||||
|
||||
var spriteSys = _entityManager.System<SpriteSystem>();
|
||||
Icon.Texture = spriteSys.Frame0(_prototypeManager.Index<MaterialPrototype>(material).Icon);
|
||||
|
||||
_ent = ent;
|
||||
Material = material;
|
||||
_canEject = canEject;
|
||||
UpdateVolume(volume);
|
||||
}
|
||||
|
||||
public void UpdateVolume(int volume)
|
||||
{
|
||||
if (_volume == volume)
|
||||
return;
|
||||
|
||||
_volume = volume;
|
||||
var matProto = _prototypeManager.Index<MaterialPrototype>(Material);
|
||||
|
||||
var sheetVolume = _materialStorage.GetSheetVolume(matProto);
|
||||
var sheets = (float) volume / sheetVolume;
|
||||
var maxEjectableSheets = (int) MathF.Floor(sheets);
|
||||
|
||||
var unit = Loc.GetString(matProto.Unit);
|
||||
var amountText = Loc.GetString("lathe-menu-material-amount", ("amount", sheets), ("unit", unit));
|
||||
var name = Loc.GetString(matProto.Name);
|
||||
var mat = Loc.GetString("lathe-menu-material-display", ("material", name), ("amount", amountText));
|
||||
ProductName.Text = mat;
|
||||
|
||||
PopulateButtons(maxEjectableSheets);
|
||||
}
|
||||
|
||||
public void PopulateButtons(int maxEjectableSheets)
|
||||
{
|
||||
Content.Children.Clear();
|
||||
if (!_canEject)
|
||||
return;
|
||||
|
||||
int[] sheetsToEjectArray = { 1, 5, 10 };
|
||||
|
||||
for (var i = 0; i < sheetsToEjectArray.Length; i++)
|
||||
{
|
||||
var sheetsToEject = sheetsToEjectArray[i];
|
||||
|
||||
var styleClass = StyleBase.ButtonOpenBoth;
|
||||
if (i == 0)
|
||||
styleClass = StyleBase.ButtonOpenRight;
|
||||
else if (i == sheetsToEjectArray.Length - 1)
|
||||
styleClass = StyleBase.ButtonOpenLeft;
|
||||
|
||||
var button = new Button
|
||||
{
|
||||
Name = $"{sheetsToEject}",
|
||||
Access = AccessLevel.Public,
|
||||
Text = Loc.GetString($"{sheetsToEject}"),
|
||||
MinWidth = 45,
|
||||
StyleClasses = { styleClass }
|
||||
};
|
||||
|
||||
button.OnPressed += _ =>
|
||||
{
|
||||
_materialUIController.SendLatheEjectMessage(_ent, Material, sheetsToEject);
|
||||
};
|
||||
|
||||
button.Disabled = maxEjectableSheets < sheetsToEject;
|
||||
|
||||
if (_prototypeManager.TryIndex<MaterialPrototype>(Material, out var proto))
|
||||
{
|
||||
button.ToolTip = Loc.GetString("lathe-menu-tooltip-display", ("amount", sheetsToEject), ("material", Loc.GetString(proto.Name)));
|
||||
}
|
||||
|
||||
Content.AddChild(button);
|
||||
}
|
||||
}
|
||||
}
|
||||
7
Content.Client/Materials/UI/MaterialStorageControl.xaml
Normal file
7
Content.Client/Materials/UI/MaterialStorageControl.xaml
Normal file
@@ -0,0 +1,7 @@
|
||||
<BoxContainer xmlns="https://spacestation14.io"
|
||||
Orientation="Vertical"
|
||||
SizeFlagsStretchRatio="8"
|
||||
HorizontalExpand="True"
|
||||
VerticalExpand="True">
|
||||
<Label Name="NoMatsLabel" Text="{Loc 'lathe-menu-no-materials-message'}" Align="Center"/>
|
||||
</BoxContainer>
|
||||
92
Content.Client/Materials/UI/MaterialStorageControl.xaml.cs
Normal file
92
Content.Client/Materials/UI/MaterialStorageControl.xaml.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
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 : BoxContainer
|
||||
{
|
||||
[Dependency] private readonly IEntityManager _entityManager = default!;
|
||||
|
||||
private EntityUid? _owner;
|
||||
|
||||
private Dictionary<string, int> _currentMaterials = new();
|
||||
|
||||
public MaterialStorageControl()
|
||||
{
|
||||
RobustXamlLoader.Load(this);
|
||||
IoCManager.InjectDependencies(this);
|
||||
}
|
||||
|
||||
public void SetOwner(EntityUid owner)
|
||||
{
|
||||
_owner = owner;
|
||||
}
|
||||
|
||||
protected override void FrameUpdate(FrameEventArgs args)
|
||||
{
|
||||
base.FrameUpdate(args);
|
||||
|
||||
if (_owner == null)
|
||||
return;
|
||||
|
||||
if (!_entityManager.TryGetComponent<MaterialStorageComponent>(_owner, out var materialStorage))
|
||||
{
|
||||
Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
var canEject = materialStorage.CanEjectStoredMaterials;
|
||||
var mats = materialStorage.Storage.Select(pair => (pair.Key.Id, pair.Value)).ToDictionary();
|
||||
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(Children.OfType<MaterialDisplay>());
|
||||
|
||||
foreach (var display in children)
|
||||
{
|
||||
var mat = display.Material;
|
||||
|
||||
if (extra.Contains(mat))
|
||||
{
|
||||
RemoveChild(display);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!mats.TryGetValue(mat, out var newAmount))
|
||||
continue;
|
||||
display.UpdateVolume(newAmount);
|
||||
}
|
||||
|
||||
foreach (var mat in missing)
|
||||
{
|
||||
var volume = mats[mat];
|
||||
AddChild(new MaterialDisplay(_owner.Value, mat, volume, canEject));
|
||||
}
|
||||
|
||||
_currentMaterials = mats;
|
||||
NoMatsLabel.Visible = ChildCount == 1;
|
||||
}
|
||||
}
|
||||
12
Content.Client/Materials/UI/MaterialStorageUIController.cs
Normal file
12
Content.Client/Materials/UI/MaterialStorageUIController.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using Content.Shared.Materials;
|
||||
using Robust.Client.UserInterface.Controllers;
|
||||
|
||||
namespace Content.Client.Materials.UI;
|
||||
|
||||
public sealed class MaterialStorageUIController : UIController
|
||||
{
|
||||
public void SendLatheEjectMessage(EntityUid uid, string material, int sheetsToEject)
|
||||
{
|
||||
EntityManager.RaisePredictiveEvent(new EjectMaterialMessage(EntityManager.GetNetEntity(uid), material, sheetsToEject));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user