@@ -14,27 +14,26 @@ import androidx.compose.foundation.layout.height
1414import androidx.compose.foundation.layout.padding
1515import androidx.compose.foundation.layout.width
1616import androidx.compose.foundation.layout.wrapContentWidth
17+ import androidx.compose.material3.MaterialTheme
1718import androidx.compose.material3.Text
1819import androidx.compose.runtime.Composable
19- import androidx.compose.runtime.LaunchedEffect
2020import androidx.compose.runtime.collectAsState
2121import androidx.compose.runtime.getValue
22- import androidx.compose.runtime.mutableStateOf
23- import androidx.compose.runtime.remember
2422import androidx.compose.ui.Alignment
2523import androidx.compose.ui.Modifier
2624import androidx.compose.ui.graphics.Color
25+ import androidx.compose.ui.res.stringResource
26+ import androidx.compose.ui.text.font.FontWeight
2727import androidx.compose.ui.unit.dp
28+ import androidx.compose.ui.unit.sp
2829import androidx.hilt.navigation.compose.hiltViewModel
2930import com.waleska404.algorithms.R
30- import com.waleska404.algorithms.domain.dijkstra.Position
3131import com.waleska404.algorithms.ui.core.components.CELL_FINISH
3232import com.waleska404.algorithms.ui.core.components.CELL_START
3333import com.waleska404.algorithms.ui.core.components.CELL_VISITED
3434import com.waleska404.algorithms.ui.core.components.CELL_WALL
3535import com.waleska404.algorithms.ui.core.components.CustomIconButton
3636import com.waleska404.algorithms.ui.core.components.PathFindingGrid
37- import kotlinx.coroutines.delay
3837
3938
4039@OptIn(ExperimentalFoundationApi ::class )
@@ -43,86 +42,92 @@ fun DijkstraScreen(
4342 navigateToHome : () -> Unit ,
4443 viewModel : DijkstraViewModel = hiltViewModel(),
4544) {
46- // val currentGridState = remember { mutableStateOf(state.drawCurrentGridState()) }
4745 val currentGridState: DijkstraGrid by viewModel.gridState.collectAsState()
46+ val isVisualizing: Boolean by viewModel.isVisualizing.collectAsState()
4847
49- val onCellClicked = { p: Position ->
50- if (viewModel.isPositionNotAtStartOrFinish(p) && ! viewModel.isVisualizing) {
51- viewModel.toggleCellTypeToWall(p)
52- // currentGridState.value = viewModel.drawCurrentGridState()
53- }
54- }
55-
56- PathFindingUi (
57- grid = currentGridState,
58- onClick = onCellClicked,
59- onVisualize = {
60- viewModel.animatedShortestPath()
61- },
62- onRandomizeWalls = {
63- viewModel.randomizeWalls()
64- },
65- onClear = {
66- viewModel.clear()
67- }
68- )
69- LaunchedEffect (Unit ) {
70- while (true ) {
71- delay(GAME_DELAY_IN_MS )
72- // currentGridState.value = viewModel.drawCurrentGridState()
73- }
48+ Column (
49+ horizontalAlignment = Alignment .CenterHorizontally ,
50+ verticalArrangement = Arrangement .Center ,
51+ ) {
52+ Text (
53+ text = stringResource(id = R .string.dijkstras_algorithm),
54+ fontWeight = FontWeight .Bold ,
55+ fontSize = 22 .sp,
56+ color = MaterialTheme .colorScheme.secondary
57+ )
58+ Spacer (modifier = Modifier .height(10 .dp))
59+ PathFindingGrid (
60+ cellData = currentGridState.toLinearGrid(),
61+ onClick = viewModel::onCellClicked
62+ )
63+ Spacer (modifier = Modifier .height(10 .dp))
64+ Legend ()
65+ Spacer (modifier = Modifier .height(10 .dp))
66+ BottomButtons (
67+ onVisualize = { viewModel.animatedShortestPath() },
68+ onRandomizeWalls = { viewModel.randomizeWalls() },
69+ onClear = { viewModel.clear() },
70+ isVisualizing = isVisualizing
71+ )
7472 }
7573}
7674
77- @ExperimentalFoundationApi
75+
7876@Composable
79- fun PathFindingUi (
80- grid : DijkstraGrid ,
81- onClick : (Position ) -> Unit ,
77+ fun BottomButtons (
8278 onVisualize : () -> Unit ,
8379 onRandomizeWalls : () -> Unit ,
8480 onClear : () -> Unit ,
81+ isVisualizing : Boolean ,
8582) {
86- val isVisualizeEnabled = remember { mutableStateOf(true ) }
87-
88- Column (
89- modifier = Modifier .padding(8 .dp),
90- verticalArrangement = Arrangement .Center ,
91- horizontalAlignment = Alignment .CenterHorizontally ,
83+ Row (
84+ verticalAlignment = Alignment .CenterVertically ,
85+ horizontalArrangement = Arrangement .Center ,
9286 ) {
93- PathFindingGrid (grid.toLinearGrid(), onClick)
87+ CustomIconButton (
88+ modifier = Modifier .padding(start = 7 .dp),
89+ onClick = onVisualize,
90+ text = stringResource(id = R .string.run),
91+ enabled = ! isVisualizing,
92+ iconResource = R .drawable.sort,
93+ iconDescriptionResource = R .string.sort_icon,
94+ )
95+ CustomIconButton (
96+ modifier = Modifier .padding(start = 7 .dp),
97+ onClick = onRandomizeWalls,
98+ text = stringResource(id = R .string.walls),
99+ enabled = ! isVisualizing,
100+ iconResource = R .drawable.shuffle,
101+ iconDescriptionResource = R .string.random,
102+ )
103+ CustomIconButton (
104+ modifier = Modifier .padding(horizontal = 7 .dp),
105+ onClick = onClear,
106+ text = stringResource(id = R .string.clear),
107+ iconResource = R .drawable.broom,
108+ iconDescriptionResource = R .string.broom_icon,
109+ )
110+ }
111+ }
94112
95- Column {
96- Spacer (modifier = Modifier .height(30 .dp))
97- Row (
98- modifier = Modifier .fillMaxWidth(),
99- horizontalArrangement = Arrangement .Center
100- ) {
101- Legend (" Start" , CELL_START )
102- Legend (" Finish" , CELL_FINISH )
103- Legend (" Visited" , CELL_VISITED )
104- Legend (" Wall" , CELL_WALL )
105- }
106- Row {
107- VisualizeButton (
108- modifier = Modifier .padding(start = 16 .dp),
109- onClick = onVisualize,
110- enabled = isVisualizeEnabled.value
111- )
112- RandomWallsButton (
113- modifier = Modifier .padding(start = 16 .dp),
114- onClick = onRandomizeWalls,
115- enabled = isVisualizeEnabled.value
116- )
117- ClearButton (modifier = Modifier .padding(horizontal = 16 .dp), onClick = onClear)
118- }
119- }
113+ @OptIn(ExperimentalFoundationApi ::class )
114+ @Composable
115+ fun Legend () {
116+ Row (
117+ modifier = Modifier .fillMaxWidth(),
118+ horizontalArrangement = Arrangement .Center
119+ ) {
120+ LegendItem (stringResource(id = R .string.start), CELL_START )
121+ LegendItem (stringResource(id = R .string.finish), CELL_FINISH )
122+ LegendItem (stringResource(id = R .string.visited), CELL_VISITED )
123+ LegendItem (stringResource(id = R .string.wall), CELL_WALL )
120124 }
121125}
122126
127+
123128@ExperimentalFoundationApi
124129@Composable
125- fun Legend (
130+ fun LegendItem (
126131 label : String ,
127132 color : Color ,
128133 hasBorder : Boolean = false
@@ -146,44 +151,3 @@ fun Legend(
146151
147152}
148153
149- @ExperimentalFoundationApi
150- @Composable
151- fun VisualizeButton (modifier : Modifier = Modifier , onClick : () -> (Unit ), enabled : Boolean = true) {
152- CustomIconButton (
153- modifier,
154- onClick = onClick,
155- text = " Vis" ,
156- enabled = enabled,
157- iconResource = R .drawable.sort,
158- iconDescriptionResource = R .string.sort_icon,
159- )
160- }
161-
162- @ExperimentalFoundationApi
163- @Composable
164- fun ClearButton (modifier : Modifier = Modifier , onClick : () -> (Unit )) {
165- CustomIconButton (
166- modifier,
167- onClick = onClick,
168- text = " Clear" ,
169- iconResource = R .drawable.sortdescending,
170- iconDescriptionResource = R .string.sort_icon,
171- )
172- }
173-
174- @ExperimentalFoundationApi
175- @Composable
176- fun RandomWallsButton (
177- modifier : Modifier = Modifier ,
178- onClick : () -> (Unit ),
179- enabled : Boolean = true
180- ) {
181- CustomIconButton (
182- modifier,
183- onClick = onClick,
184- text = " Walls" ,
185- enabled = enabled,
186- iconResource = R .drawable.shuffle,
187- iconDescriptionResource = R .string.random,
188- )
189- }
0 commit comments