aboutsummaryrefslogtreecommitdiff
path: root/Sources/Patterns/Tile.swift
blob: 389332428f29b8ebe9e22f56ba7e10e4f77a7810 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import SwiftUI

public struct Tile: View {
  
  let design: TileDesign
  var pixelSize: CGFloat = 2.0;
  var foregroundColor: Color = .black
  var backgroundColor: Color = .white
  
  private var pixels: [Int] {
    design.pixels()
  }
  
    public var body: some View {
      VStack(spacing: 0) {
        ForEach(0 ..< 8) { i in
          HStack(spacing: 0) {
            ForEach(0 ..< 8) { j in
              Rectangle()
                .frame(width: pixelSize, height: pixelSize)
                .foregroundColor(pixels[(i % 8) * 8 + j % 8] == 0
                                  ? foregroundColor
                                  : backgroundColor
                )
            }
          }
        }
      }
    }
}

struct Tile_Previews: PreviewProvider {
    static var previews: some View {
      VStack {
        Text("Default")
        Tile(design: .grid)
        Text("Color override")
        Tile(design: .balls, foregroundColor: .pink, backgroundColor: .cyan)
        Text("Pixel size override")
        Tile(design: .shingles, pixelSize: 8.0)
      }
    }
}