Adds a UI for gas filters (#5052)

* Adds a UI for gas filters

* Address reviews

* Update Content.Client/Atmos/UI/GasFilterBoundUserInterface.cs

* Update Content.Server/Atmos/Piping/Trinary/EntitySystems/GasFilterSystem.cs

* Fix build

Co-authored-by: ike709 <ike709@github.com>
Co-authored-by: Vera Aguilera Puerto <6766154+Zumorica@users.noreply.github.com>
Co-authored-by: Vera Aguilera Puerto <gradientvera@outlook.com>
This commit is contained in:
ike709
2021-11-02 04:44:40 -05:00
committed by GitHub
parent 025a930c08
commit 9da98e3916
8 changed files with 363 additions and 0 deletions

View File

@@ -0,0 +1,98 @@
using System;
using Content.Client.Atmos.EntitySystems;
using Content.Shared.Atmos;
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="GasFilterWindow"/> and updates it when new server messages are received.
/// </summary>
[UsedImplicitly]
public class GasFilterBoundUserInterface : BoundUserInterface
{
private GasFilterWindow? _window;
private const float MaxTransferRate = Atmospherics.MaxTransferRate;
public GasFilterBoundUserInterface(ClientUserInterfaceComponent owner, object uiKey) : base(owner, uiKey)
{
}
protected override void Open()
{
base.Open();
var atmosSystem = EntitySystem.Get<AtmosphereSystem>();
_window = new GasFilterWindow(atmosSystem.Gases);
if(State != null)
UpdateState(State);
_window.OpenCentered();
_window.OnClose += Close;
_window.ToggleStatusButtonPressed += OnToggleStatusButtonPressed;
_window.FilterTransferRateChanged += OnFilterTransferRatePressed;
_window.SelectGasPressed += OnSelectGasPressed;
}
private void OnToggleStatusButtonPressed()
{
if (_window is null) return;
SendMessage(new GasFilterToggleStatusMessage(_window.FilterStatus));
}
private void OnFilterTransferRatePressed(string value)
{
float rate = float.TryParse(value, out var parsed) ? parsed : 0f;
if (rate > MaxTransferRate) rate = MaxTransferRate;
SendMessage(new GasFilterChangeRateMessage(rate));
}
private void OnSelectGasPressed()
{
if (_window is null || _window.SelectedGas is null) return;
if (!Int32.TryParse(_window.SelectedGas, out var gas)) return;
SendMessage(new GasFilterSelectGasMessage(gas));
}
/// <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 GasFilterBoundUserInterfaceState cast)
return;
_window.Title = (cast.FilterLabel);
_window.SetFilterStatus(cast.Enabled);
_window.SetTransferRate(cast.TransferRate);
if (cast.FilteredGas is not null)
{
var atmos = EntitySystem.Get<AtmosphereSystem>();
var gas = atmos.GetGas((Gas) cast.FilteredGas);
_window.SetGasFiltered(gas.ID, gas.Name);
}
else
{
_window.SetGasFiltered(null, "None");
}
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (!disposing) return;
_window?.Dispose();
}
}
}

View File

@@ -0,0 +1,28 @@
<SS14Window xmlns="https://spacestation14.io"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:Content.Client.Stylesheets"
MinSize="480 400" Title="Filter">
<BoxContainer Orientation="Vertical" Margin="5 5 5 5" SeparationOverride="10">
<BoxContainer Orientation="Horizontal" HorizontalExpand="True">
<Label Text="{Loc comp-gas-filter-ui-filter-status} "/>
<Button Name="ToggleStatusButton"/>
</BoxContainer>
<BoxContainer Orientation="Horizontal" HorizontalExpand="True">
<Label Text="{Loc comp-gas-filter-ui-filter-transfer-rate} "/>
<LineEdit Name="FilterTransferRateInput" MinSize="40 0" />
<Button Name="SetFilterRate" Text="{Loc comp-gas-filter-ui-filter-set-rate}" Disabled="True"/>
</BoxContainer>
<BoxContainer Orientation="Horizontal" HorizontalExpand="True">
<Label Name="CurrentGasLabel" />
</BoxContainer>
<BoxContainer Orientation="Vertical" VerticalExpand="True">
<Label Text="{Loc comp-gas-filter-ui-filter-gas-select}" />
<ItemList Name="GasList" SelectMode="Single" VerticalExpand="True"
SizeFlagsStretchRatio="0.9" />
<Button Name="SelectGasButton" Text="{Loc comp-gas-filter-ui-filter-gas-confirm}" HorizontalExpand="True" Disabled="True" />
</BoxContainer>
</BoxContainer>
</SS14Window>

