-
Notifications
You must be signed in to change notification settings - Fork 202
/
Copy pathruntime_nested_artboard.dart
177 lines (150 loc) · 5.34 KB
/
runtime_nested_artboard.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
import 'package:flutter/rendering.dart';
import 'package:rive/src/controllers/state_machine_controller.dart';
import 'package:rive/src/core/core.dart';
import 'package:rive/src/rive_core/animation/linear_animation_instance.dart';
import 'package:rive/src/rive_core/animation/nested_linear_animation.dart';
import 'package:rive/src/rive_core/animation/nested_state_machine.dart';
import 'package:rive/src/rive_core/artboard.dart';
import 'package:rive/src/rive_core/math/aabb.dart';
import 'package:rive/src/rive_core/math/mat2d.dart';
import 'package:rive/src/rive_core/math/vec2d.dart';
import 'package:rive/src/rive_core/nested_artboard.dart';
class RuntimeNestedArtboard extends NestedArtboard {
Artboard? sourceArtboard;
@override
K? clone<K extends Core<CoreContext>>() {
var object = RuntimeNestedArtboard();
object.copy(this);
if (sourceArtboard != null) {
object.sourceArtboard = sourceArtboard;
var runtimeArtboardInstance =
sourceArtboard!.instance() as RuntimeArtboard;
object.mountedArtboard = RuntimeMountedArtboard(runtimeArtboardInstance);
}
return object as K;
}
@override
void onAdded() {
super.onAdded();
if (mountedArtboard is! RuntimeMountedArtboard ||
sourceArtboard == null ||
animations.isEmpty) {
return;
}
var runtimeLinearAnimations =
sourceArtboard!.linearAnimations.toList(growable: false);
var runtimeStateMachines =
sourceArtboard!.stateMachines.toList(growable: false);
for (final animation in animations) {
if (animation is NestedLinearAnimation) {
var animationId = animation.animationId;
if (animationId >= 0 && animationId < runtimeLinearAnimations.length) {
animation.linearAnimationInstance =
RuntimeNestedLinearAnimationInstance(LinearAnimationInstance(
runtimeLinearAnimations[animationId]));
}
} else if (animation is NestedStateMachine) {
var animationId = animation.animationId;
if (animationId >= 0 && animationId < runtimeStateMachines.length) {
animation.stateMachineInstance = RuntimeNestedStateMachineInstance(
(mountedArtboard as RuntimeMountedArtboard).artboardInstance,
StateMachineController(runtimeStateMachines[animationId]),
);
}
}
}
}
}
class RuntimeNestedLinearAnimationInstance
extends NestedLinearAnimationInstance {
final LinearAnimationInstance linearAnimation;
RuntimeNestedLinearAnimationInstance(this.linearAnimation);
@override
void apply(RuntimeMountedArtboard artboard, double mix) {
linearAnimation.animation.apply(linearAnimation.time,
coreContext: artboard.artboardInstance, mix: mix);
}
@override
bool advance(double elapsedSeconds) {
linearAnimation.advance(elapsedSeconds * speed);
return linearAnimation.keepGoing;
}
@override
double speed = 1;
@override
void goto(double value) {
var localTime = linearAnimation.animation.globalToLocalTime(value);
if (localTime == linearAnimation.time) {
return;
}
linearAnimation.time = localTime;
}
@override
double get durationSeconds => linearAnimation.animation.durationSeconds;
}
class RuntimeNestedStateMachineInstance extends NestedStateMachineInstance {
final StateMachineController stateMachineController;
RuntimeNestedStateMachineInstance(
RuntimeArtboard artboard, this.stateMachineController) {
stateMachineController.init(artboard);
}
@override
void apply(RuntimeMountedArtboard artboard, double elapsedSeconds) {
stateMachineController.apply(artboard.artboardInstance, elapsedSeconds);
}
@override
bool get isActive => stateMachineController.isActive;
@override
ValueListenable<bool> get isActiveChanged =>
stateMachineController.isActiveChanged;
@override
void pointerDown(Vec2D position, PointerDownEvent event) =>
stateMachineController.pointerDown(position, event);
@override
void pointerMove(Vec2D position) =>
stateMachineController.pointerMove(position);
@override
void pointerUp(Vec2D position) => stateMachineController.pointerUp(position);
@override
void setInputValue(int id, dynamic value) {
var inputs = stateMachineController.stateMachine.inputs;
if (id < inputs.length && id >= 0) {
stateMachineController.setInputValue(inputs[id].id, value);
}
}
}
class RuntimeMountedArtboard extends MountedArtboard {
final RuntimeArtboard artboardInstance;
RuntimeMountedArtboard(this.artboardInstance) {
artboardInstance.frameOrigin = false;
artboardInstance.advance(0, nested: true);
}
@override
void dispose() {}
@override
Mat2D worldTransform = Mat2D();
@override
void draw(Canvas canvas) {
canvas.save();
canvas.transform(worldTransform.mat4);
artboardInstance.draw(canvas);
canvas.restore();
}
@override
AABB get bounds {
var width = artboardInstance.width;
var height = artboardInstance.height;
var x = -artboardInstance.originX * width;
var y = -artboardInstance.originY * height;
return AABB.fromValues(x, y, x + width, y + height);
}
@override
double get renderOpacity => artboardInstance.opacity;
@override
set renderOpacity(double value) {
artboardInstance.opacity = value;
}
@override
bool advance(double seconds) =>
artboardInstance.advance(seconds, nested: true);
}