X-Git-Url: https://git.r.bdr.sh/rbdr/patterns/blobdiff_plain/8b0b5b394e5eca8538956fecbaa70ca66fdde24b..1418fe49617f5d35b14b19fe0ead15fcc43526c8:/Sources/Patterns/PatternPicker.swift?ds=inline diff --git a/Sources/Patterns/PatternPicker.swift b/Sources/Patterns/PatternPicker.swift new file mode 100644 index 0000000..1f7d17b --- /dev/null +++ b/Sources/Patterns/PatternPicker.swift @@ -0,0 +1,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) + } + } +}