Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Content.Client/_ES/Keypad/ESKeypadSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Content.Shared._ES.Keypad;
using Content.Shared._ES.Keypad.Components;
using Content.Shared.Power;
using Robust.Shared.Timing;

namespace Content.Client._ES.Keypad;

/// <inheritdoc/>
public sealed class ESKeypadSystem : ESSharedKeypadSystem
{
[Dependency] private readonly IGameTiming _timing = default!;

public event Action<Entity<ESKeypadComponent>>? OnCurrentCodeUpdated;
public event Action<Entity<ESKeypadComponent>>? OnLockedUpdated;

/// <inheritdoc/>
public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<ESKeypadComponent, AfterAutoHandleStateEvent>(OnAfterAutoHandleState);
SubscribeLocalEvent<ESKeypadComponent, PowerChangedEvent>(OnPowerChanged);
}

private void OnAfterAutoHandleState(Entity<ESKeypadComponent> ent, ref AfterAutoHandleStateEvent args)
{
if (args.State is not ESKeypadComponent.ESKeypadComponent_AutoState)
return;

// Causes mispredicts
if (!_timing.IsFirstTimePredicted)
return;

OnCurrentCodeUpdated?.Invoke(ent);
OnLockedUpdated?.Invoke(ent);
}

private void OnPowerChanged(Entity<ESKeypadComponent> ent, ref PowerChangedEvent args)
{
OnCurrentCodeUpdated?.Invoke(ent);
OnLockedUpdated?.Invoke(ent);
}
}
20 changes: 20 additions & 0 deletions Content.Client/_ES/Keypad/Ui/ESKeypadBui.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using JetBrains.Annotations;
using Robust.Client.UserInterface;

namespace Content.Client._ES.Keypad.Ui;

[UsedImplicitly]
public sealed class ESKeypadBui(EntityUid owner, Enum uiKey) : BoundUserInterface(owner, uiKey)
{
private ESKeypadWindow? _window;

protected override void Open()
{
base.Open();

_window = this.CreateWindow<ESKeypadWindow>();
_window.OpenCentered();
_window.SetEntity(Owner);
}
}

165 changes: 165 additions & 0 deletions Content.Client/_ES/Keypad/Ui/ESKeypadControl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
using System.Text;
using Content.Client.Message;
using Content.Client.Power.EntitySystems;
using Content.Shared._ES.Keypad.Components;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Timing;

namespace Content.Client._ES.Keypad.Ui;

[GenerateTypedNameReferences]
public sealed partial class ESKeypadControl : BoxContainer
{
[Dependency] private readonly IEntityManager _entityManager = default!;
private readonly ISawmill _logger;

private EntityUid? _uid;

public Color UnpoweredColor { get; set; } = Color.FromHex("#313131");

public ESKeypadControl()
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);

_logger = Logger.GetSawmill("es_keypad_control");

CanKeyboardFocus = true;
KeyboardFocusOnClick = true;
MouseFilter = MouseFilterMode.Stop;

GrabKeyboardFocus();

Button1.OnPressed += _ => PressKey('1');
Button2.OnPressed += _ => PressKey('2');
Button3.OnPressed += _ => PressKey('3');
Button4.OnPressed += _ => PressKey('4');
Button5.OnPressed += _ => PressKey('5');
Button6.OnPressed += _ => PressKey('6');
Button7.OnPressed += _ => PressKey('7');
Button8.OnPressed += _ => PressKey('8');
Button9.OnPressed += _ => PressKey('9');
Button0.OnPressed += _ => PressKey('0');

ButtonC.OnPressed += _ => PressClearKey();
ButtonReturn.OnPressed += _ => PressSubmitKey();

_entityManager.System<ESKeypadSystem>().OnCurrentCodeUpdated += ent =>
{
if (ent != _uid)
return;
UpdateKeypadScreen();
};

_entityManager.System<ESKeypadSystem>().OnLockedUpdated += ent =>
{
if (ent != _uid)
return;
UpdateKeypadIndicator();
};
}

protected override void TextEntered(GUITextEnteredEventArgs args)
{
base.TextEntered(args);

_logger.Debug($"text typed: {args.Text}");

switch (args.TextEnteredEvent.Text)
{
case "0":
PressKey('0');
break;
}
}

