Adds a UI for gas mixers (#5165)

Co-authored-by: E F R <602406+Efruit@users.noreply.github.com>
Co-authored-by: Paul Ritter <ritter.paul1@googlemail.com>
Co-authored-by: ike709 <ike709@github.com>
This commit is contained in:
ike709
2021-11-11 16:10:21 -06:00
committed by GitHub
parent c6615fd805
commit c06f52a456
12 changed files with 401 additions and 6 deletions

View File

@@ -0,0 +1,91 @@
using System;
using Content.Client.Atmos.EntitySystems;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Piping.Binary.Components;
using Content.Shared.Atmos.Piping.Trinary.Components;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Shared.GameObjects;
namespace Content.Client.Atmos.UI
{
/// <summary>
/// Initializes a <see cref="GasMixerWindow"/> and updates it when new server messages are received.
/// </summary>
[UsedImplicitly]
public class GasMixerBoundUserInterface : BoundUserInterface
{
private GasMixerWindow? _window;
private const float MaxPressure = Atmospherics.MaxOutputPressure;
public GasMixerBoundUserInterface(ClientUserInterfaceComponent owner, object uiKey) : base(owner, uiKey)
{
}
protected override void Open()
{
base.Open();
_window = new GasMixerWindow();
if(State != null)
UpdateState(State);
_window.OpenCentered();
_window.OnClose += Close;
_window.ToggleStatusButtonPressed += OnToggleStatusButtonPressed;
_window.MixerOutputPressureChanged += OnMixerOutputPressurePressed;
_window.MixerNodePercentageChanged += OnMixerSetPercentagePressed;
}
private void OnToggleStatusButtonPressed()
{
if (_window is null) return;
SendMessage(new GasMixerToggleStatusMessage(_window.MixerStatus));
}
private void OnMixerOutputPressurePressed(string value)
{
float pressure = float.TryParse(value, out var parsed) ? parsed : 0f;
if (pressure > MaxPressure) pressure = MaxPressure;
SendMessage(new GasMixerChangeOutputPressureMessage(pressure));
}
private void OnMixerSetPercentagePressed(string value)
{
// We don't need to send both nodes because it's just 1.0f - node
float node = float.TryParse(value, out var parsed) ? parsed : 1.0f;
if(_window is not null) node = _window.NodeOneLastEdited ? node : 1.0f - node;
SendMessage(new GasMixerChangeNodePercentageMessage(node));
}
/// <summary>
/// Update the UI state based on server-sent info
/// </summary>
/// <param name="state"></param>
protected override void UpdateState(BoundUserInterfaceState state)
{
base.UpdateState(state);
if (_window == null || state is not GasMixerBoundUserInterfaceState cast)
return;
_window.Title = (cast.MixerLabel);
_window.SetMixerStatus(cast.Enabled);
_window.SetOutputPressure(cast.OutputPressure);
_window.SetNodePercentages(cast.NodeOne);
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (!disposing) return;
_window?.Dispose();
}
}
}

View File

@@ -0,0 +1,38 @@
<SS14Window xmlns="https://spacestation14.io"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:Content.Client.Stylesheets"
MinSize="200 200" Title="Gas Mixer">
<BoxContainer Orientation="Vertical" Margin="5 5 5 5" SeparationOverride="10">
<BoxContainer Orientation="Horizontal" HorizontalExpand="True">
<Label Text="{Loc comp-gas-mixer-ui-mixer-status} "/>
<Control MinSize="5 0" />
<Button Name="ToggleStatusButton"/>
</BoxContainer>
<BoxContainer Orientation="Horizontal" HorizontalExpand="True">
<Label Text="{Loc comp-gas-mixer-ui-mixer-output-pressure} "/>
<Control MinSize="5 0" />
<LineEdit Name="MixerPressureOutputInput" MinSize="60 0" />
<Control MinSize="5 0" />
<Button Name="SetMaxPressureButton" Text="{Loc comp-gas-mixer-ui-mixer-max}" />
<Control MinSize="5 0" />
<Control HorizontalExpand="True" />
<Button Name="SetOutputPressureButton" Text="{Loc comp-gas-mixer-ui-mixer-set}" HorizontalAlignment="Right" Disabled="True"/>
<Control MinSize="0 5" />
</BoxContainer>
<BoxContainer Orientation="Horizontal" HorizontalExpand="True">
<Control MinSize="0 5" />
<Label Text="{Loc comp-gas-mixer-ui-mixer-node-primary} "/>
<Control MinSize="5 0" />
<LineEdit Name="MixerNodeOneInput" MinSize="60 0" />
<Label Text=" %"/>
<Control MinSize="5 0" />
<Label Text="{Loc comp-gas-mixer-ui-mixer-node-side} "/>
<Control MinSize="5 0" />
<LineEdit Name="MixerNodeTwoInput" MinSize="60 0" />
<Label Text=" %"/>
</BoxContainer>
<BoxContainer Orientation="Horizontal" HorizontalExpand="True">
<Button Name="SetMixerPercentageButton" Text="{Loc comp-gas-mixer-ui-mixer-set}" HorizontalAlignment="Right" Disabled="True"/>
</BoxContainer>
</BoxContainer>
</SS14Window>

View File

@@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using Content.Client.Atmos.EntitySystems;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Prototypes;
using Robust.Client.AutoGenerated;
using Robust.Client.Graphics;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Localization;
using Robust.Shared.Maths;
namespace Content.Client.Atmos.UI
{
/// <summary>
/// Client-side UI used to control a gas mixer.
/// </summary>
[GenerateTypedNameReferences]
public partial class GasMixerWindow : SS14Window
{
public bool MixerStatus = true;
public event Action? ToggleStatusButtonPressed;
public event Action<string>? MixerOutputPressureChanged;
public event Action<string>? MixerNodePercentageChanged;
public bool NodeOneLastEdited = true;
public GasMixerWindow()
{
RobustXamlLoader.Load(this);
ToggleStatusButton.OnPressed += _ => SetMixerStatus(!MixerStatus);
ToggleStatusButton.OnPressed += _ => ToggleStatusButtonPressed?.Invoke();
MixerPressureOutputInput.OnTextChanged += _ => SetOutputPressureButton.Disabled = false;
SetOutputPressureButton.OnPressed += _ =>
{
MixerOutputPressureChanged?.Invoke(MixerPressureOutputInput.Text ??= "");
SetOutputPressureButton.Disabled = true;
};
SetMaxPressureButton.OnPressed += _ =>
{
MixerPressureOutputInput.Text = Atmospherics.MaxOutputPressure.ToString(CultureInfo.InvariantCulture);
SetOutputPressureButton.Disabled = false;
};
MixerNodeOneInput.OnTextChanged += _ =>
{
SetMixerPercentageButton.Disabled = false;
NodeOneLastEdited = true;
};
MixerNodeTwoInput.OnTextChanged += _ =>
{
SetMixerPercentageButton.Disabled = false;
NodeOneLastEdited = false;
};
SetMixerPercentageButton.OnPressed += _ =>
{
MixerNodePercentageChanged?.Invoke(NodeOneLastEdited ? MixerNodeOneInput.Text ??= "" : MixerNodeTwoInput.Text ??= "");
SetMixerPercentageButton.Disabled = true;
};
}
public void SetOutputPressure(float pressure)
{
MixerPressureOutputInput.Text = pressure.ToString(CultureInfo.InvariantCulture);
}
public void SetNodePercentages(float nodeOne)
{
nodeOne *= 100.0f;
MixerNodeOneInput.Text = nodeOne.ToString(CultureInfo.InvariantCulture);
float nodeTwo = 100.0f - nodeOne;
MixerNodeTwoInput.Text = nodeTwo.ToString(CultureInfo.InvariantCulture);
}
public void SetMixerStatus(bool enabled)
{
MixerStatus = enabled;
if (enabled)
{
ToggleStatusButton.Text = Loc.GetString("comp-gas-mixer-ui-status-enabled");
}
else
{
ToggleStatusButton.Text = Loc.GetString("comp-gas-mixer-ui-status-disabled");
}
}
}
}

View File

@@ -8,6 +8,7 @@ using Content.Shared.Atmos.Piping;
using Content.Shared.Atmos.Piping.Binary.Components; using Content.Shared.Atmos.Piping.Binary.Components;
using Content.Shared.Examine; using Content.Shared.Examine;
using Content.Shared.Interaction; using Content.Shared.Interaction;
using Content.Shared.Popups;
using JetBrains.Annotations; using JetBrains.Annotations;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
@@ -94,8 +95,15 @@ namespace Content.Server.Atmos.Piping.Binary.EntitySystems
if (!args.User.TryGetComponent(out ActorComponent? actor)) if (!args.User.TryGetComponent(out ActorComponent? actor))
return; return;
_userInterfaceSystem.TryOpen(uid, GasPressurePumpUiKey.Key, actor.PlayerSession); if (component.Owner.Transform.Anchored)
DirtyUI(uid, component); {
_userInterfaceSystem.TryOpen(uid, GasPressurePumpUiKey.Key, actor.PlayerSession);
DirtyUI(uid, component);
}
else
{
args.User.PopupMessageCursor(Loc.GetString("comp-gas-pump-ui-needs-anchor"));
}
args.Handled = true; args.Handled = true;
} }

