RingtoneMenu.xaml.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System.Numerics;
  2. using Robust.Client.AutoGenerated;
  3. using Robust.Client.UserInterface.CustomControls;
  4. using Robust.Client.UserInterface.XAML;
  5. using Content.Shared.PDA;
  6. using Robust.Client.UserInterface.Controls;
  7. namespace Content.Client.PDA.Ringer
  8. {
  9. [GenerateTypedNameReferences]
  10. public sealed partial class RingtoneMenu : DefaultWindow
  11. {
  12. public string[] PreviousNoteInputs = new[] { "A", "A", "A", "A", "A", "A" };
  13. public LineEdit[] RingerNoteInputs = default!;
  14. public RingtoneMenu()
  15. {
  16. RobustXamlLoader.Load(this);
  17. RingerNoteInputs = new[] { RingerNoteOneInput, RingerNoteTwoInput, RingerNoteThreeInput, RingerNoteFourInput, RingerNoteFiveInput, RingerNoteSixInput };
  18. for (var i = 0; i < RingerNoteInputs.Length; ++i)
  19. {
  20. var input = RingerNoteInputs[i];
  21. var index = i;
  22. var foo = () => // Prevents unauthorized characters from being entered into the LineEdit
  23. {
  24. input.Text = input.Text.ToUpper();
  25. if (!IsNote(input.Text))
  26. {
  27. input.Text = PreviousNoteInputs[index];
  28. }
  29. else
  30. PreviousNoteInputs[index] = input.Text;
  31. input.RemoveStyleClass("Caution");
  32. };
  33. input.OnFocusExit += _ => foo();
  34. input.OnTextEntered += _ =>
  35. {
  36. foo();
  37. input.CursorPosition = input.Text.Length; // Resets caret position to the end of the typed input
  38. };
  39. input.OnTextChanged += _ =>
  40. {
  41. input.Text = input.Text.ToUpper();
  42. if (!IsNote(input.Text))
  43. input.AddStyleClass("Caution");
  44. else
  45. input.RemoveStyleClass("Caution");
  46. };
  47. }
  48. }
  49. protected override DragMode GetDragModeFor(Vector2 relativeMousePos)
  50. {
  51. //Prevents the ringtone window from being resized
  52. return DragMode.Move;
  53. }
  54. /// <summary>
  55. /// Determines whether or not the characters inputed are authorized
  56. /// </summary>
  57. public static bool IsNote(string input)
  58. {
  59. input = input.Replace("#", "sharp");
  60. return Enum.TryParse(input, true, out Note _);
  61. }
  62. }
  63. }