Skip to content

Commit

Permalink
Merge pull request space-wizards#2 from Just-a-Unity-Dev/power-contro…
Browse files Browse the repository at this point in the history
…l-computer

Power control computer
  • Loading branch information
Just-a-Unity-Dev authored May 18, 2023
2 parents ba40a51 + 888d37d commit 5fa1802
Show file tree
Hide file tree
Showing 12 changed files with 411 additions and 0 deletions.
48 changes: 48 additions & 0 deletions Content.Client/_FTL/PowerControl/PowerControlBoundUserInterface.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Content.Shared._FTL.PowerControl;
using Content.Shared.Gravity;
using JetBrains.Annotations;
using Robust.Client.GameObjects;

namespace Content.Client._FTL.PowerControl
{
[UsedImplicitly]
public sealed class PowerControlBoundUserInterface : BoundUserInterface
{
private PowerControlWindow? _window;

public PowerControlBoundUserInterface(ClientUserInterfaceComponent owner, Enum uiKey) : base (owner, uiKey) {}

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

_window = new PowerControlWindow(this, Owner);

_window.OpenCentered();
_window.OnClose += Close;
}

public void ToggleApc(EntityUid entity)
{
SendMessage(new ToggleApcMessage(entity));
}

protected override void UpdateState(BoundUserInterfaceState state)
{
base.UpdateState(state);

var castState = (PowerControlState) state;
Logger.Debug(castState.Areas.Count.ToString());
_window?.UpdateState(castState);
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (!disposing)
return;

_window?.Dispose();
}
}
}
13 changes: 13 additions & 0 deletions Content.Client/_FTL/PowerControl/PowerControlSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Content.Shared._FTL.PowerControl;

namespace Content.Client._FTL.PowerControl;

/// <inheritdoc/>
public sealed class PowerControlSystem : SharedPowerControlSystem
{
/// <inheritdoc/>
public override void Initialize()
{
base.Initialize();
}
}
42 changes: 42 additions & 0 deletions Content.Client/_FTL/PowerControl/PowerControlWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<controls:FancyWindow xmlns="https://spacestation14.io"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:Content.Client.UserInterface.Controls"
xmlns:style="clr-namespace:Content.Client.Stylesheets"
Resizable="False"
Name="PCComputer"
SetSize="450 350"
MinSize="450 350">
<BoxContainer Orientation="Vertical" Margin="5 0 5 0">
<BoxContainer Margin="0 5 10 10" Orientation="Vertical">
<PanelContainer StyleClasses="LowDivider" />
<ScrollContainer
Name="ApcContainer"
HorizontalExpand="True"
VerticalExpand="False"
MinSize="400 250"
Margin="5 5 5 5">
<BoxContainer
Name="Apcs"
Orientation="Vertical"
HorizontalExpand="True"
VerticalExpand="True"
MinSize="400 250"
Margin="25 0 25 0">
<!-- Products get added here by code -->
</BoxContainer>
</ScrollContainer>
</BoxContainer>

<!-- Footer -->
<BoxContainer Orientation="Vertical">
<PanelContainer StyleClasses="LowDivider" />
<BoxContainer Orientation="Horizontal" Margin="10 2 5 0" VerticalAlignment="Bottom">
<Label Text="{Loc 'power-control-computer-flavor-left'}" StyleClasses="WindowFooterText" />
<Label Text="{Loc 'power-control-computer-flavor-right'}" StyleClasses="WindowFooterText"
HorizontalAlignment="Right" HorizontalExpand="True" Margin="0 0 5 0" />
<TextureRect StyleClasses="NTLogoDark" Stretch="KeepAspectCentered"
VerticalAlignment="Center" HorizontalAlignment="Right" SetSize="19 19"/>
</BoxContainer>
</BoxContainer>
</BoxContainer>
</controls:FancyWindow>
42 changes: 42 additions & 0 deletions Content.Client/_FTL/PowerControl/PowerControlWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Content.Shared._FTL.PowerControl;
using Content.Shared.Gravity;
using Content.Shared.Lathe;
using Robust.Client.AutoGenerated;
using Robust.Client.GameObjects;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using FancyWindow = Content.Client.UserInterface.Controls.FancyWindow;

namespace Content.Client._FTL.PowerControl;

[GenerateTypedNameReferences]
public sealed partial class PowerControlWindow : FancyWindow
{
private readonly ButtonGroup _buttonGroup = new();
private readonly PowerControlBoundUserInterface _owner;

public PowerControlWindow(PowerControlBoundUserInterface ui, ClientUserInterfaceComponent component)
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
Title = Loc.GetString("power-control-computer-title");
_owner = ui;
}

public void UpdateState(PowerControlState state)
{
Apcs.RemoveAllChildren();
foreach (var area in state.Areas)
{
var button = new Button()
{
Text = Loc.GetString("power-control-computer-button-text", ("enabled", area.Enabled), ("name", area.Name))
};
button.OnPressed += args =>
{
_owner.ToggleApc(area.Entity);
};
Apcs.AddChild(button);
}
}
}
30 changes: 30 additions & 0 deletions Content.Server/_FTL/Areas/AreaSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Linq;
using Content.Server.Power.Components;
using Content.Shared._FTL.Areas;

