Added auto label for ChemMaster (#5596)

This commit is contained in:
Spartak
2021-11-28 18:25:23 -08:00
committed by GitHub
parent 47186a6dec
commit 30c87ca6b2
6 changed files with 137 additions and 65 deletions

View File

@@ -38,19 +38,19 @@ namespace Content.Client.Chemistry.UI
_window.OnClose += Close;
//Setup static button actions.
_window.EjectButton.OnPressed += _ => PrepareData(UiAction.Eject, null, null, null, null);
_window.BufferTransferButton.OnPressed += _ => PrepareData(UiAction.Transfer, null, null, null, null);
_window.BufferDiscardButton.OnPressed += _ => PrepareData(UiAction.Discard, null, null, null, null);
_window.CreatePillButton.OnPressed += _ => PrepareData(UiAction.CreatePills, null, null, _window.PillAmount.Value, null);
_window.CreateBottleButton.OnPressed += _ => PrepareData(UiAction.CreateBottles, null, null, null, _window.BottleAmount.Value);
_window.EjectButton.OnPressed += _ => PrepareData(UiAction.Eject, null, null, null, null, null);
_window.BufferTransferButton.OnPressed += _ => PrepareData(UiAction.Transfer, null, null, null, null, null);
_window.BufferDiscardButton.OnPressed += _ => PrepareData(UiAction.Discard, null, null, null, null, null);
_window.CreatePillButton.OnPressed += _ => PrepareData(UiAction.CreatePills, null, _window.LabelLine, null, _window.PillAmount.Value, null);
_window.CreateBottleButton.OnPressed += _ => PrepareData(UiAction.CreateBottles, null, _window.LabelLine, null, null, _window.BottleAmount.Value);
for(uint i = 0; i < _window.PillTypeButtons.Length; i++)
{
uint type = i;
_window.PillTypeButtons[i].OnPressed += _ => PrepareData(UiAction.SetPillType, null, type + 1, null, null);
_window.PillTypeButtons[i].OnPressed += _ => PrepareData(UiAction.SetPillType, null, null, type + 1, null, null);
}
_window.OnChemButtonPressed += (args, button) => PrepareData(UiAction.ChemButton, button, null, null, null);
_window.OnChemButtonPressed += (args, button) => PrepareData(UiAction.ChemButton, button, null, null, null, null);
}
/// <summary>
@@ -69,15 +69,15 @@ namespace Content.Client.Chemistry.UI
_window?.UpdateState(castState); //Update window state
}
private void PrepareData(UiAction action, ChemButton? button, uint? pillType, int? pillAmount, int? bottleAmount)
private void PrepareData(UiAction action, ChemButton? button, string? label, uint? pillType, int? pillAmount, int? bottleAmount)
{
if (button != null)
{
SendMessage(new UiActionMessage(action, button.Amount, button.Id, button.IsBuffer, null, null, null));
SendMessage(new UiActionMessage(action, button.Amount, button.Id, button.IsBuffer, null, null, null, null));
}
else
{
SendMessage(new UiActionMessage(action, null, null, null, pillType, pillAmount, bottleAmount));
SendMessage(new UiActionMessage(action, null, null, null, label, pillType, pillAmount, bottleAmount));
}
}

View File

