-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathFanMenu.swift
396 lines (330 loc) · 11.3 KB
/
FanMenu.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import UIKit
import Macaw
public enum FanMenuButtonTitlePosition {
case left
case right
case top
case bottom
}
public struct FanMenuButton {
public let id: String
public let image: UIImage?
public let color: Color
public let title: String
public let titleFont: UIFont
public let titleWeight: UIFont.Weight
public let titleColor: Color?
public let titlePosition: FanMenuButtonTitlePosition
public init(id: String,
image: UIImage?,
color: Color,
title: String = "",
titleFont: UIFont = .preferredFont(forTextStyle: .title1),
titleWeight: UIFont.Weight = .regular,
titleColor: Color? = .none,
titlePosition: FanMenuButtonTitlePosition = .bottom) {
self.id = id
self.image = image
self.color = color
self.title = title
self.titleFont = titleFont
self.titleWeight = titleWeight
self.titleColor = titleColor
self.titlePosition = titlePosition
}
public init(id: String,
image: String,
color: Color,
title: String = "",
titleFont: UIFont = .preferredFont(forTextStyle: .title1),
titleWeight: UIFont.Weight = .regular,
titleColor: Color? = .none,
titlePosition: FanMenuButtonTitlePosition = .bottom) {
self.init(id: id,
image: UIImage(named: image),
color: color,
title: title,
titleFont: titleFont,
titleWeight: titleWeight,
titleColor: titleColor,
titlePosition: titlePosition)
}
}
public class FanMenu: MacawView {
public var duration = 0.20 {
didSet {
updateNode()
}
}
public var delay = 0.05 {
didSet {
updateNode()
}
}
public var menuRadius = 95.0 {
didSet {
updateNode()
}
}
public var radius = 30.0 {
didSet {
updateNode()
}
}
public var button: FanMenuButton? {
didSet {
updateNode()
}
}
public var items: [FanMenuButton] = [] {
didSet {
updateNode()
}
}
public var interval: (Double, Double) = (0, 2.0 * Double.pi) {
didSet {
updateNode()
}
}
public var menuBackground: Color? {
didSet {
updateNode()
}
}
public var buttonsTitleIndent: Double = 8.0 {
didSet {
updateNode()
}
}
public var onItemWillClick: ((_ button: FanMenuButton) -> ())?
public var onItemDidClick: ((_ button: FanMenuButton) -> ())?
var scene: FanMenuScene?
public var isOpen: Bool {
get {
if let sceneValue = scene {
return sceneValue.isOpen
}
return false
}
}
public func open() {
scene?.updateMenu(open: true)
}
public func close() {
scene?.updateMenu(open: false)
}
public func updateNode() {
guard button != nil else {
self.node = Group()
self.scene = .none
return
}
let scene = FanMenuScene(fanMenu: self)
let node = scene.node
node.place = Transform.move(
dx: Double(self.frame.width) / 2,
dy: Double(self.frame.height) / 2
)
self.node = node
self.scene = scene
}
open override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
return findNodeAt(location: point) != nil
}
fileprivate var sizeIsTooSmall: Bool {
let minSize = CGFloat((menuRadius + radius) * 2.0)
return bounds.size.width < minSize || bounds.size.height < minSize
}
}
class FanMenuScene {
let fanMenu: FanMenu
let buttonNode: Group
let buttonsNode: Group
let backgroundCircle: Shape
let menuCircle: Shape
let menuIcon: Image?
let node: Group
init(fanMenu: FanMenu) {
self.fanMenu = fanMenu
let button = fanMenu.button!
menuCircle = Shape(
form: Circle(r: fanMenu.radius),
fill: button.color
)
buttonNode = [menuCircle].group()
if let uiImage = button.image {
menuIcon = Image(
image: uiImage,
place: Transform.move(
dx: -Double(uiImage.size.width) / 2,
dy: -Double(uiImage.size.height) / 2
)
)
buttonNode.contents.append(menuIcon!)
} else {
menuIcon = .none
}
buttonsNode = fanMenu.items.map {
return FanMenuScene.createFanButtonNode(button: $0, fanMenu: fanMenu)
}.group()
backgroundCircle = Shape(
form: Circle(r: fanMenu.radius - 1)
)
if let color = fanMenu.menuBackground {
backgroundCircle.fill = color
} else {
backgroundCircle.fill = button.color.with(a: 0.2)
}
node = [backgroundCircle, buttonsNode, buttonNode].group()
buttonNode.onTouchPressed { [unowned self] _ in
if let animationValue = self.animation {
if animationValue.state() != .paused {
return
}
}
self.updateMenu(open: !self.isOpen)
}
}
var animation: Animation?
var isOpen: Bool = false
func updateMenu(open: Bool) {
if let button = fanMenu.button {
self.fanMenu.onItemWillClick?(button)
self.updateState(open: open) { [weak self] in
self?.fanMenu.onItemDidClick?(button)
}
}
}
func updateState(open: Bool, callback: @escaping () -> Void) {
if open == isOpen {
return
}
isOpen = open
if isOpen && fanMenu.sizeIsTooSmall {
print("WARNING: FanMenu doesn't fit into view bounds. It should have both width and height set to at least \((fanMenu.menuRadius + fanMenu.radius) * 2.0)")
}
let scale = isOpen ? fanMenu.menuRadius / fanMenu.radius : fanMenu.radius / fanMenu.menuRadius
let backgroundAnimation = self.backgroundCircle.placeVar.animation(
to: Transform.scale(sx: scale, sy: scale),
during: fanMenu.duration
)
let nodes = self.buttonsNode.contents.enumerated()
let expandAnimation = nodes.map { (index, node) in
let transform = isOpen ? self.expandPlace(index: index) : Transform.identity
let mainAnimation = [
node.opacityVar.animation(to: isOpen ? 1.0 : 0.0, during: fanMenu.duration),
node.placeVar.animation(
to: transform,
during: fanMenu.duration
).easing(Easing.easeOut)
].combine()
let delay = fanMenu.delay * Double(index)
if delay == 0.0 {
return mainAnimation
}
let filterOpacity = isOpen ? 0.0 : 1.0
let fillerAnimation = node.opacityVar.animation(from: filterOpacity, to: filterOpacity, during: delay)
return [fillerAnimation, mainAnimation].sequence()
}.combine()
// stub
let buttonAnimation = self.buttonNode.opacityVar.animation(
to: 1.0,
during: fanMenu.duration + fanMenu.delay * Double(buttonsNode.contents.count - 1)
)
animation = [backgroundAnimation, expandAnimation, buttonAnimation].combine()
animation?.onComplete {
callback()
}
animation?.play()
}
class func createFanButtonNode(button: FanMenuButton, fanMenu: FanMenu) -> Group {
var contents: [Node] = [
Shape(
form: Circle(r: fanMenu.radius),
fill: button.color
)
]
if let uiImage = button.image {
let image = Image(
image: uiImage,
place: Transform.move(
dx: -Double(uiImage.size.width) / 2,
dy: -Double(uiImage.size.height) / 2
)
)
if !button.title.isEmpty {
let place: Transform
let text = Text(text: button.title, font: Font(name: button.titleFont.fontName, size: Int(button.titleFont.pointSize), weight: getWeight(button.titleWeight)))
switch button.titlePosition {
case .right:
place = Transform.move(
dx: Double(uiImage.size.width) + fanMenu.buttonsTitleIndent,
dy: -Double(uiImage.size.height) / 2
)
case .left:
place = Transform.move(
dx: -Double(uiImage.size.width) - fanMenu.buttonsTitleIndent - text.bounds.w,
dy: -Double(uiImage.size.height) / 2
)
case .bottom:
place = Transform.move(
dx: -Double(uiImage.size.width) / 2,
dy: Double(uiImage.size.height) + fanMenu.buttonsTitleIndent
)
case .top:
place = Transform.move(
dx: -Double(uiImage.size.width) / 2,
dy: -Double(uiImage.size.height) - fanMenu.buttonsTitleIndent - text.bounds.h
)
}
if let textColor = button.titleColor {
text.fill = textColor
}
text.place = place
contents.append(text)
}
contents.append(image)
}
let node = Group(contents: contents)
node.opacity = 0.0
node.onTouchPressed { [unowned fanMenu] _ in
fanMenu.onItemWillClick?(button)
fanMenu.scene?.updateState(open: false) {
fanMenu.onItemDidClick?(button)
}
}
return node
}
class func getWeight(_ weight: UIFont.Weight) -> String {
switch weight {
case .regular:
return "normal"
case .bold:
return "bold"
case .semibold:
return "bolder"
case .light:
return "lighter"
default:
return "normal"
}
}
func expandPlace(index: Int) -> Transform {
let size = Double(buttonsNode.contents.count)
let endValue = fanMenu.interval.1
let startValue = fanMenu.interval.0
let interval = endValue - startValue
var step: Double = 0.0
if interval.truncatingRemainder(dividingBy: 2 * Double.pi) < 0.00001 {
// full circle
step = interval / size
} else {
step = interval / max(size - 1, 1)
}
let alpha = startValue + step * Double(index)
return Transform.move(
dx: cos(alpha) * fanMenu.menuRadius,
dy: sin(alpha) * fanMenu.menuRadius
)
}
}