Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface ITechTreeGridLayout
- {
- float Row { get; }
- float Column { get; }
- int Act { get; }
- }
- public interface ITechTreeContainerLayout
- {
- int Act { get; }
- }
- public class TechTreeGridLayoutGroup : LayoutGroup
- {
- [SerializeField] private Vector2 cellSize;
- [SerializeField] private float padding;
- [SerializeField] private float actPadding;
- float minColumn;
- float maxColumn;
- public override void CalculateLayoutInputHorizontal()
- {
- base.CalculateLayoutInputHorizontal();
- minColumn = float.MaxValue;
- maxColumn = float.MinValue;
- foreach (var child in rectChildren)
- {
- var gridLayout = child.GetComponent<ITechTreeGridLayout>();
- var container = child.GetComponent<ITechTreeContainerLayout>();
- if (gridLayout != null)
- {
- minColumn = Mathf.Min(minColumn, gridLayout.Column);
- maxColumn = Mathf.Max(maxColumn, gridLayout.Column);
- }
- }
- float columns = maxColumn - minColumn; // edge case gdy nie ma dzieci
- if (rectChildren.Count == 0)
- columns = 0;
- var width = columns * cellSize.x + Mathf.Max(columns - 1, 0) * padding;
- SetLayoutInputForAxis(width, width, 0, 0);
- }
- private List<(float min, float max, bool any)> rows = new List<(float, float, bool)>();
- public override void CalculateLayoutInputVertical()
- {
- for (int i = 0; i < rows.Count; ++i)
- rows[i] = (float.MaxValue, float.MinValue, false);
- foreach (var child in rectChildren)
- {
- var gridLayout = child.GetComponent<ITechTreeGridLayout>();
- int act = gridLayout.Act;
- EnsureActs(act);
- var minRows = Mathf.Min(rows[act].min, gridLayout.Row);
- var maxRows = Mathf.Max(rows[act].max, gridLayout.Row);
- rows[act] = (minRows, maxRows, true);
- }
- for (int i = 0; i < rows.Count; ++i)
- {
- if (!rows[i].any)
- rows[i] = (0, 0, false);
- }
- float totalHeight = 0;
- bool any = false;
- for (int i = 0; i < rows.Count; ++i)
- {
- if (!rows[i].any)
- continue;
- any = true;
- float row = rows[i].max - rows[i].min;
- totalHeight += row * cellSize.y + Mathf.Max(row - 1, 0) * padding + actPadding;
- }
- if (any)
- totalHeight -= actPadding;
- SetLayoutInputForAxis(totalHeight, totalHeight, 0, 0);
- }
- private void EnsureActs(int act)
- {
- while (rows.Count <= act)
- {
- rows.Add((float.MaxValue, float.MinValue, false));
- }
- }
- public override void SetLayoutHorizontal()
- {
- foreach (var child in rectChildren)
- {
- var gridLayout = child.GetComponent<ITechTreeGridLayout>();
- var container = child.GetComponent<ITechTreeContainerLayout>();
- if (gridLayout != null)
- {
- var col = gridLayout.Column - minColumn;
- var pos = col * cellSize.x + Mathf.Max(col - 1, 0) * padding;
- SetChildAlongAxis(child, 0, pos, cellSize.x);
- }
- else if (container != null)
- {
- SetChildAlongAxis(child, 0, 0, cellSize.x * (maxColumn - minColumn));
- }
- }
- }
- public override void SetLayoutVertical()
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement