CasinoGames/Baccarat/Views/Chips/ChipStackView.swift

65 lines
1.5 KiB
Swift

//
// ChipStackView.swift
// Baccarat
//
// A stacked display of chips representing a bet amount.
//
import SwiftUI
/// A stack of chips showing a bet amount.
struct ChipStackView: View {
let amount: Int
let maxChips: Int
// MARK: - Layout Constants
private let chipSize: CGFloat = 40
private let stackOffset: CGFloat = 4
init(amount: Int, maxChips: Int = 5) {
self.amount = amount
self.maxChips = maxChips
}
private var chipBreakdown: [(ChipDenomination, Int)] {
var remaining = amount
var result: [(ChipDenomination, Int)] = []
for denom in ChipDenomination.allCases.reversed() {
let count = remaining / denom.rawValue
if count > 0 {
result.append((denom, min(count, maxChips)))
remaining -= count * denom.rawValue
}
}
return result
}
var body: some View {
ZStack {
ForEach(chipBreakdown.indices, id: \.self) { index in
let (denom, _) = chipBreakdown[index]
ChipView(denomination: denom, size: chipSize)
.offset(y: CGFloat(-index) * stackOffset)
}
}
}
}
#Preview {
ZStack {
Color.Table.preview
.ignoresSafeArea()
HStack(spacing: Design.Spacing.xxxLarge) {
ChipStackView(amount: 100)
ChipStackView(amount: 550)
ChipStackView(amount: 1500)
ChipStackView(amount: 10_000)
}
}
}