-
Notifications
You must be signed in to change notification settings - Fork 517
Expand file tree
/
Copy pathflare_render_box.dart
More file actions
312 lines (265 loc) · 7.9 KB
/
Copy pathflare_render_box.dart
File metadata and controls
312 lines (265 loc) · 7.9 KB
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
import 'dart:math';
import 'package:flare_flutter/asset_provider.dart';
import 'package:flare_flutter/base/math/aabb.dart';
import 'package:flare_flutter/base/math/mat2d.dart';
import 'package:flare_flutter/base/math/vec2d.dart';
import 'package:flare_flutter/flare.dart';
import 'package:flare_flutter/flare_cache.dart';
import 'package:flare_flutter/flare_cache_asset.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/scheduler.dart';
/// A render box for Flare content.
abstract class FlareRenderBox extends RenderBox {
static const double _notPlayingFlag = -1;
BoxFit _fit = BoxFit.contain;
Alignment _alignment = Alignment.center;
int _frameCallbackID = -1;
double _lastFrameTime = _notPlayingFlag;
final Set<FlareCacheAsset> _assets = {};
bool _useIntrinsicSize = false;
Size _intrinsicSize = Size.zero;
bool _isLoading = false;
bool _reloadQueued = false;
/// Get the Axis Aligned Bounding Box that encompasses the world space scene
AABB get aabb;
Alignment get alignment => _alignment;
set alignment(Alignment value) {
if (value != _alignment) {
_alignment = value;
markNeedsPaint();
}
}
/// Prevent loading when the renderbox isn't attached. This prevents
/// unneccesarily hitting an async path during load. A warmLoad would fail
/// which then falls back to a coldLoad. Due to the async nature, any further
/// sync calls would be blocked as we gate load with _isLoading.
bool get canLoad => attached;
BoxFit get fit => _fit;
set fit(BoxFit value) {
if (value != _fit) {
_fit = value;
markNeedsPaint();
}
}
Size get intrinsicSize => _intrinsicSize;
set intrinsicSize(Size value) {
if (_intrinsicSize == value) {
return;
}
_intrinsicSize = value;
if (parent != null) {
markNeedsLayoutForSizedByParentChange();
}
}
bool get isLoading => _isLoading;
bool get isPlaying;
@override
bool get sizedByParent => !_useIntrinsicSize || _intrinsicSize == Size.zero;
bool get useIntrinsicSize => _useIntrinsicSize;
set useIntrinsicSize(bool value) {
if (_useIntrinsicSize == value) {
return;
}
_useIntrinsicSize = value;
if (parent != null) {
markNeedsLayoutForSizedByParentChange();
}
}
/// Advance animations, physics, etc by elapsedSeconds.
void advance(double elapsedSeconds);
@override
void attach(PipelineOwner owner) {
super.attach(owner);
updatePlayState();
if (_assets.isEmpty) {
load();
}
}
Future<void> coldLoad() async {}
@override
void detach() {
super.detach();
dispose();
}
void dispose() {
updatePlayState();
_unload();
}
/// Load a flare file from cache
FlutterActor? getWarmFlare(AssetProvider assetProvider) {
var asset = getWarmActor(assetProvider);
if (!attached || asset == null) {
return null;
}
_assets.add(asset);
asset.ref();
return asset.actor;
}
@override
bool hitTestSelf(Offset screenOffset) => true;
/// Trigger the loading process. This will attempt a sync warm load,
/// optimizing for the case where the assets we need are already available.
/// This allows widgets using this render object to draw immediately and not
/// draw any empty frames.
///
void load() {
if (!canLoad) {
return;
}
if (_isLoading) {
_reloadQueued = true;
return;
}
_isLoading = true;
_unload();
// Try a sync warm load in case we already have what we need.
if (!warmLoad()) {
coldLoad().then((_) {
_completeLoad();
});
} else {
_completeLoad();
}
}
/// Load a flare file from cache
Future<FlutterActor?> loadFlare(AssetProvider assetProvider) async {
FlareCacheAsset asset = await cachedActor(assetProvider);
if (!attached) {
return null;
}
_assets.add(asset);
asset.ref();
return asset.actor;
}
void onUnload() {}
@override
void paint(PaintingContext context, Offset offset) {
if (isPlaying) {
// Paint again
if (_frameCallbackID != -1) {
SchedulerBinding.instance.cancelFrameCallbackWithId(_frameCallbackID);
}
_frameCallbackID =
SchedulerBinding.instance.scheduleFrameCallback(_beginFrame);
}
final Canvas canvas = context.canvas;
AABB bounds = aabb;
double contentWidth = bounds[2] - bounds[0];
double contentHeight = bounds[3] - bounds[1];
double x = -1 * bounds[0] -
contentWidth / 2.0 -
(_alignment.x * contentWidth / 2.0);
double y = -1 * bounds[1] -
contentHeight / 2.0 -
(_alignment.y * contentHeight / 2.0);
double scaleX = 1.0, scaleY = 1.0;
canvas.save();
prePaint(canvas, offset);
switch (_fit) {
case BoxFit.fill:
scaleX = size.width / contentWidth;
scaleY = size.height / contentHeight;
break;
case BoxFit.contain:
double minScale =
min(size.width / contentWidth, size.height / contentHeight);
scaleX = scaleY = minScale;
break;
case BoxFit.cover:
double maxScale =
max(size.width / contentWidth, size.height / contentHeight);
scaleX = scaleY = maxScale;
break;
case BoxFit.fitHeight:
double minScale = size.height / contentHeight;
scaleX = scaleY = minScale;
break;
case BoxFit.fitWidth:
double minScale = size.width / contentWidth;
scaleX = scaleY = minScale;
break;
case BoxFit.none:
scaleX = scaleY = 1.0;
break;
case BoxFit.scaleDown:
double minScale =
min(size.width / contentWidth, size.height / contentHeight);
scaleX = scaleY = minScale < 1.0 ? minScale : 1.0;
break;
}
Mat2D transform = Mat2D();
transform[4] =
offset.dx + size.width / 2.0 + (_alignment.x * size.width / 2.0);
transform[5] =
offset.dy + size.height / 2.0 + (_alignment.y * size.height / 2.0);
Mat2D.scale(transform, transform, Vec2D.fromValues(scaleX, scaleY));
Mat2D center = Mat2D();
center[4] = x;
center[5] = y;
Mat2D.multiply(transform, transform, center);
canvas.translate(
offset.dx + size.width / 2.0 + (_alignment.x * size.width / 2.0),
offset.dy + size.height / 2.0 + (_alignment.y * size.height / 2.0),
);
canvas.scale(scaleX, scaleY);
canvas.translate(x, y);
paintFlare(canvas, transform);
canvas.restore();
postPaint(canvas, offset);
}
void paintFlare(Canvas canvas, Mat2D viewTransform);
@override
void performLayout() {
if (!sizedByParent) {
size = constraints.constrain(_intrinsicSize);
}
}
@override
void performResize() {
size = _useIntrinsicSize ? constraints.smallest : constraints.biggest;
}
void postPaint(Canvas canvas, Offset offset) {}
void prePaint(Canvas canvas, Offset offset) {}
void updatePlayState() {
if (isPlaying && attached) {
markNeedsPaint();
} else {
_lastFrameTime = _notPlayingFlag;
if (_frameCallbackID != -1) {
SchedulerBinding.instance.cancelFrameCallbackWithId(_frameCallbackID);
}
}
}
bool warmLoad() {
return false;
}
void _beginFrame(Duration timestamp) {
_frameCallbackID = -1;
final double t = timestamp.inMicroseconds / Duration.microsecondsPerSecond;
double elapsedSeconds =
_lastFrameTime == _notPlayingFlag ? 0.0 : t - _lastFrameTime;
_lastFrameTime = t;
advance(elapsedSeconds);
if (!isPlaying) {
_lastFrameTime = _notPlayingFlag;
}
markNeedsPaint();
}
void _completeLoad() {
// Load is complete, check if a reload was requested
// during our load, and start it up again
_isLoading = false;
if (_reloadQueued) {
_reloadQueued = false;
load();
}
}
void _unload() {
for (final FlareCacheAsset asset in _assets) {
asset.deref();
}
_assets.clear();
onUnload();
}
}