Files
OldThink/Content.Client/Light/Components/HandheldLightComponent.cs

102 lines
3.1 KiB
C#
Raw Normal View History

2021-06-09 22:19:39 +02:00
using Content.Client.Items.Components;
using Content.Shared.Light.Component;
using Robust.Client.Graphics;
2020-01-09 00:27:52 +01:00
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
2021-12-27 18:15:16 +11:00
using Robust.Shared.Analyzers;
2020-01-09 00:27:52 +01:00
using Robust.Shared.GameObjects;
using Robust.Shared.Maths;
using Robust.Shared.Timing;
using Robust.Shared.ViewVariables;
using static Robust.Client.UserInterface.Controls.BoxContainer;
2020-01-09 00:27:52 +01:00
2021-06-09 22:19:39 +02:00
namespace Content.Client.Light.Components
2020-01-09 00:27:52 +01:00
{
[RegisterComponent]
2021-12-27 18:15:16 +11:00
[Friend(typeof(HandheldLightSystem))]
2020-01-09 00:27:52 +01:00
public sealed class HandheldLightComponent : SharedHandheldLightComponent, IItemStatus
{
2021-12-27 18:15:16 +11:00
public byte? Level;
2020-01-09 00:27:52 +01:00
public Control MakeControl()
{
return new StatusControl(this);
}
private sealed class StatusControl : Control
{
private const float TimerCycle = 1;
private readonly HandheldLightComponent _parent;
private readonly PanelContainer[] _sections = new PanelContainer[StatusLevels - 1];
2020-01-09 00:27:52 +01:00
private float _timer;
private static readonly StyleBoxFlat StyleBoxLit = new()
2020-01-09 00:27:52 +01:00
{
BackgroundColor = Color.LimeGreen
2020-01-09 00:27:52 +01:00
};
private static readonly StyleBoxFlat StyleBoxUnlit = new()
2020-01-09 00:27:52 +01:00
{
BackgroundColor = Color.Black
};
public StatusControl(HandheldLightComponent parent)
{
_parent = parent;
var wrapper = new BoxContainer
2020-01-09 00:27:52 +01:00
{
Orientation = LayoutOrientation.Horizontal,
2020-01-09 00:27:52 +01:00
SeparationOverride = 4,
2021-02-21 12:38:56 +01:00
HorizontalAlignment = HAlignment.Center
2020-01-09 00:27:52 +01:00
};
AddChild(wrapper);
for (var i = 0; i < _sections.Length; i++)
{
2021-02-21 12:38:56 +01:00
var panel = new PanelContainer {MinSize = (20, 20)};
2020-01-09 00:27:52 +01:00
wrapper.AddChild(panel);
_sections[i] = panel;
}
}
protected override void FrameUpdate(FrameEventArgs args)
2020-01-09 00:27:52 +01:00
{
base.FrameUpdate(args);
2020-01-09 00:27:52 +01:00
_timer += args.DeltaSeconds;
_timer %= TimerCycle;
2021-12-27 18:15:16 +11:00
var level = _parent.Level;
2020-01-09 00:27:52 +01:00
for (var i = 0; i < _sections.Length; i++)
2020-01-09 00:27:52 +01:00
{
if (i == 0)
{
if (level == 0 || level == null)
{
_sections[0].PanelOverride = StyleBoxUnlit;
}
else if (level == 1)
{
// Flash the last light.
_sections[0].PanelOverride = _timer > TimerCycle / 2 ? StyleBoxLit : StyleBoxUnlit;
}
else
{
_sections[0].PanelOverride = StyleBoxLit;
}
continue;
}
_sections[i].PanelOverride = level >= i + 2 ? StyleBoxLit : StyleBoxUnlit;
2020-01-09 00:27:52 +01:00
}
}
}
}
}