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