-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy pathflare_render_box.dart
246 lines (210 loc) · 6.12 KB
/
flare_render_box.dart
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
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:flare_dart/math/aabb.dart';
import 'package:flare_dart/math/mat2d.dart';
import 'package:flare_dart/math/vec2d.dart';
import 'flare.dart';
import 'flare_cache.dart';
import 'flare_cache_asset.dart';
/// A render box for Flare content.
abstract class FlareRenderBox extends RenderBox {
AssetBundle _assetBundle;
BoxFit _fit;
Alignment _alignment;
int _frameCallbackID;
double _lastFrameTime = 0.0;
final List<FlareCacheAsset> _assets = [];
AssetBundle get assetBundle => _assetBundle;
set assetBundle(AssetBundle value) {
if (_assetBundle == value) {
return;
}
_assetBundle = value;
if (_assetBundle != null) {
_load();
}
}
bool get isPlaying;
BoxFit get fit => _fit;
set fit(BoxFit value) {
if (value != _fit) {
_fit = value;
markNeedsPaint();
}
}
void updatePlayState() {
if (isPlaying && attached) {
markNeedsPaint();
} else {
_lastFrameTime = 0;
if (_frameCallbackID != null) {
SchedulerBinding.instance.cancelFrameCallbackWithId(_frameCallbackID);
}
}
}
Alignment get alignment => _alignment;
set alignment(Alignment value) {
if (value != _alignment) {
_alignment = value;
markNeedsPaint();
}
}
@override
bool get sizedByParent => true;
@override
bool hitTestSelf(Offset screenOffset) => true;
@override
void performResize() {
size = constraints.biggest;
}
@override
void detach() {
super.detach();
dispose();
}
@override
void attach(PipelineOwner owner) {
super.attach(owner);
updatePlayState();
if (_assets.isEmpty && assetBundle != null) {
_load();
}
}
void dispose() {
updatePlayState();
_unload();
}
void _beginFrame(Duration timestamp) {
_frameCallbackID = null;
final double t =
timestamp.inMicroseconds / Duration.microsecondsPerMillisecond / 1000.0;
double elapsedSeconds = _lastFrameTime == 0.0 ? 0.0 : t - _lastFrameTime;
_lastFrameTime = t;
advance(elapsedSeconds);
if (!isPlaying) {
_lastFrameTime = 0.0;
}
markNeedsPaint();
}
/// Get the Axis Aligned Bounding Box that encompasses the world space scene
AABB get aabb;
void paintFlare(Canvas canvas, Mat2D viewTransform);
void prePaint(Canvas canvas, Offset offset) {}
void postPaint(Canvas canvas, Offset offset) {}
@override
void paint(PaintingContext context, Offset offset) {
if (isPlaying) {
// Paint again
if (_frameCallbackID != null) {
SchedulerBinding.instance.cancelFrameCallbackWithId(_frameCallbackID);
}
_frameCallbackID =
SchedulerBinding.instance.scheduleFrameCallback(_beginFrame);
}
final Canvas canvas = context.canvas;
AABB bounds = aabb;
if (bounds != null) {
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);
}
}
/// Advance animations, physics, etc by elapsedSeconds.
void advance(double elapsedSeconds);
bool _isLoading = false;
bool get isLoading => _isLoading;
void _load() async {
if (_isLoading) {
return;
}
_isLoading = true;
_unload();
await load();
_isLoading = false;
}
/// Perform any loading logic necessary for this scene.
void load() async {}
void _unload() {
for (final FlareCacheAsset asset in _assets) {
asset.deref();
}
_assets.clear();
onUnload();
}
void onUnload() {}
/// Load a flare file from cache
Future<FlutterActor> loadFlare(String filename) async {
if (assetBundle == null || filename == null) {
return null;
}
FlareCacheAsset asset = await cachedActor(assetBundle, filename);
if (!attached || asset == null) {
return null;
}
_assets.add(asset);
asset.ref();
return asset.actor;
}
}