public void SetEntity(EntityUid uid)
{
_uid = uid;
UpdateKeypadScreen();
UpdateKeypadIndicator();
}

private void PressKey(char key)
{
if (_uid is not { } uid)
{
_logger.Error("No associated keypad entity!");
return;
}

_entityManager.RaisePredictiveEvent(new ESKeypadPressKeyEvent(_entityManager.GetNetEntity(uid), key));
UpdateKeypadScreen();
}

private void PressClearKey()
{
if (_uid is not { } uid)
{
_logger.Error("No associated keypad entity!");
return;
}

_entityManager.RaisePredictiveEvent(new ESKeypadClearEvent(_entityManager.GetNetEntity(uid)));
UpdateKeypadScreen();
}

private void PressSubmitKey()
{
if (_uid is not { } uid)
{
_logger.Error("No associated keypad entity!");
return;
}

_entityManager.RaisePredictiveEvent(new ESKeypadSubmitEvent(_entityManager.GetNetEntity(uid)));
UpdateKeypadScreen();
UpdateKeypadIndicator();
}

private void UpdateKeypadScreen()
{
if (!_entityManager.TryGetComponent<ESKeypadComponent>(_uid, out var keypad))
{
_logger.Error("Unable to retrieve keypad component");
return;
}

if (!_entityManager.System<PowerReceiverSystem>().IsPowered(_uid.Value))
{
InputLabel.SetMessage(string.Empty);
return;
}

var sb = new StringBuilder();
for (var i = 0; i < keypad.CodeLength; i++)
{
var ch = keypad.CodeInput.Length > i ? keypad.CodeInput[i] : '_';
sb.Append(ch);
sb.Append(' ');
}

InputLabel.SetMarkup(Loc.GetString("es-keypad-ui-current-code-label", ("code", sb.ToString().TrimEnd())));
}

private void UpdateKeypadIndicator()
{
if (!_entityManager.TryGetComponent<ESKeypadComponent>(_uid, out var keypad))
{
_logger.Error("Unable to retrieve keypad component");
return;
}

if (!_entityManager.System<PowerReceiverSystem>().IsPowered(_uid.Value))
{
LockedIndicator.Modulate = UnpoweredColor;
return;
}

LockedIndicator.Modulate = keypad.Locked ? keypad.LockedColor : keypad.UnlockedColor;
}
}

36 changes: 36 additions & 0 deletions Content.Client/_ES/Keypad/Ui/ESKeypadControl.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<controls:ESKeypadControl
xmlns="https://spacestation14.io"
xmlns:controls="clr-namespace:Content.Client._ES.Keypad.Ui"
xmlns:graphics="clr-namespace:Robust.Client.Graphics;assembly=Robust.Client"
Orientation="Vertical">
<BoxContainer Orientation="Horizontal">
<PanelContainer HorizontalExpand="True" VerticalExpand="True">
<PanelContainer.PanelOverride>
<graphics:StyleBoxFlat BackgroundColor="#000000"/>
</PanelContainer.PanelOverride>
<BoxContainer Margin="5">
<RichTextLabel Name="InputLabel" HorizontalExpand="True" HorizontalAlignment="Center"/>
</BoxContainer>
</PanelContainer>
<TextureRect Name="LockedIndicator"
Stretch="KeepAspectCentered"
TexturePath="/Textures/Interface/NavMap/beveled_circle.png"
SetSize="16 16"
Margin="10 0 0 0"/>
</BoxContainer>
<Control MinHeight="5"/>
<GridContainer Columns="3">
<Button Name="Button1" Text="{Loc 'es-keypad-ui-button-1'}" StyleClasses="OpenBoth" MinWidth="40"/>
<Button Name="Button2" Text="{Loc 'es-keypad-ui-button-2'}" StyleClasses="OpenBoth" MinWidth="40"/>
<Button Name="Button3" Text="{Loc 'es-keypad-ui-button-3'}" StyleClasses="OpenLeft" MinWidth="40"/>
<Button Name="Button4" Text="{Loc 'es-keypad-ui-button-4'}" StyleClasses="OpenBoth"/>
<Button Name="Button5" Text="{Loc 'es-keypad-ui-button-5'}" StyleClasses="OpenBoth"/>
<Button Name="Button6" Text="{Loc 'es-keypad-ui-button-6'}" StyleClasses="OpenBoth"/>
<Button Name="Button7" Text="{Loc 'es-keypad-ui-button-7'}" StyleClasses="OpenBoth"/>
<Button Name="Button8" Text="{Loc 'es-keypad-ui-button-8'}" StyleClasses="OpenBoth"/>
<Button Name="Button9" Text="{Loc 'es-keypad-ui-button-9'}" StyleClasses="OpenBoth"/>
<Button Name="ButtonC" Text="{Loc 'es-keypad-ui-button-C'}" StyleClasses="OpenRight"/>
<Button Name="Button0" Text="{Loc 'es-keypad-ui-button-0'}" StyleClasses="OpenBoth"/>
<Button Name="ButtonReturn" Text="{Loc 'es-keypad-ui-button-return'}" StyleClasses="OpenBoth"/>
</GridContainer>
</controls:ESKeypadControl>
27 changes: 27 additions & 0 deletions Content.Client/_ES/Keypad/Ui/ESKeypadWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Content.Client.UserInterface.Controls;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.XAML;