View File

@@ -8,6 +8,7 @@ using Content.Shared.Atmos;
using Content.Shared.Atmos.Piping.Binary.Components; using Content.Shared.Atmos.Piping.Binary.Components;
using Content.Shared.Examine; using Content.Shared.Examine;
using Content.Shared.Interaction; using Content.Shared.Interaction;
using Content.Shared.Popups;
using JetBrains.Annotations; using JetBrains.Annotations;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
@@ -99,8 +100,15 @@ namespace Content.Server.Atmos.Piping.Binary.EntitySystems
if (!args.User.TryGetComponent(out ActorComponent? actor)) if (!args.User.TryGetComponent(out ActorComponent? actor))
return; return;
_userInterfaceSystem.TryOpen(uid, GasVolumePumpUiKey.Key, actor.PlayerSession); if (component.Owner.Transform.Anchored)
DirtyUI(uid, component); {
_userInterfaceSystem.TryOpen(uid, GasVolumePumpUiKey.Key, actor.PlayerSession);
DirtyUI(uid, component);
}
else
{
args.User.PopupMessageCursor(Loc.GetString("comp-gas-pump-ui-needs-anchor"));
}
args.Handled = true; args.Handled = true;
} }

View File

@@ -8,10 +8,12 @@ using Content.Shared.Atmos;
using Content.Shared.Atmos.Piping; using Content.Shared.Atmos.Piping;
using Content.Shared.Atmos.Piping.Trinary.Components; using Content.Shared.Atmos.Piping.Trinary.Components;
using Content.Shared.Interaction; using Content.Shared.Interaction;
using Content.Shared.Popups;
using JetBrains.Annotations; using JetBrains.Annotations;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.IoC; using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Timing; using Robust.Shared.Timing;
namespace Content.Server.Atmos.Piping.Trinary.EntitySystems namespace Content.Server.Atmos.Piping.Trinary.EntitySystems
@@ -82,8 +84,15 @@ namespace Content.Server.Atmos.Piping.Trinary.EntitySystems
if (!args.User.TryGetComponent(out ActorComponent? actor)) if (!args.User.TryGetComponent(out ActorComponent? actor))
return; return;
_userInterfaceSystem.TryOpen(uid, GasFilterUiKey.Key, actor.PlayerSession); if (component.Owner.Transform.Anchored)
DirtyUI(uid, component); {
_userInterfaceSystem.TryOpen(uid, GasFilterUiKey.Key, actor.PlayerSession);
DirtyUI(uid, component);
}
else
{
args.User.PopupMessageCursor(Loc.GetString("comp-gas-filter-ui-needs-anchor"));
}
args.Handled = true; args.Handled = true;
} }

