1
0

DirectionIcon.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System.Numerics;
  2. using Robust.Client.Graphics;
  3. using Robust.Client.UserInterface.Controls;
  4. using Direction = Robust.Shared.Maths.Direction;
  5. namespace Content.Client.UserInterface.Controls;
  6. /// <summary>
  7. /// Simple control that shows an arrow pointing in some direction.
  8. /// </summary>
  9. /// <remarks>
  10. /// The actual arrow and other icons are defined in the style sheet.
  11. /// </remarks>
  12. public sealed class DirectionIcon : TextureRect
  13. {
  14. public static string StyleClassDirectionIconArrow = "direction-icon-arrow"; // south pointing arrow
  15. public static string StyleClassDirectionIconHere = "direction-icon-here"; // "you have reached your destination"
  16. public static string StyleClassDirectionIconUnknown = "direction-icon-unknown"; // unknown direction / error
  17. private Angle? _rotation;
  18. private bool _snap;
  19. float _minDistance;
  20. public Angle? Rotation
  21. {
  22. get => _rotation;
  23. set
  24. {
  25. _rotation = value;
  26. SetOnlyStyleClass(value == null ? StyleClassDirectionIconUnknown : StyleClassDirectionIconArrow);
  27. }
  28. }
  29. public DirectionIcon()
  30. {
  31. Stretch = StretchMode.KeepAspectCentered;
  32. SetOnlyStyleClass(StyleClassDirectionIconUnknown);
  33. }
  34. public DirectionIcon(bool snap = true, float minDistance = 0.1f) : this()
  35. {
  36. _snap = snap;
  37. _minDistance = minDistance;
  38. }
  39. public void UpdateDirection(Direction direction)
  40. {
  41. Rotation = direction.ToAngle();
  42. }
  43. public void UpdateDirection(Vector2 direction, Angle relativeAngle)
  44. {
  45. if (direction.EqualsApprox(Vector2.Zero, _minDistance))
  46. {
  47. SetOnlyStyleClass(StyleClassDirectionIconHere);
  48. return;
  49. }
  50. var rotation = direction.ToWorldAngle() - relativeAngle;
  51. Rotation = _snap ? rotation.GetDir().ToAngle() : rotation;
  52. }
  53. protected override void Draw(DrawingHandleScreen handle)
  54. {
  55. if (_rotation != null)
  56. {
  57. var offset = (-_rotation.Value).RotateVec(Size * UIScale / 2) - Size * UIScale / 2;
  58. handle.SetTransform(Matrix3Helpers.CreateTransform(GlobalPixelPosition - offset, -_rotation.Value));
  59. }
  60. base.Draw(handle);
  61. }
  62. }