1
0

SignalTimerWindow.xaml.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using Robust.Client.UserInterface.CustomControls;
  2. using Robust.Client.AutoGenerated;
  3. using Robust.Client.UserInterface.XAML;
  4. using Robust.Shared.Timing;
  5. using Content.Client.TextScreen;
  6. using Robust.Client.UserInterface.Controls;
  7. namespace Content.Client.MachineLinking.UI;
  8. [GenerateTypedNameReferences]
  9. public sealed partial class SignalTimerWindow : DefaultWindow
  10. {
  11. [Dependency] private readonly IGameTiming _timing = default!;
  12. private const int MaxTextLength = 5;
  13. public event Action<string>? OnCurrentTextChanged;
  14. public event Action<string>? OnCurrentDelayMinutesChanged;
  15. public event Action<string>? OnCurrentDelaySecondsChanged;
  16. private TimeSpan? _triggerTime;
  17. private bool _timerStarted;
  18. public event Action? OnStartTimer;
  19. public SignalTimerWindow()
  20. {
  21. RobustXamlLoader.Load(this);
  22. IoCManager.InjectDependencies(this);
  23. CurrentTextEdit.OnTextChanged += e => OnCurrentTextChange(e.Text);
  24. CurrentDelayEditMinutes.OnTextChanged += e => OnCurrentDelayMinutesChange(e.Text);
  25. CurrentDelayEditSeconds.OnTextChanged += e => OnCurrentDelaySecondsChange(e.Text);
  26. StartTimer.OnPressed += _ => StartTimerWeh();
  27. }
  28. private void StartTimerWeh()
  29. {
  30. if (!_timerStarted)
  31. {
  32. _timerStarted = true;
  33. _triggerTime = _timing.CurTime + GetDelay();
  34. }
  35. else
  36. {
  37. SetTimerStarted(false);
  38. }
  39. OnStartTimer?.Invoke();
  40. }
  41. protected override void FrameUpdate(FrameEventArgs args)
  42. {
  43. base.FrameUpdate(args);
  44. if (!_timerStarted || _triggerTime == null)
  45. return;
  46. if (_timing.CurTime < _triggerTime.Value)
  47. {
  48. StartTimer.Text = TextScreenSystem.TimeToString(_triggerTime.Value - _timing.CurTime);
  49. }
  50. else
  51. {
  52. SetTimerStarted(false);
  53. }
  54. }
  55. public void OnCurrentTextChange(string text)
  56. {
  57. if (CurrentTextEdit.Text.Length > MaxTextLength)
  58. {
  59. CurrentTextEdit.Text = CurrentTextEdit.Text.Remove(MaxTextLength);
  60. CurrentTextEdit.CursorPosition = MaxTextLength;
  61. }
  62. OnCurrentTextChanged?.Invoke(text);
  63. }
  64. public void OnCurrentDelayMinutesChange(string text)
  65. {
  66. List<char> toRemove = new();
  67. foreach (var a in text)
  68. {
  69. if (!char.IsDigit(a))
  70. toRemove.Add(a);
  71. }
  72. foreach (var a in toRemove)
  73. {
  74. CurrentDelayEditMinutes.Text = text.Replace(a.ToString(),"");
  75. }
  76. if (CurrentDelayEditMinutes.Text == "")
  77. return;
  78. while (CurrentDelayEditMinutes.Text[0] == '0' && CurrentDelayEditMinutes.Text.Length > 2)
  79. {
  80. CurrentDelayEditMinutes.Text = CurrentDelayEditMinutes.Text.Remove(0, 1);
  81. }
  82. if (CurrentDelayEditMinutes.Text.Length > 2)
  83. {
  84. CurrentDelayEditMinutes.Text = CurrentDelayEditMinutes.Text.Remove(2);
  85. }
  86. OnCurrentDelayMinutesChanged?.Invoke(CurrentDelayEditMinutes.Text);
  87. }
  88. public void OnCurrentDelaySecondsChange(string text)
  89. {
  90. List<char> toRemove = new();
  91. foreach (var a in text)
  92. {
  93. if (!char.IsDigit(a))
  94. toRemove.Add(a);
  95. }
  96. foreach (var a in toRemove)
  97. {
  98. CurrentDelayEditSeconds.Text = text.Replace(a.ToString(), "");
  99. }
  100. if (CurrentDelayEditSeconds.Text == "")
  101. return;
  102. while (CurrentDelayEditSeconds.Text[0] == '0' && CurrentDelayEditSeconds.Text.Length > 2)
  103. {
  104. CurrentDelayEditSeconds.Text = CurrentDelayEditSeconds.Text.Remove(0, 1);
  105. }
  106. if (CurrentDelayEditSeconds.Text.Length > 2)
  107. {
  108. CurrentDelayEditSeconds.Text = CurrentDelayEditSeconds.Text.Remove(2);
  109. }
  110. OnCurrentDelaySecondsChanged?.Invoke(CurrentDelayEditSeconds.Text);
  111. }
  112. public void SetCurrentText(string text)
  113. {
  114. CurrentTextEdit.Text = text;
  115. }
  116. public void SetCurrentDelayMinutes(string delay)
  117. {
  118. CurrentDelayEditMinutes.Text = delay;
  119. }
  120. public void SetCurrentDelaySeconds(string delay)
  121. {
  122. CurrentDelayEditSeconds.Text = delay;
  123. }
  124. public void SetShowText(bool showTime)
  125. {
  126. TextEdit.Visible = showTime;
  127. }
  128. public void SetTriggerTime(TimeSpan timeSpan)
  129. {
  130. _triggerTime = timeSpan;
  131. }
  132. public void SetTimerStarted(bool timerStarted)
  133. {
  134. _timerStarted = timerStarted;
  135. if (!timerStarted)
  136. StartTimer.Text = Loc.GetString("signal-timer-menu-start");
  137. }
  138. /// <summary>
  139. /// Disables fields and buttons if you don't have the access.
  140. /// </summary>
  141. public void SetHasAccess(bool hasAccess)
  142. {
  143. CurrentTextEdit.Editable = hasAccess;
  144. CurrentDelayEditMinutes.Editable = hasAccess;
  145. CurrentDelayEditSeconds.Editable = hasAccess;
  146. StartTimer.Disabled = !hasAccess;
  147. }
  148. /// <summary>
  149. /// Returns a TimeSpan from the currently entered delay.
  150. /// </summary>
  151. public TimeSpan GetDelay()
  152. {
  153. if (!double.TryParse(CurrentDelayEditMinutes.Text, out var minutes))
  154. minutes = 0;
  155. if (!double.TryParse(CurrentDelayEditSeconds.Text, out var seconds))
  156. seconds = 0;
  157. return TimeSpan.FromMinutes(minutes) + TimeSpan.FromSeconds(seconds);
  158. }
  159. }