Tetris/TetrisUI/TetrominoView.swift

57 lines
1.7 KiB
Swift
Raw Normal View History

2019-10-16 03:17:47 +00:00
//
// TetrominoView.swift
// Tetris
//
// Created by Shadowfacts on 10/15/19.
// Copyright © 2019 Shadowfacts. All rights reserved.
//
import SwiftUI
import TetrisKit
public struct TetrominoView: View {
let size: CGFloat
2019-10-16 03:17:47 +00:00
let tetromino: Tetromino
let color: Color
let border: Bool
2019-10-16 03:17:47 +00:00
public init(size: CGFloat, tetromino: Tetromino, color: Color? = nil, border: Bool = true) {
self.size = size
2019-10-16 03:17:47 +00:00
self.tetromino = tetromino
self.color = color ?? tetromino.color
self.border = border
2019-10-16 03:17:47 +00:00
}
public var body: some View {
GridView(rows: tetromino.shape.count, columns: tetromino.shape.first!.count) { (col, row, _) in
2019-10-20 23:21:04 +00:00
if row < self.tetromino.shape.count && col < self.tetromino.shape[row].count && self.tetromino.shape[row][col] {
if self.border {
Rectangle()
.foregroundColor(self.color)
.frame(width: self.size, height: self.size)
.border(self.tetromino.borderColor, width: 6)
} else {
Rectangle()
.foregroundColor(self.color)
.frame(width: self.size, height: self.size)
}
2019-10-20 23:21:04 +00:00
} else {
Rectangle()
.foregroundColor(.clear)
.frame(width: self.size, height: self.size)
2019-10-20 23:21:04 +00:00
}
2019-10-16 03:17:47 +00:00
}
}
}
struct TetrominoView_Previews: PreviewProvider {
static var previews: some View {
Group {
TetrominoView(size: 50, tetromino: .t)
TetrominoView(size: 50, tetromino: .i)
TetrominoView(size: 50, tetromino: .z)
TetrominoView(size: 50, tetromino: .j)
2019-10-16 03:17:47 +00:00
}
}
}