diff options
Diffstat (limited to 'Sources')
| -rw-r--r-- | Sources/Patterns/Pattern.swift | 32 | ||||
| -rw-r--r-- | Sources/Patterns/Patterns.swift | 6 | ||||
| -rw-r--r-- | Sources/Patterns/Tile.swift | 35 | ||||
| -rw-r--r-- | Sources/Patterns/TileDesign.swift | 163 |
4 files changed, 230 insertions, 6 deletions
diff --git a/Sources/Patterns/Pattern.swift b/Sources/Patterns/Pattern.swift new file mode 100644 index 0000000..1951888 --- /dev/null +++ b/Sources/Patterns/Pattern.swift @@ -0,0 +1,32 @@ +import SwiftUI + +public struct Pattern: View { + + private let pixelSize: CGFloat = 2.0; + + private var patternSize: CGFloat { + pixelSize * 8.0; + } + + @Binding var design: TileDesign; + + 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) + } + } + } + } + }.drawingGroup() + } +} + +struct Pattern_Previews: PreviewProvider { + static var previews: some View { + Pattern(design: .constant(TileDesign.grid)) + } +} diff --git a/Sources/Patterns/Patterns.swift b/Sources/Patterns/Patterns.swift deleted file mode 100644 index 52a8771..0000000 --- a/Sources/Patterns/Patterns.swift +++ /dev/null @@ -1,6 +0,0 @@ -public struct Patterns { - public private(set) var text = "Hello, World!" - - public init() { - } -} diff --git a/Sources/Patterns/Tile.swift b/Sources/Patterns/Tile.swift new file mode 100644 index 0000000..c27fcf9 --- /dev/null +++ b/Sources/Patterns/Tile.swift @@ -0,0 +1,35 @@ +import SwiftUI + +struct Tile: View { + + let pixelSize: CGFloat = 2.0; + + let design: TileDesign + + private var pixels: [Int] { + design.pixels() + } + + var body: some View { + VStack(spacing: 0) { + ForEach(0 ..< 8) { i in + HStack(spacing: 0) { + ForEach(0 ..< 8) { j in + Rectangle() + .frame(width: pixelSize, height: pixelSize) + .foregroundColor(pixels[(i % 8) * 8 + j % 8] == 0 + ? .black + : .white + ) + } + } + } + } + } +} + +struct Tile_Previews: PreviewProvider { + static var previews: some View { + Tile(design: .grid) + } +} diff --git a/Sources/Patterns/TileDesign.swift b/Sources/Patterns/TileDesign.swift new file mode 100644 index 0000000..5541102 --- /dev/null +++ b/Sources/Patterns/TileDesign.swift @@ -0,0 +1,163 @@ +public enum TileDesign: CaseIterable { + case grid + case dottedGrid + case stitch + case curvedTile + case brick + case tile + case shadowGrid + case circles + case trees + case shingles + case wicker + case rhombus + case balls + + func pixels() -> [Int] { + switch self { + case .grid: + return [ + 1, 0, 1, 0, 1, 0, 1, 0, + 0, 1, 0, 1, 0, 1, 0, 1, + 1, 0, 1, 0, 1, 0, 1, 0, + 0, 1, 0, 1, 0, 1, 0, 1, + 1, 0, 1, 0, 1, 0, 1, 0, + 0, 1, 0, 1, 0, 1, 0, 1, + 1, 0, 1, 0, 1, 0, 1, 0, + 0, 1, 0, 1, 0, 1, 0, 1 + ] + case .dottedGrid: + return [ + 1, 1, 1, 1, 0, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 0, 1, 0, 1, 0, 1, + 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 0, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 1, 1, 0, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, + ] + case .stitch: + return [ + 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, + 1, 0, 1, 1, 1, 1, 1, 1, + 0, 1, 0, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 0, 1, 1, + 1, 1, 1, 1, 0, 1, 0, 1, + ] + case .curvedTile: + return [ + 1, 1, 1, 1, 1, 1, 1, 0, + 1, 1, 1, 1, 1, 1, 0, 0, + 0, 1, 1, 1, 1, 0, 1, 1, + 1, 0, 1, 1, 0, 1, 1, 1, + 1, 1, 0, 0, 1, 1, 1, 1, + 1, 1, 1, 1, 0, 0, 1, 1, + 1, 1, 1, 1, 1, 1, 0, 1, + 1, 1, 1, 1, 1, 1, 1, 0, + ] + case .brick: + return [ + 0, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 1, 1, 0, 1, 1, 1, + 1, 1, 1, 1, 0, 1, 1, 1, + 1, 1, 1, 1, 0, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 1, 1, 1, 1, 1, 1, + ] + case .tile: + return [ + 1, 1, 1, 1, 1, 0, 1, 1, + 1, 1, 1, 1, 0, 1, 1, 1, + 1, 1, 1, 0, 0, 0, 1, 1, + 1, 1, 0, 1, 1, 1, 0, 1, + 0, 0, 1, 1, 1, 1, 1, 0, + 0, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 0, + 1, 1, 1, 1, 1, 1, 0, 1, + ] + case .shadowGrid: + return [ + 1, 1, 1, 1, 0, 1, 0, 0, + 0, 0, 0, 0, 0, 1, 0, 0, + 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 1, 0, 0, + 0, 0, 0, 0, 0, 1, 0, 0, + 1, 1, 1, 1, 0, 1, 0, 0, + 1, 1, 1, 1, 0, 1, 0, 0, + 1, 1, 1, 1, 0, 1, 0, 0, + ] + case .circles: + return [ + 1, 1, 1, 1, 1, 1, 1, 0, + 0, 1, 1, 1, 1, 1, 0, 1, + 1, 0, 1, 1, 1, 0, 1, 1, + 1, 1, 0, 0, 0, 1, 1, 0, + 1, 0, 1, 1, 1, 0, 1, 1, + 0, 1, 1, 1, 1, 1, 0, 1, + 1, 1, 1, 1, 1, 1, 1, 0, + 1, 1, 1, 1, 1, 1, 1, 0, + ] + case .trees: + return [ + 1, 0, 1, 0, 1, 0, 1, 0, + 0, 1, 1, 1, 1, 1, 0, 1, + 1, 1, 1, 1, 1, 1, 1, 0, + 1, 1, 1, 1, 1, 1, 1, 0, + 1, 0, 1, 0, 1, 0, 1, 0, + 1, 1, 0, 1, 0, 1, 1, 1, + 1, 1, 1, 0, 1, 1, 1, 1, + 1, 1, 1, 0, 1, 1, 1, 1, + ] + case .shingles: + return [ + 1, 1, 1, 1, 0, 1, 1, 1, + 1, 1, 1, 1, 0, 1, 1, 1, + 1, 1, 1, 0, 1, 0, 1, 1, + 0, 0, 0, 1, 1, 1, 0, 0, + 0, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 1, 1, 1, 1, 1, 1, + 1, 0, 1, 1, 1, 1, 1, 0, + 1, 1, 0, 0, 0, 0, 0, 1 + ] + case .wicker: + return [ + 0, 0, 0, 0, 0, 1, 1, 1, + 1, 0, 0, 0, 1, 0, 1, 1, + 1, 1, 0, 1, 1, 1, 0, 1, + 1, 0, 1, 1, 1, 0, 0, 0, + 0, 1, 1, 1, 0, 0, 0, 0, + 1, 1, 1, 0, 1, 0, 0, 0, + 1, 1, 0, 1, 1, 1, 0, 1, + 1, 0, 0, 0, 1, 1, 1, 0 + ] + case .rhombus: + return [ + 1, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 1, 1, 1, 1, 1, 1, + 1, 0, 1, 1, 1, 1, 1, 0, + 0, 1, 0, 1, 1, 1, 0, 1, + 1, 0, 1, 0, 1, 0, 1, 0, + 0, 1, 0, 1, 1, 1, 0, 1, + 1, 0, 1, 1, 1, 1, 1, 0, + 0, 1, 1, 1, 1, 1, 1, 1 + ] + case .balls: + return [ + 1, 0, 0, 0, 1, 0, 0, 0, + 0, 1, 1, 1, 0, 1, 1, 0, + 0, 1, 1, 1, 0, 0, 0, 0, + 0, 1, 1, 1, 0, 0, 0, 0, + 1, 0, 0, 0, 1, 0, 0, 0, + 0, 1, 1, 0, 0, 1, 1, 1, + 0, 0, 0, 0, 0, 1, 1, 1, + 0, 0, 0, 0, 0, 1, 1, 1 + ] + } + } +} |