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;
}
}
}