1
0

MenuButton.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Numerics;
  3. using Robust.Client.Graphics;
  4. using Robust.Client.Input;
  5. using Robust.Client.UserInterface.Controls;
  6. using Robust.Shared.Graphics;
  7. using Robust.Shared.Input;
  8. using Robust.Shared.Utility;
  9. namespace Content.Client.UserInterface.Controls;
  10. public sealed class MenuButton : ContainerButton
  11. {
  12. [Dependency] private readonly IInputManager _inputManager = default!;
  13. public const string StyleClassLabelTopButton = "topButtonLabel";
  14. public const string StyleClassRedTopButton = "topButtonLabel";
  15. private static readonly Color ColorNormal = Color.FromHex("#7b7e9e");
  16. private static readonly Color ColorRedNormal = Color.FromHex("#FEFEFE");
  17. private static readonly Color ColorHovered = Color.FromHex("#9699bb");
  18. private static readonly Color ColorRedHovered = Color.FromHex("#FFFFFF");
  19. private static readonly Color ColorPressed = Color.FromHex("#789B8C");
  20. private const float VertPad = 8f;
  21. private Color NormalColor => HasStyleClass(StyleClassRedTopButton) ? ColorRedNormal : ColorNormal;
  22. private Color HoveredColor => HasStyleClass(StyleClassRedTopButton) ? ColorRedHovered : ColorHovered;
  23. private BoundKeyFunction _function;
  24. private readonly BoxContainer _root;
  25. private readonly TextureRect? _buttonIcon;
  26. private readonly Label? _buttonLabel;
  27. public string AppendStyleClass { set => AddStyleClass(value); }
  28. public Texture? Icon { get => _buttonIcon!.Texture; set => _buttonIcon!.Texture = value; }
  29. public BoundKeyFunction BoundKey
  30. {
  31. get => _function;
  32. set
  33. {
  34. _function = value;
  35. _buttonLabel!.Text = BoundKeyHelper.ShortKeyName(value);
  36. }
  37. }
  38. public BoxContainer ButtonRoot => _root;
  39. public MenuButton()
  40. {
  41. IoCManager.InjectDependencies(this);
  42. _buttonIcon = new TextureRect()
  43. {
  44. TextureScale = new Vector2(0.5f, 0.5f),
  45. HorizontalAlignment = HAlignment.Center,
  46. VerticalAlignment = VAlignment.Center,
  47. VerticalExpand = true,
  48. Margin = new Thickness(0, VertPad),
  49. ModulateSelfOverride = NormalColor,
  50. Stretch = TextureRect.StretchMode.KeepCentered
  51. };
  52. _buttonLabel = new Label
  53. {
  54. Text = "",
  55. HorizontalAlignment = HAlignment.Center,
  56. ModulateSelfOverride = NormalColor,
  57. StyleClasses = {StyleClassLabelTopButton}
  58. };
  59. _root = new BoxContainer
  60. {
  61. Orientation = BoxContainer.LayoutOrientation.Vertical,
  62. Children =
  63. {
  64. _buttonIcon,
  65. _buttonLabel
  66. }
  67. };
  68. AddChild(_root);
  69. ToggleMode = true;
  70. }
  71. protected override void EnteredTree()
  72. {
  73. _inputManager.OnKeyBindingAdded += OnKeyBindingChanged;
  74. _inputManager.OnKeyBindingRemoved += OnKeyBindingChanged;
  75. _inputManager.OnInputModeChanged += OnKeyBindingChanged;
  76. }
  77. protected override void ExitedTree()
  78. {
  79. _inputManager.OnKeyBindingAdded -= OnKeyBindingChanged;
  80. _inputManager.OnKeyBindingRemoved -= OnKeyBindingChanged;
  81. _inputManager.OnInputModeChanged -= OnKeyBindingChanged;
  82. }
  83. private void OnKeyBindingChanged(IKeyBinding obj)
  84. {
  85. _buttonLabel!.Text = BoundKeyHelper.ShortKeyName(_function);
  86. }
  87. private void OnKeyBindingChanged()
  88. {
  89. _buttonLabel!.Text = BoundKeyHelper.ShortKeyName(_function);
  90. }
  91. protected override void StylePropertiesChanged()
  92. {
  93. // colors of children depend on style, so ensure we update when style is changed
  94. base.StylePropertiesChanged();
  95. UpdateChildColors();
  96. }
  97. private void UpdateChildColors()
  98. {
  99. if (_buttonIcon == null || _buttonLabel == null) return;
  100. switch (DrawMode)
  101. {
  102. case DrawModeEnum.Normal:
  103. _buttonIcon.ModulateSelfOverride = NormalColor;
  104. _buttonLabel.ModulateSelfOverride = NormalColor;
  105. break;
  106. case DrawModeEnum.Pressed:
  107. _buttonIcon.ModulateSelfOverride = ColorPressed;
  108. _buttonLabel.ModulateSelfOverride = ColorPressed;
  109. break;
  110. case DrawModeEnum.Hover:
  111. _buttonIcon.ModulateSelfOverride = HoveredColor;
  112. _buttonLabel.ModulateSelfOverride = HoveredColor;
  113. break;
  114. case DrawModeEnum.Disabled:
  115. break;
  116. }
  117. }
  118. protected override void DrawModeChanged()
  119. {
  120. base.DrawModeChanged();
  121. UpdateChildColors();
  122. }
  123. }