3 public struct PatternPicker: View {
5 @Binding public var selectedDesign: TileDesign;
7 public var selectedColor: Color
8 public var pixelSize: CGFloat
9 public var foregroundColor: Color
10 public var backgroundColor: Color
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
20 private let patterns = TileDesign.allCases
21 private let verticalTileCount = Int(ceil(Double(TileDesign.allCases.count) / 5.0))
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]
39 }.background(foregroundColor)
43 struct PatternPicker_Previews: PreviewProvider {
44 static var previews: some View {
47 PatternPicker(selectedDesign: .constant(TileDesign.shadowGrid))
48 Text("Selected color override")
49 PatternPicker(selectedDesign: .constant(TileDesign.shadowGrid),
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)