-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathFlareSkView.swift
304 lines (245 loc) · 9.71 KB
/
FlareSkView.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
//
// FlareSkView.swift
// FlareSkia
//
// Created by Umberto Sonnino on 4/1/19.
// Copyright © 2019 2Dimensions. All rights reserved.
//
import UIKit
import Foundation
import Skia
@IBDesignable
public class FlareSkView: UIView {
private var _eaglLayer: CAEAGLLayer?
private var _context: EAGLContext?
private var _depthRenderBuffer = GLuint()
private var _colorRenderBuffer = GLuint()
var actor: FlareSkActor!
private var _artboardInstance: FlareSkArtboard?
private var animation: ActorAnimation?
var setupAABB: AABB!
internal var shouldClip = true
internal var _color: UIColor?
private var _skiaCanvas: OpaquePointer!
private var _skiaSurface: OpaquePointer!
private var _skBackgroundPaint: OpaquePointer!
public var artboard: FlareSkArtboard? {
get { return _artboardInstance }
set {
if newValue != _artboardInstance {
_artboardInstance = newValue
_artboardInstance?.advance()
}
}
}
internal var flareViewController: FlareSkViewController? {
var parentResponder: UIResponder? = self
while let pResponder = parentResponder {
parentResponder = pResponder.next
if let fVC = parentResponder as? FlareSkViewController {
return fVC
}
}
return nil
}
internal var color: UIColor? {
get { return _color }
set {
if newValue != _color {
_color = newValue
if let artboard = artboard {
var colorArray: [Float]? = nil
if let color = _color {
colorArray = color.rgb()
}
artboard.overrideColor = colorArray
}
}
}
}
override public class var layerClass: AnyClass {
get {
return CAEAGLLayer.self
}
}
public override init(frame: CGRect) {
super.init(frame: frame)
setupLayer()
}
public required init?(coder aDecoder: NSCoder) {
fatalError("FlareSkView init(coder:) Not Supported!")
}
private func setupLayer() {
guard let glLayer = self.layer as? CAEAGLLayer else {
fatalError("Couldn't get GL layer!")
}
glLayer.isOpaque = false
_eaglLayer = glLayer
setupContext()
}
private func setupContext() {
guard let glContext = EAGLContext(api: .openGLES2) else {
fatalError("Couldn't get GL Context")
}
guard EAGLContext.setCurrent(glContext) else {
fatalError("GL Context could not be set as current!")
}
_context = glContext
glGenRenderbuffers(1, &_colorRenderBuffer)
glBindRenderbuffer(GLenum(GL_RENDERBUFFER), _colorRenderBuffer)
_context!.renderbufferStorage(Int(GL_RENDERBUFFER), from: _eaglLayer)
var framebuffer: GLuint = 0
glGenFramebuffers(1, &framebuffer)
glBindFramebuffer(GLenum(GL_FRAMEBUFFER), framebuffer)
glFramebufferRenderbuffer(GLenum(GL_FRAMEBUFFER), GLenum(GL_COLOR_ATTACHMENT0), GLenum(GL_RENDERBUFFER), _colorRenderBuffer)
let width = Int32(frame.size.width)
let height = Int32(frame.size.height)
glViewport(0, 0, GLsizei(width), GLsizei(height))
let info = sk_imageinfo_new(width, height, RGBA_8888_SK_COLORTYPE, OPAQUE_SK_ALPHATYPE, nil)
_skiaSurface = sk_surface_new_gl(info)
if _skiaSurface != nil {
_skiaCanvas = sk_surface_get_canvas(_skiaSurface)
}
_skBackgroundPaint = sk_paint_new()
sk_paint_set_color(_skBackgroundPaint, sk_color_set_argb(255, 93, 93, 93))
}
func updateBounds(with nodeName: String? = nil) {
guard let artboard = artboard else { return }
if let boundsNodeName = nodeName,
let node = artboard.getNode(name: boundsNodeName) as? ActorDrawable {
setupAABB = node.computeAABB()
} else {
setupAABB = artboard.artboardAABB()
}
}
/// Perform any pre-painting operation, if needed.
/// Override to perform custom operations.
func prePaint() {
if shouldClip {
var clipRect = sk_rect_t()
clipRect.left = 0
clipRect.top = 0
clipRect.right = Float(frame.width)
clipRect.bottom = Float(frame.height)
sk_canvas_clip_rect(_skiaCanvas, &clipRect)
}
}
func paintFlare(_ viewTransform: Mat2D) {
if let artboard = self.artboard {
if let controller = self.flareViewController {
controller.setViewTransform(viewTransform: viewTransform)
}
artboard.draw(skCanvas: _skiaCanvas)
}
}
func postPaint() {}
func paint() {
if let artboard = self.artboard {
glClear(GLbitfield(GL_COLOR_BUFFER_BIT))
if let bounds = setupAABB {
#warning("TODO: Check for alignment")
// self.alignmentRectInsets
let contentWidth = bounds.width
let contentHeight = bounds.height
// let tx = contentWidth * artboard.origin.x
// let ty = contentHeight * artboard.origin.y
let size = frame.size
// Contain the Artboard
let scaleX = Float(size.width) / contentWidth
let scaleY = Float(size.height) / contentHeight
let scale = min(scaleX, scaleY)
sk_canvas_save(_skiaCanvas)
prePaint()
let x = -1 * bounds[0] - contentWidth / 2.0
let y = -1 * bounds[1] - contentHeight / 2.0
let transform = Mat2D()
transform[4] = Float(size.width / 2.0)
transform[5] = Float(size.height / 2.0)
Mat2D.scale(transform, transform, Vec2D.init(fromValues: scaleX, scaleY))
let center = Mat2D()
center[4] = x
center[5] = y
Mat2D.multiply(transform, transform, center)
sk_canvas_translate(_skiaCanvas, Float(size.width/2.0), Float(size.height/2.0))
sk_canvas_scale(_skiaCanvas, scale, scale)
sk_canvas_translate(_skiaCanvas, x, y)
sk_canvas_draw_paint(_skiaCanvas, _skBackgroundPaint) // Clear the background.
paintFlare(transform)
artboard.draw(skCanvas: _skiaCanvas)
sk_canvas_restore(_skiaCanvas)
postPaint()
sk_canvas_flush(_skiaCanvas)
glBindRenderbuffer(GLenum(GL_RENDERBUFFER), _colorRenderBuffer)
_context!.presentRenderbuffer(Int(GL_RENDERBUFFER))
}
}
}
/* Debugging functions. */
func drawQuad(_ canvas: OpaquePointer) {
sk_canvas_save(canvas)
let path = sk_path_new()
//
let subpath = sk_path_new()
sk_path_move_to(subpath, -236.0, -236.0)
sk_path_line_to(subpath, 236, -236)
sk_path_line_to(subpath, 236, 236)
sk_path_line_to(subpath, -236, 236)
sk_path_line_to(subpath, -236, -236)
sk_path_close(subpath)
var skMat = sk_matrix_t(
mat: (
0.5, 0.0, 236/2,
0.0, 0.5, 372/2,
0, 0, 1
)
)
let matPointer = withUnsafeMutablePointer(to: &skMat){
UnsafeMutablePointer($0)
}
sk_path_add_path_with_matrix(path, subpath, 0, 0, matPointer)
let paint = sk_paint_new()
sk_paint_set_antialias(paint, true)
sk_paint_set_color(paint, sk_color_set_argb(0xFF, 0x00, 0xFF, 0xFF))
sk_path_set_evenodd(path, false)
sk_canvas_draw_path(canvas, path, paint)
sk_path_delete(subpath)
sk_path_delete(path)
sk_paint_delete(paint)
sk_canvas_restore(canvas)
sk_canvas_flush(canvas)
}
func skiaDraw(_ canvas: OpaquePointer) {
let fill = sk_paint_new()
sk_paint_set_color(fill, sk_color_set_argb(0xFF, 0x00, 0x00, 0xFF))
sk_canvas_draw_paint(canvas, fill)
sk_paint_set_color(fill, sk_color_set_argb(0xFF, 0x00, 0xFF, 0xFF))
var rect = sk_rect_t()
rect.left = 100.0
rect.top = 100.0
rect.right = 724.0
rect.bottom = 468
sk_canvas_draw_rect(canvas, &rect, fill)
let stroke = sk_paint_new()
sk_paint_set_color(stroke, sk_color_set_argb(0xFF, 0xFF, 0x00, 0x00))
sk_paint_set_antialias(stroke, true)
sk_paint_set_stroke(stroke, true)
sk_paint_set_stroke_width(stroke, 5.0)
let path = sk_path_new()
sk_path_move_to(path, 50, 50)
sk_path_line_to(path, 774, 50)
sk_path_cubic_to(path, -518, 50, 1470, 518, 50, 518)
sk_path_line_to(path, 774, 518)
sk_canvas_draw_path(canvas, path, stroke)
sk_paint_set_color(fill, sk_color_set_argb(0x80, 0x00, 0xFF, 0x00))
rect = sk_rect_t()
rect.left = 120
rect.top = 120
rect.right = 704
rect.bottom = 448
sk_canvas_draw_oval(canvas, &rect, fill)
sk_path_delete(path)
sk_paint_delete(stroke)
sk_paint_delete(fill)
sk_canvas_flush(canvas)
}
}