Files
crystall-punk-14/Content.Client/Atmos/UI/GasThermomachineBoundUserInterface.cs

102 lines
3.2 KiB
C#
Raw Permalink Normal View History

using Content.Client.Power.EntitySystems;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Piping.Unary.Components;
using Content.Shared.Atmos.Piping.Unary.Systems;
using Content.Shared.Power.Components;
2022-02-22 21:09:01 -07:00
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Client.UserInterface;
2022-02-22 21:09:01 -07:00
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
{
2023-07-08 09:02:17 -07:00
[ViewVariables]
2022-02-22 21:09:01 -07:00
private GasThermomachineWindow? _window;
2023-07-08 09:02:17 -07:00
[ViewVariables]
2022-02-22 21:09:01 -07:00
private float _minTemp = 0.0f;
2023-07-08 09:02:17 -07:00
[ViewVariables]
2022-02-22 21:09:01 -07:00
private float _maxTemp = 0.0f;
2023-09-11 21:20:46 +10:00
[ViewVariables]
private bool _isHeater = true;
2022-02-22 21:09:01 -07:00
2023-07-08 09:02:17 -07:00
public GasThermomachineBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
2022-02-22 21:09:01 -07:00
{
}
protected override void Open()
{
base.Open();
_window = this.CreateWindow<GasThermomachineWindow>();
2022-02-22 21:09:01 -07:00
_window.ToggleStatusButton.OnPressed += _ => OnToggleStatusButtonPressed();
_window.TemperatureSpinbox.OnValueChanged += _ => OnTemperatureChanged(_window.TemperatureSpinbox.Value);
_window.Entity = Owner;
Update();
2022-02-22 21:09:01 -07:00
}
private void OnToggleStatusButtonPressed()
{
if (_window is null) return;
_window.SetActive(!_window.Active);
SendPredictedMessage(new GasThermomachineToggleMessage());
2022-02-22 21:09:01 -07:00
}
private void OnTemperatureChanged(float value)
{
var actual = 0f;
if (_isHeater)
actual = Math.Min(value, _maxTemp);
else
actual = Math.Max(value, _minTemp);
actual = Math.Max(actual, Atmospherics.TCMB);
2022-02-22 21:09:01 -07:00
if (!MathHelper.CloseTo(actual, value, 0.09))
{
_window?.SetTemperature(actual);
return;
}
SendPredictedMessage(new GasThermomachineChangeTemperatureMessage(actual));
2022-02-22 21:09:01 -07:00
}
public override void Update()
2022-02-22 21:09:01 -07:00
{
if (_window == null || !EntMan.TryGetComponent(Owner, out GasThermoMachineComponent? thermo))
2022-02-22 21:09:01 -07:00
return;
var system = EntMan.System<SharedGasThermoMachineSystem>();
_minTemp = thermo.MinTemperature;
_maxTemp = thermo.MaxTemperature;
_isHeater = system.IsHeater(thermo);
_window.SetTemperature(thermo.TargetTemperature);
var receiverSys = EntMan.System<PowerReceiverSystem>();
SharedApcPowerReceiverComponent? receiver = null;
receiverSys.ResolveApc(Owner, ref receiver);
// Also set in frameupdates.
if (receiver != null)
{
_window.SetActive(!receiver.PowerDisabled);
}
2022-02-22 21:09:01 -07:00
_window.Title = _isHeater switch
2022-02-22 21:09:01 -07:00
{
false => Loc.GetString("comp-gas-thermomachine-ui-title-freezer"),
true => Loc.GetString("comp-gas-thermomachine-ui-title-heater")
2022-02-22 21:09:01 -07:00
};
}
}
}