|
| 1 | +local flow = {} |
| 2 | + |
| 3 | +--- Calculates positions for all children in a flow layout |
| 4 | +--- @param instance table The layout instance |
| 5 | +--- - container: the container to layout |
| 6 | +--- - options: layout options |
| 7 | +--- - direction: "horizontal" or "vertical" (default: "horizontal") |
| 8 | +--- - spacing: gap between elements (default: 0) |
| 9 | +--- - padding: padding around the flow (default: 0) |
| 10 | +--- - align: "start", "center", or "end" (default: "start") |
| 11 | +function flow.calculate(instance) |
| 12 | + local container = instance.container |
| 13 | + local options = instance.options or {} |
| 14 | + |
| 15 | + local children = container.get("children") |
| 16 | + local containerWidth = container.get("width") |
| 17 | + local containerHeight = container.get("height") |
| 18 | + |
| 19 | + local direction = options.direction or "horizontal" |
| 20 | + local spacing = options.spacing or 0 |
| 21 | + local padding = options.padding or 0 |
| 22 | + local align = options.align or "start" |
| 23 | + |
| 24 | + local childCount = #children |
| 25 | + if childCount == 0 then |
| 26 | + instance._positions = {} |
| 27 | + return |
| 28 | + end |
| 29 | + |
| 30 | + local positions = {} |
| 31 | + |
| 32 | + if direction == "horizontal" then |
| 33 | + local rows = {} |
| 34 | + local currentRow = {} |
| 35 | + local currentX = padding + 1 |
| 36 | + local currentY = padding + 1 |
| 37 | + local maxHeightInRow = 0 |
| 38 | + |
| 39 | + for i, child in ipairs(children) do |
| 40 | + local childWidth = child.get("width") |
| 41 | + local childHeight = child.get("height") |
| 42 | + |
| 43 | + if currentX + childWidth - 1 > containerWidth - padding and currentX > padding + 1 then |
| 44 | + |
| 45 | + table.insert(rows, { |
| 46 | + children = currentRow, |
| 47 | + y = currentY, |
| 48 | + height = maxHeightInRow |
| 49 | + }) |
| 50 | + |
| 51 | + currentRow = {} |
| 52 | + currentX = padding + 1 |
| 53 | + currentY = currentY + maxHeightInRow + spacing |
| 54 | + maxHeightInRow = 0 |
| 55 | + end |
| 56 | + |
| 57 | + table.insert(currentRow, { |
| 58 | + child = child, |
| 59 | + width = childWidth, |
| 60 | + height = childHeight |
| 61 | + }) |
| 62 | + |
| 63 | + currentX = currentX + childWidth + spacing |
| 64 | + maxHeightInRow = math.max(maxHeightInRow, childHeight) |
| 65 | + end |
| 66 | + |
| 67 | + if #currentRow > 0 then |
| 68 | + table.insert(rows, { |
| 69 | + children = currentRow, |
| 70 | + y = currentY, |
| 71 | + height = maxHeightInRow |
| 72 | + }) |
| 73 | + end |
| 74 | + |
| 75 | + for _, row in ipairs(rows) do |
| 76 | + local rowWidth = 0 |
| 77 | + for j, item in ipairs(row.children) do |
| 78 | + rowWidth = rowWidth + item.width |
| 79 | + if j < #row.children then |
| 80 | + rowWidth = rowWidth + spacing |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + local startX = padding + 1 |
| 85 | + if align == "center" then |
| 86 | + startX = padding + 1 + math.floor((containerWidth - 2 * padding - rowWidth) / 2) |
| 87 | + elseif align == "end" then |
| 88 | + startX = containerWidth - padding - rowWidth + 1 |
| 89 | + end |
| 90 | + |
| 91 | + local x = startX |
| 92 | + for _, item in ipairs(row.children) do |
| 93 | + local y = row.y |
| 94 | + if align == "center" then |
| 95 | + y = row.y + math.floor((row.height - item.height) / 2) |
| 96 | + elseif align == "end" then |
| 97 | + y = row.y + row.height - item.height |
| 98 | + end |
| 99 | + |
| 100 | + positions[item.child] = { |
| 101 | + x = x, |
| 102 | + y = y, |
| 103 | + width = item.width, |
| 104 | + height = item.height |
| 105 | + } |
| 106 | + |
| 107 | + x = x + item.width + spacing |
| 108 | + end |
| 109 | + end |
| 110 | + |
| 111 | + else |
| 112 | + local columns = {} |
| 113 | + local currentColumn = {} |
| 114 | + local currentX = padding + 1 |
| 115 | + local currentY = padding + 1 |
| 116 | + local maxWidthInColumn = 0 |
| 117 | + |
| 118 | + for i, child in ipairs(children) do |
| 119 | + local childWidth = child.get("width") |
| 120 | + local childHeight = child.get("height") |
| 121 | + |
| 122 | + if currentY + childHeight - 1 > containerHeight - padding and currentY > padding + 1 then |
| 123 | + table.insert(columns, { |
| 124 | + children = currentColumn, |
| 125 | + x = currentX, |
| 126 | + width = maxWidthInColumn |
| 127 | + }) |
| 128 | + |
| 129 | + currentColumn = {} |
| 130 | + currentY = padding + 1 |
| 131 | + currentX = currentX + maxWidthInColumn + spacing |
| 132 | + maxWidthInColumn = 0 |
| 133 | + end |
| 134 | + |
| 135 | + table.insert(currentColumn, { |
| 136 | + child = child, |
| 137 | + width = childWidth, |
| 138 | + height = childHeight |
| 139 | + }) |
| 140 | + |
| 141 | + currentY = currentY + childHeight + spacing |
| 142 | + maxWidthInColumn = math.max(maxWidthInColumn, childWidth) |
| 143 | + end |
| 144 | + |
| 145 | + if #currentColumn > 0 then |
| 146 | + table.insert(columns, { |
| 147 | + children = currentColumn, |
| 148 | + x = currentX, |
| 149 | + width = maxWidthInColumn |
| 150 | + }) |
| 151 | + end |
| 152 | + |
| 153 | + for _, column in ipairs(columns) do |
| 154 | + local columnHeight = 0 |
| 155 | + for j, item in ipairs(column.children) do |
| 156 | + columnHeight = columnHeight + item.height |
| 157 | + if j < #column.children then |
| 158 | + columnHeight = columnHeight + spacing |
| 159 | + end |
| 160 | + end |
| 161 | + |
| 162 | + local startY = padding + 1 |
| 163 | + if align == "center" then |
| 164 | + startY = padding + 1 + math.floor((containerHeight - 2 * padding - columnHeight) / 2) |
| 165 | + elseif align == "end" then |
| 166 | + startY = containerHeight - padding - columnHeight + 1 |
| 167 | + end |
| 168 | + |
| 169 | + local y = startY |
| 170 | + for _, item in ipairs(column.children) do |
| 171 | + local x = column.x |
| 172 | + if align == "center" then |
| 173 | + x = column.x + math.floor((column.width - item.width) / 2) |
| 174 | + elseif align == "end" then |
| 175 | + x = column.x + column.width - item.width |
| 176 | + end |
| 177 | + |
| 178 | + positions[item.child] = { |
| 179 | + x = x, |
| 180 | + y = y, |
| 181 | + width = item.width, |
| 182 | + height = item.height |
| 183 | + } |
| 184 | + |
| 185 | + y = y + item.height + spacing |
| 186 | + end |
| 187 | + end |
| 188 | + end |
| 189 | + |
| 190 | + instance._positions = positions |
| 191 | +end |
| 192 | + |
| 193 | +return flow |
0 commit comments