Files
Monolith/Content.Client/MachineLinking/UI/SignalTimerWindow.xaml.cs
T
2025-12-06 14:09:59 +03:00

146 lines
3.8 KiB
C#

using Robust.Client.UserInterface.CustomControls;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Timing;
using Content.Client.TextScreen;
using Robust.Client.UserInterface.Controls;
namespace Content.Client.MachineLinking.UI;
[GenerateTypedNameReferences]
public sealed partial class SignalTimerWindow : DefaultWindow
{
[Dependency] private readonly IGameTiming _timing = default!;
private const int MaxTextLength = 5;
public event Action<string>? OnCurrentTextChanged;
public event Action<TimeSpan>? OnCurrentDelayChanged; // Mono
public event Action<bool>? OnCurrentRepeatChanged; //Frontier: Repeat Changed handler
private TimeSpan? _triggerTime;
private bool _timerStarted;
public event Action? OnStartTimer;
public SignalTimerWindow()
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
CurrentTextEdit.OnTextChanged += e => OnCurrentTextChange(e.Text);
CurrentDelayEdit.OnValueChanged += e => CurrentDelayChange(TimeSpan.FromSeconds(e.Value)); // Mono
CurrentRepeatEdit.OnToggled += e => OnCurrentRepeatChange(e.Pressed); //Frontier: Repeat OnToggled
StartTimer.OnPressed += _ => StartTimerWeh();
}
private void StartTimerWeh()
{
if (!_timerStarted)
{
_timerStarted = true;
_triggerTime = _timing.CurTime + GetDelay();
}
else
{
SetTimerStarted(false);
}
OnStartTimer?.Invoke();
}
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
if (!_timerStarted || _triggerTime == null)
return;
if (_timing.CurTime < _triggerTime.Value)
{
StartTimer.Text = TextScreenSystem.TimeToString(_triggerTime.Value - _timing.CurTime);
}
else
{
SetTimerStarted(false);
}
}
public void OnCurrentTextChange(string text)
{
if (CurrentTextEdit.Text.Length > MaxTextLength)
{
CurrentTextEdit.Text = CurrentTextEdit.Text.Remove(MaxTextLength);
CurrentTextEdit.CursorPosition = MaxTextLength;
}
OnCurrentTextChanged?.Invoke(text);
}
// Frontier: OnCurrentRepeatChange handler
public void OnCurrentRepeatChange(bool toggled)
{
OnCurrentRepeatChanged?.Invoke(toggled);
}
//End Frontier
// Mono
public void CurrentDelayChange(TimeSpan newValue)
{
OnCurrentDelayChanged?.Invoke(newValue);
}
public void SetCurrentText(string text)
{
CurrentTextEdit.Text = text;
}
// Frontier: SetCurrentRepeat
public void SetCurrentRepeat(bool repeat)
{
CurrentRepeatEdit.Pressed = repeat;
}
// End Frontier
// Mono
public void SetCurrentDelay(TimeSpan delay)
{
CurrentDelayEdit.Value = (float)delay.TotalSeconds;
}
public void SetShowText(bool showTime)
{
TextEdit.Visible = showTime;
}
public void SetTriggerTime(TimeSpan timeSpan)
{
_triggerTime = timeSpan;
}
public void SetTimerStarted(bool timerStarted)
{
_timerStarted = timerStarted;
if (!timerStarted)
StartTimer.Text = Loc.GetString("signal-timer-menu-start");
}
/// <summary>
/// Disables fields and buttons if you don't have the access.
/// </summary>
public void SetHasAccess(bool hasAccess)
{
CurrentTextEdit.Editable = hasAccess;
StartTimer.Disabled = !hasAccess;
}
/// <summary>
/// Returns a TimeSpan from the currently entered delay.
/// </summary>
public TimeSpan GetDelay()
{
return TimeSpan.FromSeconds(CurrentDelayEdit.Value); // Mono
}
}