Skip to content

Commit

Permalink
Fix buttons (#22)
Browse files Browse the repository at this point in the history
Buttons now work
  • Loading branch information
homka122 authored Sep 15, 2024
2 parents e0fd264 + cb9f3c1 commit 2d7336d
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 126 deletions.
2 changes: 0 additions & 2 deletions src/main/kotlin/Config.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp

object Config {
val headerHeight = 40f
val menuWidth = 80f
val optimizeCanvas = false

object Edge {
val color = Color(0xFF00E0FF)
Expand Down
22 changes: 7 additions & 15 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.*
import model.algorithm.Clustering
import model.algorithm.PageRank
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.WindowPlacement
import androidx.compose.ui.window.WindowState
import androidx.compose.ui.window.application
import model.graph.UndirectedGraph
import view.HeaderView
import view.MainView
import view.MenuView
import viewModel.MainViewModel
import viewModel.graph.UndirectedViewModel

val AMOUNT_NODES = 2
val EDGE_CHANGE = 100
val AMOUNT_NODES = 16
val EDGE_CHANGE = 5.0

val graph = UndirectedGraph().apply {
for (i in (0 until AMOUNT_NODES)) {
Expand All @@ -32,17 +29,12 @@ val graph = UndirectedGraph().apply {
}
}

val groups = Clustering(graph).calculate()
val ranks = PageRank(graph).computePageRank(3)
val undirectedViewModel = UndirectedViewModel(graph, false, groups, ranks)
val mainViewModel = MainViewModel(graph)

fun main() = application {
var isOpen by remember { mutableStateOf(true) }
var isMaximized by remember { mutableStateOf(true) }
var isMinimize by remember { mutableStateOf(false) }
var position: WindowPosition by remember { mutableStateOf(WindowPosition.PlatformDefault) }
var headerName by remember { mutableStateOf("Dimabase.db") }
val headerName by remember { mutableStateOf("Dimabase.db") }

val windowState = WindowState(
placement = if (isMaximized) WindowPlacement.Maximized else WindowPlacement.Floating,
Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/model/algorithm/BellmanFord.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package model.algorithm
import model.graph.Edge
import model.graph.Graph
import model.graph.Vertex
import model.graph.WeightedGraph

class BellmanFord(private val graph: Graph) {
val parentMap = HashMap<Vertex, Vertex>()
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/model/algorithm/Dijkstra.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package model.algorithm

import model.graph.*
import java.util.concurrent.TransferQueue
import model.graph.Graph
import model.graph.Vertex

class Dijkstra(private val graph: Graph) {

Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/model/algorithm/FindBridges.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package model.algorithm


import model.graph.*
import model.graph.Edge
import model.graph.Graph
import model.graph.Vertex


class FindBridges(
Expand Down
33 changes: 5 additions & 28 deletions src/main/kotlin/view/MainView.kt
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
package view

import Config
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.LineHeightStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Popup
import androidx.compose.ui.window.PopupProperties
import view.canvas.CanvasView
import viewModel.MainViewModel

val HEADER_HEIGHT = Config.headerHeight
val MENU_WIDTH = Config.menuWidth

@Composable
fun DisplayAlgorithmMenu(name : String) {
fun DisplayAlgorithmMenu(name: String) {

val imageResources = listOf(
"FindBridge.svg",
Expand All @@ -40,7 +31,7 @@ fun DisplayAlgorithmMenu(name : String) {
"FindCycle.svg"
)
Box(
modifier = Modifier.padding(top = 240.dp, start = 80.dp)
modifier = Modifier.padding(top = 240.dp, start = 80.dp)
) {

// Изображение
Expand Down Expand Up @@ -86,33 +77,19 @@ fun ImageButton(imageResourceId: String, onClick: () -> Unit) {
}
}

@OptIn(ExperimentalComposeUiApi::class, ExperimentalFoundationApi::class)
@Composable
fun MainView(mainViewModel: MainViewModel) {
var isClustering by remember { mutableStateOf(false) }
var isRanked by remember { mutableStateOf(false) }
var isNodeCreatingMode by remember { mutableStateOf(false) }
var isAlgorithmMenuOpen by remember { mutableStateOf(false)}

Row(Modifier.offset(0f.dp, Config.headerHeight.dp)) {
MenuView(
isNodeCreatingMode,
{ isNodeCreatingMode = !isNodeCreatingMode },
isClustering,
{ isClustering = !isClustering },
isRanked,
{ isRanked = !isRanked },
isAlgorithmMenuOpen,
{ isAlgorithmMenuOpen = !isAlgorithmMenuOpen })
MenuView(mainViewModel.menuViewModel)


CanvasView(
mainViewModel.canvasViewModel,
Modifier.fillMaxSize()
)
}

if (isAlgorithmMenuOpen){
if (mainViewModel.menuViewModel.isAlgorithmMenuOpen) {
DisplayAlgorithmMenu("DownMenuAlgorithm.svg")
}

Expand Down
40 changes: 17 additions & 23 deletions src/main/kotlin/view/MenuView.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package view

import Config
import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
Expand All @@ -16,12 +17,12 @@ import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.layout.positionInRoot
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.DpOffset
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Popup
import androidx.compose.ui.window.PopupProperties
import viewModel.MenuViewModel
import kotlin.math.roundToInt

@OptIn(ExperimentalFoundationApi::class, ExperimentalComposeUiApi::class)
Expand All @@ -48,14 +49,14 @@ fun MenuIcon(name: String, description: String, modifier: Modifier = Modifier, o
}
)
.onGloballyPositioned { layoutCoordinates ->
iconPosition = layoutCoordinates.positionInRoot().run {
IntOffset(x.roundToInt(), y.roundToInt())
iconPosition = layoutCoordinates.positionInRoot().run {
IntOffset(x.roundToInt(), y.roundToInt())
}
iconSize = layoutCoordinates.size
}
iconSize = layoutCoordinates.size
}
)
Spacer(Modifier.height(10f.dp))
if (isHovered){
if (isHovered) {
Popup(
offset = with(LocalDensity.current) {
IntOffset(
Expand All @@ -72,7 +73,7 @@ fun MenuIcon(name: String, description: String, modifier: Modifier = Modifier, o
}

@Composable
fun DisplayDescription(name : String) {
fun DisplayDescription(name: String) {
Image(
painter = painterResource(name),
contentDescription = "Descript",
Expand All @@ -83,34 +84,27 @@ fun DisplayDescription(name : String) {

@Composable
fun MenuView(
isNodeCreating: Boolean,
onNodeCreatingChange: () -> Unit,
isClustering: Boolean,
onClusteringChange: () -> Unit,
isRanked: Boolean,
onRankedChange: () -> Unit,
isAlgorithmMenuOpen: Boolean,
onAlgorithmMenuOpenChange: () -> Unit
viewModel: MenuViewModel
) {
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(isNodeCreating)
"Nodes.svg", "AddNode.svg", Modifier.glow(viewModel.isNodeCreating)
) {
onNodeCreatingChange()
viewModel.onNodeCreatingChange()
}
MenuIcon("Ribs.svg", "AddEdge.svg", modifier = Modifier.alpha(0.2f))
MenuIcon("Clustering.svg", "ClusterD.svg", Modifier.glow(isClustering)) {
onClusteringChange()
MenuIcon("Clustering.svg", "ClusterD.svg", Modifier.glow(viewModel.isClustering)) {
viewModel.onClusteringChange()
}
MenuIcon("PageRank.svg", "AnalysisGraph.svg", Modifier.glow(isRanked)) {
onRankedChange()
MenuIcon("PageRank.svg", "AnalysisGraph.svg", Modifier.glow(viewModel.isRanked)) {
viewModel.onRankedChange()
}
MenuIcon("Algorithm.svg", "Algorithms....svg", Modifier.glow(isAlgorithmMenuOpen)){
onAlgorithmMenuOpenChange()
MenuIcon("Algorithm.svg", "Algorithms....svg", Modifier.glow(viewModel.isAlgorithmMenuOpen)) {
viewModel.onAlgorithmMenuOpenChange()
}
}
}
Expand Down
61 changes: 35 additions & 26 deletions src/main/kotlin/view/SettingsView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package view
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.material.Checkbox
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
Expand All @@ -14,7 +16,6 @@ import components.MySlider
import components.MyText
import viewModel.SettingsViewModel


@Composable
fun SettingsView(viewModel: SettingsViewModel) {
val redSlider = remember { mutableStateOf(1f / (0xFF / 0x8F)) }
Expand All @@ -26,34 +27,42 @@ fun SettingsView(viewModel: SettingsViewModel) {
viewModel.onColorChange(Color(red = redSlider.value, green = greenSlider.value, blue = blueSlider.value))
viewModel.onSizeChange(sizeSlider.value)

SettingsContainer {
Row(Modifier.fillMaxWidth().padding(top = 10f.dp), horizontalArrangement = Arrangement.Center) {
MyText("Node")
}

Row(Modifier.fillMaxWidth().padding(top = 10f.dp, start = 20f.dp)) {
Column {
MyText("Color:")
MySlider("R: ", redSlider)
MySlider("G: ", greenSlider)
MySlider("B: ", blueSlider)
MySlider("Size: ", sizeSlider, (5f..80f))
}
}

Row(
Modifier.fillMaxWidth().padding(start = 20f.dp),
verticalAlignment = Alignment.CenterVertically
) {
MyText("Orientated")
Checkbox(orientatedCheckBox.value, onCheckedChange = {
viewModel.onOrientatedChange(it)
orientatedCheckBox.value = !orientatedCheckBox.value
})
}
}
}

@Composable
fun SettingsContainer(content: @Composable () -> Unit) {
Box(Modifier.fillMaxSize().padding(top = 80f.dp, end = 20f.dp).zIndex(10f), contentAlignment = Alignment.TopEnd) {
Box(
Modifier.size(270f.dp, 320f.dp).background(Color(0xFF3D3D3D), RoundedCornerShape(10))
) {
Column {
Row(Modifier.fillMaxWidth().padding(top = 10f.dp), horizontalArrangement = Arrangement.Center) {
MyText("Node")
}
Row(Modifier.fillMaxWidth().padding(top = 10f.dp, start = 20f.dp)) {
Column {
MyText("Color:")
MySlider("R: ", redSlider)
MySlider("G: ", greenSlider)
MySlider("B: ", blueSlider)
MySlider("Size: ", sizeSlider, (5f..80f))
}
}

Row(
Modifier.fillMaxWidth().padding(start = 20f.dp),
verticalAlignment = Alignment.CenterVertically
) {
MyText("Orientated")
Checkbox(orientatedCheckBox.value, onCheckedChange = {
viewModel.onOrientatedChange(it)
orientatedCheckBox.value = !orientatedCheckBox.value
})
}
content()
}
}
}
Expand Down
14 changes: 6 additions & 8 deletions src/main/kotlin/view/canvas/CanvasView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package view.canvas

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.runtime.Composable
import androidx.compose.ui.ExperimentalComposeUiApi
Expand All @@ -23,14 +24,11 @@ fun CanvasView(
modifier = modifier.background(Color(0xFF242424))
.onPointerEvent(PointerEventType.Scroll, onEvent = viewModel.onScroll)
.pointerInput(Unit, viewModel.onDrag)
// .pointerInput(Unit) {
// detectTapGestures {
// if (isNodeCreatingMode) {
// canvasViewModel.createVertex(it - (canvasSize / 2f), center, zoom)
// zoom += 0.000001f // костыль для рекомпозиции
// }
// }
// }
.pointerInput(Unit) {
detectTapGestures {
viewModel.createNode(it)
}
}
.pointerHoverIcon(PointerIcon.Hand)
.onSizeChanged {
viewModel.canvasSize = Offset(it.width.toFloat(), it.height.toFloat())
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/viewModel/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ class MainViewModel(
graph: UndirectedGraph
) {
val canvasViewModel = CanvasViewModel(graph)

val settingsViewModel = SettingsViewModel(
canvasViewModel::onColorChange,
canvasViewModel::onSizeChange,
canvasViewModel::onOrientatedChange
)

val menuViewModel = MenuViewModel(canvasViewModel)
}
Loading

0 comments on commit 2d7336d

Please sign in to comment.