From ab4358a317594fdac6ba9694583c6e06ceeefa12 Mon Sep 17 00:00:00 2001 From: Homka122 Date: Thu, 19 Sep 2024 15:35:50 +0300 Subject: [PATCH 1/4] refactor: refactor menu, change variables initilization --- src/main/kotlin/view/MenuView.kt | 27 ++++++------- src/main/kotlin/viewModel/MenuViewModel.kt | 46 +++------------------- 2 files changed, 19 insertions(+), 54 deletions(-) diff --git a/src/main/kotlin/view/MenuView.kt b/src/main/kotlin/view/MenuView.kt index 49914c9..3a5befc 100644 --- a/src/main/kotlin/view/MenuView.kt +++ b/src/main/kotlin/view/MenuView.kt @@ -86,26 +86,25 @@ fun DisplayDescription(name: String) { fun MenuView( viewModel: MenuViewModel ) { + var isNodeCreating by viewModel::isNodeCreating + var isClustering by viewModel::isClustering + var isRanked by viewModel::isRanked + var isAlgorithmMenuOpen by viewModel::isAlgorithmMenuOpen + Column( Modifier.fillMaxHeight().width(Config.menuWidth.dp).background(color = Color(0xFF3D3D3D)), horizontalAlignment = Alignment.CenterHorizontally, ) { Spacer(Modifier.height(25f.dp)) - MenuIcon( - "Nodes.svg", "AddNode.svg", Modifier.glow(viewModel.isNodeCreating) - ) { - viewModel.onNodeCreatingChange() - } + MenuIcon("Nodes.svg", "AddNode.svg", Modifier.glow(isNodeCreating)) { isNodeCreating = !isNodeCreating } MenuIcon("Ribs.svg", "AddEdge.svg", modifier = Modifier.alpha(0.2f)) - MenuIcon("Clustering.svg", "ClusterD.svg", Modifier.glow(viewModel.isClustering)) { - viewModel.onClusteringChange() - } - MenuIcon("PageRank.svg", "AnalysisGraph.svg", Modifier.glow(viewModel.isRanked)) { - viewModel.onRankedChange() - } - MenuIcon("Algorithm.svg", "Algorithms....svg", Modifier.glow(viewModel.isAlgorithmMenuOpen)) { - viewModel.onAlgorithmMenuOpenChange() - } + MenuIcon("Clustering.svg", "ClusterD.svg", Modifier.glow(isClustering)) { isClustering = !isClustering } + MenuIcon("PageRank.svg", "AnalysisGraph.svg", Modifier.glow(viewModel.isRanked)) { isRanked = !isRanked } + MenuIcon( + "Algorithm.svg", + "Algorithms....svg", + Modifier.glow(viewModel.isAlgorithmMenuOpen) + ) { isAlgorithmMenuOpen = !isAlgorithmMenuOpen } } } diff --git a/src/main/kotlin/viewModel/MenuViewModel.kt b/src/main/kotlin/viewModel/MenuViewModel.kt index 8279cbf..de14aca 100644 --- a/src/main/kotlin/viewModel/MenuViewModel.kt +++ b/src/main/kotlin/viewModel/MenuViewModel.kt @@ -1,49 +1,15 @@ package viewModel +import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.setValue import viewModel.canvas.CanvasViewModel class MenuViewModel( val canvasViewModel: CanvasViewModel ) { - var isNodeCreating - get() = canvasViewModel.isNodeCreatingMode - set(value) { - canvasViewModel.isNodeCreatingMode = value - } - - fun onNodeCreatingChange() { - isNodeCreating = !isNodeCreating - } - - var isClustering - get() = canvasViewModel.isClustering - set(value) { - canvasViewModel.isClustering = value - } - - fun onClusteringChange() { - isClustering = !isClustering - } - - var isRanked - get() = canvasViewModel.isRanked - set(value) { - canvasViewModel.isRanked = value - } - - fun onRankedChange() { - isRanked = !isRanked - } - - val _isAlgorithmMenuOpen = mutableStateOf(false) - var isAlgorithmMenuOpen - get() = _isAlgorithmMenuOpen.value - set(value) { - _isAlgorithmMenuOpen.value = value - } - - fun onAlgorithmMenuOpenChange() { - isAlgorithmMenuOpen = !isAlgorithmMenuOpen - } + var isNodeCreating by canvasViewModel::isNodeCreatingMode + var isClustering by canvasViewModel::isClustering + var isRanked by canvasViewModel::isRanked + var isAlgorithmMenuOpen by mutableStateOf(false) } \ No newline at end of file From 4384fb96a77a52b5bc099d7250b4c1d9252faf8d Mon Sep 17 00:00:00 2001 From: Homka122 Date: Thu, 19 Sep 2024 16:02:04 +0300 Subject: [PATCH 2/4] refactor: refactor vertecies' state initilization --- .../viewModel/canvas/CanvasViewModel.kt | 9 ---- .../viewModel/canvas/VertexCanvasViewModel.kt | 54 ++++--------------- .../kotlin/viewModel/graph/VertexViewModel.kt | 33 +++--------- 3 files changed, 15 insertions(+), 81 deletions(-) diff --git a/src/main/kotlin/viewModel/canvas/CanvasViewModel.kt b/src/main/kotlin/viewModel/canvas/CanvasViewModel.kt index f8d6211..8d0c80a 100644 --- a/src/main/kotlin/viewModel/canvas/CanvasViewModel.kt +++ b/src/main/kotlin/viewModel/canvas/CanvasViewModel.kt @@ -48,7 +48,6 @@ class CanvasViewModel( val viewModel = graphViewModel.createVertex(coordinates) ?: return _vertices[viewModel] = VertexCanvasViewModel(viewModel, _zoom, _center, _canvasSize) - updateVertexes() } } @@ -58,7 +57,6 @@ class CanvasViewModel( get() = _zoom.value set(value) { _zoom.value = value - updateVertexes() updateEdges() } @@ -67,7 +65,6 @@ class CanvasViewModel( get() = _center.value set(value) { _center.value = value - updateVertexes() } private val _canvasSize = mutableStateOf(Offset(400f, 400f)) @@ -75,7 +72,6 @@ class CanvasViewModel( get() = _canvasSize.value set(value) { _canvasSize.value = value - updateVertexes() } private val _isOrientated = mutableStateOf(false) @@ -108,10 +104,6 @@ class CanvasViewModel( val edges get() = _edges - private fun updateVertexes() { - vertices.forEach { it.updateVertex() } - } - private fun updateEdges() { edges.forEach { it.updateEdge(zoom) } } @@ -148,7 +140,6 @@ class CanvasViewModel( fun onSizeChange(newSize: Float) { graphViewModel.onSizeChange(newSize) - updateVertexes() } fun onOrientatedChange(isOrientated: Boolean) { diff --git a/src/main/kotlin/viewModel/canvas/VertexCanvasViewModel.kt b/src/main/kotlin/viewModel/canvas/VertexCanvasViewModel.kt index 836a3c5..da6cfc2 100644 --- a/src/main/kotlin/viewModel/canvas/VertexCanvasViewModel.kt +++ b/src/main/kotlin/viewModel/canvas/VertexCanvasViewModel.kt @@ -11,59 +11,23 @@ class VertexCanvasViewModel( private val center: MutableState, private val canvasSize: MutableState ) { - private val _offset = mutableStateOf( - calculateOffset() - ) - var offset - get() = _offset.value - set(value) { - _offset.value = value - } - - private val _radius = mutableStateOf(calculateRadius()) - var radius - get() = _radius.value - set(value) { - _radius.value = value - } - - var color - get() = vertexViewModel.color - set(value) { - vertexViewModel.color = value - } - - private val _strokeWidth = mutableStateOf(calculateStrokeWidth()) - var strokeWidth - get() = _strokeWidth.value - set(value) { - _strokeWidth.value = value - } + var color by vertexViewModel::color - private val _textSize = mutableStateOf(calculateTextSize()) - var textSize - get() = _textSize.value - set(value) { - _textSize.value = value - } + val strokeWidth + get() = 8f * zoom.value + val radius + get() = vertexViewModel.radius * zoom.value + val offset + get() = calculateOffset() + val textSize + get() = vertexViewModel.radius * 0.6f * zoom.value fun onDrag(it: Offset): Unit { vertexViewModel.onDrag(it * (1f / zoom.value)) } - fun updateVertex() { - offset = calculateOffset() - radius = calculateRadius() - strokeWidth = calculateStrokeWidth() - textSize = calculateTextSize() - } - private fun calculateOffset() = Offset( (canvasSize.value.x / 2) + ((vertexViewModel.x - center.value.x) * zoom.value), (canvasSize.value.y / 2) + ((vertexViewModel.y - center.value.y) * zoom.value) ) - - private fun calculateRadius() = vertexViewModel.radius * zoom.value - private fun calculateStrokeWidth() = 8f * zoom.value - private fun calculateTextSize() = vertexViewModel.radius * 0.6f * zoom.value } \ No newline at end of file diff --git a/src/main/kotlin/viewModel/graph/VertexViewModel.kt b/src/main/kotlin/viewModel/graph/VertexViewModel.kt index 2806f7f..cdde6bb 100644 --- a/src/main/kotlin/viewModel/graph/VertexViewModel.kt +++ b/src/main/kotlin/viewModel/graph/VertexViewModel.kt @@ -1,6 +1,8 @@ package viewModel.graph +import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.setValue import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Color import androidx.compose.ui.unit.Dp @@ -15,33 +17,10 @@ class VertexViewModel( color: Color = Color.Black, radius: Dp = 8f.dp, ) { - private val _x = mutableStateOf(x) - private val _y = mutableStateOf(y) - private val _color = mutableStateOf(color) - private val _radius = mutableStateOf(radius) - - var x: Float - get() = _x.value - set(value) { - _x.value = value - } - - var y: Float - get() = _y.value - set(value) { - _y.value = value - } - - var color: Color - get() = _color.value - set(value) { - _color.value = value - } - var radius: Dp - get() = _radius.value - set(value) { - _radius.value = value - } + var x by mutableStateOf(x) + var y by mutableStateOf(y) + var color by mutableStateOf(color) + var radius by mutableStateOf(radius) val label get() = vertex.key.toString() From 17ff642c37e1886a26d6638b96b5185319646232 Mon Sep 17 00:00:00 2001 From: Homka122 Date: Thu, 19 Sep 2024 17:07:39 +0300 Subject: [PATCH 3/4] refactor: refactor canvas, edge, vertex state's initialization --- src/main/kotlin/view/canvas/EdgeCanvasView.kt | 2 +- .../viewModel/canvas/CanvasViewModel.kt | 78 ++++--------------- .../viewModel/canvas/EdgeCanvasViewModel.kt | 14 ++-- .../viewModel/canvas/VertexCanvasViewModel.kt | 22 +++--- .../kotlin/viewModel/graph/EdgeViewModel.kt | 17 +--- .../viewModel/graph/UndirectedViewModel.kt | 10 +-- 6 files changed, 40 insertions(+), 103 deletions(-) diff --git a/src/main/kotlin/view/canvas/EdgeCanvasView.kt b/src/main/kotlin/view/canvas/EdgeCanvasView.kt index ae3da58..e4679a1 100644 --- a/src/main/kotlin/view/canvas/EdgeCanvasView.kt +++ b/src/main/kotlin/view/canvas/EdgeCanvasView.kt @@ -44,7 +44,7 @@ fun EdgeCanvasView( ) } - if (viewModel.showOrientation.value) { + if (viewModel.showOrientation) { drawLine( start = end, end = end - rotateVector(radiusVectorSecond * 0.8f, 30.0), diff --git a/src/main/kotlin/viewModel/canvas/CanvasViewModel.kt b/src/main/kotlin/viewModel/canvas/CanvasViewModel.kt index 8d0c80a..e75c0a4 100644 --- a/src/main/kotlin/viewModel/canvas/CanvasViewModel.kt +++ b/src/main/kotlin/viewModel/canvas/CanvasViewModel.kt @@ -3,8 +3,10 @@ package viewModel.canvas import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.PointerMatcher import androidx.compose.foundation.gestures.detectDragGestures +import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateMapOf import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.setValue import androidx.compose.ui.awt.awtEventOrNull import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Color @@ -22,24 +24,16 @@ class CanvasViewModel( ) { private val graphViewModel = UndirectedViewModel(graph, true) - var isClustering - get() = graphViewModel.clustering - set(value) { - graphViewModel.clustering = value - } - - var isRanked - get() = graphViewModel.ranked - set(value) { - graphViewModel.ranked = value - } + var isClustering by graphViewModel::clustering + var isRanked by graphViewModel::ranked - val _isNodeCreatingMode = mutableStateOf(false) - var isNodeCreatingMode - get() = _isNodeCreatingMode.value - set(value) { - _isNodeCreatingMode.value = value - } + var isNodeCreatingMode by mutableStateOf(false) + var zoom by mutableStateOf(1f) + var center by mutableStateOf(Offset(0f, 0f)) + var canvasSize by mutableStateOf(Offset(400f, 400f)) + var isOrientated by mutableStateOf(false) + + private val _vertices = mutableStateMapOf() fun createNode(offset: Offset) { if (isNodeCreatingMode) { @@ -47,45 +41,13 @@ class CanvasViewModel( println(offset - (canvasSize / 2.0F)) val viewModel = graphViewModel.createVertex(coordinates) ?: return - _vertices[viewModel] = VertexCanvasViewModel(viewModel, _zoom, _center, _canvasSize) + _vertices[viewModel] = VertexCanvasViewModel(viewModel, this) } } - - private val _zoom = mutableStateOf(1f) - var zoom - get() = _zoom.value - set(value) { - _zoom.value = value - updateEdges() - } - - private val _center = mutableStateOf(Offset(0f, 0f)) - var center - get() = _center.value - set(value) { - _center.value = value - } - - private val _canvasSize = mutableStateOf(Offset(400f, 400f)) - var canvasSize - get() = _canvasSize.value - set(value) { - _canvasSize.value = value - } - - private val _isOrientated = mutableStateOf(false) - var isOrientated - get() = _isOrientated.value - set(value) { - _isOrientated.value = value - } - - private val _vertices = mutableStateMapOf() - init { graphViewModel.vertices.forEach { v -> - _vertices[v] = VertexCanvasViewModel(v, _zoom, _center, _canvasSize) + _vertices[v] = VertexCanvasViewModel(v, this) } } @@ -95,7 +57,7 @@ class CanvasViewModel( val vertex2 = _vertices[it.second] ?: throw IllegalStateException("There is no VertexCanvasViewModel for ${it.second}") - EdgeCanvasViewModel(vertex1, vertex2, it.color, it.strokeWidth, zoom, _isOrientated) + EdgeCanvasViewModel(vertex1, vertex2, it.color, it, this) } val vertices @@ -104,10 +66,6 @@ class CanvasViewModel( val edges get() = _edges - private fun updateEdges() { - edges.forEach { it.updateEdge(zoom) } - } - val onScroll: AwaitPointerEventScope.(PointerEvent) -> Unit = { if (it.changes.first().scrollDelta.y > 0) { zoom -= zoom / 8 @@ -145,12 +103,4 @@ class CanvasViewModel( fun onOrientatedChange(isOrientated: Boolean) { this.isOrientated = isOrientated } - -// fun getViews(): Collection { -// if (Config.optimizeCanvas) { -// return _vertices.filter { abs(it.value.offset.x) < canvasSize.x && abs(it.value.offset.y) < canvasSize.y }.values -// } -// -// return _vertices.values -// } } \ No newline at end of file diff --git a/src/main/kotlin/viewModel/canvas/EdgeCanvasViewModel.kt b/src/main/kotlin/viewModel/canvas/EdgeCanvasViewModel.kt index 3df074b..b94acdb 100644 --- a/src/main/kotlin/viewModel/canvas/EdgeCanvasViewModel.kt +++ b/src/main/kotlin/viewModel/canvas/EdgeCanvasViewModel.kt @@ -1,19 +1,17 @@ package viewModel.canvas -import androidx.compose.runtime.MutableState import androidx.compose.ui.graphics.Color +import viewModel.graph.EdgeViewModel class EdgeCanvasViewModel( val first: VertexCanvasViewModel, val second: VertexCanvasViewModel, val color: Color, - strokeWidth: Float, - zoom: Float, - val showOrientation: MutableState + val edgeViewModel: EdgeViewModel, + private val canvasViewModel: CanvasViewModel, ) { - var strokeWidth = strokeWidth * (zoom) + var showOrientation by canvasViewModel::isOrientated - fun updateEdge(zoom: Float) { - strokeWidth = 8f * zoom - } + val strokeWidth + get() = edgeViewModel.strokeWidth * canvasViewModel.zoom } \ No newline at end of file diff --git a/src/main/kotlin/viewModel/canvas/VertexCanvasViewModel.kt b/src/main/kotlin/viewModel/canvas/VertexCanvasViewModel.kt index da6cfc2..3fbbd96 100644 --- a/src/main/kotlin/viewModel/canvas/VertexCanvasViewModel.kt +++ b/src/main/kotlin/viewModel/canvas/VertexCanvasViewModel.kt @@ -1,33 +1,33 @@ package viewModel.canvas -import androidx.compose.runtime.MutableState -import androidx.compose.runtime.mutableStateOf import androidx.compose.ui.geometry.Offset import viewModel.graph.VertexViewModel class VertexCanvasViewModel( val vertexViewModel: VertexViewModel, - private val zoom: MutableState, - private val center: MutableState, - private val canvasSize: MutableState + private val canvasViewModel: CanvasViewModel, ) { var color by vertexViewModel::color + private var zoom by canvasViewModel::zoom + private var center by canvasViewModel::center + private var canvasSize by canvasViewModel::canvasSize val strokeWidth - get() = 8f * zoom.value + get() = 8f * zoom val radius - get() = vertexViewModel.radius * zoom.value + get() = vertexViewModel.radius * zoom val offset get() = calculateOffset() + val textSize - get() = vertexViewModel.radius * 0.6f * zoom.value + get() = vertexViewModel.radius * 0.6f * zoom fun onDrag(it: Offset): Unit { - vertexViewModel.onDrag(it * (1f / zoom.value)) + vertexViewModel.onDrag(it * (1f / zoom)) } private fun calculateOffset() = Offset( - (canvasSize.value.x / 2) + ((vertexViewModel.x - center.value.x) * zoom.value), - (canvasSize.value.y / 2) + ((vertexViewModel.y - center.value.y) * zoom.value) + (canvasSize.x / 2) + ((vertexViewModel.x - center.x) * zoom), + (canvasSize.y / 2) + ((vertexViewModel.y - center.y) * zoom) ) } \ No newline at end of file diff --git a/src/main/kotlin/viewModel/graph/EdgeViewModel.kt b/src/main/kotlin/viewModel/graph/EdgeViewModel.kt index cca76b4..3b3027b 100644 --- a/src/main/kotlin/viewModel/graph/EdgeViewModel.kt +++ b/src/main/kotlin/viewModel/graph/EdgeViewModel.kt @@ -2,7 +2,9 @@ package viewModel.graph import Config import androidx.compose.runtime.State +import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.setValue import androidx.compose.ui.graphics.Color import model.graph.Edge @@ -22,17 +24,6 @@ class EdgeViewModel( edge.weight = value } - private var _color = mutableStateOf(color) - var color - get() = _color.value - set(value) { - _color.value = value - } - - private var _strokeWidth = mutableStateOf(strokeWidth) - var strokeWidth - get() = _strokeWidth.value - set(value) { - _strokeWidth.value = value - } + var color by mutableStateOf(color) + var strokeWidth by mutableStateOf(strokeWidth) } diff --git a/src/main/kotlin/viewModel/graph/UndirectedViewModel.kt b/src/main/kotlin/viewModel/graph/UndirectedViewModel.kt index ce010e1..d24069e 100644 --- a/src/main/kotlin/viewModel/graph/UndirectedViewModel.kt +++ b/src/main/kotlin/viewModel/graph/UndirectedViewModel.kt @@ -1,6 +1,8 @@ package viewModel.graph +import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.setValue import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Color import androidx.compose.ui.unit.dp @@ -18,16 +20,12 @@ class UndirectedViewModel( private val _vertices = hashMapOf() private val _adjacencyList = hashMapOf>() private val groupColors = hashMapOf(0 to Color.Black) + private val _color = mutableStateOf(Color.Black) - private val _size = mutableStateOf(10f) private val _clustering = mutableStateOf(false) private val _ranked = mutableStateOf(false) - private var size - get() = _size.value - set(value) { - _size.value = value - } + private var size by mutableStateOf(10f) val vertices get() = _vertices.values From 52d519766fda38a03caa777826ff33a0fb6f11e1 Mon Sep 17 00:00:00 2001 From: Homka122 Date: Sun, 22 Sep 2024 20:07:03 +0300 Subject: [PATCH 4/4] refactor: refactor modelView of canvas and vertex --- .../kotlin/view/canvas/VertexCanvasView.kt | 2 +- .../viewModel/canvas/CanvasViewModel.kt | 3 +-- .../viewModel/canvas/VertexCanvasViewModel.kt | 20 +++++++++---------- .../viewModel/graph/UndirectedViewModel.kt | 4 +--- 4 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/view/canvas/VertexCanvasView.kt b/src/main/kotlin/view/canvas/VertexCanvasView.kt index 4be14f9..9c39f6c 100644 --- a/src/main/kotlin/view/canvas/VertexCanvasView.kt +++ b/src/main/kotlin/view/canvas/VertexCanvasView.kt @@ -31,6 +31,6 @@ fun VertexCanvasView( .onDrag(onDrag = viewModel::onDrag), contentAlignment = Alignment.Center ) { - MyText(viewModel.vertexViewModel.label, viewModel.textSize.value) + MyText(viewModel.label, viewModel.textSize.value) } } \ No newline at end of file diff --git a/src/main/kotlin/viewModel/canvas/CanvasViewModel.kt b/src/main/kotlin/viewModel/canvas/CanvasViewModel.kt index e75c0a4..669751c 100644 --- a/src/main/kotlin/viewModel/canvas/CanvasViewModel.kt +++ b/src/main/kotlin/viewModel/canvas/CanvasViewModel.kt @@ -32,13 +32,12 @@ class CanvasViewModel( var center by mutableStateOf(Offset(0f, 0f)) var canvasSize by mutableStateOf(Offset(400f, 400f)) var isOrientated by mutableStateOf(false) - + private val _vertices = mutableStateMapOf() fun createNode(offset: Offset) { if (isNodeCreatingMode) { val coordinates = (offset - (canvasSize / 2.0F)) * (1 / zoom) + center - println(offset - (canvasSize / 2.0F)) val viewModel = graphViewModel.createVertex(coordinates) ?: return _vertices[viewModel] = VertexCanvasViewModel(viewModel, this) diff --git a/src/main/kotlin/viewModel/canvas/VertexCanvasViewModel.kt b/src/main/kotlin/viewModel/canvas/VertexCanvasViewModel.kt index 3fbbd96..b4f993a 100644 --- a/src/main/kotlin/viewModel/canvas/VertexCanvasViewModel.kt +++ b/src/main/kotlin/viewModel/canvas/VertexCanvasViewModel.kt @@ -4,30 +4,28 @@ import androidx.compose.ui.geometry.Offset import viewModel.graph.VertexViewModel class VertexCanvasViewModel( - val vertexViewModel: VertexViewModel, + private val vertexViewModel: VertexViewModel, private val canvasViewModel: CanvasViewModel, ) { - var color by vertexViewModel::color - private var zoom by canvasViewModel::zoom - private var center by canvasViewModel::center - private var canvasSize by canvasViewModel::canvasSize + val color by vertexViewModel::color + val label by vertexViewModel::label val strokeWidth - get() = 8f * zoom + get() = 8f * canvasViewModel.zoom val radius - get() = vertexViewModel.radius * zoom + get() = vertexViewModel.radius * canvasViewModel.zoom val offset get() = calculateOffset() val textSize - get() = vertexViewModel.radius * 0.6f * zoom + get() = vertexViewModel.radius * 0.6f * canvasViewModel.zoom fun onDrag(it: Offset): Unit { - vertexViewModel.onDrag(it * (1f / zoom)) + vertexViewModel.onDrag(it * (1f / canvasViewModel.zoom)) } private fun calculateOffset() = Offset( - (canvasSize.x / 2) + ((vertexViewModel.x - center.x) * zoom), - (canvasSize.y / 2) + ((vertexViewModel.y - center.y) * zoom) + (canvasViewModel.canvasSize.x / 2) + ((vertexViewModel.x - canvasViewModel.center.x) * canvasViewModel.zoom), + (canvasViewModel.canvasSize.y / 2) + ((vertexViewModel.y - canvasViewModel.center.y) * canvasViewModel.zoom) ) } \ No newline at end of file diff --git a/src/main/kotlin/viewModel/graph/UndirectedViewModel.kt b/src/main/kotlin/viewModel/graph/UndirectedViewModel.kt index d24069e..d7a26a3 100644 --- a/src/main/kotlin/viewModel/graph/UndirectedViewModel.kt +++ b/src/main/kotlin/viewModel/graph/UndirectedViewModel.kt @@ -99,9 +99,7 @@ class UndirectedViewModel( } fun createVertex(coordinates: Offset): VertexViewModel? { - val vertex = graph.addVertex(graph.vertices.last().key + 1) - - if (vertex == null) return null + val vertex = graph.addVertex(graph.vertices.last().key + 1) ?: return null val viewModel = VertexViewModel( showVerticesLabels,