-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHexagon Pendulums.swift
291 lines (242 loc) · 9.71 KB
/
Hexagon Pendulums.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Just copy and paste the code into a blank playground template
// in Swift Playgrounds app on an iPad or a Mac
// !!! Turn off Enable Results in the settings, otherwise you'll get an error !!!
// Created by Roman Gaditskiy: https://GitHub.com/gadirom/Art-in-Swift
import SwiftUI
import PlaygroundSupport
import UIKit
let screenSize = UIScreen.main.bounds
let scaleFactor = CGSize(
width:screenSize.width / 4.5,
height:screenSize.height / 2.8)
let cellWidth : CGFloat = 55
let cellHeight = cellWidth
let angleRadius : CGFloat = 5//rounded corners
let cellSpacing : CGFloat = 5
let cellCount = 97
let rowWidth = 8 //width of the grid
struct Cell {
var angle: Double = 90
var selection: Int = 0
}
let palette: [Color] = [.gray, .blue, .red, .purple, .yellow]
struct Game: View {
@State private var offset = CGSize.zero
@State private var idDragged = -1
@State private var idSelected = -1
@State private var selectedX = -1
@State private var selectedY = -1
@State private var draggedValue = 0
@State private var gestureLocation = CGSize.zero
@State private var currentAmount: CGFloat = 0
@State private var finalAmount: CGFloat = 1
var data: [Int] = Array.init(repeating: 0, count: cellCount)
//the grid
@State private var rows : [[Cell]] = []
@State var scale : CGFloat = 1.0
var body: some View{
ZStack{
//the grid
GridView(rows: $rows)
}.scaledToFill()
.background(Color.white)
.scaleEffect(finalAmount + currentAmount)
.animation(.easeInOut(duration: 1), value:currentAmount)
.gesture(magnificationGesture)
.onAppear(perform: {generateHoney()})
.drawingGroup()
}
//Magnification gesture - for view
var magnificationGesture : some Gesture{
MagnificationGesture()
.onChanged { amount in
let newAmount = (amount - 1) * (currentAmount + finalAmount)
if finalAmount + newAmount >= 1 && finalAmount + newAmount <= 50 {
currentAmount = newAmount
}
}
.onEnded { amount in
finalAmount += currentAmount
currentAmount = 0
}
}
// generating HoneyComb Rows.....
func generateHoney(){
var count = 0
var generated : [Cell] = []
for _ in 0..<cellCount{
let a: Double = 1.5 //Double(generated.count)-(Double(rowWidth)-0.5)/2
generated.append(Cell(angle: generated.count % 2 == 0 ? 20 * a : -20 * a))
// creating rows...
if generated.count == rowWidth - 1 {
if let last = rows.last{
if last.count == rowWidth {
rows.append(generated)
generated.removeAll()
}
}
if rows.isEmpty{
rows.append(generated)
generated.removeAll()
}
}
if generated.count == rowWidth {
rows.append(generated)
generated.removeAll()
}
count += 1
if count == cellCount && !generated.isEmpty{
rows.append(generated)
}
}
}
}
struct GridView: View {
@Binding var rows : [[Cell]]
@State var selection = 0
@State var selectedCount = 0
public init(rows: Binding<[[Cell]]>){
self._rows = rows
}
var body: some View {
VStack(spacing: CGFloat(-cellWidth/4) + cellSpacing){
ForEach(rows.indices,id: \.self){index in
HStack(spacing: cellSpacing){
EnumeratedForEach(rows[index]){idx, cell in
CellObject(idx:idx, idy:index, angle: cell.angle,
selection: cell.selection)
.onTapGesture {
select(index: index, idx: idx, selection: selection, deep: 0)
selection += 1
if selection == palette.count {selection = 0}
}
}
}
}
}
}
func select(index: Int, idx: Int, selection: Int, deep: Int){
if rows[index][idx].selection == selection || deep > rows.count {return}
let deep = deep + 1
withAnimation(){rows[index][idx].selection = selection}
DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + 0.1 ){
if idx > 0 {
select(index: index, idx: idx-1, selection: selection, deep: deep)
}
if index % 2 != 0{
if idx < rowWidth-1{
select(index: index, idx: idx+1, selection: selection, deep: deep)
}
if index > 0 && idx > 0{
select(index: index-1, idx: idx-1, selection: selection, deep: deep)
}
if index < rows.count-1 && idx > 0 {
select(index: index+1, idx: idx-1, selection: selection, deep: deep)
}
if idx < rowWidth - 2{
if index > 0 {
select(index: index-1, idx: idx, selection: selection, deep: deep)
}
if index < rows.count-1 {
select(index: index+1, idx: idx, selection: selection, deep: deep)
}
}
}else{
if idx < rowWidth-2{
select(index: index, idx: idx+1, selection: selection, deep: deep)
}
if index > 1 {
select(index: index-1, idx: idx, selection: selection, deep: deep)
}
if index < rows.count-2 {
select(index: index+1, idx: idx, selection: selection, deep: deep)
}
if index > 0{
select(index: index-1, idx: idx+1, selection: selection, deep: deep)
}
if index < rows.count-1{
select(index: index+1, idx: idx+1, selection: selection, deep: deep)
}
}
}
}
}
struct CellObject: View {
var idx: Int
var idy: Int
var angle: Double
var selection: Int
var body: some View {
ZStack{
Hexagon(angleRadius: angleRadius)
.fill(palette[selection])
.shadow(radius: 3)
.frame(width: cellWidth, height: cellHeight)
Pendulum(height: cellWidth/2, width: cellWidth/6, radius: cellWidth/5, period: 1, angle: Double((selection*selection))*angle)
.frame(width: cellWidth, height: cellHeight)
}
}
}
struct Hexagon: Shape {
let angleRadius : CGFloat
func path(in rect: CGRect) -> Path {
return Path { path in
let pt1 = CGPoint(x: 0, y: rect.height / 4)
let pt2 = CGPoint(x: 0, y: rect.height - rect.height / 4)
let pt3 = CGPoint(x: rect.width / 2, y: rect.height)
let pt4 = CGPoint(x: rect.width, y: rect.height - rect.height / 4)
let pt5 = CGPoint(x: rect.width, y: rect.width / 4)
let pt6 = CGPoint(x: rect.width / 2, y: 0)
path.move(to: pt6)
path.addArc(tangent1End: pt1, tangent2End: pt2, radius: angleRadius)
path.addArc(tangent1End: pt2, tangent2End: pt3, radius: angleRadius)
path.addArc(tangent1End: pt3, tangent2End: pt4, radius: angleRadius)
path.addArc(tangent1End: pt4, tangent2End: pt5, radius: angleRadius)
path.addArc(tangent1End: pt5, tangent2End: pt6, radius: angleRadius)
path.addArc(tangent1End: pt6, tangent2End: pt1, radius: angleRadius)
}
}
init(angleRadius :CGFloat){
self.angleRadius = angleRadius
}
}
// a struct that does the same thing as "ForEach" but gets index along with the item
struct EnumeratedForEach<ItemType, ContentView: View>: View {
let data: [ItemType]
let content: (Int, ItemType) -> ContentView
init(_ data: [ItemType], @ViewBuilder content: @escaping (Int, ItemType) -> ContentView) {
self.data = data
self.content = content
}
var body: some View {
ForEach(Array(self.data.enumerated()), id: \.offset) { idx, item in
self.content(idx, item)
}
}
}
struct Pendulum: View {
let height: CGFloat
let width: CGFloat
let radius: CGFloat
let period: Double
let angle: Double
@State private var a: Double = 0
@State private var side: Bool = false
var body: some View{
ZStack{
ZStack{
Rectangle().frame(width: width, height: height-radius/2)
Circle().frame(width: radius, height: radius).offset(x: 0, y: -height / 2)
Circle().frame(width: radius/3, height: radius/3).offset(x: 0, y: -height / 2)
Rectangle().frame(width: radius, height: radius)
.offset(x: 0, y: height/2)
}.frame(width: width, height: height)
.rotationEffect(Angle(degrees: a), anchor: .init(x: 0.5, y: 0))
.onChange(of: side){ _ in
DispatchQueue.global(qos: .userInteractive).asyncAfter(deadline: .now() + period){ side.toggle()}
withAnimation(.easeInOut(duration: period)){ a = a==angle ? -angle : angle}
}.onAppear(){ a = angle; side.toggle()}
}.shadow(radius: 2)
}
}
PlaygroundPage.current.setLiveView(Game())