Skip to content

Commit

Permalink
Extract magnetize function
Browse files Browse the repository at this point in the history
  • Loading branch information
Sdmitrioul committed Jun 2, 2023
1 parent 37226ff commit 048042e
Showing 1 changed file with 23 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,40 +146,34 @@ class FBConnectionController(context: EditorContext, view: NetworkConnectionView
}

private fun magnetizeHorizontal(index: Int, bendPoints: MutableList<Point>) {
if (index >= bendPoints.size) {
return
}
val u = bendPoints[index - 1]
val v = bendPoints[index]
val uPrev = bendPoints[index - 2]
val vNext = bendPoints[index + 1]
if (abs(vNext.y - v.y) < scale(SELECTION_PADDING)) {
u.y = vNext.y
bendPoints.removeAt(index + 1)
bendPoints.removeAt(index)
}
if (abs(u.y - uPrev.y) < scale(SELECTION_PADDING)) {
v.y = uPrev.y
bendPoints.removeAt(index - 1)
bendPoints.removeAt(index - 2)
}
magnetize(index, bendPoints, { p -> p.y }, { p, v -> p.y = v })
}

private fun magnetizeVertical(index: Int, bendPoints: MutableList<Point>) {
magnetize(index, bendPoints, { p -> p.x }, { p, v -> p.x = v })
}

private fun magnetize(
index: Int,
bendPoints: MutableList<Point>,
extractor: (p: Point) -> Int,
setter: (p: Point, x: Int) -> Unit
) {
if (index >= bendPoints.size) {
return
}
val u = bendPoints[index - 1]
val v = bendPoints[index]
val uPrev = if (index - 2 >= 0) bendPoints[index - 2] else null
val vNext = if (index + 1 < bendPoints.size) bendPoints[index + 1] else null
if (vNext != null && abs(vNext.x - v.x) < scale(SELECTION_PADDING)) {
u.x = vNext.x
if (vNext != null && abs(extractor(vNext) - extractor(v)) < scale(SELECTION_PADDING)) {
setter(u, extractor(vNext))
bendPoints.removeAt(index + 1)
bendPoints.removeAt(index)
}
if (uPrev != null && abs(u.x - uPrev.x) < scale(SELECTION_PADDING)) {
v.x = uPrev.x
setter(v, extractor(uPrev))
bendPoints.removeAt(index - 1)
bendPoints.removeAt(index - 2)
}
Expand Down Expand Up @@ -330,10 +324,12 @@ class FBConnectionController(context: EditorContext, view: NetworkConnectionView
x1 = s.x + scale(ENDPOINTS_PADDING)
x2 = ntx - scale(ENDPOINTS_PADDING)
y = (s.y + nty) / 2
if (y >= s.y && y - scale(ENDPOINTS_PADDING) < s.y) {
y = s.y + scale(ENDPOINTS_PADDING)
y = if (y >= s.y && y - scale(ENDPOINTS_PADDING) < s.y) {
s.y + scale(ENDPOINTS_PADDING)
} else if (y < s.y && y + scale(ENDPOINTS_PADDING) > s.y) {
y = s.y - scale(ENDPOINTS_PADDING)
s.y - scale(ENDPOINTS_PADDING)
} else {
y
}
}

Expand Down Expand Up @@ -383,10 +379,12 @@ class FBConnectionController(context: EditorContext, view: NetworkConnectionView
x1 = nsx + scale(ENDPOINTS_PADDING)
x2 = t.x - scale(ENDPOINTS_PADDING)
y = (t.y + nsy) / 2
if (y >= t.y && y - scale(ENDPOINTS_PADDING) < t.y) {
y = t.y + scale(ENDPOINTS_PADDING)
y = if (y >= t.y && y - scale(ENDPOINTS_PADDING) < t.y) {
t.y + scale(ENDPOINTS_PADDING)
} else if (y < t.y && y + scale(ENDPOINTS_PADDING) > t.y) {
y = t.y - scale(ENDPOINTS_PADDING)
t.y - scale(ENDPOINTS_PADDING)
} else {
y
}
}

Expand Down

0 comments on commit 048042e

Please sign in to comment.