GravityGeneratorWindow to XAML UI (#4638)

* Move color handling to localization strings, small improvements

* Create/move files to UI namespace

* Actually change namespace

* Move to XAML

* Improve localization
This commit is contained in:
Visne 2021-09-19 18:59:55 +02:00 committed by GitHub
parent a9b3b5136b
commit 5063689d81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 111 additions and 110 deletions

View File

@ -1,107 +0,0 @@
using Content.Shared.Gravity;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Maths;
using static Robust.Client.UserInterface.Controls.BoxContainer;
namespace Content.Client.Gravity
{
[UsedImplicitly]
public class GravityGeneratorBoundUserInterface : BoundUserInterface
{
private GravityGeneratorWindow? _window;
public bool IsOn;
public GravityGeneratorBoundUserInterface(ClientUserInterfaceComponent owner, object uiKey) : base (owner, uiKey)
{
SendMessage(new SharedGravityGeneratorComponent.GeneratorStatusRequestMessage());
}
protected override void Open()
{
base.Open();
IsOn = false;
_window = new GravityGeneratorWindow(this);
_window.Switch.OnPressed += (_) =>
{
SendMessage(new SharedGravityGeneratorComponent.SwitchGeneratorMessage(!IsOn));
SendMessage(new SharedGravityGeneratorComponent.GeneratorStatusRequestMessage());
};
_window.OpenCentered();
}
protected override void UpdateState(BoundUserInterfaceState state)
{
base.UpdateState(state);
var castState = (SharedGravityGeneratorComponent.GeneratorState) state;
IsOn = castState.On;
_window?.UpdateButton();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (!disposing) return;
_window?.Dispose();
}
}
public class GravityGeneratorWindow : SS14Window
{
public Label Status;
public Button Switch;
public GravityGeneratorBoundUserInterface Owner;
public GravityGeneratorWindow(GravityGeneratorBoundUserInterface ui)
{
IoCManager.InjectDependencies(this);
Owner = ui;
Title = Loc.GetString("gravity-generator-window-title");
var vBox = new BoxContainer
{
Orientation = LayoutOrientation.Vertical,
MinSize = new Vector2(250, 100)
};
Status = new Label
{
Text = $"{Loc.GetString("gravity-generator-window-status-label")} {Loc.GetString(Owner.IsOn ? "gravity-generator-window-is-on" : "gravity-generator-window-is-off")}",
FontColorOverride = Owner.IsOn ? Color.ForestGreen : Color.Red
};
Switch = new Button
{
Text = Loc.GetString(Owner.IsOn ? "gravity-generator-window-turn-off-button" : "gravity-generator-window-turn-on-button"),
TextAlign = Label.AlignMode.Center,
MinSize = new Vector2(150, 60)
};
vBox.AddChild(Status);
vBox.AddChild(Switch);
Contents.AddChild(vBox);
}
public void UpdateButton()
{
Status.Text = $"{Loc.GetString("gravity-generator-window-status-label")} {Loc.GetString(Owner.IsOn ? "gravity-generator-window-is-on" : "gravity-generator-window-is-off")}";
Status.FontColorOverride = Owner.IsOn ? Color.ForestGreen : Color.Red;
Switch.Text = Loc.GetString(Owner.IsOn ? "gravity-generator-window-turn-off-button" : "gravity-generator-window-turn-on-button");
}
}
}

View File

@ -0,0 +1,54 @@
using Content.Shared.Gravity;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Shared.GameObjects;
namespace Content.Client.Gravity.UI
{
[UsedImplicitly]
public class GravityGeneratorBoundUserInterface : BoundUserInterface
{
private GravityGeneratorWindow? _window;
public bool IsOn;
public GravityGeneratorBoundUserInterface(ClientUserInterfaceComponent owner, object uiKey) : base (owner, uiKey)
{
SendMessage(new SharedGravityGeneratorComponent.GeneratorStatusRequestMessage());
}
protected override void Open()
{
base.Open();
IsOn = false;
_window = new GravityGeneratorWindow(this);
_window.Switch.OnPressed += _ =>
{
SendMessage(new SharedGravityGeneratorComponent.SwitchGeneratorMessage(!IsOn));
SendMessage(new SharedGravityGeneratorComponent.GeneratorStatusRequestMessage());
};
_window.OpenCentered();
}
protected override void UpdateState(BoundUserInterfaceState state)
{
base.UpdateState(state);
var castState = (SharedGravityGeneratorComponent.GeneratorState) state;
IsOn = castState.On;
_window?.UpdateButton();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (!disposing) return;
_window?.Dispose();
}
}
}

View File

@ -0,0 +1,13 @@
<SS14Window xmlns="https://spacestation14.io"
Title="{Loc 'gravity-generator-window-title'}"
MinSize="270 130"
SetSize="270 130">
<BoxContainer Orientation="Vertical">
<BoxContainer Orientation="Horizontal">
<RichTextLabel Name="Status" />
</BoxContainer>
<Button Name="SwitchProtected"
TextAlign="Center"
MinHeight="60" />
</BoxContainer>
</SS14Window>

View File

@ -0,0 +1,41 @@
using Content.Client.Message;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
namespace Content.Client.Gravity.UI
{
[GenerateTypedNameReferences]
public partial class GravityGeneratorWindow : SS14Window
{
private readonly GravityGeneratorBoundUserInterface _owner;
public Button Switch => SwitchProtected;
public GravityGeneratorWindow(GravityGeneratorBoundUserInterface ui)
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
_owner = ui;
UpdateButton();
}
public void UpdateButton()
{
string status = Loc.GetString(_owner.IsOn
? "gravity-generator-window-is-on"
: "gravity-generator-window-is-off");
Status.SetMarkup(Loc.GetString("gravity-generator-window-status-label", ("status", status)));
Switch.Text = Loc.GetString(_owner.IsOn
? "gravity-generator-window-turn-off-button"
: "gravity-generator-window-turn-on-button");
}
}
}

View File

@ -1,8 +1,8 @@
## UI
gravity-generator-window-title = Gravity Generator Control
gravity-generator-window-status-label = Current Status:
gravity-generator-window-status-label = Current Status: {$status}
gravity-generator-window-turn-off-button = Turn off
gravity-generator-window-turn-on-button = Turn on
gravity-generator-window-is-on = On
gravity-generator-window-is-off = Off
gravity-generator-window-is-on = [color=green]On[/color]
gravity-generator-window-is-off = [color=red]Off[/color]