Clean up reagent dispenser and make it slightly better.

This commit is contained in:
Pieter-Jan Briers
2019-10-20 01:30:38 +02:00
parent 9c60d4936d
commit 6630e454c6
8 changed files with 247 additions and 219 deletions

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using Content.Shared.GameObjects.Components.Chemistry;
using JetBrains.Annotations;
using Robust.Client.GameObjects.Components.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.GameObjects.Components.UserInterface;
@@ -13,6 +14,7 @@ namespace Content.Client.GameObjects.Components.Chemistry
/// <summary>
/// Initializes a <see cref="ReagentDispenserWindow"/> and updates it when new server messages are received.
/// </summary>
[UsedImplicitly]
public class ReagentDispenserBoundUserInterface : BoundUserInterface
{
#pragma warning disable 649
@@ -60,7 +62,10 @@ namespace Content.Client.GameObjects.Components.Chemistry
/// <summary>
/// Update the ui each time new state data is sent from the server.
/// </summary>
/// <param name="state">Data of the <see cref="ReagentDispenserComponent"/> that this ui represents. Sent from the server.</param>
/// <param name="state">
/// Data of the <see cref="SharedReagentDispenserComponent"/> that this ui represents.
/// Sent from the server.
/// </param>
protected override void UpdateState(BoundUserInterfaceState state)
{
base.UpdateState(state);
@@ -70,8 +75,6 @@ namespace Content.Client.GameObjects.Components.Chemistry
_window?.UpdateState(castState); //Update window state
UpdateReagentsList(castState.Inventory); //Update reagents list & reagent button actions
_window.ForceRunLayoutUpdate();
}
/// <summary>
@@ -103,7 +106,7 @@ namespace Content.Client.GameObjects.Components.Chemistry
}
}
public void ButtonPressed(UiButton button, int dispenseIndex = -1)
private void ButtonPressed(UiButton button, int dispenseIndex = -1)
{
SendMessage(new UiButtonPressedMessage(button, dispenseIndex));
}

View File

