/
tween_property.dart
47 lines (36 loc) · 1.37 KB
/
tween_property.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
part of '../animation.dart';
/// The [TweenProperty] class controls the value of a property while
/// the tween is running. This class is returned by the getters of
/// the [TweenPropertyAccessor] implementations.
class TweenProperty {
final TweenPropertyAccessor _tweenPropertyAccessor;
final int _propertyID;
num _startValue = double.nan;
num _targetValue = double.nan;
num _deltaValue = double.nan;
TweenProperty._(this._tweenPropertyAccessor, this._propertyID);
/// Animate the property from the current value to a given target value.
void to(num targetValue) {
_targetValue = targetValue.toDouble();
}
/// Animate the property from the current value by a given delta value.
void by(num deltaValue) {
_deltaValue = deltaValue.toDouble();
}
void _init() {
_startValue = _tweenPropertyAccessor._getValue(_propertyID);
if (_deltaValue.isNaN && _targetValue.isFinite) {
_deltaValue = _targetValue - _startValue;
}
if (_targetValue.isNaN && _deltaValue.isFinite) {
_targetValue = _startValue + _deltaValue;
}
}
void _update(num transition, bool roundToInt) {
if (_startValue.isFinite && _targetValue.isFinite) {
var value = _startValue + transition * (_targetValue - _startValue);
value = roundToInt ? value.roundToDouble() : value;
_tweenPropertyAccessor._setValue(_propertyID, value);
}
}
}