Extract Board to Composable
This commit is contained in:
parent
b7ab14c437
commit
1703cd87cb
3 changed files with 90 additions and 43 deletions
55
src/main/kotlin/GameBoard.kt
Normal file
55
src/main/kotlin/GameBoard.kt
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,14 +1,8 @@
|
||||||
import androidx.compose.foundation.layout.*
|
|
||||||
import androidx.compose.material.MaterialTheme
|
import androidx.compose.material.MaterialTheme
|
||||||
import androidx.compose.runtime.*
|
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.Window
|
||||||
import androidx.compose.ui.window.application
|
import androidx.compose.ui.window.application
|
||||||
import components.EndCard
|
import components.EndCard
|
||||||
import components.GameHeader
|
|
||||||
import components.PlayerBar
|
|
||||||
import components.QuestionGrid
|
|
||||||
import data.*
|
import data.*
|
||||||
|
|
||||||
val game = Game(
|
val game = Game(
|
||||||
|
@ -17,7 +11,8 @@ val game = Game(
|
||||||
endGameAfter = 2,
|
endGameAfter = 2,
|
||||||
players = listOf(
|
players = listOf(
|
||||||
Player(name = "Oli"),
|
Player(name = "Oli"),
|
||||||
Player(name = "Gesina")
|
Player(name = "Gesina"),
|
||||||
|
Player(name = "Siggi")
|
||||||
),
|
),
|
||||||
topics = listOf(
|
topics = listOf(
|
||||||
Topic(
|
Topic(
|
||||||
|
@ -26,7 +21,7 @@ val game = Game(
|
||||||
green = 0x44,
|
green = 0x44,
|
||||||
blue = 0xAA
|
blue = 0xAA
|
||||||
),
|
),
|
||||||
topic = "Wer MACKt's?",
|
topic = "Wer?",
|
||||||
questions = listOf(
|
questions = listOf(
|
||||||
QuestionData(
|
QuestionData(
|
||||||
hint = "Mack",
|
hint = "Mack",
|
||||||
|
@ -90,6 +85,21 @@ val game = Game(
|
||||||
points = 5u
|
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) }
|
var questionsPlayed by remember { mutableStateOf(0) }
|
||||||
val secondRoundDouble by remember { derivedStateOf { questionsPlayed >= game.doubleAfter } }
|
val secondRoundDouble by remember { derivedStateOf { questionsPlayed >= game.doubleAfter } }
|
||||||
val gameEnded by remember { derivedStateOf { questionsPlayed >= game.endGameAfter } }
|
val gameEnded by remember { derivedStateOf { questionsPlayed >= game.endGameAfter } }
|
||||||
|
var reviewModeActive by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
if (!gameEnded) {
|
if (!gameEnded || reviewModeActive) {
|
||||||
Column(modifier = Modifier.fillMaxHeight().fillMaxWidth()) {
|
GameBoard(
|
||||||
Row(
|
reviewModeActive,
|
||||||
modifier = Modifier.fillMaxHeight(0.2f)
|
secondRoundDouble,
|
||||||
) {
|
playerPointMap,
|
||||||
GameHeader(
|
onDisableReviewMode = { reviewModeActive = false },
|
||||||
title = game.title,
|
onChangeQuestionsPlayed = { questionsPlayed++ }
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
EndCard(
|
EndCard(
|
||||||
playerPointMap = playerPointMap,
|
playerPointMap = playerPointMap,
|
||||||
|
reviewBoard = { reviewModeActive = true },
|
||||||
close = ::exitApplication
|
close = ::exitApplication
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ import data.toColor
|
||||||
@Composable
|
@Composable
|
||||||
fun EndCard(
|
fun EndCard(
|
||||||
playerPointMap: MutableMap<Player, Long>,
|
playerPointMap: MutableMap<Player, Long>,
|
||||||
|
reviewBoard: () -> Unit,
|
||||||
close: () -> Unit
|
close: () -> Unit
|
||||||
) {
|
) {
|
||||||
// TODO: Style
|
// TODO: Style
|
||||||
|
@ -35,7 +36,7 @@ fun EndCard(
|
||||||
for ((index, data) in sortedPlayersPoints.withIndex()) {
|
for ((index, data) in sortedPlayersPoints.withIndex()) {
|
||||||
val player = data.key
|
val player = data.key
|
||||||
Row(horizontalArrangement = Arrangement.SpaceBetween) {
|
Row(horizontalArrangement = Arrangement.SpaceBetween) {
|
||||||
Text( text = "${index+1}.")
|
Text(text = "${index + 1}.")
|
||||||
Surface(
|
Surface(
|
||||||
color = player.color.toColor()
|
color = player.color.toColor()
|
||||||
) {
|
) {
|
||||||
|
@ -43,7 +44,11 @@ fun EndCard(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Button(
|
||||||
|
onClick = reviewBoard
|
||||||
|
) {
|
||||||
|
Text(text = "Review")
|
||||||
|
}
|
||||||
Button(
|
Button(
|
||||||
onClick = close
|
onClick = close
|
||||||
) {
|
) {
|
||||||
|
|
Loading…
Reference in a new issue