Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete useful code with by operator #23

Merged
merged 4 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions src/main/kotlin/view/MenuView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/view/canvas/EdgeCanvasView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fun EdgeCanvasView(
)
}

if (viewModel.showOrientation.value) {
if (viewModel.showOrientation) {
drawLine(
start = end,
end = end - rotateVector(radiusVectorSecond * 0.8f, 30.0),
Expand Down
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)
}
}
46 changes: 6 additions & 40 deletions src/main/kotlin/viewModel/MenuViewModel.kt
Original file line number Diff line number Diff line change
@@ -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)
}
86 changes: 13 additions & 73 deletions src/main/kotlin/viewModel/canvas/CanvasViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,74 +24,29 @@ class CanvasViewModel(
) {
private val graphViewModel = UndirectedViewModel(graph, true)

var isClustering
get() = graphViewModel.clustering
set(value) {
graphViewModel.clustering = value
}
var isClustering by graphViewModel::clustering
var isRanked by graphViewModel::ranked

var isRanked
get() = graphViewModel.ranked
set(value) {
graphViewModel.ranked = 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)

val _isNodeCreatingMode = mutableStateOf(false)
var isNodeCreatingMode
get() = _isNodeCreatingMode.value
set(value) {
_isNodeCreatingMode.value = value
}
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, _zoom, _center, _canvasSize)
updateVertexes()
_vertices[viewModel] = VertexCanvasViewModel(viewModel, this)
}
}


private val _zoom = mutableStateOf(1f)
var zoom
get() = _zoom.value
set(value) {
_zoom.value = value
updateVertexes()
updateEdges()
}

private val _center = mutableStateOf(Offset(0f, 0f))
var center
get() = _center.value
set(value) {
_center.value = value
updateVertexes()
}

private val _canvasSize = mutableStateOf(Offset(400f, 400f))
var canvasSize
get() = _canvasSize.value
set(value) {
_canvasSize.value = value
updateVertexes()
}

private val _isOrientated = mutableStateOf(false)
var isOrientated
get() = _isOrientated.value
set(value) {
_isOrientated.value = value
}

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

init {
graphViewModel.vertices.forEach { v ->
_vertices[v] = VertexCanvasViewModel(v, _zoom, _center, _canvasSize)
_vertices[v] = VertexCanvasViewModel(v, this)
}
}

Expand All @@ -99,7 +56,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
Expand All @@ -108,14 +65,6 @@ class CanvasViewModel(
val edges
get() = _edges

private fun updateVertexes() {
vertices.forEach { it.updateVertex() }
}

private fun updateEdges() {
edges.forEach { it.updateEdge(zoom) }
}

val onScroll: AwaitPointerEventScope.(PointerEvent) -> Unit = {
if (it.changes.first().scrollDelta.y > 0) {
zoom -= zoom / 8
Expand Down Expand Up @@ -148,18 +97,9 @@ class CanvasViewModel(

fun onSizeChange(newSize: Float) {
graphViewModel.onSizeChange(newSize)
updateVertexes()
}

fun onOrientatedChange(isOrientated: Boolean) {
this.isOrientated = isOrientated
}

// fun getViews(): Collection<VertexCanvasViewModel> {
// if (Config.optimizeCanvas) {
// return _vertices.filter { abs(it.value.offset.x) < canvasSize.x && abs(it.value.offset.y) < canvasSize.y }.values
// }
//
// return _vertices.values
// }
}
14 changes: 6 additions & 8 deletions src/main/kotlin/viewModel/canvas/EdgeCanvasViewModel.kt
Original file line number Diff line number Diff line change
@@ -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<Boolean>
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
}
68 changes: 15 additions & 53 deletions src/main/kotlin/viewModel/canvas/VertexCanvasViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,69 +1,31 @@
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<Float>,
private val center: MutableState<Offset>,
private val canvasSize: MutableState<Offset>
private val vertexViewModel: VertexViewModel,
private val canvasViewModel: CanvasViewModel,
) {
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
}
val color by vertexViewModel::color
val label by vertexViewModel::label

private val _strokeWidth = mutableStateOf(calculateStrokeWidth())
var strokeWidth
get() = _strokeWidth.value
set(value) {
_strokeWidth.value = value
}
val strokeWidth
get() = 8f * canvasViewModel.zoom
val radius
get() = vertexViewModel.radius * canvasViewModel.zoom
val offset
get() = calculateOffset()

private val _textSize = mutableStateOf(calculateTextSize())
var textSize
get() = _textSize.value
set(value) {
_textSize.value = value
}
val textSize
get() = vertexViewModel.radius * 0.6f * canvasViewModel.zoom

fun onDrag(it: Offset): Unit {
vertexViewModel.onDrag(it * (1f / zoom.value))
}

fun updateVertex() {
offset = calculateOffset()
radius = calculateRadius()
strokeWidth = calculateStrokeWidth()
textSize = calculateTextSize()
vertexViewModel.onDrag(it * (1f / canvasViewModel.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)
(canvasViewModel.canvasSize.x / 2) + ((vertexViewModel.x - canvasViewModel.center.x) * canvasViewModel.zoom),
(canvasViewModel.canvasSize.y / 2) + ((vertexViewModel.y - canvasViewModel.center.y) * canvasViewModel.zoom)
)

private fun calculateRadius() = vertexViewModel.radius * zoom.value
private fun calculateStrokeWidth() = 8f * zoom.value
private fun calculateTextSize() = vertexViewModel.radius * 0.6f * zoom.value
}
17 changes: 4 additions & 13 deletions src/main/kotlin/viewModel/graph/EdgeViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
}
Loading