Thermomachine UI (#6833)
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Content.Client/Atmos/UI/GasThermomachineWindow.xaml
Normal file
14
Content.Client/Atmos/UI/GasThermomachineWindow.xaml
Normal 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>
|
||||
41
Content.Client/Atmos/UI/GasThermomachineWindow.xaml.cs
Normal file
41
Content.Client/Atmos/UI/GasThermomachineWindow.xaml.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Content.Shared.Atmos;
|
||||
using Content.Shared.Atmos.Piping.Unary.Components;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
@@ -42,10 +43,4 @@ namespace Content.Server.Atmos.Piping.Unary.Components
|
||||
InitialMaxTemperature = MaxTemperature;
|
||||
}
|
||||
}
|
||||
|
||||
public enum ThermoMachineMode : byte
|
||||
{
|
||||
Freezer = 0,
|
||||
Heater = 1,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,9 @@ using Content.Server.NodeContainer;
|
||||
using Content.Server.NodeContainer.Nodes;
|
||||
using Content.Shared.Atmos;
|
||||
using Content.Shared.Atmos.Piping;
|
||||
using Content.Shared.Atmos.Piping.Unary.Components;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
|
||||
@@ -16,6 +18,7 @@ namespace Content.Server.Atmos.Piping.Unary.EntitySystems
|
||||
public sealed class GasThermoMachineSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly AtmosphereSystem _atmosphereSystem = default!;
|
||||
[Dependency] private readonly UserInterfaceSystem _userInterfaceSystem = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
@@ -24,6 +27,10 @@ namespace Content.Server.Atmos.Piping.Unary.EntitySystems
|
||||
SubscribeLocalEvent<GasThermoMachineComponent, AtmosDeviceUpdateEvent>(OnThermoMachineUpdated);
|
||||
SubscribeLocalEvent<GasThermoMachineComponent, AtmosDeviceDisabledEvent>(OnThermoMachineLeaveAtmosphere);
|
||||
SubscribeLocalEvent<GasThermoMachineComponent, RefreshPartsEvent>(OnGasThermoRefreshParts);
|
||||
|
||||
// UI events
|
||||
SubscribeLocalEvent<GasThermoMachineComponent, GasThermomachineToggleMessage>(OnToggleMessage);
|
||||
SubscribeLocalEvent<GasThermoMachineComponent, GasThermomachineChangeTemperatureMessage>(OnChangeTemperature);
|
||||
}
|
||||
|
||||
private void OnThermoMachineUpdated(EntityUid uid, GasThermoMachineComponent thermoMachine, AtmosDeviceUpdateEvent args)
|
||||
@@ -34,6 +41,7 @@ namespace Content.Server.Atmos.Piping.Unary.EntitySystems
|
||||
|| !EntityManager.TryGetComponent(uid, out NodeContainerComponent? nodeContainer)
|
||||
|| !nodeContainer.TryGetNode(thermoMachine.InletName, out PipeNode? inlet))
|
||||
{
|
||||
DirtyUI(uid, thermoMachine);
|
||||
appearance?.SetData(ThermoMachineVisuals.Enabled, false);
|
||||
return;
|
||||
}
|
||||
@@ -58,6 +66,8 @@ namespace Content.Server.Atmos.Piping.Unary.EntitySystems
|
||||
{
|
||||
appearance.SetData(ThermoMachineVisuals.Enabled, false);
|
||||
}
|
||||
|
||||
DirtyUI(uid, component);
|
||||
}
|
||||
|
||||
private void OnGasThermoRefreshParts(EntityUid uid, GasThermoMachineComponent component, RefreshPartsEvent args)
|
||||
@@ -91,6 +101,32 @@ namespace Content.Server.Atmos.Piping.Unary.EntitySystems
|
||||
component.MinTemperature = MathF.Max(Atmospherics.T0C - component.InitialMinTemperature + laserRating * 15f, Atmospherics.TCMB);
|
||||
break;
|
||||
}
|
||||
|
||||
DirtyUI(uid, component);
|
||||
}
|
||||
|
||||
private void OnToggleMessage(EntityUid uid, GasThermoMachineComponent component, GasThermomachineToggleMessage args)
|
||||
{
|
||||
component.Enabled = !component.Enabled;
|
||||
|
||||
DirtyUI(uid, component);
|
||||
}
|
||||
|
||||
private void OnChangeTemperature(EntityUid uid, GasThermoMachineComponent component, GasThermomachineChangeTemperatureMessage args)
|
||||
{
|
||||
component.TargetTemperature =
|
||||
Math.Clamp(args.Temperature, component.MinTemperature, component.MaxTemperature);
|
||||
|
||||
DirtyUI(uid, component);
|
||||
}
|
||||
|
||||
private void DirtyUI(EntityUid uid, GasThermoMachineComponent? thermo, ServerUserInterfaceComponent? ui=null)
|
||||
{
|
||||
if (!Resolve(uid, ref thermo, ref ui, false))
|
||||
return;
|
||||
|
||||
_userInterfaceSystem.TrySetUiState(uid, ThermomachineUiKey.Key,
|
||||
new GasThermomachineBoundUserInterfaceState(thermo.MinTemperature, thermo.MaxTemperature, thermo.TargetTemperature, thermo.Enabled, thermo.Mode), null, ui);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.Atmos.Piping.Unary.Components;
|
||||
|
||||
[Serializable]
|
||||
[NetSerializable]
|
||||
public enum ThermomachineUiKey
|
||||
{
|
||||
Key
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
[NetSerializable]
|
||||
public enum ThermoMachineMode : byte
|
||||
{
|
||||
Freezer = 0,
|
||||
Heater = 1,
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
[NetSerializable]
|
||||
public sealed class GasThermomachineToggleMessage : BoundUserInterfaceMessage
|
||||
{
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
[NetSerializable]
|
||||
public sealed class GasThermomachineChangeTemperatureMessage : BoundUserInterfaceMessage
|
||||
{
|
||||
public float Temperature { get; }
|
||||
|
||||
public GasThermomachineChangeTemperatureMessage(float temperature)
|
||||
{
|
||||
Temperature = temperature;
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
[NetSerializable]
|
||||
public sealed class GasThermomachineBoundUserInterfaceState : BoundUserInterfaceState
|
||||
{
|
||||
public float MinTemperature { get; }
|
||||
public float MaxTemperature { get; }
|
||||
public float Temperature { get; }
|
||||
public bool Enabled { get; }
|
||||
public ThermoMachineMode Mode { get; }
|
||||
|
||||
public GasThermomachineBoundUserInterfaceState(float minTemperature, float maxTemperature, float temperature, bool enabled, ThermoMachineMode mode)
|
||||
{
|
||||
MinTemperature = minTemperature;
|
||||
MaxTemperature = maxTemperature;
|
||||
Temperature = temperature;
|
||||
Enabled = enabled;
|
||||
Mode = mode;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
comp-gas-thermomachine-ui-title-freezer = Freezer
|
||||
comp-gas-thermomachine-ui-title-heater = Heater
|
||||
|
||||
comp-gas-thermomachine-ui-temperature = Temperature (K):
|
||||
comp-gas-thermomachine-ui-toggle = Toggle
|
||||
comp-gas-thermomachine-ui-status-disabled = Off
|
||||
comp-gas-thermomachine-ui-status-enabled = On
|
||||
@@ -148,6 +148,13 @@
|
||||
- type: GasThermoMachine
|
||||
- type: AtmosPipeColor
|
||||
- type: AtmosDevice
|
||||
- type: UserInterface
|
||||
interfaces:
|
||||
- key: enum.ThermomachineUiKey.Key
|
||||
type: GasThermomachineBoundUserInterface
|
||||
- type: ActivatableUI
|
||||
inHandsOnly: false
|
||||
key: enum.ThermomachineUiKey.Key
|
||||
- type: Construction
|
||||
graph: Machine
|
||||
node: machine
|
||||
|
||||
Reference in New Issue
Block a user