Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1.  
  2. public interface ITechTreeGridLayout
  3. {
  4. float Row { get; }
  5. float Column { get; }
  6. int Act { get; }
  7. }
  8.  
  9. public interface ITechTreeContainerLayout
  10. {
  11. int Act { get; }
  12. }
  13.  
  14.  
  15. public class TechTreeGridLayoutGroup : LayoutGroup
  16. {
  17. [SerializeField] private Vector2 cellSize;
  18. [SerializeField] private float padding;
  19. [SerializeField] private float actPadding;
  20.  
  21. float minColumn;
  22. float maxColumn;
  23.  
  24. public override void CalculateLayoutInputHorizontal()
  25. {
  26. base.CalculateLayoutInputHorizontal();
  27.  
  28. minColumn = float.MaxValue;
  29. maxColumn = float.MinValue;
  30.  
  31. foreach (var child in rectChildren)
  32. {
  33. var gridLayout = child.GetComponent<ITechTreeGridLayout>();
  34. var container = child.GetComponent<ITechTreeContainerLayout>();
  35.  
  36. if (gridLayout != null)
  37. {
  38. minColumn = Mathf.Min(minColumn, gridLayout.Column);
  39. maxColumn = Mathf.Max(maxColumn, gridLayout.Column);
  40. }
  41. }
  42.  
  43. float columns = maxColumn - minColumn; // edge case gdy nie ma dzieci
  44.  
  45. if (rectChildren.Count == 0)
  46. columns = 0;
  47.  
  48. var width = columns * cellSize.x + Mathf.Max(columns - 1, 0) * padding;
  49. SetLayoutInputForAxis(width, width, 0, 0);
  50. }
  51.  
  52. private List<(float min, float max, bool any)> rows = new List<(float, float, bool)>();
  53.  
  54. public override void CalculateLayoutInputVertical()
  55. {
  56. for (int i = 0; i < rows.Count; ++i)
  57. rows[i] = (float.MaxValue, float.MinValue, false);
  58.  
  59. foreach (var child in rectChildren)
  60. {
  61. var gridLayout = child.GetComponent<ITechTreeGridLayout>();
  62. int act = gridLayout.Act;
  63. EnsureActs(act);
  64. var minRows = Mathf.Min(rows[act].min, gridLayout.Row);
  65. var maxRows = Mathf.Max(rows[act].max, gridLayout.Row);
  66. rows[act] = (minRows, maxRows, true);
  67. }
  68.  
  69. for (int i = 0; i < rows.Count; ++i)
  70. {
  71. if (!rows[i].any)
  72. rows[i] = (0, 0, false);
  73. }
  74.  
  75. float totalHeight = 0;
  76. bool any = false;
  77.  
  78. for (int i = 0; i < rows.Count; ++i)
  79. {
  80. if (!rows[i].any)
  81. continue;
  82. any = true;
  83. float row = rows[i].max - rows[i].min;
  84. totalHeight += row * cellSize.y + Mathf.Max(row - 1, 0) * padding + actPadding;
  85. }
  86. if (any)
  87. totalHeight -= actPadding;
  88.  
  89. SetLayoutInputForAxis(totalHeight, totalHeight, 0, 0);
  90. }
  91.  
  92. private void EnsureActs(int act)
  93. {
  94. while (rows.Count <= act)
  95. {
  96. rows.Add((float.MaxValue, float.MinValue, false));
  97. }
  98. }
  99.  
  100. public override void SetLayoutHorizontal()
  101. {
  102. foreach (var child in rectChildren)
  103. {
  104. var gridLayout = child.GetComponent<ITechTreeGridLayout>();
  105. var container = child.GetComponent<ITechTreeContainerLayout>();
  106.  
  107. if (gridLayout != null)
  108. {
  109. var col = gridLayout.Column - minColumn;
  110.  
  111. var pos = col * cellSize.x + Mathf.Max(col - 1, 0) * padding;
  112.  
  113. SetChildAlongAxis(child, 0, pos, cellSize.x);
  114. }
  115. else if (container != null)
  116. {
  117. SetChildAlongAxis(child, 0, 0, cellSize.x * (maxColumn - minColumn));
  118. }
  119. }
  120. }
  121.  
  122. public override void SetLayoutVertical()
  123. {
  124.  
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement