]> git.r.bdr.sh - rbdr/patterns/blobdiff - Sources/Patterns/PatternPicker.swift
Use CoreGraphics to draw instead of SwifTUI
[rbdr/patterns] / Sources / Patterns / PatternPicker.swift
index 1f7d17bab8fe00a369a5241fdc06806b43322158..32c9f3992f615af32b9e5a8345d311743cbf8b03 100644 (file)
@@ -2,35 +2,42 @@ import SwiftUI
 
 public struct PatternPicker: View {
   
 
 public struct PatternPicker: View {
   
-  @Binding var selectedDesign: TileDesign;
+  @Binding public var selectedDesign: TileDesign;
   
   
-  var selectedColor: Color = .accentColor
-  var pixelSize: CGFloat = 2.0;
-  var foregroundColor: Color = .black
-  var backgroundColor: Color = .white
+  public var selectedColor: Color
+  public var pixelSize: CGFloat
+  public var foregroundColor: Color
+  public var backgroundColor: Color
   
   
-  let patterns = TileDesign.allCases
-
-  let verticalTileCount = Int(ceil(Double(TileDesign.allCases.count) / 5.0))
+  public init(selectedDesign: Binding<TileDesign>, selectedColor: Color = .accentColor, pixelSize: CGFloat = 2.0, foregroundColor: Color = .black, backgroundColor: Color = .white) {
+    self._selectedDesign = selectedDesign
+    self.selectedColor = selectedColor
+    self.pixelSize = pixelSize
+    self.foregroundColor = foregroundColor
+    self.backgroundColor = backgroundColor
+  }
+  
+  private let patterns = TileDesign.allCases
+  private let verticalTileCount = Int(ceil(Double(TileDesign.allCases.count) / 5.0))
   
   
-    public var body: some View {
-      VStack(alignment: .leading, spacing: 0) {
-        ForEach(0 ..< verticalTileCount, id: \.self) { i in
-          HStack(alignment: .top, spacing: 0) {
-            ForEach(0 ..< 5) { j in
-              if i * 5 + j < patterns.count {
-                Pattern(design: .constant(patterns[i * 5 + j]), pixelSize: pixelSize, foregroundColor: foregroundColor, backgroundColor: backgroundColor)
-                  .frame(width: pixelSize * 16, height: pixelSize * 12)
-                  .border(selectedDesign == patterns[i * 5 + j] ? selectedColor : foregroundColor, width: pixelSize / 2.0)
-                  .onTapGesture(perform: {
-                    selectedDesign = patterns[i * 5 + j]
-                  })
-              }
+  public var body: some View {
+    VStack(alignment: .leading, spacing: 0) {
+      ForEach(0 ..< verticalTileCount, id: \.self) { i in
+        HStack(alignment: .top, spacing: 0) {
+          ForEach(0 ..< 5) { j in
+            if i * 5 + j < patterns.count {
+              PatternView(design: .constant(patterns[i * 5 + j]), pixelSize: pixelSize, foregroundColor: foregroundColor, backgroundColor: backgroundColor)
+                .frame(width: pixelSize * 16, height: pixelSize * 12)
+                .border(selectedDesign == patterns[i * 5 + j] ? selectedColor : foregroundColor, width: pixelSize / 2.0)
+                .onTapGesture(perform: {
+                  selectedDesign = patterns[i * 5 + j]
+                })
             }
           }
         }
             }
           }
         }
-      }.background(foregroundColor)
-    }
+      }
+    }.background(foregroundColor)
+  }
 }
 
 struct PatternPicker_Previews: PreviewProvider {
 }
 
 struct PatternPicker_Previews: PreviewProvider {