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>
This commit is contained in:
chromiumboy
2023-08-17 12:22:01 -05:00
committed by GitHub
parent 89b3231de0
commit a39fa80d58
13 changed files with 288 additions and 10 deletions

View File

@@ -14,6 +14,9 @@ namespace Content.Client.Lathe.UI
[ViewVariables]
private LatheQueueMenu? _queueMenu;
[ViewVariables]
private LatheMaterialsEjectionMenu? _materialsEjectionMenu;
public LatheBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{
}
@@ -24,6 +27,7 @@ namespace Content.Client.Lathe.UI
_menu = new LatheMenu(this);
_queueMenu = new LatheQueueMenu();
_materialsEjectionMenu = new LatheMaterialsEjectionMenu();
_menu.OnClose += Close;
@@ -34,15 +38,30 @@ namespace Content.Client.Lathe.UI
else
_queueMenu.OpenCenteredLeft();
};
_menu.OnMaterialsEjectionButtonPressed += _ =>
{
if (_materialsEjectionMenu.IsOpen)
_materialsEjectionMenu.Close();
else
_materialsEjectionMenu.OpenCenteredRight();
};
_menu.OnServerListButtonPressed += _ =>
{
SendMessage(new ConsoleServerSelectionMessage());
};
_menu.RecipeQueueAction += (recipe, amount) =>
{
SendMessage(new LatheQueueRecipeMessage(recipe, amount));
};
_materialsEjectionMenu.OnEjectPressed += (material, sheetsToExtract) =>
{
SendMessage(new LatheEjectMaterialMessage(material, sheetsToExtract));
};
_menu.OpenCentered();
}
@@ -59,6 +78,7 @@ namespace Content.Client.Lathe.UI
_menu?.PopulateMaterials(Owner);
_queueMenu?.PopulateList(msg.Queue);
_queueMenu?.SetInfo(msg.CurrentlyProducing);
_materialsEjectionMenu?.PopulateMaterials(Owner);
break;
}
}
@@ -70,6 +90,7 @@ namespace Content.Client.Lathe.UI
return;
_menu?.Dispose();
_queueMenu?.Dispose();
_materialsEjectionMenu?.Dispose();
}
}
}

View File

@@ -0,0 +1,21 @@
<PanelContainer xmlns="https://spacestation14.io"
HorizontalExpand="True">
<BoxContainer Name="Content"
Orientation="Horizontal"
HorizontalExpand="True">
<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="0 4 0 0"/>
</BoxContainer>
<!--Here go buttons which added in c#-->
</BoxContainer>
</PanelContainer>

View File

@@ -0,0 +1,64 @@
using Content.Shared.Materials;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Prototypes;
namespace Content.Client.Lathe.UI;
/// <summary>
/// This widget is one row in the lathe eject menu.
/// </summary>
[GenerateTypedNameReferences]
public sealed partial class LatheMaterialEjector : PanelContainer
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
public string Material;
public Action<string, int>? OnEjectPressed;
public int VolumePerSheet;
public LatheMaterialEjector(string material, Action<string, int>? onEjectPressed, int volumePerSheet, int maxEjectableSheets)
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
Material = material;
OnEjectPressed = onEjectPressed;
VolumePerSheet = volumePerSheet;
PopulateButtons(maxEjectableSheets);
}
public void PopulateButtons(int maxEjectableSheets)
{
int[] sheetsToEjectArray = { 1, 5, 10, 30 };
foreach (int sheetsToEject in sheetsToEjectArray)
{
var button = new Button()
{
Name = $"{sheetsToEject}",
Access = AccessLevel.Public,
Text = Loc.GetString($"{sheetsToEject}"),
MinWidth = 45,
};
button.OnPressed += (_) =>
{
OnEjectPressed?.Invoke(Material, sheetsToEject);
};
button.Disabled = maxEjectableSheets < sheetsToEject;
if (_prototypeManager.TryIndex<MaterialPrototype>(Material, out var proto))
{
button.ToolTip = Loc.GetString("lathe-menu-tooltip-display", ("amount", sheetsToEject * VolumePerSheet), ("material", Loc.GetString(proto.Name)));
}
Content.AddChild(button);
}
}
}

View File

