TableTower/ui/board_layout.gd

108 lines
4 KiB
GDScript3
Raw Permalink Normal View History

class_name BoardLayout
extends RefCounted
## Pure layout helpers for whole blocks on the board (group frames, collapsed
## group cards, loose cards), given as world rects. Each returns one offset
## per rect, in the same order; Main moves the blocks.
## Closes the big gaps between blocks while keeping their order.
##
## Blocks whose rows overlap form a band (top to bottom); inside a band,
## blocks whose columns overlap form a column (left to right), which keeps its
## inner layout. Gaps wider than `gap_x` between columns shrink to `gap_x`, the
## first column of every band moves to the board's left edge, and gaps wider
## than `gap_y` between bands shrink to `gap_y`. The mapping only ever moves
## blocks left and up, in order, so nothing that didn't overlap before
## overlaps after, and reading order (left to right, top to bottom) holds.
static func pack(rects: Array, gap_x: float = 60.0, gap_y: float = 60.0) -> Array[Vector2]:
var out: Array[Vector2] = []
out.resize(rects.size())
for i in rects.size():
out[i] = Vector2.ZERO
if rects.is_empty():
return out
var left := INF
for r: Rect2 in rects:
left = minf(left, r.position.x)
var bands := _clusters(rects, range(rects.size()), false)
var y_cursor := 0.0
for b in bands.size():
var band: Dictionary = bands[b]
var new_top: float = band["lo"]
if b > 0:
var prev: Dictionary = bands[b - 1]
new_top = y_cursor + minf(float(band["lo"]) - float(prev["hi"]), gap_y)
var dy := new_top - float(band["lo"])
y_cursor = float(band["hi"]) + dy
var cols := _clusters(rects, band["members"], true)
var x_cursor := left
for c in cols.size():
var col: Dictionary = cols[c]
var new_left := left
if c > 0:
var pc: Dictionary = cols[c - 1]
new_left = x_cursor + minf(float(col["lo"]) - float(pc["hi"]), gap_x)
var dx := new_left - float(col["lo"])
x_cursor = float(col["hi"]) + dx
for i: int in col["members"]:
out[i] = Vector2(dx, dy)
return out
## Groups indices whose rects overlap along one axis (x when `horizontal`)
## into clusters sorted along that axis: [{lo, hi, members}].
static func _clusters(rects: Array, idx: Array, horizontal: bool) -> Array:
var order := idx.duplicate()
order.sort_custom(func(a: int, b: int) -> bool:
var ra: Rect2 = rects[a]
var rb: Rect2 = rects[b]
return (ra.position.x < rb.position.x) if horizontal else (ra.position.y < rb.position.y))
var out: Array = []
for i: int in order:
var r: Rect2 = rects[i]
var lo := r.position.x if horizontal else r.position.y
var hi := r.end.x if horizontal else r.end.y
if not out.is_empty() and lo < float(out[-1]["hi"]):
out[-1]["hi"] = maxf(float(out[-1]["hi"]), hi)
(out[-1]["members"] as Array).append(i)
else:
out.append({"lo": lo, "hi": hi, "members": [i]})
return out
## Makes room for a block that grew from `compact` to `grown` (a group
## expanding in place; both share their top-left corner). Blocks to its right
## whose rows it now covers shift right together, and the blocks below it
## shift down together, each by just enough to clear it with `gap`. Blocks
## that already clear it don't move. Returns one offset per rect.
static func make_room(rects: Array, compact: Rect2, grown: Rect2, gap: float = 60.0) -> Array[Vector2]:
var out: Array[Vector2] = []
out.resize(rects.size())
for i in rects.size():
out[i] = Vector2.ZERO
var right: Array[int] = []
var below: Array[int] = []
for i in rects.size():
var r: Rect2 = rects[i]
if r.position.x >= compact.end.x - 1.0 and r.position.y < grown.end.y + gap and r.end.y > grown.position.y:
right.append(i)
elif r.position.y >= compact.end.y - 1.0:
below.append(i)
var dx := 0.0
for i in right:
var r: Rect2 = rects[i]
if r.position.y < grown.end.y and r.end.y > grown.position.y:
dx = maxf(dx, grown.end.x + gap - r.position.x)
if dx > 0.0:
for i in right:
out[i].x = dx
var dy := 0.0
for i in below:
var r: Rect2 = rects[i]
if r.position.x < grown.end.x + gap and r.end.x > grown.position.x - gap:
dy = maxf(dy, grown.end.y + gap - r.position.y)
if dy > 0.0:
for i in below:
out[i].y = dy
return out