TreeLine.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using Robust.Client.Graphics;
  2. using Robust.Client.UserInterface;
  3. using Robust.Shared.Utility;
  4. namespace Content.Client.UserInterface.Controls.FancyTree;
  5. /// <summary>
  6. /// This is a basic control that draws the lines connecting parents & children in a tree.
  7. /// </summary>
  8. /// <remarks>
  9. /// Ideally this would just be a draw method in <see cref="TreeItem"/>, but sadly the draw override gets called BEFORE children are drawn.
  10. /// </remarks>
  11. public sealed class TreeLine : Control
  12. {
  13. protected override void Draw(DrawingHandleScreen handle)
  14. {
  15. base.Draw(handle);
  16. // This is basically just a shitty hack to call Draw() after children get drawn.
  17. if (Parent is not TreeItem parent)
  18. return;
  19. if (!parent.Expanded || !parent.Tree.DrawLines || parent.Body.ChildCount == 0)
  20. return;
  21. var width = Math.Max(1, (int) (parent.Tree.LineWidth * UIScale));
  22. var w1 = width / 2;
  23. var w2 = width - w1;
  24. var global = parent.GlobalPixelPosition;
  25. var iconPos = parent.Icon.GlobalPixelPosition - global;
  26. var iconSize = parent.Icon.PixelSize;
  27. var x = iconPos.X + iconSize.X / 2;
  28. DebugTools.Assert(parent.Icon.Visible);
  29. var buttonPos = parent.Button.GlobalPixelPosition - global;
  30. var buttonSize = parent.Button.PixelSize;
  31. var y1 = buttonPos.Y + buttonSize.Y;
  32. var lastItem = (TreeItem) parent.Body.GetChild(parent.Body.ChildCount - 1);
  33. var childPos = lastItem.Button.GlobalPixelPosition - global;
  34. var y2 = childPos.Y + lastItem.Button.PixelSize.Y / 2;
  35. // Vertical line
  36. var rect = new UIBox2i((x - w1, y1), (x + w2, y2));
  37. handle.DrawRect(rect, parent.Tree.LineColor);
  38. // Horizontal lines
  39. var dx = Math.Max(1, (int) (FancyTree.Indentation * UIScale / 2));
  40. foreach (var child in parent.Body.Children)
  41. {
  42. var item = (TreeItem) child;
  43. var pos = item.Button.GlobalPixelPosition - global;
  44. var y = pos.Y + item.Button.PixelSize.Y / 2;
  45. rect = new UIBox2i((x - w1, y - w1), (x + dx, y + w2));
  46. handle.DrawRect(rect, parent.Tree.LineColor);
  47. }
  48. }
  49. }