Files
diehammer/DieHammer/Views/Elements/DieView.swift
T

44 lines
1.4 KiB
Swift

//
// DieView.swift
// Bunch O' Dice
//
import SwiftUI
struct DieView: View {
let whichDie: Dice.DiceType
@EnvironmentObject var current: Roll
var body: some View {
let size = 22.0
let displayColorHit = (whichDie.rawValue >= current.cutoff.rawValue) ? Color.red : Color.primary
let displayColorArmor = (whichDie.rawValue <= current.cutoff.rawValue) ? Color.red : Color.primary
let displayColorArmorText = (whichDie.rawValue < current.cutoff.rawValue) ? Color.red : Color.primary
VStack(spacing: 2) {
Text(String(current.dice.value(which: whichDie)))
.foregroundColor(current.state == .armour ? displayColorArmorText : displayColorHit)
.frame(minWidth: 30, alignment: .center)
Image(systemName: "die.face." + String(whichDie.rawValue) + ".fill")
.foregroundColor(current.state == .armour ? displayColorArmor : displayColorHit)
}
.font(.system(size: size))
.padding(4)
.frame(maxWidth: .infinity)
.gesture(TapGesture().onEnded {
if current.state == .hit {
if whichDie.rawValue != 1 {
current.hitCutoff = whichDie
}
} else if current.state == .armour {
current.armourSave = Int(whichDie.rawValue)
}
})
}
}
#Preview {
DieView(whichDie: .four)
.environmentObject(Roll())
}