aboutsummaryrefslogtreecommitdiff
path: root/Sources/Patterns
diff options
context:
space:
mode:
Diffstat (limited to 'Sources/Patterns')
-rw-r--r--Sources/Patterns/Pattern.swift16
-rw-r--r--Sources/Patterns/PatternPicker.swift51
-rw-r--r--Sources/Patterns/Tile.swift22
3 files changed, 78 insertions, 11 deletions
diff --git a/Sources/Patterns/Pattern.swift b/Sources/Patterns/Pattern.swift
index 1951888..4fbc528 100644
--- a/Sources/Patterns/Pattern.swift
+++ b/Sources/Patterns/Pattern.swift
@@ -1,14 +1,15 @@
import SwiftUI
public struct Pattern: View {
-
- private let pixelSize: CGFloat = 2.0;
private var patternSize: CGFloat {
pixelSize * 8.0;
}
@Binding var design: TileDesign;
+ var pixelSize: CGFloat = 2.0;
+ var foregroundColor: Color = .black
+ var backgroundColor: Color = .white
public var body: some View {
GeometryReader { gr in
@@ -16,7 +17,7 @@ public struct Pattern: View {
ForEach(0 ..< 1 + Int(ceil(gr.size.height / patternSize)), id: \.self) { i in
HStack(spacing: 0) {
ForEach(0 ..< Int(ceil(gr.size.width / patternSize)), id: \.self) { j in
- Tile(design: design)
+ Tile(design: design, pixelSize: pixelSize, foregroundColor: foregroundColor, backgroundColor: backgroundColor)
}
}
}
@@ -27,6 +28,13 @@ public struct Pattern: View {
struct Pattern_Previews: PreviewProvider {
static var previews: some View {
- Pattern(design: .constant(TileDesign.grid))
+ VStack {
+ Text("Default")
+ Pattern(design: .constant(TileDesign.grid))
+ Text("Color override")
+ Pattern(design: .constant(TileDesign.balls), foregroundColor: .pink, backgroundColor: .cyan)
+ Text("Pixel size override")
+ Pattern(design: .constant(TileDesign.shingles), pixelSize: 8.0)
+ }
}
}
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)
+ }
+ }
+}
diff --git a/Sources/Patterns/Tile.swift b/Sources/Patterns/Tile.swift
index c27fcf9..3893324 100644
--- a/Sources/Patterns/Tile.swift
+++ b/Sources/Patterns/Tile.swift
@@ -1,16 +1,17 @@
import SwiftUI
-struct Tile: View {
-
- let pixelSize: CGFloat = 2.0;
+public struct Tile: View {
let design: TileDesign
+ var pixelSize: CGFloat = 2.0;
+ var foregroundColor: Color = .black
+ var backgroundColor: Color = .white
private var pixels: [Int] {
design.pixels()
}
- var body: some View {
+ public var body: some View {
VStack(spacing: 0) {
ForEach(0 ..< 8) { i in
HStack(spacing: 0) {
@@ -18,8 +19,8 @@ struct Tile: View {
Rectangle()
.frame(width: pixelSize, height: pixelSize)
.foregroundColor(pixels[(i % 8) * 8 + j % 8] == 0
- ? .black
- : .white
+ ? foregroundColor
+ : backgroundColor
)
}
}
@@ -30,6 +31,13 @@ struct Tile: View {
struct Tile_Previews: PreviewProvider {
static var previews: some View {
- Tile(design: .grid)
+ VStack {
+ Text("Default")
+ Tile(design: .grid)
+ Text("Color override")
+ Tile(design: .balls, foregroundColor: .pink, backgroundColor: .cyan)
+ Text("Pixel size override")
+ Tile(design: .shingles, pixelSize: 8.0)
+ }
}
}