using Content.Client.UserInterface.Controls; using Content.Shared._NF.Power; using Robust.Client.AutoGenerated; using Robust.Client.UserInterface.Controls; using Robust.Client.UserInterface.XAML; namespace Content.Client._NF.Power; /// /// Interface control for items with adjustable power draw. /// /// [GenerateTypedNameReferences] public sealed partial class AdjustablePowerDrawMenu : FancyWindow { // Dependencies [Dependency] private readonly IEntityManager _entityManager = null!; // The entity that this UI is for. private EntityUid _entity; // True if there are unsaved changes that should be maintained. private bool _unsavedChanges; // Events for the BUI to subscribe to. public event Action? OnSetLoad; public event Action? OnSetPowered; // Public state. public bool On; public AdjustablePowerDrawMenu() { RobustXamlLoader.Load(this); IoCManager.InjectDependencies(this); PowerDropDown.AddItem("W", 0); PowerDropDown.AddItem("kW", 1); PowerDropDown.AddItem("MW", 2); PowerDropDown.AddItem("GW", 3); PowerDropDown.OnItemSelected += x => PowerDropDown.SelectId(x.Id); PowerDropDown.OnItemSelected += _ => SetUnsavedChanges(); Load.OnTextChanged += _ => SetUnsavedChanges(); SubmitButton.OnPressed += _ => CheckAndInvokeLoad(); TogglePowerButton.OnPressed += _ => SetPowered(!On); TogglePowerButton.OnPressed += _ => OnSetPowered?.Invoke(TogglePowerButton.Pressed); } public void SetEntity(EntityUid entity) { _entity = entity; this.SetInfoFromEntity(_entityManager, _entity); } public void Update(AdjustablePowerDrawBuiState msg) { SetPowered(msg.On); if (string.IsNullOrEmpty(msg.Text)) { ArbitraryText.Text = ""; ArbitraryText.Visible = false; } else { ArbitraryText.Text = msg.Text; ArbitraryText.Visible = true; } // Set power text to current value in proper units // unless user has pending changes if (!_unsavedChanges && msg.Load >= 0) { if (msg.Load >= 1_000_000_000) { Load.Text = $"{msg.Load / 1_000_000_000:G3}"; PowerDropDown.TrySelect(3); } else if (msg.Load >= 1_000_000) { Load.Text = $"{msg.Load / 1_000_000:G3}"; PowerDropDown.TrySelect(2); } else if (msg.Load >= 1_000) { Load.Text = $"{msg.Load / 1_000:G3}"; PowerDropDown.TrySelect(1); } else { Load.Text = $"{msg.Load:G3}"; PowerDropDown.TrySelect(0); } } } private void CheckAndInvokeLoad() { _unsavedChanges = false; UnsavedChanges.Visible = false; if (!float.TryParse(Load.Text, out var result)) return; if (!float.IsFinite(result) || !float.IsPositive(result)) return; if (PowerDropDown.SelectedId > 0) result *= MathF.Pow(1000.0f, PowerDropDown.SelectedId); OnSetLoad?.Invoke(result); } private void SetUnsavedChanges() { _unsavedChanges = true; UnsavedChanges.Visible = true; } public void SetPowered(bool on) { On = on; TogglePowerButton.Pressed = on; TogglePowerButton.Text = Loc.GetString( on ? "comp-gas-pump-ui-status-enabled" : "comp-gas-pump-ui-status-disabled"); } }