From bf2790fca182f504255df877beff0ac0334cb2ca Mon Sep 17 00:00:00 2001 From: Ruben Beltran del Rio Date: Wed, 26 Apr 2023 18:20:34 +0200 Subject: Add explicit initializers --- Sources/Patterns/Pattern.swift | 40 ---------------------------- Sources/Patterns/PatternPicker.swift | 51 ++++++++++++++++++++---------------- Sources/Patterns/PatternView.swift | 47 +++++++++++++++++++++++++++++++++ Sources/Patterns/Tile.swift | 7 +++++ 4 files changed, 83 insertions(+), 62 deletions(-) delete mode 100644 Sources/Patterns/Pattern.swift create mode 100644 Sources/Patterns/PatternView.swift (limited to 'Sources/Patterns') diff --git a/Sources/Patterns/Pattern.swift b/Sources/Patterns/Pattern.swift deleted file mode 100644 index 6c63870..0000000 --- a/Sources/Patterns/Pattern.swift +++ /dev/null @@ -1,40 +0,0 @@ -import SwiftUI - -public struct Pattern: View { - - @Binding public var design: TileDesign; - public var pixelSize: CGFloat = 2.0; - public var foregroundColor: Color = .black - public var backgroundColor: Color = .white - - private var patternSize: CGFloat { - pixelSize * 8.0; - } - - public var body: some View { - GeometryReader { gr in - VStack(spacing: 0) { - 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, pixelSize: pixelSize, foregroundColor: foregroundColor, backgroundColor: backgroundColor) - } - } - } - } - }.drawingGroup() - } -} - -struct Pattern_Previews: PreviewProvider { - static var previews: some View { - 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 index 3f550e5..32c9f39 100644 --- a/Sources/Patterns/PatternPicker.swift +++ b/Sources/Patterns/PatternPicker.swift @@ -4,33 +4,40 @@ public struct PatternPicker: View { @Binding public var selectedDesign: TileDesign; - public var selectedColor: Color = .accentColor - public var pixelSize: CGFloat = 2.0; - public var foregroundColor: Color = .black - public var backgroundColor: Color = .white + public var selectedColor: Color + public var pixelSize: CGFloat + public var foregroundColor: Color + public var backgroundColor: Color - let patterns = TileDesign.allCases - - let verticalTileCount = Int(ceil(Double(TileDesign.allCases.count) / 5.0)) + public init(selectedDesign: Binding, selectedColor: Color = .accentColor, pixelSize: CGFloat = 2.0, foregroundColor: Color = .black, backgroundColor: Color = .white) { + self._selectedDesign = selectedDesign + self.selectedColor = selectedColor + self.pixelSize = pixelSize + self.foregroundColor = foregroundColor + self.backgroundColor = backgroundColor + } + + private let patterns = TileDesign.allCases + private 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] - }) - } + 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 { + PatternView(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) - } + } + }.background(foregroundColor) + } } struct PatternPicker_Previews: PreviewProvider { diff --git a/Sources/Patterns/PatternView.swift b/Sources/Patterns/PatternView.swift new file mode 100644 index 0000000..be78bde --- /dev/null +++ b/Sources/Patterns/PatternView.swift @@ -0,0 +1,47 @@ +import SwiftUI + +public struct PatternView: View { + + @Binding public var design: TileDesign + public var pixelSize: CGFloat + public var foregroundColor: Color + public var backgroundColor: Color + + private var patternSize: CGFloat { + pixelSize * 8.0; + } + + public init(design: Binding, pixelSize: CGFloat = 2.0, foregroundColor: Color = .black, backgroundColor: Color = .white) { + self._design = design + self.pixelSize = pixelSize + self.foregroundColor = foregroundColor + self.backgroundColor = backgroundColor + } + + public var body: some View { + GeometryReader { gr in + VStack(spacing: 0) { + 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, pixelSize: pixelSize, foregroundColor: foregroundColor, backgroundColor: backgroundColor) + } + } + } + } + }.drawingGroup() + } +} + +struct Pattern_Previews: PreviewProvider { + static var previews: some View { + VStack { + Text("Default") + PatternView(design: .constant(TileDesign.grid)) + Text("Color override") + PatternView(design: .constant(TileDesign.balls), foregroundColor: .pink, backgroundColor: .cyan) + Text("Pixel size override") + PatternView(design: .constant(TileDesign.shingles), pixelSize: 8.0) + } + } +} diff --git a/Sources/Patterns/Tile.swift b/Sources/Patterns/Tile.swift index 816ebc5..9c39640 100644 --- a/Sources/Patterns/Tile.swift +++ b/Sources/Patterns/Tile.swift @@ -11,6 +11,13 @@ public struct Tile: View { design.pixels() } + public init(design: TileDesign, pixelSize: CGFloat = 2.0, foregroundColor: Color = .black, backgroundColor: Color = .white) { + self.design = design + self.pixelSize = pixelSize + self.foregroundColor = foregroundColor + self.backgroundColor = backgroundColor + } + public var body: some View { VStack(spacing: 0) { ForEach(0 ..< 8) { i in -- cgit