Add UI for setting solution transfer amount (#4074)

* basic eui and window

* finish EUI, update defaults

* unnecessary usings

* convert to bounduserinterface

* merge me up

merge me up inside

* Fix repeated define for component in prototype

* impl swept UI suggestion

* apply discord reviews

* small changes
This commit is contained in:
mirrorcult
2021-07-25 00:53:53 -07:00
committed by GitHub
parent 8b6fa75e0e
commit d27164528e
21 changed files with 393 additions and 29 deletions

View File

@@ -0,0 +1,35 @@
using Content.Shared.Chemistry;
using Content.Shared.Chemistry.Reagent;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Shared.GameObjects;
namespace Content.Client.Chemistry.UI
{
[UsedImplicitly]
public class TransferAmountBoundUserInterface : BoundUserInterface
{
private TransferAmountWindow? _window;
protected override void Open()
{
base.Open();
_window = new TransferAmountWindow();
_window.applyButton.OnPressed += _ =>
{
if (int.TryParse(_window.amountLineEdit.Text, out var i))
{
SendMessage(new TransferAmountSetValueMessage(ReagentUnit.New(i)));
_window.Close();
}
};
_window.OpenCentered();
}
public TransferAmountBoundUserInterface(ClientUserInterfaceComponent owner, object uiKey) : base(owner, uiKey)
{
}
}
}

View File

@@ -0,0 +1,11 @@
<SS14Window xmlns="https://spacestation14.io"
Title="{Loc 'ui-transfer-amount-title'}"
Resizable="False">
<VBoxContainer SeparationOverride="4" CustomMinimumSize="240 80">
<HBoxContainer>
<LineEdit Name="AmountLineEdit" HorizontalExpand="True" PlaceHolder="{Loc 'ui-transfer-amount-line-edit-placeholder'}"/>
</HBoxContainer>
<Button Name="ApplyButton" Text="{Loc 'ui-transfer-amount-apply'}"/>
</VBoxContainer>
</SS14Window>

View File

@@ -0,0 +1,19 @@
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
namespace Content.Client.Chemistry.UI
{
[GenerateTypedNameReferences]
public partial class TransferAmountWindow : SS14Window
{
public Button applyButton => ApplyButton;
public LineEdit amountLineEdit => AmountLineEdit;
public TransferAmountWindow()
{
RobustXamlLoader.Load(this);
}
}
}

View File

@@ -1,9 +1,15 @@
using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Content.Server.UserInterface;
using Content.Shared.ActionBlocker;
using Content.Shared.Chemistry;
using Content.Shared.Chemistry.Reagent; using Content.Shared.Chemistry.Reagent;
using Content.Shared.Chemistry.Solution.Components; using Content.Shared.Chemistry.Solution.Components;
using Content.Shared.Interaction; using Content.Shared.Interaction;
using Content.Shared.Interaction.Helpers; using Content.Shared.Interaction.Helpers;
using Content.Shared.Notification.Managers; using Content.Shared.Notification.Managers;
using Content.Shared.Verbs;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.Localization; using Robust.Shared.Localization;
using Robust.Shared.Serialization.Manager.Attributes; using Robust.Shared.Serialization.Manager.Attributes;
@@ -31,6 +37,35 @@ namespace Content.Server.Chemistry.Components
[ViewVariables(VVAccess.ReadWrite)] [ViewVariables(VVAccess.ReadWrite)]
public ReagentUnit TransferAmount { get; set; } = ReagentUnit.New(5); public ReagentUnit TransferAmount { get; set; } = ReagentUnit.New(5);
/// <summary>
/// The minimum amount of solution that can be transferred at once from this solution.
/// </summary>
[DataField("minTransferAmount")]
[ViewVariables(VVAccess.ReadWrite)]
public ReagentUnit MinimumTransferAmount { get; set; } = ReagentUnit.New(5);
/// <summary>
/// The maximum amount of solution that can be transferred at once from this solution.
/// </summary>
[DataField("maxTransferAmount")]
[ViewVariables(VVAccess.ReadWrite)]
public ReagentUnit MaximumTransferAmount { get; set; } = ReagentUnit.New(50);
/// <summary>
/// Subjectively, which transfer amount would be best for most activities given the maximum
/// transfer amount.
/// </summary>
public ReagentUnit SubjectiveBestTransferAmount() =>
MaximumTransferAmount.Int() switch
{
<= 5 => ReagentUnit.New(1),
(> 5) and (<= 25) => ReagentUnit.New(5),
(> 25) and (<= 50) => ReagentUnit.New(10),
(> 50) and (<= 100) => ReagentUnit.New(20),
(> 100) and (<= 500) => ReagentUnit.New(50),
(> 500) => ReagentUnit.New(100)
};
/// <summary> /// <summary>
/// Can this entity take reagent from reagent tanks? /// Can this entity take reagent from reagent tanks?
/// </summary> /// </summary>
@@ -45,6 +80,46 @@ namespace Content.Server.Chemistry.Components
[ViewVariables(VVAccess.ReadWrite)] [ViewVariables(VVAccess.ReadWrite)]
public bool CanSend { get; set; } = true; public bool CanSend { get; set; } = true;
/// <summary>
/// Whether you're allowed to change the transfer amount.
/// </summary>
[DataField("canChangeTransferAmount")]
[ViewVariables(VVAccess.ReadWrite)]
public bool CanChangeTransferAmount { get; set; } = false;
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(TransferAmountUiKey.Key);
protected override void Initialize()
{
base.Initialize();
if (UserInterface != null)
{
UserInterface.OnReceiveMessage += UserInterfaceOnReceiveMessage;
}
}
public void UserInterfaceOnReceiveMessage(ServerBoundUserInterfaceMessage serverMsg)
{
switch (serverMsg.Message)
{
case TransferAmountSetValueMessage svm:
var sval = svm.Value.Float();
var amount = Math.Clamp(sval, MinimumTransferAmount.Float(),
MaximumTransferAmount.Float());
serverMsg.Session.AttachedEntity?.PopupMessage(Loc.GetString("comp-solution-transfer-set-amount", ("amount", amount)));
SetTransferAmount(ReagentUnit.New(amount));
break;
}
}
public void SetTransferAmount(ReagentUnit amount)
{
amount = ReagentUnit.New(Math.Clamp(amount.Int(), MinimumTransferAmount.Int(), MaximumTransferAmount.Int()));
TransferAmount = amount;
}
async Task<bool> IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs) async Task<bool> IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs)
{ {
if (!eventArgs.InRangeUnobstructed() || eventArgs.Target == null) if (!eventArgs.InRangeUnobstructed() || eventArgs.Target == null)
@@ -67,8 +142,8 @@ namespace Content.Server.Chemistry.Components
{ {
var toTheBrim = ownerSolution.RefillSpaceAvailable == 0; var toTheBrim = ownerSolution.RefillSpaceAvailable == 0;
var msg = toTheBrim var msg = toTheBrim
? "solution-transfer-component-fill-to-brim-message" ? "comp-solution-transfer-fill-fully"
: "solution-transfer-component-fill--message"; : "comp-solution-transfer-fill-normal";
target.PopupMessage(eventArgs.User, Loc.GetString(msg,("owner", Owner),("amount", transferred),("target", target))); target.PopupMessage(eventArgs.User, Loc.GetString(msg,("owner", Owner),("amount", transferred),("target", target)));
return true; return true;
@@ -82,7 +157,7 @@ namespace Content.Server.Chemistry.Components
if (transferred > 0) if (transferred > 0)
{ {
Owner.PopupMessage(eventArgs.User, Owner.PopupMessage(eventArgs.User,
Loc.GetString("solution-transfer-component-transfer-success-message", Loc.GetString("comp-solution-transfer-transfer-solution",
("amount",transferred), ("amount",transferred),
("target",target))); ("target",target)));
@@ -102,13 +177,13 @@ namespace Content.Server.Chemistry.Components
{ {
if (source.DrainAvailable == 0) if (source.DrainAvailable == 0)
{ {
source.Owner.PopupMessage(user, Loc.GetString("solution-transfer-component-do-transfer-component-is-empty", ("entity",source.Owner))); source.Owner.PopupMessage(user, Loc.GetString("comp-solution-transfer-is-empty", ("target", source.Owner)));
return ReagentUnit.Zero; return ReagentUnit.Zero;
} }
if (target.RefillSpaceAvailable == 0) if (target.RefillSpaceAvailable == 0)
{ {
target.Owner.PopupMessage(user, Loc.GetString("solution-transfer-component-do-transfer-component-is-full", ("entity", target.Owner))); target.Owner.PopupMessage(user, Loc.GetString("comp-solution-transfer-is-full", ("target", target.Owner)));
return ReagentUnit.Zero; return ReagentUnit.Zero;
} }
@@ -120,5 +195,114 @@ namespace Content.Server.Chemistry.Components
return actualAmount; return actualAmount;
} }
// TODO refactor when dynamic verbs are a thing
[Verb]
public sealed class MinimumTransferVerb : Verb<SolutionTransferComponent>
{
protected override void GetData(IEntity user, SolutionTransferComponent component, VerbData data)
{
if (!EntitySystem.Get<ActionBlockerSystem>().CanInteract(user) || !component.CanChangeTransferAmount)
{
data.Visibility = VerbVisibility.Invisible;
return;
}
data.Visibility = VerbVisibility.Visible;
data.Text = Loc.GetString("comp-solution-transfer-verb-transfer-amount-min",
("amount", component.MinimumTransferAmount.Int()));
data.CategoryData = VerbCategories.SetTransferAmount;
}
protected override void Activate(IEntity user, SolutionTransferComponent component)
{
component.TransferAmount = component.MinimumTransferAmount;
user.PopupMessage(Loc.GetString("comp-solution-transfer-set-amount",
("amount", component.TransferAmount.Int())));
}
}
[Verb]
public sealed class DefaultTransferVerb : Verb<SolutionTransferComponent>
{
protected override void GetData(IEntity user, SolutionTransferComponent component, VerbData data)
{
if (!EntitySystem.Get<ActionBlockerSystem>().CanInteract(user) || !component.CanChangeTransferAmount)
{
data.Visibility = VerbVisibility.Invisible;
return;
}
var amt = component.SubjectiveBestTransferAmount();
if (amt > component.MinimumTransferAmount && amt < component.MaximumTransferAmount)
{
data.Visibility = VerbVisibility.Visible;
data.Text = Loc.GetString("comp-solution-transfer-verb-transfer-amount-ideal",
("amount", amt.Int()));
data.CategoryData = VerbCategories.SetTransferAmount;
}
else
{
data.Visibility = VerbVisibility.Invisible;
}
}
protected override void Activate(IEntity user, SolutionTransferComponent component)
{
component.TransferAmount = component.SubjectiveBestTransferAmount();
user.PopupMessage(Loc.GetString("comp-solution-transfer-set-amount", ("amount", component.TransferAmount.Int())));
}
}
[Verb]
public sealed class MaximumTransferVerb : Verb<SolutionTransferComponent>
{
protected override void GetData(IEntity user, SolutionTransferComponent component, VerbData data)
{
if (!EntitySystem.Get<ActionBlockerSystem>().CanInteract(user) || !component.CanChangeTransferAmount)
{
data.Visibility = VerbVisibility.Invisible;
return;
}
data.Visibility = VerbVisibility.Visible;
data.Text = Loc.GetString("comp-solution-transfer-verb-transfer-amount-max",
("amount", component.MaximumTransferAmount));
data.CategoryData = VerbCategories.SetTransferAmount;
}
protected override void Activate(IEntity user, SolutionTransferComponent component)
{
component.TransferAmount = component.MaximumTransferAmount;
user.PopupMessage(Loc.GetString("comp-solution-transfer-set-amount", ("amount", component.TransferAmount.Int())));
}
}
[Verb]
public sealed class CustomTransferVerb : Verb<SolutionTransferComponent>
{
protected override void GetData(IEntity user, SolutionTransferComponent component, VerbData data)
{
if (!EntitySystem.Get<ActionBlockerSystem>().CanInteract(user) || !component.CanChangeTransferAmount)
{
data.Visibility = VerbVisibility.Invisible;
return;
}
data.Visibility = VerbVisibility.Visible;
data.Text = Loc.GetString("comp-solution-transfer-verb-transfer-amount-custom");
data.CategoryData = VerbCategories.SetTransferAmount;
}
protected override void Activate(IEntity user, SolutionTransferComponent component)
{
if (!user.TryGetComponent<ActorComponent>(out var actor))
{
return;
}
component.UserInterface?.Open(actor.PlayerSession);
}
}
} }
} }

View File

@@ -0,0 +1,37 @@
using System;
using Content.Shared.Chemistry.Reagent;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
namespace Content.Shared.Chemistry
{
[Serializable, NetSerializable]
public class TransferAmountBoundInterfaceState : BoundUserInterfaceState
{
public ReagentUnit Max;
public ReagentUnit Min;
public TransferAmountBoundInterfaceState(ReagentUnit max, ReagentUnit min)
{
Max = max;
Min = min;
}
}
[Serializable, NetSerializable]
public class TransferAmountSetValueMessage : BoundUserInterfaceMessage
{
public ReagentUnit Value;
public TransferAmountSetValueMessage(ReagentUnit value)
{
Value = value;
}
}
[Serializable, NetSerializable]
public enum TransferAmountUiKey
{
Key,
}
}

View File

@@ -11,5 +11,7 @@ namespace Content.Shared.Verbs
public static readonly VerbCategoryData Rotate = ("Rotate", null); public static readonly VerbCategoryData Rotate = ("Rotate", null);
public static readonly VerbCategoryData Construction = ("Construction", null); public static readonly VerbCategoryData Construction = ("Construction", null);
public static readonly VerbCategoryData SetTransferAmount =
("Set Transfer Amount", "/Textures/Interface/VerbIcons/spill.svg.192dpi.png");
} }
} }

