// // EmojiMemoryGameView.swift // Memorize // // Created by ching on 2023/2/12. // import SwiftUI struct EmojiMemoryGameView: View { @ObservedObject var game: EmojiMemoryGame var body: some View { ScrollView { LazyVGrid(columns: [GridItem(.adaptive(minimum: const.DrawingConstants.gridWidth))]) { ForEach(game.cards) { card in CardView(card: card).aspectRatio(const.DrawingConstants.gridAspectRatio, contentMode: .fit) .onTapGesture { game.choose(card) } } } } .foregroundColor(/*@START_MENU_TOKEN@*/ .red/*@END_MENU_TOKEN@*/) .padding(.horizontal) } } struct CardView: View { let card: EmojiMemoryGame.Card var body: some View { GeometryReader { geometry in ZStack { let shape = RoundedRectangle(cornerRadius: const.DrawingConstants.cornerRadius) if card.isFaceUp && !card.isMatched { shape.fill().foregroundColor(.white) shape.strokeBorder(lineWidth: const.DrawingConstants.lineWidth) Text(card.content).font(font(in: geometry.size)) } else if card.isMatched { shape.opacity(const.DrawingConstants.matchedCardOpacity) } else { shape.fill() } } } } private func font(in size: CGSize) -> Font { Font.system(size: min(size.width, size.height) * const.DrawingConstants.fontScale) } } struct ContentView_Previews: PreviewProvider { static var previews: some View { let game = EmojiMemoryGame() EmojiMemoryGameView(game: game) .preferredColorScheme(.dark) EmojiMemoryGameView(game: game) .preferredColorScheme(.light) } }