ConfigurationMenu.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System.Collections.Generic;
  2. using System.Numerics;
  3. using System.Text.RegularExpressions;
  4. using Robust.Client.UserInterface;
  5. using Robust.Client.UserInterface.Controls;
  6. using Robust.Client.UserInterface.CustomControls;
  7. using Robust.Shared.Localization;
  8. using Robust.Shared.Maths;
  9. using static Content.Shared.Configurable.ConfigurationComponent;
  10. using static Robust.Client.UserInterface.Controls.BaseButton;
  11. using static Robust.Client.UserInterface.Controls.BoxContainer;
  12. namespace Content.Client.Configurable.UI
  13. {
  14. public sealed class ConfigurationMenu : DefaultWindow
  15. {
  16. private readonly BoxContainer _column;
  17. private readonly BoxContainer _row;
  18. private readonly List<(string name, LineEdit input)> _inputs;
  19. [ViewVariables]
  20. public Regex? Validation { get; internal set; }
  21. public event Action<Dictionary<string, string>>? OnConfiguration;
  22. public ConfigurationMenu()
  23. {
  24. MinSize = SetSize = new Vector2(300, 250);
  25. _inputs = new List<(string name, LineEdit input)>();
  26. Title = Loc.GetString("configuration-menu-device-title");
  27. var baseContainer = new BoxContainer
  28. {
  29. Orientation = LayoutOrientation.Vertical,
  30. VerticalExpand = true,
  31. HorizontalExpand = true
  32. };
  33. _column = new BoxContainer
  34. {
  35. Orientation = LayoutOrientation.Vertical,
  36. Margin = new Thickness(8),
  37. SeparationOverride = 16,
  38. };
  39. _row = new BoxContainer
  40. {
  41. Orientation = LayoutOrientation.Horizontal,
  42. SeparationOverride = 16,
  43. HorizontalExpand = true
  44. };
  45. var confirmButton = new Button
  46. {
  47. Text = Loc.GetString("configuration-menu-confirm"),
  48. HorizontalAlignment = HAlignment.Center,
  49. VerticalAlignment = VAlignment.Center
  50. };
  51. confirmButton.OnButtonUp += OnConfirm;
  52. var outerColumn = new ScrollContainer
  53. {
  54. VerticalExpand = true,
  55. HorizontalExpand = true,
  56. ModulateSelfOverride = Color.FromHex("#202025")
  57. };
  58. outerColumn.AddChild(_column);
  59. baseContainer.AddChild(outerColumn);
  60. baseContainer.AddChild(confirmButton);
  61. Contents.AddChild(baseContainer);
  62. }
  63. public void Populate(ConfigurationBoundUserInterfaceState state)
  64. {
  65. _column.Children.Clear();
  66. _inputs.Clear();
  67. foreach (var field in state.Config)
  68. {
  69. var label = new Label
  70. {
  71. Margin = new Thickness(0, 0, 8, 0),
  72. Name = field.Key,
  73. Text = field.Key + ":",
  74. VerticalAlignment = VAlignment.Center,
  75. HorizontalExpand = true,
  76. SizeFlagsStretchRatio = .2f,
  77. MinSize = new Vector2(60, 0)
  78. };
  79. var input = new LineEdit
  80. {
  81. Name = field.Key + "-input",
  82. Text = field.Value ?? "",
  83. IsValid = Validate,
  84. HorizontalExpand = true,
  85. SizeFlagsStretchRatio = .8f
  86. };
  87. _inputs.Add((field.Key, input));
  88. var row = new BoxContainer
  89. {
  90. Orientation = LayoutOrientation.Horizontal
  91. };
  92. CopyProperties(_row, row);
  93. row.AddChild(label);
  94. row.AddChild(input);
  95. _column.AddChild(row);
  96. }
  97. }
  98. private void OnConfirm(ButtonEventArgs args)
  99. {
  100. var config = GenerateDictionary(_inputs, "Text");
  101. OnConfiguration?.Invoke(config);
  102. Close();
  103. }
  104. private bool Validate(string value)
  105. {
  106. return Validation?.IsMatch(value) != false;
  107. }
  108. private Dictionary<string, string> GenerateDictionary(IEnumerable<(string name, LineEdit input)> inputs, string propertyName)
  109. {
  110. var dictionary = new Dictionary<string, string>();
  111. foreach (var input in inputs)
  112. {
  113. dictionary.Add(input.name, input.input.Text);
  114. }
  115. return dictionary;
  116. }
  117. private static void CopyProperties<T>(T from, T to) where T : Control
  118. {
  119. foreach (var property in from.AllAttachedProperties)
  120. {
  121. to.SetValue(property.Key, property.Value);
  122. }
  123. }
  124. }
  125. }