View File

@@ -1,5 +1,18 @@
solution-transfer-component-fill-to-brim-message = You fill {$owner} to the brim with {$amount}u from {$target} ### Solution transfer component
solution-transfer-component-fill--message = You fill {$owner} with {$amount}u from {$target}
solution-transfer-component-transfer-success-message = You transfer {$amount}u to {$target}. comp-solution-transfer-fill-normal = You fill {THE($target)} with {$amount}u from {THE($owner)}.
solution-transfer-component-do-transfer-component-is-empty = {$entity} is empty! comp-solution-transfer-fill-fully = You fill {THE($target)} to the brim with {$amount}u from {THE($owner)}.
solution-transfer-component-do-transfer-component-is-full = {$entity} is full! comp-solution-transfer-transfer-solution = You transfer {$amount}u to {THE($target)}.
## Displayed when trying to transfer to a solution, but either the giver is empty or the taker is full
comp-solution-transfer-is-empty = {THE($target)} is empty!
comp-solution-transfer-is-full = {THE($target)} is full!
## Displayed in change transfer amount verb's name
comp-solution-transfer-verb-transfer-amount-min = {$amount}u
comp-solution-transfer-verb-transfer-amount-max = {$amount}u
comp-solution-transfer-verb-transfer-amount-ideal = {$amount}u
comp-solution-transfer-verb-transfer-amount-custom = Custom
## Displayed after you successfully change a solution's amount using the BUI
comp-solution-transfer-set-amount = Transfer amount set to {$amount}u.