View File

@@ -4,19 +4,31 @@ using Content.Server.Atmos.Piping.Trinary.Components;
using Content.Server.NodeContainer; using Content.Server.NodeContainer;
using Content.Server.NodeContainer.Nodes; using Content.Server.NodeContainer.Nodes;
using Content.Shared.Atmos; using Content.Shared.Atmos;
using Content.Shared.Atmos.Piping.Trinary.Components;
using Content.Shared.Interaction;
using Content.Shared.Popups;
using JetBrains.Annotations; using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
namespace Content.Server.Atmos.Piping.Trinary.EntitySystems namespace Content.Server.Atmos.Piping.Trinary.EntitySystems
{ {
[UsedImplicitly] [UsedImplicitly]
public class GasMixerSystem : EntitySystem public class GasMixerSystem : EntitySystem
{ {
[Dependency] private UserInterfaceSystem _userInterfaceSystem = default!;
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
SubscribeLocalEvent<GasMixerComponent, AtmosDeviceUpdateEvent>(OnMixerUpdated); SubscribeLocalEvent<GasMixerComponent, AtmosDeviceUpdateEvent>(OnMixerUpdated);
SubscribeLocalEvent<GasMixerComponent, InteractHandEvent>(OnMixerInteractHand);
// Bound UI subscriptions
SubscribeLocalEvent<GasMixerComponent, GasMixerChangeOutputPressureMessage>(OnOutputPressureChangeMessage);
SubscribeLocalEvent<GasMixerComponent, GasMixerChangeNodePercentageMessage>(OnChangeNodePercentageMessage);
SubscribeLocalEvent<GasMixerComponent, GasMixerToggleStatusMessage>(OnToggleStatusMessage);
} }
private void OnMixerUpdated(EntityUid uid, GasMixerComponent mixer, AtmosDeviceUpdateEvent args) private void OnMixerUpdated(EntityUid uid, GasMixerComponent mixer, AtmosDeviceUpdateEvent args)
@@ -91,5 +103,53 @@ namespace Content.Server.Atmos.Piping.Trinary.EntitySystems
outlet.AssumeAir(removed); outlet.AssumeAir(removed);
} }
} }
private void OnMixerInteractHand(EntityUid uid, GasMixerComponent component, InteractHandEvent args)
{
if (!args.User.TryGetComponent(out ActorComponent? actor))
return;
if (component.Owner.Transform.Anchored)
{
_userInterfaceSystem.TryOpen(uid, GasMixerUiKey.Key, actor.PlayerSession);
DirtyUI(uid, component);
}
else
{
args.User.PopupMessageCursor(Loc.GetString("comp-gas-mixer-ui-needs-anchor"));
}
args.Handled = true;
}
private void DirtyUI(EntityUid uid, GasMixerComponent? mixer)
{
if (!Resolve(uid, ref mixer))
return;
_userInterfaceSystem.TrySetUiState(uid, GasMixerUiKey.Key,
new GasMixerBoundUserInterfaceState(mixer.Owner.Name, mixer.TargetPressure, mixer.Enabled, mixer.InletOneConcentration));
}
private void OnToggleStatusMessage(EntityUid uid, GasMixerComponent mixer, GasMixerToggleStatusMessage args)
{
mixer.Enabled = args.Enabled;
DirtyUI(uid, mixer);
}
private void OnOutputPressureChangeMessage(EntityUid uid, GasMixerComponent mixer, GasMixerChangeOutputPressureMessage args)
{
mixer.TargetPressure = Math.Clamp(args.Pressure, 0f, Atmospherics.MaxOutputPressure);
DirtyUI(uid, mixer);
}
private void OnChangeNodePercentageMessage(EntityUid uid, GasMixerComponent mixer,
GasMixerChangeNodePercentageMessage args)
{
float nodeOne = Math.Clamp(args.NodeOne, 0f, 100.0f) / 100.0f;
mixer.InletOneConcentration = nodeOne;
mixer.InletTwoConcentration = 1.0f - mixer.InletOneConcentration;
DirtyUI(uid, mixer);
}
} }
} }

