Skip to content

Commit

Permalink
refactor: refactor modelView of canvas and vertex
Browse files Browse the repository at this point in the history
  • Loading branch information
homka122 committed Sep 22, 2024
1 parent 17ff642 commit 52d5197
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/view/canvas/VertexCanvasView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
3 changes: 1 addition & 2 deletions src/main/kotlin/viewModel/canvas/CanvasViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<VertexViewModel, VertexCanvasViewModel>()

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)
Expand Down
20 changes: 9 additions & 11 deletions src/main/kotlin/viewModel/canvas/VertexCanvasViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
}
4 changes: 1 addition & 3 deletions src/main/kotlin/viewModel/graph/UndirectedViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 52d5197

Please sign in to comment.