View File

@@ -0,0 +1,13 @@
### Loc for the transfer amount eui window
## Title
ui-transfer-amount-title = Change Transfer Amount
## Text for the button to apply changes
ui-transfer-amount-apply = Set Amount
## Placeholder text for the amount line edit
ui-transfer-amount-line-edit-placeholder = Amount

View File

@@ -14,7 +14,7 @@
- type: SolutionContainer - type: SolutionContainer
maxVol: 10 maxVol: 10
- type: SolutionTransfer - type: SolutionTransfer
transferAmount: 5 maxTransferAmount: 10
- type: Sprite - type: Sprite
netsync: false netsync: false
sprite: Objects/Consumable/Food/condiments.rsi sprite: Objects/Consumable/Food/condiments.rsi
@@ -313,7 +313,7 @@
- type: SolutionContainer - type: SolutionContainer
maxVol: 30 maxVol: 30
- type: SolutionTransfer - type: SolutionTransfer
transferAmount: 5 maxTransferAmount: 30
- type: Sprite - type: Sprite
netsync: false netsync: false
sprite: Objects/Consumable/Food/condiments.rsi sprite: Objects/Consumable/Food/condiments.rsi
@@ -448,7 +448,7 @@
- type: SolutionContainer - type: SolutionContainer
maxVol: 15 maxVol: 15
- type: SolutionTransfer - type: SolutionTransfer
transferAmount: 5 maxTransferAmount: 15
- type: Sprite - type: Sprite
netsync: false netsync: false
sprite: Objects/Consumable/Food/condiments.rsi sprite: Objects/Consumable/Food/condiments.rsi