View File

@@ -0,0 +1,63 @@
using System;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
namespace Content.Shared.Atmos.Piping.Trinary.Components
{
[Serializable, NetSerializable]
public enum GasMixerUiKey
{
Key,
}
[Serializable, NetSerializable]
public class GasMixerBoundUserInterfaceState : BoundUserInterfaceState
{
public string MixerLabel { get; }
public float OutputPressure { get; }
public bool Enabled { get; }
public float NodeOne { get; }
public GasMixerBoundUserInterfaceState(string mixerLabel, float outputPressure, bool enabled, float nodeOne)
{
MixerLabel = mixerLabel;
OutputPressure = outputPressure;
Enabled = enabled;
NodeOne = nodeOne;
}
}
[Serializable, NetSerializable]
public class GasMixerToggleStatusMessage : BoundUserInterfaceMessage
{
public bool Enabled { get; }
public GasMixerToggleStatusMessage(bool enabled)
{
Enabled = enabled;
}
}
[Serializable, NetSerializable]
public class GasMixerChangeOutputPressureMessage : BoundUserInterfaceMessage
{
public float Pressure { get; }
public GasMixerChangeOutputPressureMessage(float pressure)
{
Pressure = pressure;
}
}
[Serializable, NetSerializable]
public class GasMixerChangeNodePercentageMessage : BoundUserInterfaceMessage
{
public float NodeOne { get; }
public GasMixerChangeNodePercentageMessage(float nodeOne)
{
NodeOne = nodeOne;
}
}
}