@@ -1,8 +1,9 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using Content.Client.UserInterface;
using Content.Shared.Chemistry;
using Content.Shared.GameObjects.Components.Chemistry;
using Robust.Client.Graphics.Drawing;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Shared.GameObjects.Components.UserInterface;
@@ -15,33 +16,39 @@ using static Content.Shared.GameObjects.Components.Chemistry.SharedReagentDispen
namespace Content.Client.GameObjects.Components.Chemistry
{
/// <summary>
/// Client-side UI used to control a <see cref="ReagentDispenserComponent"/>
/// Client-side UI used to control a <see cref="SharedReagentDispenserComponent"/>
/// </summary>
public class ReagentDispenserWindow : SS14Window
{
/// <summary>Sets the dispense amount to 1 when pressed.</summary>
public Button DispenseButton1;
/// <summary>Sets the dispense amount to 5 when pressed.</summary>
public Button DispenseButton5;
/// <summary>Sets the dispense amount to 10 when pressed.</summary>
public Button DispenseButton10;
/// <summary>Sets the dispense amount to 25 when pressed.</summary>
public Button DispenseButton25;
/// <summary>Sets the dispense amount to 50 when pressed.</summary>
public Button DispenseButton50;
/// <summary>Sets the dispense amount to 100 when pressed.</summary>
public Button DispenseButton100;
/// <summary>Contains info about the reagent container such as it's contents, if one is loaded into the dispenser.</summary>
public VBoxContainer ContainerInfo;
private readonly VBoxContainer ContainerInfo;
/// <summary>Sets the dispense amount to 1 when pressed.</summary>
public Button DispenseButton1 { get; }
/// <summary>Sets the dispense amount to 5 when pressed.</summary>
public Button DispenseButton5 { get; }
/// <summary>Sets the dispense amount to 10 when pressed.</summary>
public Button DispenseButton10 { get; }
/// <summary>Sets the dispense amount to 25 when pressed.</summary>
public Button DispenseButton25 { get; }
/// <summary>Sets the dispense amount to 50 when pressed.</summary>
public Button DispenseButton50 { get; }
/// <summary>Sets the dispense amount to 100 when pressed.</summary>
public Button DispenseButton100 { get; }
/// <summary>Ejects the reagent container from the dispenser.</summary>
public Button ClearButton;
public Button ClearButton { get; }
/// <summary>Removes all reagents from the reagent container.</summary>
public Button EjectButton;
public Button EjectButton { get; }
/// <summary>A grid of buttons for each reagent which can be dispensed.</summary>
public GridContainer ChemicalList;
public GridContainer ChemicalList { get; }
#pragma warning disable 649
[Dependency] private readonly IPrototypeManager _prototypeManager;
@@ -54,8 +61,9 @@ namespace Content.Client.GameObjects.Components.Chemistry
/// </summary>
public ReagentDispenserWindow()
{
_prototypeManager = IoCManager.Resolve<IPrototypeManager>();
_localizationManager = IoCManager.Resolve<ILocalizationManager>();
IoCManager.InjectDependencies(this);
var dispenseAmountGroup = new ButtonGroup();
Contents.AddChild(new VBoxContainer
{
@@ -67,15 +75,16 @@ namespace Content.Client.GameObjects.Components.Chemistry
Children =
{
new Label {Text = _localizationManager.GetString("Amount")},
(DispenseButton1 = new Button{Text = "1"}),
(DispenseButton5 = new Button{Text = "5"}),
(DispenseButton10 = new Button{Text = "10"}),
(DispenseButton25 = new Button{Text = "25"}),
(DispenseButton50 = new Button{Text = "50"}),
(DispenseButton100 = new Button{Text = "100"}),
new Control {CustomMinimumSize = (20, 0)}, //Padding
(DispenseButton1 = new Button {Text = "1", Group = dispenseAmountGroup}),
(DispenseButton5 = new Button {Text = "5", Group = dispenseAmountGroup}),
(DispenseButton10 = new Button {Text = "10", Group = dispenseAmountGroup}),
(DispenseButton25 = new Button {Text = "25", Group = dispenseAmountGroup}),
(DispenseButton50 = new Button {Text = "50", Group = dispenseAmountGroup}),
(DispenseButton100 = new Button {Text = "100", Group = dispenseAmountGroup}),
}
},
new Panel{CustomMinimumSize = (0.0f, 10.0f)}, //Padding
new Control {CustomMinimumSize = (0.0f, 10.0f)}, //Padding
(ChemicalList = new GridContainer //Grid of which reagents can be dispensed.
{
CustomMinimumSize = (470.0f, 200.0f),
@@ -83,7 +92,7 @@ namespace Content.Client.GameObjects.Components.Chemistry
SizeFlagsHorizontal = SizeFlags.FillExpand,
Columns = 5
}),
new Panel{CustomMinimumSize = (0.0f, 10.0f)}, //Padding
new Control {CustomMinimumSize = (0.0f, 10.0f)}, //Padding
new HBoxContainer
{
Children =
@@ -93,7 +102,8 @@ namespace Content.Client.GameObjects.Components.Chemistry
(EjectButton = new Button {Text = _localizationManager.GetString("Eject")})
}
},
new PanelContainer //Wrap the container info in a PanelContainer so we can color it's background differently.
new
PanelContainer //Wrap the container info in a PanelContainer so we can color it's background differently.
{
SizeFlagsVertical = SizeFlags.FillExpand,
SizeFlagsStretchRatio = 6,
@@ -103,13 +113,18 @@ namespace Content.Client.GameObjects.Components.Chemistry
},
Children =
{
(ContainerInfo = new VBoxContainer //Currently empty, when server sends state data this will have container contents and fill volume.
(ContainerInfo =
new
VBoxContainer //Currently empty, when server sends state data this will have container contents and fill volume.
{
MarginLeft = 5.0f,
SizeFlagsHorizontal = SizeFlags.FillExpand,
Children =
{
new Label{Text = _localizationManager.GetString("No container loaded.")}
new Label
{
Text = _localizationManager.GetString("No container loaded.")
}
}
}),
}
@@ -140,7 +155,6 @@ namespace Content.Client.GameObjects.Components.Chemistry
{
ChemicalList.AddChild(new Button {Text = _localizationManager.GetString("Reagent name not found")});
}
}
}
@@ -153,6 +167,28 @@ namespace Content.Client.GameObjects.Components.Chemistry
var castState = (ReagentDispenserBoundUserInterfaceState) state;
Title = castState.DispenserName;
UpdateContainerInfo(castState);
switch (castState.SelectedDispenseAmount)
{
case 1:
DispenseButton1.Pressed = true;
break;
case 5:
DispenseButton5.Pressed = true;
break;
case 10:
DispenseButton10.Pressed = true;
break;
case 25:
DispenseButton25.Pressed = true;
break;
case 50:
DispenseButton50.Pressed = true;
break;
case 100:
DispenseButton100.Pressed = true;
break;
}
}
/// <summary>
@@ -161,28 +197,44 @@ namespace Content.Client.GameObjects.Components.Chemistry
/// </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 = "InvalidReagent")
public void UpdateContainerInfo(ReagentDispenserBoundUserInterfaceState state,
string highlightedReagentId = null)
{
ContainerInfo.Children.Clear();
if (state.HasBeaker) //If the dispenser doesn't have a beaker/container don't bother with this.
if (!state.HasBeaker)
{
ContainerInfo.Children.Add(new HBoxContainer //Name of the container and it's fill status (Ex: 44/100u)
ContainerInfo.Children.Add(new Label {Text = _localizationManager.GetString("No container loaded.")});
return;
}
ContainerInfo.Children.Add(new HBoxContainer // Name of the container and its fill status (Ex: 44/100u)
{
Children =
{
new Label {Text = $"{state.ContainerName}: "},
new Label{Text = $"{state.BeakerCurrentVolume}/{state.BeakerMaxVolume}", StyleClasses = { NanoStyle.StyleClassLabelSecondaryColor }}
new Label
{
Text = $"{state.BeakerCurrentVolume}/{state.BeakerMaxVolume}",
StyleClasses = {NanoStyle.StyleClassLabelSecondaryColor}
}
}
});
//List the reagents in the container if it has any at all.
if (state.ContainerReagents != null)
if (state.ContainerReagents == null)
{
//Loop through the reagents in the container.
return;
}
foreach (var reagent in state.ContainerReagents)
{
var name = _localizationManager.GetString("Unknown reagent");
//Try to the prototype for the given reagent. This gives us it's name.
if (_prototypeManager.TryIndex(reagent.ReagentId, out ReagentPrototype proto))
{
name = proto.Name;
}
//Check if the reagent is being moused over. If so, color it green.
if (proto.ID == highlightedReagentId)
{
@@ -190,7 +242,11 @@ namespace Content.Client.GameObjects.Components.Chemistry
{
Children =
{
new Label {Text = $"{proto.Name}: ", StyleClasses = {NanoStyle.StyleClassPowerStateGood}},
new Label
{
Text = $"{name}: ",
StyleClasses = {NanoStyle.StyleClassPowerStateGood}
},
new Label
{
Text = $"{reagent.Quantity}u",
@@ -205,23 +261,7 @@ namespace Content.Client.GameObjects.Components.Chemistry
{
Children =
{
new Label {Text = $"{proto.Name}: "},
new Label
{
Text = $"{reagent.Quantity}u",
StyleClasses = {NanoStyle.StyleClassLabelSecondaryColor}
}
}
});
}
}
else //If you fail to get the reagents name, just call it "Unknown reagent".
{
ContainerInfo.Children.Add(new HBoxContainer
{
Children =
{
new Label {Text = _localizationManager.GetString("Unknown reagent: ")},
new Label {Text = $"{name}: "},
new Label
{
Text = $"{reagent.Quantity}u",
@@ -233,11 +273,4 @@ namespace Content.Client.GameObjects.Components.Chemistry
}
}
}
else
{
ContainerInfo.Children.Add(new Label{Text = _localizationManager.GetString("No container loaded.")});
}
ForceRunLayoutUpdate(); //Force a layout update to avoid text hanging off the window until the user manually resizes it.
}
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Linq;
using Content.Server.GameObjects.Components.Sound;
using Content.Server.GameObjects.EntitySystems;
using Content.Server.Interfaces;
using Content.Server.Interfaces.GameObjects;
@@ -8,11 +9,13 @@ using Content.Shared.GameObjects.Components.Chemistry;
using Robust.Server.GameObjects.Components.Container;
using Robust.Server.GameObjects.Components.UserInterface;
using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Chemistry
{
@@ -32,14 +35,15 @@ namespace Content.Server.GameObjects.Components.Chemistry
[Dependency] private readonly ILocalizationManager _localizationManager;
#pragma warning restore 649
private BoundUserInterface _userInterface;
private ContainerSlot _beakerContainer;
private string _packPrototypeId;
[ViewVariables] private BoundUserInterface _userInterface;
[ViewVariables] private ContainerSlot _beakerContainer;
[ViewVariables] private string _packPrototypeId;
public bool HasBeaker => _beakerContainer.ContainedEntity != null;
public int DispenseAmount = 10;
[ViewVariables] private bool HasBeaker => _beakerContainer.ContainedEntity != null;
[ViewVariables] private int DispenseAmount = 10;
private SolutionComponent _solution => _beakerContainer.ContainedEntity.GetComponent<SolutionComponent>();
[ViewVariables]
private SolutionComponent Solution => _beakerContainer.ContainedEntity.GetComponent<SolutionComponent>();
/// <summary>
/// Shows the serializer how to save/load this components yaml prototype.
@@ -59,10 +63,12 @@ namespace Content.Server.GameObjects.Components.Chemistry
public override void Initialize()
{
base.Initialize();
_userInterface = Owner.GetComponent<ServerUserInterfaceComponent>().GetBoundUserInterface(ReagentDispenserUiKey.Key);
_userInterface = Owner.GetComponent<ServerUserInterfaceComponent>()
.GetBoundUserInterface(ReagentDispenserUiKey.Key);
_userInterface.OnReceiveMessage += OnUiReceiveMessage;
_beakerContainer = ContainerManagerComponent.Ensure<ContainerSlot>($"{Name}-reagentContainerContainer", Owner);
_beakerContainer =
ContainerManagerComponent.Ensure<ContainerSlot>($"{Name}-reagentContainerContainer", Owner);
InitializeFromPrototype();
UpdateUserInterface();
@@ -127,10 +133,13 @@ namespace Content.Server.GameObjects.Components.Chemistry
{
TryDispense(msg.DispenseIndex);
}
break;
default:
throw new ArgumentOutOfRangeException();
}
ClickSound();
}
/// <summary>
@@ -143,18 +152,15 @@ namespace Content.Server.GameObjects.Components.Chemistry
if (beaker == null)
{
return new ReagentDispenserBoundUserInterfaceState(false, 0, 0,
"", Inventory, Owner.Name, null);
"", Inventory, Owner.Name, null, DispenseAmount);
}
var solution = beaker.GetComponent<SolutionComponent>();
return new ReagentDispenserBoundUserInterfaceState(true, solution.CurrentVolume, solution.MaxVolume,
beaker.Name, Inventory, Owner.Name, solution.ReagentList.ToList());
beaker.Name, Inventory, Owner.Name, solution.ReagentList.ToList(), DispenseAmount);
}
/// <summary>
/// Gets current component data as a <see cref="SharedReagentDispenserComponent.ReagentDispenserBoundUserInterfaceState"/> and sends it to the client.
/// </summary>
public void UpdateUserInterface()
private void UpdateUserInterface()
{
var state = GetUserInterfaceState();
_userInterface.SetState(state);
@@ -163,10 +169,10 @@ namespace Content.Server.GameObjects.Components.Chemistry
/// <summary>
/// If this component contains an entity with a <see cref="SolutionComponent"/>, eject it.
/// </summary>
public void TryEject()
private void TryEject()
{
if (!HasBeaker) return;
_solution.SolutionChanged -= HandleSolutionChangedEvent;
Solution.SolutionChanged -= HandleSolutionChangedEvent;
_beakerContainer.Remove(_beakerContainer.ContainedEntity);
UpdateUserInterface();
@@ -193,7 +199,7 @@ namespace Content.Server.GameObjects.Components.Chemistry
if (!HasBeaker) return;
var solution = _beakerContainer.ContainedEntity.GetComponent<SolutionComponent>();
solution.TryAddReagent(Inventory[dispenseIndex].ID, DispenseAmount, out int acceptedQuantity);
solution.TryAddReagent(Inventory[dispenseIndex].ID, DispenseAmount, out _);
UpdateUserInterface();
}
@@ -202,12 +208,13 @@ namespace Content.Server.GameObjects.Components.Chemistry
/// Called when you click the owner entity with an empty hand. Opens the UI client-side if possible.
/// </summary>
/// <param name="args">Data relevant to the event such as the actor which triggered it.</param>
public void Activate(ActivateEventArgs args)
void IActivate.Activate(ActivateEventArgs args)
{
if (!args.User.TryGetComponent(out IActorComponent actor))
{
return;
}
if (!args.User.TryGetComponent(out IHandsComponent hands))
{
_notifyManager.PopupMessage(Owner.Transform.GridPosition, args.User,
@@ -229,7 +236,7 @@ namespace Content.Server.GameObjects.Components.Chemistry
/// </summary>
/// <param name="args">Data relevant to the event such as the actor which triggered it.</param>
/// <returns></returns>
public bool AttackBy(AttackByEventArgs args)
bool IAttackBy.AttackBy(AttackByEventArgs args)
{
if (!args.User.TryGetComponent(out IHandsComponent hands))
{
@@ -255,7 +262,7 @@ namespace Content.Server.GameObjects.Components.Chemistry
else
{
_beakerContainer.Insert(activeHandEntity);
_solution.SolutionChanged += HandleSolutionChangedEvent;
Solution.SolutionChanged += HandleSolutionChangedEvent;
UpdateUserInterface();
}
}
@@ -268,9 +275,17 @@ namespace Content.Server.GameObjects.Components.Chemistry
return true;
}
void HandleSolutionChangedEvent()
private void HandleSolutionChangedEvent()
{
UpdateUserInterface();
}
private void ClickSound()
{
if (Owner.TryGetComponent(out SoundComponent sound))
{
sound.Play("/Audio/machines/machine_switch.ogg", AudioParams.Default.WithVolume(-2f));
}
}
}
}

View File

@@ -20,7 +20,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry
/// <summary>
/// A list of reagents which this may dispense. Defined in yaml prototype, see <see cref="ReagentDispenserInventoryPrototype"/>.
/// </summary>
public List<ReagentDispenserInventoryEntry> Inventory = new List<ReagentDispenserInventoryEntry>();
protected readonly List<ReagentDispenserInventoryEntry> Inventory = new List<ReagentDispenserInventoryEntry>();
[Serializable, NetSerializable]
public class ReagentDispenserBoundUserInterfaceState : BoundUserInterfaceState
@@ -38,9 +38,10 @@ namespace Content.Shared.GameObjects.Components.Chemistry
/// </summary>
public readonly List<Solution.ReagentQuantity> ContainerReagents;
public readonly string DispenserName;
public readonly int SelectedDispenseAmount;
public ReagentDispenserBoundUserInterfaceState(bool hasBeaker, int beakerCurrentVolume, int beakerMaxVolume, string containerName,
List<ReagentDispenserInventoryEntry> inventory, string dispenserName, List<Solution.ReagentQuantity> containerReagents)
List<ReagentDispenserInventoryEntry> inventory, string dispenserName, List<Solution.ReagentQuantity> containerReagents, int selectedDispenseAmount)
{
HasBeaker = hasBeaker;
BeakerCurrentVolume = beakerCurrentVolume;
@@ -49,6 +50,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry
Inventory = inventory;
DispenserName = dispenserName;
ContainerReagents = containerReagents;
SelectedDispenseAmount = selectedDispenseAmount;
}
}
@@ -98,9 +100,10 @@ namespace Content.Shared.GameObjects.Components.Chemistry
/// Information about a reagent which the dispenser can dispense.
/// </summary>
[Serializable, NetSerializable]
public class ReagentDispenserInventoryEntry
public struct ReagentDispenserInventoryEntry
{
public string ID;
public readonly string ID;
public ReagentDispenserInventoryEntry(string id)
{
ID = id;

View File

@@ -2,31 +2,14 @@
id: booze_dispenser
name: Booze Dispenser
description: A booze dispenser with a single slot for a container to be filled.
parent: reagent_dispenser_base
components:
- type: Sprite
texture: Buildings/booze_dispenser.png
- type: Icon
texture: Buildings/booze_dispenser.png
- type: Clickable
- type: Collidable
shapes:
- !type:PhysShapeAabb
bounds: "-0.4,-0.25,0.4,0.25"
mask: 19
layer: 16
IsScrapingFloor: true
- type: Physics
mass: 25
Anchored: true
- type: SnapGrid
offset: Center
- type: ReagentDispenser
pack: BoozeDispenserInventory
- type: PowerDevice
- type: UserInterface
interfaces:
- key: enum.ReagentDispenserUiKey.Key
type: ReagentDispenserBoundUserInterface
- type: reagentDispenserInventory
id: BoozeDispenserInventory

View File

@@ -1,32 +1,15 @@
- type: entity
id: chem_dispenser
name: Chemical Dispenser
parent: reagent_dispenser_base
description: An industrial grade chemical dispenser with a sizeable chemical supply.
components:
- type: Sprite
texture: Buildings/industrial_dispenser.png
- type: Icon
texture: Buildings/industrial_dispenser.png
- type: Clickable
- type: Collidable
shapes:
- !type:PhysShapeAabb
bounds: "-0.4,-0.25,0.4,0.25"
mask: 19
layer: 16
IsScrapingFloor: true
- type: Physics
mass: 25
Anchored: true
- type: SnapGrid
offset: Center
- type: ReagentDispenser
pack: ChemDispenserStandardInventory
- type: PowerDevice
- type: UserInterface
interfaces:
- key: enum.ReagentDispenserUiKey.Key
type: ReagentDispenserBoundUserInterface
- type: reagentDispenserInventory
id: ChemDispenserStandardInventory

View File

@@ -0,0 +1,25 @@
- type: entity
abstract: true
id: reagent_dispenser_base
components:
- type: Clickable
- type: Collidable
shapes:
- !type:PhysShapeAabb
bounds: "-0.4,-0.25,0.4,0.25"
mask: 19
layer: 16
IsScrapingFloor: true
- type: Physics
mass: 25
Anchored: true
- type: SnapGrid
offset: Center
- type: ReagentDispenser
- type: PowerDevice
- type: UserInterface
interfaces:
- key: enum.ReagentDispenserUiKey.Key
type: ReagentDispenserBoundUserInterface
- type: Sound

View File

@@ -1,32 +1,15 @@
- type: entity
id: soda_dispenser
name: Soda Dispenser
parent: reagent_dispenser_base
description: A beverage dispenser with a selection of soda and several other common beverages. Has a single fill slot for containers.
components:
- type: Sprite
texture: Buildings/soda_dispenser.png
- type: Icon
texture: Buildings/soda_dispenser.png
- type: Clickable
- type: Collidable
shapes:
- !type:PhysShapeAabb
bounds: "-0.4,-0.25,0.4,0.25"
mask: 19
layer: 16
IsScrapingFloor: true
- type: Physics
mass: 25
Anchored: true
- type: SnapGrid
offset: Center
- type: ReagentDispenser
pack: SodaDispenserInventory
- type: PowerDevice
- type: UserInterface
interfaces:
- key: enum.ReagentDispenserUiKey.Key
type: ReagentDispenserBoundUserInterface
- type: reagentDispenserInventory
id: SodaDispenserInventory