Skip to content

Commit

Permalink
feat: add methods for change edge's color
Browse files Browse the repository at this point in the history
  • Loading branch information
homka122 committed Sep 23, 2024
1 parent cb29024 commit ffd989b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 10 deletions.
33 changes: 26 additions & 7 deletions src/main/kotlin/viewModel/canvas/CanvasViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.AwaitPointerEventScope
import androidx.compose.ui.input.pointer.PointerEvent
import androidx.compose.ui.input.pointer.PointerInputScope
import model.graph.Edge
import model.graph.UndirectedGraph
import view.HEADER_HEIGHT
import view.MENU_WIDTH
import viewModel.graph.EdgeViewModel
import viewModel.graph.UndirectedViewModel
import viewModel.graph.VertexViewModel

Expand All @@ -35,6 +37,10 @@ class CanvasViewModel(

private val _vertices = mutableStateMapOf<VertexViewModel, VertexCanvasViewModel>()

private fun getVertex(vm: VertexViewModel): VertexCanvasViewModel {
return _vertices[vm] ?: throw IllegalArgumentException("There is no VertexCanvasViewModel for $vm")
}

fun createNode(offset: Offset) {
if (isNodeCreatingMode) {
val coordinates = (offset - (canvasSize / 2.0F)) * (1 / zoom) + center
Expand All @@ -50,20 +56,22 @@ class CanvasViewModel(
}
}

private val _edges = graphViewModel.adjacencyList.map { it.value }.flatten().map {
val vertex1 =
_vertices[it.first] ?: throw IllegalStateException("There is no VertexCanvasViewModel for ${it.first}")
val vertex2 =
_vertices[it.second] ?: throw IllegalStateException("There is no VertexCanvasViewModel for ${it.second}")
private val _edges = graphViewModel.adjacencyList.mapValues {
it.value.map { edgeViewModel ->
val vertex1 = getVertex(edgeViewModel.first)
val vertex2 = getVertex(edgeViewModel.second)

EdgeCanvasViewModel(vertex1, vertex2, it.color, it, this)
EdgeCanvasViewModel(vertex1, vertex2, edgeViewModel, this)
}
}.mapKeys {
getVertex(it.key)
}

val vertices
get() = _vertices.values

val edges
get() = _edges
get() = _edges.values.flatten()

val onScroll: AwaitPointerEventScope.(PointerEvent) -> Unit = {
if (it.changes.first().scrollDelta.y > 0) {
Expand Down Expand Up @@ -102,4 +110,15 @@ class CanvasViewModel(
fun onOrientatedChange(isOrientated: Boolean) {
this.isOrientated = isOrientated
}

/*
* Change edges' color
* */
fun changeEdgesColor(edges: List<Pair<Edge, Color>>) {
graphViewModel.changeEdgesColor(edges)
}

fun resetEdgesColorToDefault() {
graphViewModel.resetEdgesColorToDefault()
}
}
3 changes: 1 addition & 2 deletions src/main/kotlin/viewModel/canvas/EdgeCanvasViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package viewModel.canvas

import androidx.compose.ui.graphics.Color
import viewModel.graph.EdgeViewModel

class EdgeCanvasViewModel(
val first: VertexCanvasViewModel,
val second: VertexCanvasViewModel,
val color: Color,
val edgeViewModel: EdgeViewModel,
private val canvasViewModel: CanvasViewModel,
) {
var showOrientation by canvasViewModel::isOrientated
var color by edgeViewModel::color

val strokeWidth
get() = edgeViewModel.strokeWidth * canvasViewModel.zoom
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/viewModel/graph/EdgeViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import model.graph.Edge
class EdgeViewModel(
val first: VertexViewModel,
val second: VertexViewModel,
private val edge: Edge,
val edge: Edge,
private val _weightVisibility: State<Boolean>,
color: Color = Config.Edge.color,
strokeWidth: Float = Config.Edge.strokeWidth
Expand Down
30 changes: 30 additions & 0 deletions src/main/kotlin/viewModel/graph/UndirectedViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package viewModel.graph

import Config
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
Expand All @@ -8,6 +9,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import model.algorithm.Clustering
import model.algorithm.PageRank
import model.graph.Edge
import model.graph.UndirectedGraph
import model.graph.Vertex

Expand Down Expand Up @@ -115,6 +117,34 @@ class UndirectedViewModel(
return viewModel
}

/*
* Change edges' color
* */
fun changeEdgesColor(edges: List<Pair<Edge, Color>>) {
edges.forEach { p ->
val edge = p.first
val color = p.second

val vertex1 = _vertices[edge.first] ?: return
val vertex2 = _vertices[edge.second] ?: return

val edgeViewModelList1 = _adjacencyList[vertex1] ?: return
val edgeViewModel1 = edgeViewModelList1.find { it.second == vertex2 } ?: return
edgeViewModel1.color = color

val edgeViewModelList2 = _adjacencyList[vertex2] ?: return
val edgeViewModel2 = edgeViewModelList2.find { it.second == vertex1 } ?: return
edgeViewModel2.color = color
}
}

/*
* Reset current color on all edges to default in Config
* */
fun resetEdgesColorToDefault() {
adjacencyList.values.flatten().forEach { it.color = Config.Edge.color }
}

init {
graph.vertices.forEachIndexed { i, vertex ->
val group = groups.getOrDefault(vertex, 0)
Expand Down

0 comments on commit ffd989b

Please sign in to comment.