-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathflare_sk_actor.swift
160 lines (129 loc) · 4.27 KB
/
flare_sk_actor.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
//
// flare_sk_actor.swift
// FlareSkia
//
// Created by Umberto Sonnino on 2/22/19.
// Copyright © 2019 2Dimensions. All rights reserved.
//
import Foundation
import Skia
public class FlareSkActor: Actor {
public var maxTextureIndex: Int = 0
public var _version: Int = -1
public var _artboardCount: Int = 0
public var images: [OpaquePointer]?
public var _artboards: [ActorArtboard?] = []
public var artboard: FlareSkArtboard? {
return _artboards.count > 0 ? (_artboards.first as! FlareSkArtboard) : nil
}
required public init() {}
public func makeArtboard() -> ActorArtboard {
return FlareSkArtboard(actor: self)
}
public func makeShapeNode(_ source: ActorShape?) -> ActorShape {
return FlareSkShape()
}
public func makePathNode() -> ActorPath {
return FlareSkActorPath()
}
public func makeImageNode() -> ActorImage {
return FlareSkImage()
}
public func makeRectangle() -> ActorRectangle {
return FlareSkRectangle()
}
public func makeTriangle() -> ActorTriangle {
return FlareSkTriangle()
}
public func makeStar() -> ActorStar {
return FlareSkStar()
}
public func makePolygon() -> ActorPolygon {
return FlareSkPolygon()
}
public func makeEllipse() -> ActorEllipse {
return FlareSkEllipse()
}
public func makeColorFill() -> ColorFill {
return FlareSkColorFill()
}
public func makeColorStroke() -> ColorStroke {
return FlareSkColorStroke()
}
public func makeGradientFill() -> GradientFill {
return FlareSkGradientFill()
}
public func makeGradientStroke() -> GradientStroke {
return FlareSkGradientStroke()
}
public func makeRadialFill() -> RadialGradientFill {
return FlareSkRadialFill()
}
public func makeRadialStroke() -> RadialGradientStroke {
return FlareSkRadialStroke()
}
func loadData(_ data: Data) {
self.load(data: data)
}
public func onImageData(_ rawData: [Data]) {
images = []
for imageData in rawData {
imageData.withUnsafeBytes { (buffer: UnsafeRawBufferPointer) in
let skData = sk_data_new_with_copy(buffer.baseAddress, imageData.count)
images!.append(sk_image_new_from_encoded(skData, nil))
sk_data_unref(skData)
}
}
}
public func dispose(){
guard let images = images else {
return
}
for image in images {
sk_image_unref(image)
}
}
public func loadFromFile(filename: String) -> Bool {
let endIndex = filename.index(filename.endIndex, offsetBy: -4)
let fname = String(filename.prefix(upTo: endIndex))
if let path = Bundle.main.path(forResource: fname, ofType: "flr") {
if let data = FileManager.default.contents(atPath: path) {
self.load(data: data)
return true
}
}
return false
}
// By using a semaphore, this function won't return until
// that semaphore has been signaled.
public static func loadFromFileAwait(filename: String) -> FlareSkActor? {
guard !filename.isEmpty else {
return nil
}
var flareActor: FlareSkActor?
// Create semaphore with a single token.
let semaphore = DispatchSemaphore(value: 0)
let queue = DispatchQueue.global()
queue.async {
// Loading operations
let endIndex = filename.index(filename.endIndex, offsetBy: -4)
let fname = String(filename.prefix(upTo: endIndex))
if let path = Bundle.main.path(forResource: fname, ofType: "flr") {
if let data = FileManager.default.contents(atPath: path) {
flareActor = FlareSkActor()
flareActor!.load(data: data)
}
}
// Loaded or not, unblock.
semaphore.signal()
}
// Wait for the signal.
_ = semaphore.wait(timeout: .distantFuture)
return flareActor
}
/**
TODO:
- loadImages
- readOutOfBandAsset
*/
}