Skip to main content

LayoutHelper2D

A class in Root.Township, the global namespace.

Lays out a number of equal elements in a grid on a background panel, shrinking them if needed. Used by the gymnasium prefab spawner to place its buttons.

Constructors​

MemberWhat it is
LayoutHelper2D(Vector3 backgroundSize, Vector3 elementSize, int elementCount, bool isVertical = true, float minBuffer = 0.025)Creates a LayoutHelper2D from backgroundSize, elementSize, elementCount, isVertical and minBuffer.

Fields​

10 private and internal fields
MemberWhat it is
private float clearanceHalf the panel thickness, so elements sit on its surface.
private int elementCountHow many elements to place.
private float elementHeightElement height (or depth on a flat panel) plus the buffer.
private float elementWidthElement width plus the buffer.
private Vector3 gridOffsetTop left corner of the panel, relative to its centre.
private Vector2Int gridSizeColumns and rows found by TryFitScaled.
private float heightPanel height, or depth on a flat panel.
private Func<float, float, Vector3> projectionEither ProjectVertical or ProjectHorizontal, chosen in the constructor.
private float scaleFactorScale found by CanFitObjects.
private float widthPanel width.

Methods​

MemberWhat it is
LayoutHelper2D.LayoutInfo GetPositionsAndScaleFactor()Returns the grid positions and scale. It relies on gridSize from CanFitObjects, which nothing calls, so it probably returns an empty result.
4 private and internal methods
MemberWhat it is
private bool CanFitObjects()Shrinks the scale in steps of 0.05 from 1 until the grid fits, giving up below 0.3. Nothing calls it in this code.
private Vector3 ProjectHorizontal(float width, float depth)Turns a grid position into a point on a flat panel, sitting clearance above it.
private Vector3 ProjectVertical(float width, float height)Turns a grid position into a point on an upright panel, clearance in front of it.
private bool TryFitScaled(float scaleFactor)Checks whether all elements fit at a scale and, if so, stores the column and row count.