View File

@@ -27,7 +27,6 @@
- type: SolutionContainer - type: SolutionContainer
maxVol: 50 maxVol: 50
- type: SolutionTransfer - type: SolutionTransfer
transferAmount: 5
- type: Drink - type: Drink
openSounds: packetOpenSounds openSounds: packetOpenSounds
useSound: /Audio/Items/eating_1.ogg useSound: /Audio/Items/eating_1.ogg

View File

@@ -9,11 +9,15 @@
- type: SolutionContainer - type: SolutionContainer
maxVol: 50 maxVol: 50
- type: SolutionTransfer - type: SolutionTransfer
transferAmount: 5 canChangeTransferAmount: true
- type: Drink - type: Drink
- type: Sprite - type: Sprite
state: icon state: icon
- type: Spillable - type: Spillable
- type: UserInterface
interfaces:
- key: enum.TransferAmountUiKey.Key
type: TransferAmountBoundUserInterface
- type: entity - type: entity
parent: DrinkBase parent: DrinkBase
@@ -56,8 +60,12 @@
- type: SolutionContainer - type: SolutionContainer
maxVol: 50 maxVol: 50
- type: SolutionTransfer - type: SolutionTransfer
transferAmount: 5 canChangeTransferAmount: true
- type: TransformableContainer - type: TransformableContainer
- type: UserInterface
interfaces:
- key: enum.TransferAmountUiKey.Key
type: TransferAmountBoundUserInterface
- type: entity - type: entity
parent: DrinkGlassBase parent: DrinkGlassBase

