]> git.r.bdr.sh - rbdr/patterns/blob - Sources/Patterns/PatternView.swift
Add explicit initializers
[rbdr/patterns] / Sources / Patterns / PatternView.swift
1 import SwiftUI
2
3 public struct PatternView: View {
4
5 @Binding public var design: TileDesign
6 public var pixelSize: CGFloat
7 public var foregroundColor: Color
8 public var backgroundColor: Color
9
10 private var patternSize: CGFloat {
11 pixelSize * 8.0;
12 }
13
14 public init(design: Binding<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 GeometryReader { gr in
23 VStack(spacing: 0) {
24 ForEach(0 ..< 1 + Int(ceil(gr.size.height / patternSize)), id: \.self) { i in
25 HStack(spacing: 0) {
26 ForEach(0 ..< Int(ceil(gr.size.width / patternSize)), id: \.self) { j in
27 Tile(design: design, pixelSize: pixelSize, foregroundColor: foregroundColor, backgroundColor: backgroundColor)
28 }
29 }
30 }
31 }
32 }.drawingGroup()
33 }
34 }
35
36 struct Pattern_Previews: PreviewProvider {
37 static var previews: some View {
38 VStack {
39 Text("Default")
40 PatternView(design: .constant(TileDesign.grid))
41 Text("Color override")
42 PatternView(design: .constant(TileDesign.balls), foregroundColor: .pink, backgroundColor: .cyan)
43 Text("Pixel size override")
44 PatternView(design: .constant(TileDesign.shingles), pixelSize: 8.0)
45 }
46 }
47 }