Extract Board to Composable

This commit is contained in:
Oliver Rümpelein 2021-08-19 11:27:25 +02:00
parent b7ab14c437
commit 1703cd87cb
3 changed files with 90 additions and 43 deletions

View File

@ -0,0 +1,55 @@
import androidx.compose.foundation.layout.*
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import components.GameHeader
import components.PlayerBar
import components.QuestionGrid
import data.Player
@Composable
@Suppress("FunctionName")
fun GameBoard(
reviewModeActive: Boolean,
secondRoundDouble: Boolean,
playerPointMap: MutableMap<Player, Long>,
onDisableReviewMode: () -> Unit,
onChangeQuestionsPlayed: () -> Unit
) {
Column(modifier = Modifier.fillMaxHeight().fillMaxWidth()) {
Row(
modifier = Modifier.fillMaxHeight(0.2f)
) {
GameHeader(
title = game.title,
color = game.headerColor
)
}
Spacer(modifier = Modifier.height(5.dp))
Row(
modifier = Modifier.fillMaxHeight()
) {
if (reviewModeActive) {
Button(onClick = onDisableReviewMode) {
Text(text = "Back")
}
}
Column(Modifier.fillMaxWidth(0.8f)) {
QuestionGrid(
game,
secondRoundDouble,
onResolveQuestion = onChangeQuestionsPlayed,
onPointsChange = { player, points ->
playerPointMap[player] = playerPointMap[player]?.plus(points) ?: 0
}
)
}
Spacer(modifier = Modifier.width(5.dp))
Column(Modifier.fillMaxWidth()) {
PlayerBar(players = playerPointMap)
}
}
}
}

View File

@ -1,14 +1,8 @@
import androidx.compose.foundation.layout.*
import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import components.EndCard
import components.GameHeader
import components.PlayerBar
import components.QuestionGrid
import data.*
val game = Game(
@ -17,7 +11,8 @@ val game = Game(
endGameAfter = 2,
players = listOf(
Player(name = "Oli"),
Player(name = "Gesina")
Player(name = "Gesina"),
Player(name = "Siggi")
),
topics = listOf(
Topic(
@ -26,7 +21,7 @@ val game = Game(
green = 0x44,
blue = 0xAA
),
topic = "Wer MACKt's?",
topic = "Wer?",
questions = listOf(
QuestionData(
hint = "Mack",
@ -90,6 +85,21 @@ val game = Game(
points = 5u
)
)
),
Topic(
topic = "Topic4",
questions = listOf(
QuestionData(
hint = "Some",
answer = "thing",
points = 1000u
),
QuestionData(
hint = "Other",
answer = "Thing",
points = 5u
)
)
)
)
)
@ -108,43 +118,20 @@ fun main() = application {
var questionsPlayed by remember { mutableStateOf(0) }
val secondRoundDouble by remember { derivedStateOf { questionsPlayed >= game.doubleAfter } }
val gameEnded by remember { derivedStateOf { questionsPlayed >= game.endGameAfter } }
var reviewModeActive by remember { mutableStateOf(false) }
if (!gameEnded) {
Column(modifier = Modifier.fillMaxHeight().fillMaxWidth()) {
Row(
modifier = Modifier.fillMaxHeight(0.2f)
) {
GameHeader(
title = game.title,
color = game.headerColor
)
}
Spacer(modifier = Modifier.height(5.dp))
Row(
modifier = Modifier.fillMaxHeight()
) {
Column(Modifier.fillMaxWidth(0.8f)) {
QuestionGrid(
game,
secondRoundDouble,
onResolveQuestion = {
questionsPlayed++
println("Questions: $questionsPlayed, Double: $secondRoundDouble")
},
onPointsChange = { player, points ->
playerPointMap[player] = playerPointMap[player]?.plus(points) ?: 0
}
)
}
Spacer(modifier = Modifier.width(5.dp))
Column(Modifier.fillMaxWidth()) {
PlayerBar(players = playerPointMap)
}
}
}
if (!gameEnded || reviewModeActive) {
GameBoard(
reviewModeActive,
secondRoundDouble,
playerPointMap,
onDisableReviewMode = { reviewModeActive = false },
onChangeQuestionsPlayed = { questionsPlayed++ }
)
} else {
EndCard(
playerPointMap = playerPointMap,
reviewBoard = { reviewModeActive = true },
close = ::exitApplication
)
}

View File

@ -14,6 +14,7 @@ import data.toColor
@Composable
fun EndCard(
playerPointMap: MutableMap<Player, Long>,
reviewBoard: () -> Unit,
close: () -> Unit
) {
// TODO: Style
@ -35,7 +36,7 @@ fun EndCard(
for ((index, data) in sortedPlayersPoints.withIndex()) {
val player = data.key
Row(horizontalArrangement = Arrangement.SpaceBetween) {
Text( text = "${index+1}.")
Text(text = "${index + 1}.")
Surface(
color = player.color.toColor()
) {
@ -43,7 +44,11 @@ fun EndCard(
}
}
}
Button(
onClick = reviewBoard
) {
Text(text = "Review")
}
Button(
onClick = close
) {