ReagentDispenser ECS (#11418)

This commit is contained in:
0x6273
2022-10-04 02:57:32 +02:00
committed by GitHub
parent f89b4f0a1d
commit 0c24f8b69b
17 changed files with 351 additions and 640 deletions

View File

@@ -1,28 +1,27 @@
using System.Collections.Generic;
using System.Linq;
using Content.Client.Stylesheets;
using Content.Client.UserInterface;
using Content.Shared.Chemistry.Dispenser;
using Content.Shared.Chemistry;
using Content.Shared.Chemistry.Reagent;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Prototypes;
using static Content.Shared.Chemistry.Dispenser.SharedReagentDispenserComponent;
using static Robust.Client.UserInterface.Controls.BoxContainer;
namespace Content.Client.Chemistry.UI
{
/// <summary>
/// Client-side UI used to control a <see cref="SharedReagentDispenserComponent"/>
/// Client-side UI used to control a <see cref="ReagentDispenserComponent"/>.
/// </summary>
[GenerateTypedNameReferences]
public sealed partial class ReagentDispenserWindow : DefaultWindow
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
public event Action<BaseButton.ButtonEventArgs, DispenseReagentButton>? OnDispenseReagentButtonPressed;
public event Action<GUIMouseHoverEventArgs, DispenseReagentButton>? OnDispenseReagentButtonMouseEntered;
public event Action<GUIMouseHoverEventArgs, DispenseReagentButton>? OnDispenseReagentButtonMouseExited;
/// <summary>
/// Create and initialize the dispenser UI client-side. Creates the basic layout,
@@ -47,26 +46,27 @@ namespace Content.Client.Chemistry.UI
/// <summary>
/// Update the button grid of reagents which can be dispensed.
/// <para>The actions for these buttons are set in <see cref="ReagentDispenserBoundUserInterface.UpdateReagentsList"/>.</para>
/// </summary>
/// <param name="inventory">Reagents which can be dispensed by this dispenser</param>
public void UpdateReagentsList(List<ReagentDispenserInventoryEntry> inventory)
public void UpdateReagentsList(List<string> inventory)
{
if (ChemicalList == null) return;
if (inventory == null) return;
ChemicalList.Children.Clear();
foreach (var entry in inventory)
foreach (var entry in inventory
.OrderBy(r => {_prototypeManager.TryIndex(r, out ReagentPrototype? p); return p?.LocalizedName;}))
{
if (_prototypeManager.TryIndex(entry.ID, out ReagentPrototype? proto))
{
ChemicalList.AddChild(new Button {Text = proto.LocalizedName});
}
else
{
ChemicalList.AddChild(new Button {Text = Loc.GetString("reagent-dispenser-window-reagent-name-not-found-text") });
}
var localizedName = _prototypeManager.TryIndex(entry, out ReagentPrototype? p)
? p.LocalizedName
: Loc.GetString("reagent-dispenser-window-reagent-name-not-found-text");
var button = new DispenseReagentButton(entry, localizedName);
button.OnPressed += args => OnDispenseReagentButtonPressed?.Invoke(args, button);
button.OnMouseEntered += args => OnDispenseReagentButtonMouseEntered?.Invoke(args, button);
button.OnMouseExited += args => OnDispenseReagentButtonMouseExited?.Invoke(args, button);
ChemicalList.AddChild(button);
}
}
@@ -77,50 +77,40 @@ namespace Content.Client.Chemistry.UI
public void UpdateState(BoundUserInterfaceState state)
{
var castState = (ReagentDispenserBoundUserInterfaceState) state;
Title = castState.DispenserName;
UpdateContainerInfo(castState);
// Disable all buttons if not powered
if (Contents.Children != null)
{
ButtonHelpers.SetButtonDisabledRecursive(Contents, !castState.HasPower);
EjectButton.Disabled = false;
}
UpdateReagentsList(castState.Inventory);
// Disable the Clear & Eject button if no beaker
if (!castState.HasBeaker)
{
ClearButton.Disabled = true;
EjectButton.Disabled = true;
}
ClearButton.Disabled = castState.OutputContainer is null;
EjectButton.Disabled = castState.OutputContainer is null;
switch (castState.SelectedDispenseAmount.Int())
switch (castState.SelectedDispenseAmount)
{
case 1:
case ReagentDispenserDispenseAmount.U1:
DispenseButton1.Pressed = true;
break;
case 5:
case ReagentDispenserDispenseAmount.U5:
DispenseButton5.Pressed = true;
break;
case 10:
case ReagentDispenserDispenseAmount.U10:
DispenseButton10.Pressed = true;
break;
case 15:
case ReagentDispenserDispenseAmount.U15:
DispenseButton15.Pressed = true;
break;
case 20:
case ReagentDispenserDispenseAmount.U20:
DispenseButton20.Pressed = true;
break;
case 25:
case ReagentDispenserDispenseAmount.U25:
DispenseButton25.Pressed = true;
break;
case 30:
case ReagentDispenserDispenseAmount.U30:
DispenseButton30.Pressed = true;
break;
case 50:
case ReagentDispenserDispenseAmount.U50:
DispenseButton50.Pressed = true;
break;
case 100:
case ReagentDispenserDispenseAmount.U100:
DispenseButton100.Pressed = true;
break;
}
@@ -131,12 +121,13 @@ namespace Content.Client.Chemistry.UI
/// <para>Also highlights a reagent if it's dispense button is being mouse hovered.</para>
/// </summary>
/// <param name="state">State data for the dispenser.</param>
/// <param name="highlightedReagentId">Prototype id of the reagent whose dispense button is currently being mouse hovered.</param>
public void UpdateContainerInfo(ReagentDispenserBoundUserInterfaceState state, string highlightedReagentId = "")
/// <param name="highlightedReagentId">Prototype ID of the reagent whose dispense button is currently being mouse hovered,
/// or null if no button is being hovered.</param>
public void UpdateContainerInfo(ReagentDispenserBoundUserInterfaceState state, string? highlightedReagentId = null)
{
ContainerInfo.Children.Clear();
if (!state.HasBeaker)
if (state.OutputContainer is null)
{
ContainerInfo.Children.Add(new Label {Text = Loc.GetString("reagent-dispenser-window-no-container-loaded-text") });
return;
@@ -147,67 +138,55 @@ namespace Content.Client.Chemistry.UI
Orientation = LayoutOrientation.Horizontal,
Children =
{
new Label {Text = $"{state.ContainerName}: "},
new Label {Text = $"{state.OutputContainer.DisplayName}: "},
new Label
{
Text = $"{state.BeakerCurrentVolume}/{state.BeakerMaxVolume}",
Text = $"{state.OutputContainer.CurrentVolume}/{state.OutputContainer.MaxVolume}",
StyleClasses = {StyleNano.StyleClassLabelSecondaryColor}
}
}
});
if (state.ContainerReagents == null)
foreach (var reagent in state.OutputContainer.Contents)
{
return;
}
// Try get to the prototype for the given reagent. This gives us its name.
var localizedName = _prototypeManager.TryIndex(reagent.Id, out ReagentPrototype? p)
? p.LocalizedName
: Loc.GetString("reagent-dispenser-window-reagent-name-not-found-text");
foreach (var reagent in state.ContainerReagents)
{
var name = Loc.GetString("reagent-dispenser-window-unknown-reagent-text");
//Try to the prototype for the given reagent. This gives us it's name.
if (_prototypeManager.TryIndex(reagent.ReagentId, out ReagentPrototype? proto))
var nameLabel = new Label {Text = $"{localizedName}: "};
var quantityLabel = new Label
{
name = proto.LocalizedName;
Text = Loc.GetString("reagent-dispenser-window-quantity-label-text", ("quantity", reagent.Quantity)),
StyleClasses = {StyleNano.StyleClassLabelSecondaryColor},
};
// Check if the reagent is being moused over. If so, color it green.
if (reagent.Id == highlightedReagentId) {
nameLabel.SetOnlyStyleClass(StyleNano.StyleClassPowerStateGood);
quantityLabel.SetOnlyStyleClass(StyleNano.StyleClassPowerStateGood);
}
//Check if the reagent is being moused over. If so, color it green.
if (proto != null && proto.ID == highlightedReagentId)
ContainerInfo.Children.Add(new BoxContainer
{
ContainerInfo.Children.Add(new BoxContainer
Orientation = LayoutOrientation.Horizontal,
Children =
{
Orientation = LayoutOrientation.Horizontal,
Children =
{
new Label
{
Text = $"{name}: ",
StyleClasses = {StyleNano.StyleClassPowerStateGood}
},
new Label
{
Text = Loc.GetString("reagent-dispenser-window-quantity-label-text", ("quantity", reagent.Quantity)),
StyleClasses = {StyleNano.StyleClassPowerStateGood}
}
}
});
}
else //Otherwise, color it the normal colors.
{
ContainerInfo.Children.Add(new BoxContainer
{
Orientation = LayoutOrientation.Horizontal,
Children =
{
new Label {Text = $"{name}: "},
new Label
{
Text = Loc.GetString("reagent-dispenser-window-quantity-label-text", ("quantity", reagent.Quantity)),
StyleClasses = {StyleNano.StyleClassLabelSecondaryColor}
}
}
});
}
nameLabel,
quantityLabel,
}
});
}
}
}
public sealed class DispenseReagentButton : Button {
public string ReagentId { get; }
public DispenseReagentButton(string reagentId, string text)
{
ReagentId = reagentId;
Text = text;
}
}
}