View File

@@ -0,0 +1,105 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using Content.Shared.Atmos.Prototypes;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Localization;
namespace Content.Client.Atmos.UI
{
/// <summary>
/// Client-side UI used to control a gas filter.
/// </summary>
[GenerateTypedNameReferences]
public partial class GasFilterWindow : SS14Window
{
private readonly ButtonGroup _buttonGroup = new();
public bool FilterStatus = true;
public string? SelectedGas;
public string? CurrentGasId;
public event Action? ToggleStatusButtonPressed;
public event Action<string>? FilterTransferRateChanged;
public event Action? SelectGasPressed;
public GasFilterWindow(IEnumerable<GasPrototype> gases)
{
RobustXamlLoader.Load(this);
PopulateGasList(gases);
ToggleStatusButton.OnPressed += _ => SetFilterStatus(!FilterStatus);
ToggleStatusButton.OnPressed += _ => ToggleStatusButtonPressed?.Invoke();
FilterTransferRateInput.OnTextChanged += _ => SetFilterRate.Disabled = false;
SetFilterRate.OnPressed += _ =>
{
FilterTransferRateChanged?.Invoke(FilterTransferRateInput.Text ??= "");
SetFilterRate.Disabled = true;
};
SelectGasButton.OnPressed += _ => SelectGasPressed?.Invoke();
GasList.OnItemSelected += GasListOnItemSelected;
GasList.OnItemDeselected += GasListOnItemDeselected;
}
public void SetTransferRate(float rate)
{
FilterTransferRateInput.Text = rate.ToString(CultureInfo.InvariantCulture);
}
public void SetFilterStatus(bool enabled)
{
FilterStatus = enabled;
if (enabled)
{
ToggleStatusButton.Text = Loc.GetString("comp-gas-filter-ui-status-enabled");
}
else
{
ToggleStatusButton.Text = Loc.GetString("comp-gas-filter-ui-status-disabled");
}
}
public void SetGasFiltered(string? id, string name)
{
CurrentGasId = id;
CurrentGasLabel.Text = Loc.GetString("comp-gas-filter-ui-filter-gas-current") + $" {name}";
GasList.ClearSelected();
SelectGasButton.Disabled = true;
}
private void PopulateGasList(IEnumerable<GasPrototype> gases)
{
foreach (GasPrototype gas in gases)
{
GasList.Add(GetGasItem(gas.ID, gas.Name, GasList));
}
}
private static ItemList.Item GetGasItem(string id, string name, ItemList itemList)
{
return new(itemList)
{
Metadata = id,
Text = name
};
}
private void GasListOnItemSelected(ItemList.ItemListSelectedEventArgs obj)
{
SelectedGas = (string) obj.ItemList[obj.ItemIndex].Metadata!;
if(SelectedGas != CurrentGasId) SelectGasButton.Disabled = false;
}
private void GasListOnItemDeselected(ItemList.ItemListDeselectedEventArgs obj)
{
SelectedGas = CurrentGasId;
SelectGasButton.Disabled = true;
}
}
}

View File

@@ -1,3 +1,5 @@
using System;
using Content.Server.Atmos.Piping.Trinary.EntitySystems;
using Content.Shared.Atmos;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;

View File

