Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix node placement #8277

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 6 additions & 29 deletions app/gui2/src/components/ComponentBrowser/placement.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { bail } from '@/util/assert'
import { MultiRange, Range } from '@/util/range'
import { Rect } from '@/util/rect'
import { Vec2 } from '@/util/vec2'

Expand Down Expand Up @@ -42,23 +41,12 @@ export function nonDictatedPlacement(
const initialRect = new Rect(initialPosition, nodeSize)
let top = initialPosition.y
const height = nodeSize.y
const minimumVerticalSpace = height + gap * 2
const bottom = () => top + height
const occupiedYRanges = new MultiRange()
for (const rect of nodeRects) {
const nodeRectsSorted = Array.from(nodeRects).sort((a, b) => a.top - b.top)
for (const rect of nodeRectsSorted) {
if (initialRect.intersectsX(rect) && rect.bottom + gap > top) {
if (rect.top - bottom() >= gap) {
const range = new Range(rect.top, rect.bottom)
occupiedYRanges.insert(range, range.expand(gap))
} else {
if (rect.top - bottom() < gap) {
top = rect.bottom + gap
const rangeIncludingTop = occupiedYRanges
.remove(new Range(-Infinity, rect.bottom + minimumVerticalSpace))
.at(-1)
if (rangeIncludingTop) {
top = Math.max(top, rangeIncludingTop.end + gap)
occupiedYRanges.remove(rangeIncludingTop)
}
}
}
}
Expand Down Expand Up @@ -105,24 +93,13 @@ export function previousNodeDictatedPlacement(
let left = initialLeft
const width = nodeSize.x
const right = () => left + width
const minimumHorizontalSpace = width + gap * 2
const initialPosition = new Vec2(left, top)
const initialRect = new Rect(initialPosition, nodeSize)
const occupiedXRanges = new MultiRange()
for (const rect of nodeRects) {
const sortedNodeRects = Array.from(nodeRects).sort((a, b) => a.left - b.left)
for (const rect of sortedNodeRects) {
if (initialRect.intersectsY(rect) && rect.right + gap > left) {
if (rect.left - right() >= gap) {
const range = new Range(rect.left, rect.right)
occupiedXRanges.insert(range, range.expand(gap))
} else {
if (rect.left - right() < gap) {
left = rect.right + gap
const rangeIncludingLeft = occupiedXRanges
.remove(new Range(-Infinity, rect.right + minimumHorizontalSpace))
.at(-1)
if (rangeIncludingLeft) {
left = Math.max(left, rangeIncludingLeft.end + gap)
occupiedXRanges.remove(rangeIncludingLeft)
}
}
}
}
Expand Down
Loading