|
| 1 | +package cz.frantisekmasa.wfrp_master.common.character.combat |
| 2 | + |
| 3 | +import androidx.compose.animation.animateContentSize |
| 4 | +import androidx.compose.foundation.clickable |
| 5 | +import androidx.compose.foundation.layout.Arrangement |
| 6 | +import androidx.compose.foundation.layout.Column |
| 7 | +import androidx.compose.foundation.layout.Row |
| 8 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 9 | +import androidx.compose.foundation.layout.padding |
| 10 | +import androidx.compose.material.Icon |
| 11 | +import androidx.compose.material.ListItem |
| 12 | +import androidx.compose.material.MaterialTheme |
| 13 | +import androidx.compose.material.Text |
| 14 | +import androidx.compose.material.icons.Icons |
| 15 | +import androidx.compose.material.icons.rounded.ExpandLess |
| 16 | +import androidx.compose.material.icons.rounded.ExpandMore |
| 17 | +import androidx.compose.runtime.Composable |
| 18 | +import androidx.compose.runtime.Stable |
| 19 | +import androidx.compose.runtime.getValue |
| 20 | +import androidx.compose.runtime.key |
| 21 | +import androidx.compose.runtime.mutableStateOf |
| 22 | +import androidx.compose.runtime.remember |
| 23 | +import androidx.compose.runtime.setValue |
| 24 | +import androidx.compose.ui.Alignment |
| 25 | +import androidx.compose.ui.Modifier |
| 26 | +import androidx.compose.ui.text.font.FontWeight |
| 27 | +import androidx.compose.ui.unit.dp |
| 28 | +import cz.frantisekmasa.wfrp_master.common.character.combat.CharacterCombatScreenModel.WornArmourPiece |
| 29 | +import cz.frantisekmasa.wfrp_master.common.core.domain.HitLocation |
| 30 | +import cz.frantisekmasa.wfrp_master.common.core.domain.localizedName |
| 31 | +import cz.frantisekmasa.wfrp_master.common.core.domain.trappings.Armour |
| 32 | +import cz.frantisekmasa.wfrp_master.common.core.domain.trappings.ArmourPoints |
| 33 | +import cz.frantisekmasa.wfrp_master.common.core.domain.trappings.InventoryItem |
| 34 | +import cz.frantisekmasa.wfrp_master.common.core.ui.cards.CardContainer |
| 35 | +import cz.frantisekmasa.wfrp_master.common.core.ui.cards.CardTitle |
| 36 | +import cz.frantisekmasa.wfrp_master.common.core.ui.forms.Chip |
| 37 | +import cz.frantisekmasa.wfrp_master.common.core.ui.primitives.Spacing |
| 38 | +import cz.frantisekmasa.wfrp_master.common.core.ui.primitives.UserTip |
| 39 | +import cz.frantisekmasa.wfrp_master.common.core.ui.primitives.UserTipCard |
| 40 | +import cz.frantisekmasa.wfrp_master.common.localization.LocalStrings |
| 41 | + |
| 42 | +@Composable |
| 43 | +fun ArmourCard( |
| 44 | + armour: Armour, |
| 45 | + armourPieces: Map<HitLocation, List<WornArmourPiece>>, |
| 46 | + toughnessBonus: UInt, |
| 47 | + onTrappingClick: (InventoryItem) -> Unit, |
| 48 | +) { |
| 49 | + UserTipCard(UserTip.ARMOUR_TRAPPINGS, Modifier.padding(horizontal = 8.dp)) |
| 50 | + |
| 51 | + CardContainer( |
| 52 | + Modifier |
| 53 | + .padding(horizontal = 8.dp) |
| 54 | + .padding(bottom = 8.dp) |
| 55 | + ) { |
| 56 | + CardTitle(LocalStrings.current.armour.title) |
| 57 | + |
| 58 | + Row( |
| 59 | + modifier = Modifier.padding(top = Spacing.large), |
| 60 | + horizontalArrangement = Arrangement.spacedBy( |
| 61 | + Spacing.large, |
| 62 | + Alignment.CenterHorizontally |
| 63 | + ), |
| 64 | + ) { |
| 65 | + Row( |
| 66 | + horizontalArrangement = Arrangement.End, |
| 67 | + verticalAlignment = Alignment.CenterVertically, |
| 68 | + modifier = Modifier.weight(1f), |
| 69 | + ) { |
| 70 | + Text( |
| 71 | + LocalStrings.current.characteristics.toughnessBonusShortcut, |
| 72 | + Modifier.padding(end = Spacing.medium), |
| 73 | + ) |
| 74 | + Points(toughnessBonus.toInt()) |
| 75 | + } |
| 76 | + |
| 77 | + Row( |
| 78 | + verticalAlignment = Alignment.CenterVertically, |
| 79 | + modifier = Modifier.weight(1f), |
| 80 | + ) { |
| 81 | + Points(armour.shield) |
| 82 | + Text( |
| 83 | + LocalStrings.current.armour.shield, |
| 84 | + Modifier.padding(start = Spacing.medium), |
| 85 | + ) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + val locations = remember { HitLocation.values().sortedBy { it.rollRange.first } } |
| 91 | + |
| 92 | + locations.forEach { location -> |
| 93 | + key(location) { |
| 94 | + Location( |
| 95 | + location = location, |
| 96 | + points = armour.armourPoints(location), |
| 97 | + trappings = armourPieces[location] ?: emptyList(), |
| 98 | + onTrappingClick = onTrappingClick, |
| 99 | + ) |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +@Composable |
| 106 | +@Stable |
| 107 | +private fun formatRoll(roll: Int): String { |
| 108 | + if (roll == 100) { |
| 109 | + return "00" |
| 110 | + } |
| 111 | + |
| 112 | + return roll.toString().padStart(2, '0') |
| 113 | +} |
| 114 | + |
| 115 | +@Composable |
| 116 | +private fun Points(value: Int) { |
| 117 | + Chip(padding = Spacing.tiny) { |
| 118 | + Text(value.toString()) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +@Composable |
| 123 | +private fun Location( |
| 124 | + location: HitLocation, |
| 125 | + points: ArmourPoints, |
| 126 | + trappings: List<WornArmourPiece>, |
| 127 | + onTrappingClick: (InventoryItem) -> Unit, |
| 128 | +) { |
| 129 | + val rollRange = location.rollRange |
| 130 | + var expanded by remember { mutableStateOf(false) } |
| 131 | + |
| 132 | + Row( |
| 133 | + verticalAlignment = Alignment.CenterVertically, |
| 134 | + modifier = Modifier |
| 135 | + .fillMaxWidth() |
| 136 | + .padding(start = Spacing.large, top = Spacing.large) |
| 137 | + .let { |
| 138 | + if (trappings.isNotEmpty()) |
| 139 | + it.clickable { expanded = !expanded } |
| 140 | + else it |
| 141 | + } |
| 142 | + ) { |
| 143 | + Row(Modifier.weight(1f)) { |
| 144 | + Row { |
| 145 | + Text( |
| 146 | + "${formatRoll(rollRange.first)}-${formatRoll(rollRange.last)}", |
| 147 | + fontWeight = FontWeight.Bold, |
| 148 | + modifier = Modifier.padding(end = Spacing.medium), |
| 149 | + ) |
| 150 | + |
| 151 | + Text(location.localizedName) |
| 152 | + } |
| 153 | + |
| 154 | + if (trappings.isNotEmpty()) { |
| 155 | + Icon( |
| 156 | + if (expanded) |
| 157 | + Icons.Rounded.ExpandLess |
| 158 | + else Icons.Rounded.ExpandMore, |
| 159 | + null, |
| 160 | + ) |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + Points(points.value) |
| 165 | + } |
| 166 | + |
| 167 | + Column(Modifier.animateContentSize()) { |
| 168 | + if (!expanded) { |
| 169 | + return@Column |
| 170 | + } |
| 171 | + |
| 172 | + trappings.forEach { item -> |
| 173 | + key(item.trapping.id) { |
| 174 | + val armour = item.armour |
| 175 | + |
| 176 | + ListItem( |
| 177 | + modifier = Modifier.clickable { onTrappingClick(item.trapping) }, |
| 178 | + text = { Text(item.trapping.name) }, |
| 179 | + secondaryText = if (armour.qualities.isNotEmpty() || armour.flaws.isNotEmpty()) |
| 180 | + ({ |
| 181 | + TrappingFeatureList( |
| 182 | + armour.qualities, |
| 183 | + armour.flaws, |
| 184 | + Modifier.fillMaxWidth() |
| 185 | + ) |
| 186 | + }) |
| 187 | + else null, |
| 188 | + trailing = { |
| 189 | + Text( |
| 190 | + text = armour.points.value.toString(), |
| 191 | + style = MaterialTheme.typography.body2, |
| 192 | + ) |
| 193 | + } |
| 194 | + ) |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments