]> git.r.bdr.sh - rbdr/patterns/blob - Sources/Patterns/PatternPicker.swift
32c9f3992f615af32b9e5a8345d311743cbf8b03
[rbdr/patterns] / Sources / Patterns / PatternPicker.swift
1 import SwiftUI
2
3 public struct PatternPicker: View {
4
5 @Binding public var selectedDesign: TileDesign;
6
7 public var selectedColor: Color
8 public var pixelSize: CGFloat
9 public var foregroundColor: Color
10 public var backgroundColor: Color
11
12 public init(selectedDesign: Binding<TileDesign>, selectedColor: Color = .accentColor, pixelSize: CGFloat = 2.0, foregroundColor: Color = .black, backgroundColor: Color = .white) {
13 self._selectedDesign = selectedDesign
14 self.selectedColor = selectedColor
15 self.pixelSize = pixelSize
16 self.foregroundColor = foregroundColor
17 self.backgroundColor = backgroundColor
18 }
19
20 private let patterns = TileDesign.allCases
21 private let verticalTileCount = Int(ceil(Double(TileDesign.allCases.count) / 5.0))
22
23 public var body: some View {
24 VStack(alignment: .leading, spacing: 0) {
25 ForEach(0 ..< verticalTileCount, id: \.self) { i in
26 HStack(alignment: .top, spacing: 0) {
27 ForEach(0 ..< 5) { j in
28 if i * 5 + j < patterns.count {
29 PatternView(design: .constant(patterns[i * 5 + j]), pixelSize: pixelSize, foregroundColor: foregroundColor, backgroundColor: backgroundColor)
30 .frame(width: pixelSize * 16, height: pixelSize * 12)
31 .border(selectedDesign == patterns[i * 5 + j] ? selectedColor : foregroundColor, width: pixelSize / 2.0)
32 .onTapGesture(perform: {
33 selectedDesign = patterns[i * 5 + j]
34 })
35 }
36 }
37 }
38 }
39 }.background(foregroundColor)
40 }
41 }
42
43 struct PatternPicker_Previews: PreviewProvider {
44 static var previews: some View {
45 VStack {
46 Text("Default")
47 PatternPicker(selectedDesign: .constant(TileDesign.shadowGrid))
48 Text("Selected color override")
49 PatternPicker(selectedDesign: .constant(TileDesign.shadowGrid),
50 selectedColor: .red)
51 Text("Color override")
52 PatternPicker(selectedDesign: .constant(TileDesign.shadowGrid),
53 foregroundColor: .pink, backgroundColor: .cyan)
54 Text("Pixel size override")
55 PatternPicker(selectedDesign: .constant(TileDesign.shadowGrid), pixelSize: 8.0)
56 }
57 }
58 }