HandheldGpsStatusControl.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using Content.Shared.GPS.Components;
  2. using Content.Client.Message;
  3. using Content.Client.Stylesheets;
  4. using Robust.Client.GameObjects;
  5. using Robust.Client.UserInterface;
  6. using Robust.Client.UserInterface.Controls;
  7. using Robust.Shared.Timing;
  8. namespace Content.Client.GPS.UI;
  9. public sealed class HandheldGpsStatusControl : Control
  10. {
  11. private readonly Entity<HandheldGPSComponent> _parent;
  12. private readonly RichTextLabel _label;
  13. private float _updateDif;
  14. private readonly IEntityManager _entMan;
  15. private readonly SharedTransformSystem _transform;
  16. public HandheldGpsStatusControl(Entity<HandheldGPSComponent> parent)
  17. {
  18. _parent = parent;
  19. _entMan = IoCManager.Resolve<IEntityManager>();
  20. _transform = _entMan.System<TransformSystem>();
  21. _label = new RichTextLabel { StyleClasses = { StyleNano.StyleClassItemStatus } };
  22. AddChild(_label);
  23. UpdateGpsDetails();
  24. }
  25. protected override void FrameUpdate(FrameEventArgs args)
  26. {
  27. base.FrameUpdate(args);
  28. // don't display the label if the gps component is being removed
  29. if (_parent.Comp.LifeStage > ComponentLifeStage.Running)
  30. {
  31. _label.Visible = false;
  32. return;
  33. }
  34. _updateDif += args.DeltaSeconds;
  35. if (_updateDif < _parent.Comp.UpdateRate)
  36. return;
  37. _updateDif -= _parent.Comp.UpdateRate;
  38. UpdateGpsDetails();
  39. }
  40. private void UpdateGpsDetails()
  41. {
  42. var posText = "Error";
  43. if (_entMan.TryGetComponent(_parent, out TransformComponent? transComp))
  44. {
  45. var pos = _transform.GetMapCoordinates(_parent.Owner, xform: transComp);
  46. var x = (int)pos.X;
  47. var y = (int)pos.Y;
  48. posText = $"({x}, {y})";
  49. }
  50. _label.SetMarkup(Loc.GetString("handheld-gps-coordinates-title", ("coordinates", posText)));
  51. }
  52. }