@@ -41,7 +41,7 @@
StyleClasses="OpenLeft" />
</BoxContainer>
<!-- Buffer info -->
<PanelContainer VerticalExpand="True" SizeFlagsStretchRatio="6" MinSize="0 100">
<PanelContainer VerticalExpand="True" SizeFlagsStretchRatio="1" MinSize="0 150">
<PanelContainer.PanelOverride>
<gfx:StyleBoxFlat BackgroundColor="#1B1B1E" />
</PanelContainer.PanelOverride>
@@ -66,17 +66,21 @@
<PanelContainer.PanelOverride>
<gfx:StyleBoxFlat BackgroundColor="#1B1B1E" />
</PanelContainer.PanelOverride>
<!-- Packaging options -->
<BoxContainer Orientation="Vertical"
HorizontalExpand="True">
<!-- Packaging Info -->
<BoxContainer Orientation="Vertical"
HorizontalExpand="True">
HorizontalExpand="True"
VerticalExpand="True"
SeparationOverride="5">
<!-- Label for output -->
<BoxContainer Orientation="Horizontal">
<Label Text="{Loc 'chem-master-current-text-label'}" />
<Control HorizontalExpand="True" MinSize="50 0"/>
<LineEdit Name="LabelLineEdit" SetWidth="455"/>
</BoxContainer>
<!-- Pills Type Buttons -->
<BoxContainer Orientation="Horizontal">
<Label Text="{Loc 'chem-master-window-pill-type-label'}"/>
<Control HorizontalExpand="True"
MinSize="50 0"></Control>
<Control HorizontalExpand="True" MinSize="50 0"/>
<GridContainer Name="Grid" Columns="10">
<!-- Pills type buttons are generated in the code -->
</GridContainer>
@@ -108,7 +112,6 @@
StyleClasses="LabelSecondaryColor" />
</BoxContainer>
</BoxContainer>
</BoxContainer>
</PanelContainer>
</PanelContainer>
</BoxContainer>

View File

@@ -29,6 +29,7 @@ namespace Content.Client.Chemistry.UI
public partial class ChemMasterWindow : SS14Window
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
public event Action<string>? OnLabelEntered;
public event Action<BaseButton.ButtonEventArgs, ChemButton>? OnChemButtonPressed;
public readonly Button[] PillTypeButtons;
@@ -47,6 +48,7 @@ namespace Content.Client.Chemistry.UI
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
LabelLineEdit.OnTextEntered += e => OnLabelEntered?.Invoke(e.Text);
//Pill type selection buttons, in total there are 20 pills.
//Pill rsi file should have states named as pill1, pill2, and so on.
@@ -75,7 +77,6 @@ namespace Content.Client.Chemistry.UI
Group = pillTypeGroup
};
//Generate buttons textures
var specifier = new SpriteSpecifier.Rsi(resourcePath, "pill" + (i + 1));
TextureRect pillTypeTexture = new TextureRect
@@ -111,6 +112,7 @@ namespace Content.Client.Chemistry.UI
{
var castState = (ChemMasterBoundUserInterfaceState) state;
Title = castState.DispenserName;
LabelLine = castState.Label;
UpdatePanelInfo(castState);
if (Contents.Children != null)
{
@@ -194,7 +196,8 @@ namespace Content.Client.Chemistry.UI
if (!state.BufferReagents.Any())
{
BufferInfo.Children.Add(new Label {Text = Loc.GetString("chem-master-window-buffer-empty-text") });
BufferInfo.Children.Add(new Label { Text = Loc.GetString("chem-master-window-buffer-empty-text") });
return;
}
@@ -250,6 +253,18 @@ namespace Content.Client.Chemistry.UI
}
}
}
public String LabelLine
{
get
{
return LabelLineEdit.Text;
}
set
{
LabelLineEdit.Text = value;
}
}
}
public class ChemButton : Button

View File