@@ -0,0 +1,16 @@
<DefaultWindow
xmlns="https://spacestation14.io"
Title="{Loc 'lathe-menu-materials-ejection-title'}"
MinSize="300 100"
SetSize="350 475">
<ScrollContainer MinHeight="80">
<BoxContainer
Name="MaterialsList"
Orientation="Vertical"
SizeFlagsStretchRatio="8"
HorizontalExpand="True"
VerticalExpand="True">
<!-- Materials populated in C# file -->
</BoxContainer>
</ScrollContainer>
</DefaultWindow>

View File

@@ -0,0 +1,74 @@
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();
}
}
}

View File

@@ -1,9 +1,9 @@
<DefaultWindow
<DefaultWindow
xmlns="https://spacestation14.io"
xmlns:gfx="clr-namespace:Robust.Client.Graphics;assembly=Robust.Client"
Title="{Loc 'lathe-menu-title'}"
MinSize="300 450"
SetSize="300 450">
SetSize="350 475">
<BoxContainer
Orientation="Vertical"
VerticalExpand="True"
@@ -79,6 +79,13 @@
VerticalExpand="True">
</ItemList>
</BoxContainer>
<Button
Name="MaterialsEjectionButton"
Text="{Loc 'lathe-menu-materials-ejection'}"
TextAlign="Center"
Mode="Press"
StyleClasses="OpenRight">
</Button>
</BoxContainer>
</DefaultWindow>

View File

@@ -1,4 +1,4 @@
using System.Linq;
using System.Linq;
using System.Text;
using Content.Client.Stylesheets;
using Content.Shared.Lathe;
@@ -22,6 +22,7 @@ public sealed partial class LatheMenu : DefaultWindow
private readonly LatheSystem _lathe;
public event Action<BaseButton.ButtonEventArgs>? OnQueueButtonPressed;
public event Action<BaseButton.ButtonEventArgs>? OnMaterialsEjectionButtonPressed;
public event Action<BaseButton.ButtonEventArgs>? OnServerListButtonPressed;
public event Action<string, int>? RecipeQueueAction;
@@ -47,6 +48,7 @@ public sealed partial class LatheMenu : DefaultWindow
};
QueueButton.OnPressed += a => OnQueueButtonPressed?.Invoke(a);
MaterialsEjectionButton.OnPressed += a => OnMaterialsEjectionButtonPressed?.Invoke(a);
ServerListButton.OnPressed += a => OnServerListButtonPressed?.Invoke(a);
if (_entityManager.TryGetComponent<LatheComponent>(owner.Owner, out var latheComponent))
@@ -57,6 +59,11 @@ public sealed partial class LatheMenu : DefaultWindow
QueueButton.RemoveStyleClass(StyleBase.ButtonOpenRight);
//QueueButton.AddStyleClass(StyleBase.ButtonSquare);
}
if (MaterialsEjectionButton != null && !latheComponent.CanEjectStoredMaterials)
{
MaterialsEjectionButton.Dispose();
}
}
}
@@ -69,14 +76,24 @@ public sealed partial class LatheMenu : DefaultWindow
foreach (var (id, amount) in materials.Storage)
{
if (amount <= 0)
continue;
if (!_prototypeManager.TryIndex(id, out MaterialPrototype? material))
continue;
var name = Loc.GetString(material.Name);
var mat = Loc.GetString("lathe-menu-material-display",
("material", name), ("amount", amount));
Materials.AddItem(mat, _spriteSystem.Frame0(material.Icon), false);
}
if (MaterialsEjectionButton != null)
{
MaterialsEjectionButton.Disabled = Materials.Count == 0;
}
if (Materials.Count == 0)
{
var noMaterialsMsg = Loc.GetString("lathe-menu-no-materials-message");
@@ -132,9 +149,7 @@ public sealed partial class LatheMenu : DefaultWindow
var adjustedAmount = SharedLatheSystem.AdjustMaterial(amount, prototype.ApplyMaterialDiscount, component.MaterialUseMultiplier);
sb.Append(adjustedAmount);
sb.Append(' ');
sb.Append(Loc.GetString(proto.Name));
sb.Append(Loc.GetString("lathe-menu-tooltip-display", ("amount", adjustedAmount), ("material", Loc.GetString(proto.Name))));
}
var icon = prototype.Icon == null

View File