namespace Content.Server._FTL.Areas;

public sealed class AreaSystem : SharedAreasSystem
{
public List<Area> GetAreasOnGrid(EntityUid? gridUid)
{
var areas = new List<Area>();

foreach (var apc in EntityQuery<ApcComponent>())
{
var xform = Transform(apc.Owner);
if (xform.GridUid != gridUid)
continue;
var meta = MetaData(apc.Owner);
var area = new Area
{
Entity = apc.Owner,
Name = meta.EntityName,
Enabled = apc.MainBreakerEnabled
};
areas.Add(area);
}

return areas;
}
}
9 changes: 9 additions & 0 deletions Content.Server/_FTL/PowerControl/PowerControlComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Content.Shared._FTL.PowerControl;

namespace Content.Server._FTL.PowerControl;

[RegisterComponent]
public sealed class PowerControlComponent : SharedPowerControlComponent
{

}
51 changes: 51 additions & 0 deletions Content.Server/_FTL/PowerControl/PowerControlSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Content.Server._FTL.Areas;
using Content.Server.Power.Components;
using Content.Server.Power.EntitySystems;
using Content.Server.UserInterface;
using Content.Shared._FTL.PowerControl;
using Robust.Server.GameObjects;

namespace Content.Server._FTL.PowerControl;

/// <inheritdoc/>
public sealed class PowerControlSystem : SharedPowerControlSystem
{
[Dependency] private readonly UserInterfaceSystem _userInterface = default!;
[Dependency] private readonly AreaSystem _areas = default!;
[Dependency] private readonly ApcSystem _apc = default!;

/// <inheritdoc/>
public override void Initialize()
{
SubscribeLocalEvent<PowerControlComponent, AfterActivatableUIOpenEvent>(OnToggleInterface);
SubscribeLocalEvent<PowerControlComponent, ToggleApcMessage>(OnApcToggleRequestMessage);
base.Initialize();
}

private void OnApcToggleRequestMessage(EntityUid uid, PowerControlComponent component, ToggleApcMessage message)
{
var apc = message.ApcEntity;

Logger.Debug(apc.ToString());
_apc.ApcToggleBreaker(apc);
UpdateUserInterface(uid, component);
}

private void UpdateUserInterface(EntityUid uid, PowerControlComponent? component = null)
{
if (!Resolve(uid, ref component))
return;

var xform = Transform(uid);
if (xform.GridUid == null)
return;

var state = new PowerControlState(_areas.GetAreasOnGrid(xform.GridUid));
_userInterface.TrySetUiState(uid, PowerControlUiKey.Key, state);
}

private void OnToggleInterface(EntityUid uid, PowerControlComponent component, AfterActivatableUIOpenEvent args)
{
UpdateUserInterface(uid, component);
}
}
16 changes: 16 additions & 0 deletions Content.Shared/_FTL/Areas/SharedAreasSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Robust.Shared.Serialization;

namespace Content.Shared._FTL.Areas;

[Serializable, NetSerializable]
public struct Area
{
public EntityUid Entity;
public string Name;
public bool Enabled;
}

public abstract class SharedAreasSystem : EntitySystem
{

}
23 changes: 23 additions & 0 deletions Content.Shared/_FTL/PowerControl/SharedPowerControlComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Robust.Shared.Serialization;

namespace Content.Shared._FTL.PowerControl;

/// <summary>
/// This is used for...
/// </summary>
public abstract class SharedPowerControlComponent : Component
{

}

/// <summary>
/// Contains network state for SharedPowerControlComponent.
/// </summary>
[Serializable, NetSerializable]
public sealed class SharedPowerControlComponentState : ComponentState
{
public SharedPowerControlComponentState(SharedPowerControlComponent component)
{

}
}
43 changes: 43 additions & 0 deletions Content.Shared/_FTL/PowerControl/SharedPowerControlSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Content.Shared._FTL.Areas;
using Robust.Shared.Serialization;

namespace Content.Shared._FTL.PowerControl;

/// <summary>
/// Sent to the server to set whether the generator should be on or off
/// </summary>
[Serializable, NetSerializable]
public sealed class ToggleApcMessage : BoundUserInterfaceMessage
{
public EntityUid ApcEntity;

public ToggleApcMessage(EntityUid entity)
{
ApcEntity = entity;
}
}

[Serializable, NetSerializable]
public sealed class PowerControlState : BoundUserInterfaceState
{
public List<Area> Areas;

public PowerControlState(List<Area> areas)
{
Areas = areas;
}
}

[NetSerializable, Serializable]
public enum PowerControlUiKey : byte
{
Key,
}

/// <summary>
/// This handles...
/// </summary>
public abstract class SharedPowerControlSystem : EntitySystem
{

}
8 changes: 8 additions & 0 deletions Resources/Locale/en-US/_ftl/power-control.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
power-control-computer-flavor-left = Control power responsibly.
power-control-computer-flavor-right = v0.1
power-control-computer-title = power control computer
power-control-computer-button-text =
{$name} {$enabled ->
[true] (enabled)
*[false] (disabled)
}
Loading

0 comments on commit 5fa1802

Please sign in to comment.