]> git.r.bdr.sh - rbdr/patterns/blob - Sources/Patterns/Tile.swift
9c39640b8ab4dbfdae2ab29988ac9851a8310079
[rbdr/patterns] / Sources / Patterns / Tile.swift
1 import SwiftUI
2
3 public struct Tile: View {
4
5 public let design: TileDesign
6 public var pixelSize: CGFloat = 2.0;
7 public var foregroundColor: Color = .black
8 public var backgroundColor: Color = .white
9
10 private var pixels: [Int] {
11 design.pixels()
12 }
13
14 public init(design: TileDesign, pixelSize: CGFloat = 2.0, foregroundColor: Color = .black, backgroundColor: Color = .white) {
15 self.design = design
16 self.pixelSize = pixelSize
17 self.foregroundColor = foregroundColor
18 self.backgroundColor = backgroundColor
19 }
20
21 public var body: some View {
22 VStack(spacing: 0) {
23 ForEach(0 ..< 8) { i in
24 HStack(spacing: 0) {
25 ForEach(0 ..< 8) { j in
26 Rectangle()
27 .frame(width: pixelSize, height: pixelSize)
28 .foregroundColor(pixels[(i % 8) * 8 + j % 8] == 0
29 ? foregroundColor
30 : backgroundColor
31 )
32 }
33 }
34 }
35 }
36 }
37 }
38
39 struct Tile_Previews: PreviewProvider {
40 static var previews: some View {
41 VStack {
42 Text("Default")
43 Tile(design: .grid)
44 Text("Color override")
45 Tile(design: .balls, foregroundColor: .pink, backgroundColor: .cyan)
46 Text("Pixel size override")
47 Tile(design: .shingles, pixelSize: 8.0)
48 }
49 }
50 }