@@ -1,9 +1,13 @@
using System;
using Content.Server.Atmos.Piping.Components;
using Content.Server.Atmos.Piping.Trinary.Components;
using Content.Server.NodeContainer;
using Content.Server.NodeContainer.Nodes;
using Content.Server.UserInterface;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Piping;
using Content.Shared.Atmos.Piping.Trinary.Components;
using Content.Shared.Interaction;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
@@ -16,12 +20,18 @@ namespace Content.Server.Atmos.Piping.Trinary.EntitySystems
public class GasFilterSystem : EntitySystem
{
[Dependency] private IGameTiming _gameTiming = default!;
[Dependency] private UserInterfaceSystem _userInterfaceSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GasFilterComponent, AtmosDeviceUpdateEvent>(OnFilterUpdated);
SubscribeLocalEvent<GasFilterComponent, InteractHandEvent>(OnFilterInteractHand);
// Bound UI subscriptions
SubscribeLocalEvent<GasFilterComponent, GasFilterChangeRateMessage>(OnTransferRateChangeMessage);
SubscribeLocalEvent<GasFilterComponent, GasFilterSelectGasMessage>(OnSelectGasMessage);
SubscribeLocalEvent<GasFilterComponent, GasFilterToggleStatusMessage>(OnToggleStatusMessage);
}
private void OnFilterUpdated(EntityUid uid, GasFilterComponent filter, AtmosDeviceUpdateEvent args)
@@ -66,5 +76,49 @@ namespace Content.Server.Atmos.Piping.Trinary.EntitySystems
outletNode.AssumeAir(removed);
}
private void OnFilterInteractHand(EntityUid uid, GasFilterComponent component, InteractHandEvent args)
{
if (!args.User.TryGetComponent(out ActorComponent? actor))
return;
_userInterfaceSystem.TryOpen(uid, GasFilterUiKey.Key, actor.PlayerSession);
DirtyUI(uid, component);
args.Handled = true;
}
private void DirtyUI(EntityUid uid, GasFilterComponent? filter)
{
if (!Resolve(uid, ref filter))
return;
_userInterfaceSystem.TrySetUiState(uid, GasFilterUiKey.Key,
new GasFilterBoundUserInterfaceState(filter.Owner.Name, filter.TransferRate, filter.Enabled, filter.FilteredGas));
}
private void OnToggleStatusMessage(EntityUid uid, GasFilterComponent filter, GasFilterToggleStatusMessage args)
{
filter.Enabled = args.Enabled;
DirtyUI(uid, filter);
}
private void OnTransferRateChangeMessage(EntityUid uid, GasFilterComponent filter, GasFilterChangeRateMessage args)
{
filter.TransferRate = Math.Clamp(args.Rate, 0f, Atmospherics.MaxTransferRate);
DirtyUI(uid, filter);
}
private void OnSelectGasMessage(EntityUid uid, GasFilterComponent filter, GasFilterSelectGasMessage args)
{
if (Enum.TryParse<Gas>(args.ID.ToString(), true, out var parsedGas))
{
filter.FilteredGas = parsedGas;
DirtyUI(uid, filter);
}
}
}
}

View File

@@ -0,0 +1,62 @@
using System;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
namespace Content.Shared.Atmos.Piping.Trinary.Components
{
[Serializable, NetSerializable]
public enum GasFilterUiKey
{
Key,
}
[Serializable, NetSerializable]
public class GasFilterBoundUserInterfaceState : BoundUserInterfaceState
{
public string FilterLabel { get; }
public float TransferRate { get; }
public bool Enabled { get; }
public Gas? FilteredGas { get; }
public GasFilterBoundUserInterfaceState(string filterLabel, float transferRate, bool enabled, Gas? filteredGas)
{
FilterLabel = filterLabel;
TransferRate = transferRate;
Enabled = enabled;
FilteredGas = filteredGas;
}
}
[Serializable, NetSerializable]
public class GasFilterToggleStatusMessage : BoundUserInterfaceMessage
{
public bool Enabled { get; }
public GasFilterToggleStatusMessage(bool enabled)
{
Enabled = enabled;
}
}
[Serializable, NetSerializable]
public class GasFilterChangeRateMessage : BoundUserInterfaceMessage
{
public float Rate { get; }
public GasFilterChangeRateMessage(float rate)
{
Rate = rate;
}
}
[Serializable, NetSerializable]
public class GasFilterSelectGasMessage : BoundUserInterfaceMessage
{
public int ID { get; }
public GasFilterSelectGasMessage(int id)
{
ID = id;
}
}
}

View File

@@ -0,0 +1,10 @@
comp-gas-filter-ui-filter-status = Status:
comp-gas-filter-ui-status-enabled = On
comp-gas-filter-ui-status-disabled = Off
comp-gas-filter-ui-filter-transfer-rate = Transfer Rate (L/s):
comp-gas-filter-ui-filter-set-rate = Set
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-confirm = Set Gas

View File

@@ -47,6 +47,10 @@
- type: GasFilterVisualizer
disabledState: gasFilter
enabledState: gasFilterOn
- type: UserInterface
interfaces:
- key: enum.GasFilterUiKey.Key
type: GasFilterBoundUserInterface
- type: GasFilter
- type: entity