Skip to content

Commit

Permalink
only add knots in layer0
Browse files Browse the repository at this point in the history
  • Loading branch information
MichiSpebach committed Sep 10, 2024
1 parent b3ed3cc commit ed7a4d8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
62 changes: 48 additions & 14 deletions plugin-src/boxOrderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Layer } from './boxOrderer/Layer'
import { Suggestion } from './boxOrderer/LayerSide'
import { NodeToOrder } from './boxOrderer/NodeToOrder'
import { EmptySpaceFinder } from '../dist/core/box/EmptySpaceFinder'
import { LinkEnd } from '../dist/core/link/LinkEnd'

contextMenu.addFolderBoxMenuItem((box: FolderBox) => new MenuItemFile({label: 'order boxes', click: () => orderBoxes(box)}))

Expand Down Expand Up @@ -42,10 +43,11 @@ Box.Sidebar.BasicToolkit.add({

async function orderBoxes(box: FolderBox): Promise<void> {
console.log(`order boxes in '${box.getName()}'...`)
const childs: (Box|NodeWidget)[] = box.getChilds()
const children: (Box|NodeWidget)[] = [...box.getChilds()]
const borderingLinks: Link[] = box.borderingLinks.getAll()

const layer0Nodes: NodeToOrder[] = []
const layer1Nodes: NodeToOrder[] = []
for (const link of borderingLinks) {
const child: Box|NodeWidget|undefined = getEndThatIsInBox(link)
if (child) {
Expand All @@ -57,20 +59,62 @@ async function orderBoxes(box: FolderBox): Promise<void> {
const intersections: LocalPosition[] = new LocalRect(0, 0, 100, 100).calculateIntersectionsWithLine(lineInLocalCoords)
if (intersections.length < 1) {
console.warn(`boxOrderer.orderBoxes(box: '${box.getName()}') intersections.length < 1`)
layer0Nodes.push({node: child, wishPosition: new LocalPosition(50, 50)})
if (child instanceof NodeWidget) {
layer0Nodes.push({node: child, wishPosition: new LocalPosition(50, 50)})
} else {
layer1Nodes.push({node: child, wishPosition: new LocalPosition(50, 50)})
}
children.splice(children.indexOf(child), 1)
continue
}
if (intersections.length > 1) {
console.warn(`boxOrderer.orderBoxes(box: '${box.getName()}') intersections.length > 1`)
}
layer0Nodes.push({node: child, wishPosition: intersections[0]})
if (child instanceof NodeWidget) {
layer0Nodes.push({node: child, wishPosition: intersections[0]})
} else {
layer1Nodes.push({node: child, wishPosition: intersections[0]})
}
children.splice(children.indexOf(child), 1)
} else {
console.warn(`boxOrderer.orderBoxes(box: '${box.getName()}') does not contain end of ${link.describe()}`)
}
}

for (const node of layer0Nodes) {
const borderingLinks = node.node.borderingLinks.getAll()
for (const borderingLink of borderingLinks) {
const otherEnd: LinkEnd = borderingLink.from.getTargetNodeId() === node.node.getId()
? borderingLink.to
: borderingLink.from
for (const child of children) {
if (otherEnd.isBoxInPath(child)) {
layer1Nodes.push({node: child, wishPosition: node.wishPosition})
}
}
}
}

const layer0 = new Layer(layer0Nodes)
await Promise.all(layer0.getSuggestions().map(async (suggestion: Suggestion) => {
await applySuggestions(layer0)

const layer1 = new Layer(layer1Nodes)
await applySuggestions(layer1)

await box.rearrangeBoxesWithoutMapData()
console.log(`...ordered boxes in '${box.getName()}'`)

function getEndThatIsInBox(link: Link): Box|NodeWidget|undefined {
for (const child of children) {
if (link.from.isBoxInPath(child) || link.to.isBoxInPath(child)) {
return child
}
}
}
}

async function applySuggestions(layer: Layer): Promise<void> {
await Promise.all(layer.getSuggestions().map(async (suggestion: Suggestion) => {
if (suggestion.node instanceof Box) {
await suggestion.node.updateMeasuresAndBorderingLinks({
x: suggestion.suggestedPosition.percentX,
Expand All @@ -83,16 +127,6 @@ async function orderBoxes(box: FolderBox): Promise<void> {
await suggestion.node.setPositionAndRenderAndSave(suggestion.suggestedPosition)
}
}))
await box.rearrangeBoxesWithoutMapData()
console.log(`...ordered boxes in '${box.getName()}'`)

function getEndThatIsInBox(link: Link): Box|NodeWidget|undefined {
for (const child of childs) {
if (link.from.isBoxInPath(child) || link.to.isBoxInPath(child)) {
return child
}
}
}
}

async function unorderBoxes(box: FolderBox): Promise<void> {
Expand Down
2 changes: 0 additions & 2 deletions plugin-src/boxOrderer/Layer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Box } from '../../dist/core/box/Box'
import { NodeWidget } from '../../dist/pluginFacade'
import { LayerSide, Suggestion } from './LayerSide'
import { NodeToOrder } from './NodeToOrder'

Expand Down

0 comments on commit ed7a4d8

Please sign in to comment.