]> git.r.bdr.sh - rbdr/patterns/blame - Sources/Patterns/PatternView.swift
Use CoreGraphics to draw instead of SwifTUI
[rbdr/patterns] / Sources / Patterns / PatternView.swift
CommitLineData
8b0b5b39
RBR
1import SwiftUI
2
bf2790fc 3public struct PatternView: View {
cd8a7cf4 4
bf2790fc
RBR
5 @Binding public var design: TileDesign
6 public var pixelSize: CGFloat
7 public var foregroundColor: Color
8 public var backgroundColor: Color
8b0b5b39 9
ba4ee0ed 10 private let image: CGImage
8b0b5b39
RBR
11 private var patternSize: CGFloat {
12 pixelSize * 8.0;
13 }
ba4ee0ed 14
bf2790fc
RBR
15 public init(design: Binding<TileDesign>, pixelSize: CGFloat = 2.0, foregroundColor: Color = .black, backgroundColor: Color = .white) {
16 self._design = design
17 self.pixelSize = pixelSize
18 self.foregroundColor = foregroundColor
19 self.backgroundColor = backgroundColor
ba4ee0ed
RBR
20
21 #if os(iOS) || os(watchOS) || os(tvOS)
22 let foregroundCGColor = UIColor(foregroundColor).cgColor
23 let backgroundCGColor = UIColor(backgroundColor).cgColor
24 #else
25 let foregroundCGColor = NSColor(foregroundColor).cgColor
26 let backgroundCGColor = NSColor(backgroundColor).cgColor
27 #endif
28
29 self.image = TileImage.image(design.wrappedValue, pixelSize: pixelSize, foregroundColor: foregroundCGColor, backgroundColor: backgroundCGColor)
bf2790fc 30 }
ba4ee0ed 31
8b0b5b39
RBR
32 public var body: some View {
33 GeometryReader { gr in
ba4ee0ed 34 Image(image, scale: 1, label: Text("Test")).resizable(resizingMode: .tile)
8b0b5b39
RBR
35 }.drawingGroup()
36 }
37}
38
39struct Pattern_Previews: PreviewProvider {
40 static var previews: some View {
1418fe49
RBR
41 VStack {
42 Text("Default")
bf2790fc 43 PatternView(design: .constant(TileDesign.grid))
1418fe49 44 Text("Color override")
bf2790fc 45 PatternView(design: .constant(TileDesign.balls), foregroundColor: .pink, backgroundColor: .cyan)
1418fe49 46 Text("Pixel size override")
bf2790fc 47 PatternView(design: .constant(TileDesign.shingles), pixelSize: 8.0)
1418fe49 48 }
8b0b5b39
RBR
49 }
50}