aboutsummaryrefslogtreecommitdiff
path: root/Sources/Patterns/PatternView.swift
blob: d4ed37b3df8c241f2d1b4eec85e75b105c525d08 (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
44
45
46
47
48
49
50
import SwiftUI

public struct PatternView: View {

  @Binding public var design: TileDesign
  public var pixelSize: CGFloat
  public var foregroundColor: Color
  public var backgroundColor: Color
  
  private let image: CGImage
  private var patternSize: CGFloat {
    pixelSize * 8.0;
  }

  public init(design: Binding<TileDesign>, pixelSize: CGFloat = 2.0, foregroundColor: Color = .black, backgroundColor: Color = .white) {
    self._design = design
    self.pixelSize = pixelSize
    self.foregroundColor = foregroundColor
    self.backgroundColor = backgroundColor
    
    #if os(iOS) || os(watchOS) || os(tvOS)
      let foregroundCGColor = UIColor(foregroundColor).cgColor
      let backgroundCGColor = UIColor(backgroundColor).cgColor
    #else
      let foregroundCGColor = NSColor(foregroundColor).cgColor
    let backgroundCGColor = NSColor(backgroundColor).cgColor
    #endif
    
    self.image = TileImage.image(design.wrappedValue, pixelSize: pixelSize, foregroundColor: foregroundCGColor, backgroundColor: backgroundCGColor)
  }

  public var body: some View {
    GeometryReader { gr in
      Image(image, scale: 1, label: Text("Test")).resizable(resizingMode: .tile)
    }.drawingGroup()
  }
}

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