Yahtzee: two-column score card, larger text #22
@@ -6,6 +6,9 @@ import androidx.compose.foundation.border
|
|||||||
import androidx.compose.foundation.clickable
|
import androidx.compose.foundation.clickable
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
|
import androidx.compose.foundation.layout.BoxWithConstraints
|
||||||
|
import androidx.compose.foundation.horizontalScroll
|
||||||
|
import androidx.compose.foundation.layout.width
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.Canvas
|
import androidx.compose.foundation.Canvas
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
@@ -254,29 +257,62 @@ private fun ScoreCard(
|
|||||||
writable: Set<YahtzeeCategory>,
|
writable: Set<YahtzeeCategory>,
|
||||||
onWrite: (YahtzeeCategory) -> Unit,
|
onWrite: (YahtzeeCategory) -> Unit,
|
||||||
) {
|
) {
|
||||||
Column(modifier = Modifier.fillMaxWidth()) {
|
// Upper boxes and bonus on the left, lower boxes and total on the right, so
|
||||||
|
// each half is wide enough for text that can actually be read. Each half
|
||||||
|
// has a floor on its width; if two of them do not fit (many players, large
|
||||||
|
// text) the pair scrolls sideways instead of squeezing.
|
||||||
|
BoxWithConstraints(modifier = Modifier.fillMaxWidth()) {
|
||||||
|
val needed = (CARD_LABEL_WIDTH + CARD_SEAT_WIDTH * state.cards.size).dp
|
||||||
|
val half = maxOf((maxWidth - CARD_GAP.dp) / 2, needed)
|
||||||
|
Row(
|
||||||
|
modifier = Modifier.horizontalScroll(rememberScrollState()),
|
||||||
|
horizontalArrangement = Arrangement.spacedBy(CARD_GAP.dp),
|
||||||
|
) {
|
||||||
|
Column(modifier = Modifier.width(half)) {
|
||||||
|
CardHeader(state, localSeat)
|
||||||
|
CardSection(YahtzeeSection.UPPER, state, localSeat, writable, onWrite)
|
||||||
|
TotalRow("Bonus at 63", state.cards.map { it.upperBonus })
|
||||||
|
}
|
||||||
|
Column(modifier = Modifier.width(half)) {
|
||||||
|
CardHeader(state, localSeat)
|
||||||
|
CardSection(YahtzeeSection.LOWER, state, localSeat, writable, onWrite)
|
||||||
|
Divider()
|
||||||
|
TotalRow("Total", state.cards.indices.map { state.totalFor(it) }, bold = true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private const val CARD_LABEL_WIDTH = 120
|
||||||
|
private const val CARD_SEAT_WIDTH = 56
|
||||||
|
private const val CARD_GAP = 12
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
private fun CardHeader(state: YahtzeeState, localSeat: Int) {
|
||||||
Row(modifier = Modifier.fillMaxWidth()) {
|
Row(modifier = Modifier.fillMaxWidth()) {
|
||||||
Text("", modifier = Modifier.weight(2f), style = MaterialTheme.typography.labelSmall)
|
Text("", modifier = Modifier.weight(2f))
|
||||||
state.cards.indices.forEach { seat ->
|
state.cards.indices.forEach { seat ->
|
||||||
Text(
|
Text(
|
||||||
text = if (seat == localSeat) "You" else "S$seat",
|
text = if (seat == localSeat) "You" else "S$seat",
|
||||||
modifier = Modifier.weight(1f),
|
modifier = Modifier.weight(1f),
|
||||||
style = MaterialTheme.typography.labelSmall,
|
style = MaterialTheme.typography.labelLarge,
|
||||||
fontWeight = FontWeight.Bold,
|
fontWeight = FontWeight.Bold,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
YahtzeeSection.entries.forEach { section ->
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
private fun CardSection(
|
||||||
|
section: YahtzeeSection,
|
||||||
|
state: YahtzeeState,
|
||||||
|
localSeat: Int,
|
||||||
|
writable: Set<YahtzeeCategory>,
|
||||||
|
onWrite: (YahtzeeCategory) -> Unit,
|
||||||
|
) {
|
||||||
YahtzeeCategory.entries.filter { it.section == section }.forEach { category ->
|
YahtzeeCategory.entries.filter { it.section == section }.forEach { category ->
|
||||||
CardRow(state, localSeat, category, writable, onWrite)
|
CardRow(state, localSeat, category, writable, onWrite)
|
||||||
}
|
}
|
||||||
if (section == YahtzeeSection.UPPER) {
|
|
||||||
TotalRow("Bonus at 63", state.cards.map { it.upperBonus })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Divider()
|
|
||||||
TotalRow("Total", state.cards.indices.map { state.totalFor(it) }, bold = true)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
@@ -292,12 +328,12 @@ private fun CardRow(
|
|||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
.clickable(enabled = open) { onWrite(category) }
|
.clickable(enabled = open) { onWrite(category) }
|
||||||
.padding(vertical = 2.dp),
|
.padding(vertical = 5.dp),
|
||||||
) {
|
) {
|
||||||
Text(
|
Text(
|
||||||
text = category.label,
|
text = category.label,
|
||||||
modifier = Modifier.weight(2f),
|
modifier = Modifier.weight(2f),
|
||||||
style = MaterialTheme.typography.bodySmall,
|
style = MaterialTheme.typography.bodyMedium,
|
||||||
fontWeight = if (open) FontWeight.Bold else FontWeight.Normal,
|
fontWeight = if (open) FontWeight.Bold else FontWeight.Normal,
|
||||||
)
|
)
|
||||||
state.cards.forEachIndexed { seat, card ->
|
state.cards.forEachIndexed { seat, card ->
|
||||||
@@ -311,7 +347,7 @@ private fun CardRow(
|
|||||||
else -> "—"
|
else -> "—"
|
||||||
},
|
},
|
||||||
modifier = Modifier.weight(1f),
|
modifier = Modifier.weight(1f),
|
||||||
style = MaterialTheme.typography.bodySmall,
|
style = MaterialTheme.typography.bodyMedium,
|
||||||
color = if (written == null && open) WallaceRed else MaterialTheme.colorScheme.onSurface,
|
color = if (written == null && open) WallaceRed else MaterialTheme.colorScheme.onSurface,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -320,18 +356,18 @@ private fun CardRow(
|
|||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
private fun TotalRow(label: String, values: List<Int>, bold: Boolean = false) {
|
private fun TotalRow(label: String, values: List<Int>, bold: Boolean = false) {
|
||||||
Row(modifier = Modifier.fillMaxWidth().padding(vertical = 2.dp)) {
|
Row(modifier = Modifier.fillMaxWidth().padding(vertical = 5.dp)) {
|
||||||
Text(
|
Text(
|
||||||
text = label,
|
text = label,
|
||||||
modifier = Modifier.weight(2f),
|
modifier = Modifier.weight(2f),
|
||||||
style = MaterialTheme.typography.bodySmall,
|
style = MaterialTheme.typography.bodyMedium,
|
||||||
fontWeight = if (bold) FontWeight.Bold else FontWeight.Normal,
|
fontWeight = if (bold) FontWeight.Bold else FontWeight.Normal,
|
||||||
)
|
)
|
||||||
values.forEach {
|
values.forEach {
|
||||||
Text(
|
Text(
|
||||||
text = "$it",
|
text = "$it",
|
||||||
modifier = Modifier.weight(1f),
|
modifier = Modifier.weight(1f),
|
||||||
style = MaterialTheme.typography.bodySmall,
|
style = MaterialTheme.typography.bodyMedium,
|
||||||
fontWeight = if (bold) FontWeight.Bold else FontWeight.Normal,
|
fontWeight = if (bold) FontWeight.Bold else FontWeight.Normal,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user