@@ -1,8 +1,8 @@
<DefaultWindow
<DefaultWindow
xmlns="https://spacestation14.io"
Title="{Loc 'lathe-queue-menu-title'}"
MinSize="300 450"
SetSize="300 450">
SetSize="350 475">
<BoxContainer Orientation="Vertical">
<BoxContainer
Orientation="Horizontal"
@@ -19,7 +19,8 @@
SizeFlagsStretchRatio="3">
<Label
Name="NameLabel"
RectClipContent="True">
RectClipContent="True"
Margin="36 0 0 0">
</Label>
<Label
Name="Description"

View File

@@ -32,6 +32,8 @@ namespace Content.Server.Lathe
[Dependency] private readonly UserInterfaceSystem _uiSys = default!;
[Dependency] private readonly MaterialStorageSystem _materialStorage = default!;
[Dependency] private readonly StackSystem _stack = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
public override void Initialize()
{
base.Initialize();
@@ -50,6 +52,36 @@ namespace Content.Server.Lathe
SubscribeLocalEvent<LatheComponent, MaterialAmountChangedEvent>(OnMaterialAmountChanged);
SubscribeLocalEvent<TechnologyDatabaseComponent, LatheGetRecipesEvent>(OnGetRecipes);
SubscribeLocalEvent<EmagLatheRecipesComponent, LatheGetRecipesEvent>(GetEmagLatheRecipes);
SubscribeLocalEvent<LatheComponent, LatheEjectMaterialMessage>(OnLatheEjectMessage);
}
private void OnLatheEjectMessage(EntityUid uid, LatheComponent lathe, LatheEjectMaterialMessage message)
{
if (!lathe.CanEjectStoredMaterials)
return;
if (!_prototypeManager.TryIndex<MaterialPrototype>(message.Material, out var material))
return;
int volume = 0;
if (material.StackEntity != null)
{
var entProto = _prototypeManager.Index<EntityPrototype>(material.StackEntity);
if (!entProto.TryGetComponent<PhysicalCompositionComponent>(out var composition))
return;
var volumePerSheet = composition.MaterialComposition.FirstOrDefault(kvp => kvp.Key == message.Material).Value;
int sheetsToExtract = Math.Min(message.SheetsToExtract, _stack.GetMaxCount(material.StackEntity));
volume = sheetsToExtract * volumePerSheet;
}
if (volume > 0 && _materialStorage.TryChangeMaterialAmount(uid, message.Material, -volume))
{
_materialStorage.SpawnMultipleFromMaterial(volume, material, Transform(uid).Coordinates, out var overflow);
}
}
public override void Update(float frameTime)

View File

@@ -47,6 +47,12 @@ namespace Content.Shared.Lathe
[ViewVariables]
public LatheRecipePrototype? CurrentRecipe;
/// <summary>
/// Whether the lathe can eject the materials stored within it
/// </summary>
[DataField("canEjectStoredMaterials")]
public bool CanEjectStoredMaterials = true;
#region MachineUpgrading
/// <summary>
/// A modifier that changes how long it takes to print a recipe

View File

@@ -0,0 +1,16 @@
using Robust.Shared.Serialization;
namespace Content.Shared.Lathe;
[Serializable, NetSerializable]
public sealed class LatheEjectMaterialMessage : BoundUserInterfaceMessage
{
public string Material;
public int SheetsToExtract;
public LatheEjectMaterialMessage(string material, int sheetsToExtract)
{
Material = material;
SheetsToExtract = sheetsToExtract;
}
}

View File

@@ -5,5 +5,8 @@ lathe-menu-sync = Sync
lathe-menu-search-designs = Search designs
lathe-menu-search-filter = Filter
lathe-menu-amount = Amount:
lathe-menu-material-display = {$material} {$amount} cm³
lathe-menu-material-display = {$material} ({$amount} cm³)
lathe-menu-tooltip-display = {$amount} cm³ of {$material}
lathe-menu-no-materials-message = No materials loaded
lathe-menu-materials-ejection = Eject materials
lathe-menu-materials-ejection-title = Eject materials

View File

@@ -817,6 +817,7 @@
- type: Lathe
idleState: icon
runningState: building
canEjectStoredMaterials: false
staticRecipes:
- SheetSteel30
- SheetGlass30
@@ -854,5 +855,6 @@
- type: Lathe
idleState: base_machine
runningState: base_machine_processing
canEjectStoredMaterials: false
staticRecipes:
- MaterialSheetMeat