View File

@@ -8,3 +8,5 @@ comp-gas-filter-ui-filter-set-rate = Set
comp-gas-filter-ui-filter-gas-current = Currently Filtering: comp-gas-filter-ui-filter-gas-current = Currently Filtering:
comp-gas-filter-ui-filter-gas-select = Select a gas to filter out: comp-gas-filter-ui-filter-gas-select = Select a gas to filter out:
comp-gas-filter-ui-filter-gas-confirm = Set Gas comp-gas-filter-ui-filter-gas-confirm = Set Gas
comp-gas-filter-ui-needs-anchor = Anchor it first!

View File

@@ -0,0 +1,13 @@
comp-gas-mixer-ui-mixer-status = Status:
comp-gas-mixer-ui-status-enabled = On
comp-gas-mixer-ui-status-disabled = Off
comp-gas-mixer-ui-mixer-output-pressure = Output Pressure (kPa):
comp-gas-mixer-ui-mixer-node-primary = Primary Port:
comp-gas-mixer-ui-mixer-node-side = Side Port:
comp-gas-mixer-ui-mixer-set = Set
comp-gas-mixer-ui-mixer-max = Max
comp-gas-mixer-ui-needs-anchor = Anchor it first!

View File

@@ -8,3 +8,5 @@ comp-gas-pump-ui-pump-set-max = Max
comp-gas-pump-ui-pump-output-pressure = Output Pressure (kPa): comp-gas-pump-ui-pump-output-pressure = Output Pressure (kPa):
comp-gas-pump-ui-pump-transfer-rate = Transfer Rate (L/s): comp-gas-pump-ui-pump-transfer-rate = Transfer Rate (L/s):
comp-gas-pump-ui-needs-anchor = Anchor it first!

View File

@@ -76,6 +76,10 @@
- type: SubFloorShowLayerVisualizer - type: SubFloorShowLayerVisualizer
- type: PipeConnectorVisualizer - type: PipeConnectorVisualizer
- type: PipeColorVisualizer - type: PipeColorVisualizer
- type: UserInterface
interfaces:
- key: enum.GasMixerUiKey.Key
type: GasMixerBoundUserInterface
- type: GasMixer - type: GasMixer
inletOne: inlet inletOne: inlet
inletTwo: filter inletTwo: filter