namespace Content.Client._ES.Keypad.Ui;

[GenerateTypedNameReferences]
public sealed partial class ESKeypadWindow : FancyWindow
{
[Dependency] private readonly IEntityManager _entityManager = default!;

public ESKeypadWindow()
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
}

public void SetEntity(EntityUid uid)
{
if (!_entityManager.EntityExists(uid))
return;

Title = Loc.GetString("es-keypad-menu-ui-title", ("name", _entityManager.GetComponent<MetaDataComponent>(uid).EntityName));
Keypad.SetEntity(uid);
}
}

9 changes: 9 additions & 0 deletions Content.Client/_ES/Keypad/Ui/ESKeypadWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<controls:ESKeypadWindow
xmlns="https://spacestation14.io"
xmlns:controls="clr-namespace:Content.Client._ES.Keypad.Ui"
MinSize="150 205"
Resizable="False">
<BoxContainer Margin="10 5 10 10" HorizontalAlignment="Center">
<controls:ESKeypadControl Name="Keypad"/>
</BoxContainer>
</controls:ESKeypadWindow>
17 changes: 17 additions & 0 deletions Content.Server/_ES/Keypad/ESKeypadSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Content.Server.DeviceLinking.Systems;
using Content.Shared._ES.Keypad;
using Content.Shared._ES.Keypad.Components;

namespace Content.Server._ES.Keypad;

/// <inheritdoc/>
public sealed class ESKeypadSystem : ESSharedKeypadSystem
{
[Dependency] private readonly DeviceLinkSystem _deviceLink = default!;

protected override void OnLockToggled(Entity<ESKeypadComponent> ent)
{
var port = ent.Comp.Locked ? ent.Comp.LockPort: ent.Comp.UnlockPort;
_deviceLink.InvokePort(ent, port);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Content.Shared._ES.Keypad.Components;
using Content.Shared.Construction;
using Content.Shared.Examine;
using JetBrains.Annotations;

namespace Content.Shared._ES.Construction.Conditions;

[UsedImplicitly]
public sealed partial class ESKeypadUnlockedCondition : IGraphCondition
{
public bool Condition(EntityUid uid, IEntityManager entityManager)
{
return !entityManager.TryGetComponent<ESKeypadComponent>(uid, out var component) || !component.Locked;
}

public bool DoExamine(ExaminedEvent args)
{
if (Condition(args.Examined, IoCManager.Resolve<IEntityManager>()))
return false;

args.PushMarkup(Loc.GetString("es-construction-examine-condition-keypad-unlocked"));
return true;
}

public IEnumerable<ConstructionGuideEntry> GenerateGuideEntry()
{
yield return new ConstructionGuideEntry()
{
Localization = "es-construction-examine-condition-keypad-unlocked",
};
}
}
Loading
Loading