From 048042e2164ede1767fd748ea72c9706a286a76f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9DDmitri?= Date: Fri, 2 Jun 2023 23:30:34 +0200 Subject: [PATCH] Extract magnetize function --- .../fbnetwork/FBConnectionController.kt | 48 +++++++++---------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/code/richediting/src/main/kotlin/org/fbme/ide/richediting/adapters/fbnetwork/FBConnectionController.kt b/code/richediting/src/main/kotlin/org/fbme/ide/richediting/adapters/fbnetwork/FBConnectionController.kt index 27fe94e7c..43e7b68e7 100644 --- a/code/richediting/src/main/kotlin/org/fbme/ide/richediting/adapters/fbnetwork/FBConnectionController.kt +++ b/code/richediting/src/main/kotlin/org/fbme/ide/richediting/adapters/fbnetwork/FBConnectionController.kt @@ -146,26 +146,19 @@ class FBConnectionController(context: EditorContext, view: NetworkConnectionView } private fun magnetizeHorizontal(index: Int, bendPoints: MutableList) { - 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) { + magnetize(index, bendPoints, { p -> p.x }, { p, v -> p.x = v }) + } + + private fun magnetize( + index: Int, + bendPoints: MutableList, + extractor: (p: Point) -> Int, + setter: (p: Point, x: Int) -> Unit + ) { if (index >= bendPoints.size) { return } @@ -173,13 +166,14 @@ class FBConnectionController(context: EditorContext, view: NetworkConnectionView 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) } @@ -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 } } @@ -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 } }