aboutsummaryrefslogtreecommitdiff
path: root/Sources/Patterns/PatternPicker.swift
blob: 1f7d17bab8fe00a369a5241fdc06806b43322158 (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
51
import SwiftUI

public struct PatternPicker: View {
  
  @Binding var selectedDesign: TileDesign;
  
  var selectedColor: Color = .accentColor
  var pixelSize: CGFloat = 2.0;
  var foregroundColor: Color = .black
  var backgroundColor: Color = .white
  
  let patterns = TileDesign.allCases

  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]
                  })
              }
            }
          }
        }
      }.background(foregroundColor)
    }
}

struct PatternPicker_Previews: PreviewProvider {
    static var previews: some View {
      VStack {
        Text("Default")
        PatternPicker(selectedDesign: .constant(TileDesign.shadowGrid))
        Text("Selected color override")
        PatternPicker(selectedDesign: .constant(TileDesign.shadowGrid),
                      selectedColor: .red)
        Text("Color override")
        PatternPicker(selectedDesign: .constant(TileDesign.shadowGrid),
                      foregroundColor: .pink, backgroundColor: .cyan)
        Text("Pixel size override")
        PatternPicker(selectedDesign: .constant(TileDesign.shadowGrid), pixelSize: 8.0)
      }
    }
}