View File

@@ -8,7 +8,11 @@
- type: SolutionContainer - type: SolutionContainer
maxVol: 100 maxVol: 100
- type: SolutionTransfer - type: SolutionTransfer
transferAmount: 5 canChangeTransferAmount: true
- type: UserInterface
interfaces:
- key: enum.TransferAmountUiKey.Key
type: TransferAmountBoundUserInterface
- type: Sprite - type: Sprite
state: icon state: icon
- type: DamageOnLand - type: DamageOnLand

View File

@@ -14,7 +14,12 @@
- ReagentId: Cola - ReagentId: Cola
Quantity: 20 Quantity: 20
- type: SolutionTransfer - type: SolutionTransfer
transferAmount: 5 canChangeTransferAmount: true
maxTransferAmount: 10
- type: UserInterface
interfaces:
- key: enum.TransferAmountUiKey.Key
type: TransferAmountBoundUserInterface
- type: Sprite - type: Sprite
state: icon state: icon

View File

@@ -8,7 +8,12 @@
- type: SolutionContainer - type: SolutionContainer
maxVol: 20 maxVol: 20
- type: SolutionTransfer - type: SolutionTransfer
transferAmount: 5 canChangeTransferAmount: true
maxTransferAmount: 10
- type: UserInterface
interfaces:
- key: enum.TransferAmountUiKey.Key
type: TransferAmountBoundUserInterface
- type: Drink - type: Drink
isOpen: true isOpen: true
- type: Sprite - type: Sprite

