Thermomachine UI (#6833)

This commit is contained in:
mirrorcult
2022-02-22 21:09:01 -07:00
committed by GitHub
parent e15f70fe90
commit 73ff1513de
8 changed files with 254 additions and 6 deletions

View File

@@ -0,0 +1,92 @@
using Content.Shared.Atmos;
using Content.Shared.Atmos.Piping.Binary.Components;
using Content.Shared.Atmos.Piping.Unary.Components;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Shared.GameObjects;
namespace Content.Client.Atmos.UI
{
/// <summary>
/// Initializes a <see cref="GasThermomachineWindow"/> and updates it when new server messages are received.
/// </summary>
[UsedImplicitly]
public sealed class GasThermomachineBoundUserInterface : BoundUserInterface
{
private GasThermomachineWindow? _window;
private float _minTemp = 0.0f;
private float _maxTemp = 0.0f;
public GasThermomachineBoundUserInterface(ClientUserInterfaceComponent owner, object uiKey) : base(owner, uiKey)
{
}
protected override void Open()
{
base.Open();
_window = new GasThermomachineWindow();
if(State != null)
UpdateState(State);
_window.OpenCentered();
_window.OnClose += Close;
_window.ToggleStatusButton.OnPressed += _ => OnToggleStatusButtonPressed();
_window.TemperatureSpinbox.OnValueChanged += _ => OnTemperatureChanged(_window.TemperatureSpinbox.Value);
}
private void OnToggleStatusButtonPressed()
{
if (_window is null) return;
_window.SetActive(!_window.Active);
SendMessage(new GasThermomachineToggleMessage());
}
private void OnTemperatureChanged(float value)
{
var actual = Math.Clamp(value, _minTemp, _maxTemp);
if (!MathHelper.CloseTo(actual, value, 0.09))
{
_window?.SetTemperature(actual);
return;
}
SendMessage(new GasThermomachineChangeTemperatureMessage(actual));
}
/// <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 GasThermomachineBoundUserInterfaceState cast)
return;
_minTemp = cast.MinTemperature;
_maxTemp = cast.MaxTemperature;
_window.SetTemperature(cast.Temperature);
_window.SetActive(cast.Enabled);
_window.Title = cast.Mode switch
{
ThermoMachineMode.Freezer => Loc.GetString("comp-gas-thermomachine-ui-title-freezer"),
ThermoMachineMode.Heater => Loc.GetString("comp-gas-thermomachine-ui-title-heater"),
_ => string.Empty
};
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (!disposing) return;
_window?.Dispose();
}
}
}

View File

@@ -0,0 +1,14 @@
<DefaultWindow xmlns="https://spacestation14.io"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
MinSize="300 120" Title="{Loc comp-gas-thermomachine-ui-title-freezer}">
<BoxContainer Name="VboxContainer" Orientation="Vertical" Margin="5 5 5 5" SeparationOverride="10">
<BoxContainer Orientation="Horizontal" HorizontalExpand="True">
<Label Text="{Loc comp-gas-thermomachine-ui-toggle} "/>
<Control MinSize="5 0" />
<Button Access="Public" Name="ToggleStatusButton"/>
</BoxContainer>
<BoxContainer Name="SpinboxHBox" Orientation="Horizontal">
<Label Text="{Loc comp-gas-thermomachine-ui-temperature} "/>
</BoxContainer>
</BoxContainer>
</DefaultWindow>

View File

@@ -0,0 +1,41 @@
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
namespace Content.Client.Atmos.UI;
[GenerateTypedNameReferences]
public sealed partial class GasThermomachineWindow : DefaultWindow
{
public bool Active = true;
public FloatSpinBox TemperatureSpinbox;
public GasThermomachineWindow()
{
RobustXamlLoader.Load(this);
SpinboxHBox.AddChild(
TemperatureSpinbox = new FloatSpinBox(.1f, 2) { MaxWidth = 150, HorizontalExpand = true }
);
}
public void SetActive(bool active)
{
Active = active;
if (active)
{
ToggleStatusButton.Text = Loc.GetString("comp-gas-thermomachine-ui-status-enabled");
}
else
{
ToggleStatusButton.Text = Loc.GetString("comp-gas-thermomachine-ui-status-disabled");
}
}
public void SetTemperature(float temperature)
{
TemperatureSpinbox.Value = temperature;
}
}