From 73ff1513de3698115925fe4e884680a76b12216c Mon Sep 17 00:00:00 2001 From: mirrorcult Date: Tue, 22 Feb 2022 21:09:01 -0700 Subject: [PATCH] Thermomachine UI (#6833) --- .../UI/GasThermomachineBoundUserInterface.cs | 92 +++++++++++++++++++ .../Atmos/UI/GasThermomachineWindow.xaml | 14 +++ .../Atmos/UI/GasThermomachineWindow.xaml.cs | 41 +++++++++ .../Components/GasThermoMachineComponent.cs | 7 +- .../EntitySystems/GasThermoMachineSystem.cs | 36 ++++++++ .../SharedGasThermomachineComponent.cs | 56 +++++++++++ .../gas-thermomachine-component.ftl | 7 ++ .../Structures/Piping/Atmospherics/unary.yml | 7 ++ 8 files changed, 254 insertions(+), 6 deletions(-) create mode 100644 Content.Client/Atmos/UI/GasThermomachineBoundUserInterface.cs create mode 100644 Content.Client/Atmos/UI/GasThermomachineWindow.xaml create mode 100644 Content.Client/Atmos/UI/GasThermomachineWindow.xaml.cs create mode 100644 Content.Shared/Atmos/Piping/Unary/Components/SharedGasThermomachineComponent.cs create mode 100644 Resources/Locale/en-US/components/gas-thermomachine-component.ftl diff --git a/Content.Client/Atmos/UI/GasThermomachineBoundUserInterface.cs b/Content.Client/Atmos/UI/GasThermomachineBoundUserInterface.cs new file mode 100644 index 0000000000..eb809e6f7c --- /dev/null +++ b/Content.Client/Atmos/UI/GasThermomachineBoundUserInterface.cs @@ -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 +{ + /// + /// Initializes a and updates it when new server messages are received. + /// + [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)); + } + + /// + /// Update the UI state based on server-sent info + /// + /// + 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(); + } + } +} diff --git a/Content.Client/Atmos/UI/GasThermomachineWindow.xaml b/Content.Client/Atmos/UI/GasThermomachineWindow.xaml new file mode 100644 index 0000000000..65bd918888 --- /dev/null +++ b/Content.Client/Atmos/UI/GasThermomachineWindow.xaml @@ -0,0 +1,14 @@ + + + +