View File

@@ -9,14 +9,19 @@
- type: LoopingSound - type: LoopingSound
- type: Sprite - type: Sprite
state: icon state: icon
- type: SolutionContainer - type: SolutionContainer
maxVol: 10 maxVol: 10
- type: SolutionTransfer - type: SolutionTransfer
transferAmount: 5 canChangeTransferAmount: true
maxTransferAmount: 5
- type: Drink - type: Drink
isOpen: true isOpen: true
- type: Spillable - type: Spillable
- type: UserInterface
interfaces:
- key: enum.TransferAmountUiKey.Key
type: TransferAmountBoundUserInterface
# Containers # Containers

View File

@@ -18,7 +18,6 @@
maxVol: 100 maxVol: 100
caps: Refillable, Drainable caps: Refillable, Drainable
- type: SolutionTransfer - type: SolutionTransfer
transferAmount: 5.0
- type: Spillable - type: Spillable
- type: ItemCooldown - type: ItemCooldown
- type: Spray - type: Spray

View File

@@ -18,7 +18,12 @@
maxVol: 30 maxVol: 30
caps: OpenContainer caps: OpenContainer
- type: SolutionTransfer - type: SolutionTransfer
transferAmount: 5 maxTransferAmount: 30
canChangeTransferAmount: true
- type: UserInterface
interfaces:
- key: enum.TransferAmountUiKey.Key
type: TransferAmountBoundUserInterface
- type: Item - type: Item
sprite: Objects/Specific/Chemistry/beaker.rsi sprite: Objects/Specific/Chemistry/beaker.rsi
- type: Spillable - type: Spillable

View File

@@ -21,7 +21,11 @@
maxVol: 50 maxVol: 50
caps: OpenContainer, FitsInDispenser # can add and remove solutions and fits in the chemmaster. caps: OpenContainer, FitsInDispenser # can add and remove solutions and fits in the chemmaster.
- type: SolutionTransfer - type: SolutionTransfer
transferAmount: 5.0 canChangeTransferAmount: true
- type: UserInterface
interfaces:
- key: enum.TransferAmountUiKey.Key
type: TransferAmountBoundUserInterface
- type: Spillable - type: Spillable
- type: Drink - type: Drink
isOpen: true isOpen: true
@@ -120,7 +124,14 @@
caps: OpenContainer caps: OpenContainer
maxVol: 5 maxVol: 5
- type: SolutionTransfer - type: SolutionTransfer
transferAmount: 5.0 minTransferAmount: 1
transferAmount: 1
maxTransferAmount: 5
canChangeTransferAmount: true
- type: UserInterface
interfaces:
- key: enum.TransferAmountUiKey.Key
type: TransferAmountBoundUserInterface
- type: Spillable - type: Spillable
- type: Item - type: Item
sprite: Objects/Specific/Chemistry/dropper.rsi sprite: Objects/Specific/Chemistry/dropper.rsi

View File

@@ -20,6 +20,13 @@
maxVol: 500 maxVol: 500
- type: SolutionTransfer - type: SolutionTransfer
transferAmount: 50 transferAmount: 50
maxTransferAmount: 100
minTransferAmount: 10
canChangeTransferAmount: true
- type: UserInterface
interfaces:
- key: enum.TransferAmountUiKey.Key
type: TransferAmountBoundUserInterface
- type: Physics - type: Physics
bodyType: Dynamic bodyType: Dynamic
fixtures: fixtures:

View File

@@ -101,3 +101,4 @@
maxVol: 15 maxVol: 15
caps: Refillable, Drainable caps: Refillable, Drainable
- type: SolutionTransfer - type: SolutionTransfer
maxTransferAmount: 15

View File

@@ -27,6 +27,7 @@
caps: Refillable caps: Refillable
maxVol: 5 maxVol: 5
- type: SolutionTransfer - type: SolutionTransfer
maxTransferAmount: 5
- type: MeleeWeaponAnimation - type: MeleeWeaponAnimation
id: spear id: spear