@@ -24,6 +24,8 @@ using Robust.Shared.Localization;
using Robust.Shared.Player;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
using Robust.Shared.Log;
using Content.Server.Labels.Components;
namespace Content.Server.Chemistry.Components
{
@@ -41,6 +43,9 @@ namespace Content.Server.Chemistry.Components
[ViewVariables]
private uint _pillType = 1;
[ViewVariables]
private string _label = "";
[ViewVariables]
private bool _bufferModeTransfer = true;
@@ -102,9 +107,7 @@ namespace Content.Server.Chemistry.Components
private void OnUiReceiveMessage(ServerBoundUserInterfaceMessage obj)
{
if (obj.Session.AttachedEntity == null)
{
return;
}
var msg = (UiActionMessage) obj.Message;
var needsPower = msg.Action switch
@@ -138,7 +141,8 @@ namespace Content.Server.Chemistry.Components
break;
case UiAction.CreatePills:
case UiAction.CreateBottles:
TryCreatePackage(obj.Session.AttachedEntity, msg.Action, msg.PillAmount, msg.BottleAmount);
_label = msg.Label;
TryCreatePackage(obj.Session.AttachedEntity, msg.Action, msg.Label, msg.PillAmount, msg.BottleAmount);
break;
default:
throw new ArgumentOutOfRangeException();
@@ -182,13 +186,13 @@ namespace Content.Server.Chemistry.Components
!EntitySystem.Get<SolutionContainerSystem>().TryGetSolution(beaker.Uid, fits.Solution, out var beakerSolution))
{
return new ChemMasterBoundUserInterfaceState(Powered, false, FixedPoint2.New(0), FixedPoint2.New(0),
"", Owner.Name, new List<Solution.ReagentQuantity>(), BufferSolution.Contents, _bufferModeTransfer,
"", _label, Owner.Name, new List<Solution.ReagentQuantity>(), BufferSolution.Contents, _bufferModeTransfer,
BufferSolution.TotalVolume, _pillType);
}
return new ChemMasterBoundUserInterfaceState(Powered, true, beakerSolution.CurrentVolume,
beakerSolution.MaxVolume,
beaker.Name, Owner.Name, beakerSolution.Contents, BufferSolution.Contents, _bufferModeTransfer,
beaker.Name, _label, Owner.Name, beakerSolution.Contents, BufferSolution.Contents, _bufferModeTransfer,
BufferSolution.TotalVolume, _pillType);
}
@@ -260,26 +264,51 @@ namespace Content.Server.Chemistry.Components
}
}
}
_label = GenerateLabel();
UpdateUserInterface();
}
private void TryCreatePackage(IEntity user, UiAction action, int pillAmount, int bottleAmount)
/// <summary>
/// Handles label generation depending from solutions and their amount.
/// Label is generated by taking the most significant solution name.
/// </summary>
private string GenerateLabel()
{
if (_bufferSolution == null || _bufferSolution.Contents.Count == 0)
return "";
_bufferSolution.Contents.Sort();
return _bufferSolution.Contents[_bufferSolution.Contents.Count - 1].ReagentId;
}
private void TryCreatePackage(IEntity user, UiAction action, string label, int pillAmount, int bottleAmount)
{
if (BufferSolution.TotalVolume == 0)
{
user.PopupMessageCursor(Loc.GetString("chem-master-window-buffer-empty-text"));
return;
}
if (action == UiAction.CreateBottles)
{
var individualVolume = BufferSolution.TotalVolume / FixedPoint2.New(bottleAmount);
if (individualVolume < FixedPoint2.New(1))
{
user.PopupMessageCursor(Loc.GetString("chem-master-window-buffer-low-text"));
return;
}
var actualVolume = FixedPoint2.Min(individualVolume, FixedPoint2.New(30));
for (int i = 0; i < bottleAmount; i++)
{
var bottle = Owner.EntityManager.SpawnEntity("ChemistryEmptyBottle01", Owner.Transform.Coordinates);
//Adding label
LabelComponent labelComponent = bottle.EnsureComponent<LabelComponent>();
labelComponent.OriginalName = bottle.Name;
bottle.Name += $" ({label})";
labelComponent.CurrentLabel = label;
var bufferSolution = BufferSolution.SplitSolution(actualVolume);
var bottleSolution = EntitySystem.Get<SolutionContainerSystem>().EnsureSolution(bottle.Uid, "drink");
@@ -306,15 +335,23 @@ namespace Content.Server.Chemistry.Components
{
var individualVolume = BufferSolution.TotalVolume / FixedPoint2.New(pillAmount);
if (individualVolume < FixedPoint2.New(1))
{
user.PopupMessageCursor(Loc.GetString("chem-master-window-buffer-low-text"));
return;
}
var actualVolume = FixedPoint2.Min(individualVolume, FixedPoint2.New(50));
for (int i = 0; i < pillAmount; i++)
{
var pill = Owner.EntityManager.SpawnEntity("pill", Owner.Transform.Coordinates);
var bufferSolution = BufferSolution.SplitSolution(actualVolume);
//Adding label
LabelComponent labelComponent = pill.EnsureComponent<LabelComponent>();
labelComponent.OriginalName = pill.Name;
pill.Name += $" ({label})";
labelComponent.CurrentLabel = label;
var bufferSolution = BufferSolution.SplitSolution(actualVolume);
var pillSolution = EntitySystem.Get<SolutionContainerSystem>().EnsureSolution(pill.Uid, "food");
EntitySystem.Get<SolutionContainerSystem>().TryAddSolution(pill.Uid, pillSolution, bufferSolution);
@@ -343,6 +380,9 @@ namespace Content.Server.Chemistry.Components
}
}
if (_bufferSolution?.Contents.Count == 0)
_label = "";
UpdateUserInterface();
}

View File

@@ -29,6 +29,7 @@ namespace Content.Shared.Chemistry.Components
public readonly FixedPoint2 BeakerCurrentVolume;
public readonly FixedPoint2 BeakerMaxVolume;
public readonly string ContainerName;
public readonly string Label;
/// <summary>
/// A list of the reagents and their amounts within the beaker/reagent container, if applicable.
@@ -45,7 +46,7 @@ namespace Content.Shared.Chemistry.Components
public readonly FixedPoint2 BufferCurrentVolume;
public readonly uint SelectedPillType;
public ChemMasterBoundUserInterfaceState(bool hasPower, bool hasBeaker, FixedPoint2 beakerCurrentVolume, FixedPoint2 beakerMaxVolume, string containerName,
public ChemMasterBoundUserInterfaceState(bool hasPower, bool hasBeaker, FixedPoint2 beakerCurrentVolume, FixedPoint2 beakerMaxVolume, string containerName, string label,
string dispenserName, IReadOnlyList<Solution.ReagentQuantity> containerReagents, IReadOnlyList<Solution.ReagentQuantity> bufferReagents, bool bufferModeTransfer, FixedPoint2 bufferCurrentVolume, uint selectedPillType)
{
HasPower = hasPower;
@@ -53,6 +54,7 @@ namespace Content.Shared.Chemistry.Components
BeakerCurrentVolume = beakerCurrentVolume;
BeakerMaxVolume = beakerMaxVolume;
ContainerName = containerName;
Label = label;
DispenserName = dispenserName;
ContainerReagents = containerReagents;
BufferReagents = bufferReagents;
@@ -72,11 +74,12 @@ namespace Content.Shared.Chemistry.Components
public readonly FixedPoint2 Amount;
public readonly string Id = "";
public readonly bool IsBuffer;
public readonly string Label = "";
public readonly uint PillType;
public readonly int PillAmount;
public readonly int BottleAmount;
public UiActionMessage(UiAction action, FixedPoint2? amount, string? id, bool? isBuffer, uint? pillType, int? pillAmount, int? bottleAmount)
public UiActionMessage(UiAction action, FixedPoint2? amount, string? id, bool? isBuffer, string? label, uint? pillType, int? pillAmount, int? bottleAmount)
{
Action = action;
if (Action == UiAction.ChemButton)
@@ -91,13 +94,22 @@ namespace Content.Shared.Chemistry.Components
Id = id;
}
isBuffer = isBuffer.GetValueOrDefault();
IsBuffer = isBuffer.GetValueOrDefault();
}
else
{
PillAmount = pillAmount.GetValueOrDefault();
PillType = pillType.GetValueOrDefault();
BottleAmount = bottleAmount.GetValueOrDefault();
if (label == null)
{
Label = "null";
}
else
{
Label = label;
}
}
}
}

View File

@@ -15,9 +15,11 @@ chem-master-window-buffer-text = Buffer
chem-master-window-buffer-label = buffer:
chem-master-window-buffer-all-amount = All
chem-master-window-buffer-empty-text = Buffer empty.
chem-master-window-buffer-low-text = Not enough solution in buffer
chem-master-window-transfer-button = Transfer
chem-master-window-discard-button = Discard
chem-master-window-packaging-text = Packaging
chem-master-current-text-label = Label:
chem-master-window-pills-label = Pills:
chem-master-window-pill-type-label = Pill type:
chem-master-window-max-pills-volume-text = max 50u/each