|
| 1 | +// Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:vector_math/vector_math.dart'; |
| 6 | + |
| 7 | +import 'package:sky/animation/animation_performance.dart'; |
| 8 | +import 'package:sky/animation/curves.dart'; |
| 9 | +import 'package:sky/base/lerp.dart'; |
| 10 | +import 'package:sky/painting/box_painter.dart'; |
| 11 | +import 'package:sky/widgets/basic.dart'; |
| 12 | +import 'package:sky/widgets/animated_component.dart'; |
| 13 | + |
| 14 | +class AnimatedBoxConstraintsValue extends AnimatedType<BoxConstraints> { |
| 15 | + AnimatedBoxConstraintsValue(BoxConstraints begin, { BoxConstraints end, Curve curve: linear }) |
| 16 | + : super(begin, end: end, curve: curve); |
| 17 | + |
| 18 | + void setFraction(double t) { |
| 19 | + // TODO(abarth): We should lerp the BoxConstraints. |
| 20 | + value = end; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +class AnimatedBoxDecorationValue extends AnimatedType<BoxDecoration> { |
| 25 | + AnimatedBoxDecorationValue(BoxDecoration begin, { BoxDecoration end, Curve curve: linear }) |
| 26 | + : super(begin, end: end, curve: curve); |
| 27 | + |
| 28 | + void setFraction(double t) { |
| 29 | + if (t == 1.0) { |
| 30 | + value = end; |
| 31 | + return; |
| 32 | + } |
| 33 | + value = lerpBoxDecoration(begin, end, t); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +class AnimatedEdgeDimsValue extends AnimatedType<EdgeDims> { |
| 38 | + AnimatedEdgeDimsValue(EdgeDims begin, { EdgeDims end, Curve curve: linear }) |
| 39 | + : super(begin, end: end, curve: curve); |
| 40 | + |
| 41 | + void setFraction(double t) { |
| 42 | + if (t == 1.0) { |
| 43 | + value = end; |
| 44 | + return; |
| 45 | + } |
| 46 | + value = new EdgeDims( |
| 47 | + lerpNum(begin.top, end.top, t), |
| 48 | + lerpNum(begin.right, end.right, t), |
| 49 | + lerpNum(begin.bottom, end.bottom, t), |
| 50 | + lerpNum(begin.bottom, end.left, t) |
| 51 | + ); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +class ImplicitlyAnimatedValue<T> { |
| 56 | + final AnimationPerformance performance = new AnimationPerformance(); |
| 57 | + final AnimatedType<T> _variable; |
| 58 | + |
| 59 | + ImplicitlyAnimatedValue(this._variable, Duration duration) { |
| 60 | + performance |
| 61 | + ..variable = _variable |
| 62 | + ..duration = duration; |
| 63 | + } |
| 64 | + |
| 65 | + T get value => _variable.value; |
| 66 | + void set value(T newValue) { |
| 67 | + _variable.begin = _variable.value; |
| 68 | + _variable.end = newValue; |
| 69 | + if (_variable.value != _variable.end) { |
| 70 | + performance |
| 71 | + ..progress = 0.0 |
| 72 | + ..play(); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +class AnimatedContainer extends AnimatedComponent { |
| 78 | + AnimatedContainer({ |
| 79 | + String key, |
| 80 | + this.child, |
| 81 | + this.duration, |
| 82 | + this.constraints, |
| 83 | + this.decoration, |
| 84 | + this.width, |
| 85 | + this.height, |
| 86 | + this.margin, |
| 87 | + this.padding, |
| 88 | + this.transform |
| 89 | + }) : super(key: key); |
| 90 | + |
| 91 | + Widget child; |
| 92 | + Duration duration; // TODO(abarth): Support separate durations for each value. |
| 93 | + BoxConstraints constraints; |
| 94 | + BoxDecoration decoration; |
| 95 | + EdgeDims margin; |
| 96 | + EdgeDims padding; |
| 97 | + Matrix4 transform; |
| 98 | + double width; |
| 99 | + double height; |
| 100 | + |
| 101 | + ImplicitlyAnimatedValue<BoxConstraints> _constraints; |
| 102 | + ImplicitlyAnimatedValue<BoxDecoration> _decoration; |
| 103 | + ImplicitlyAnimatedValue<EdgeDims> _margin; |
| 104 | + ImplicitlyAnimatedValue<EdgeDims> _padding; |
| 105 | + ImplicitlyAnimatedValue<Matrix4> _transform; |
| 106 | + ImplicitlyAnimatedValue<double> _width; |
| 107 | + ImplicitlyAnimatedValue<double> _height; |
| 108 | + |
| 109 | + void initState() { |
| 110 | + _updateFields(); |
| 111 | + } |
| 112 | + |
| 113 | + void syncFields(AnimatedContainer source) { |
| 114 | + child = source.child; |
| 115 | + constraints = source.constraints; |
| 116 | + decoration = source.decoration; |
| 117 | + margin = source.margin; |
| 118 | + padding = source.padding; |
| 119 | + width = source.width; |
| 120 | + height = source.height; |
| 121 | + _updateFields(); |
| 122 | + } |
| 123 | + |
| 124 | + void _updateFields() { |
| 125 | + _updateConstraints(); |
| 126 | + _updateDecoration(); |
| 127 | + _updateMargin(); |
| 128 | + _updatePadding(); |
| 129 | + _updateTransform(); |
| 130 | + _updateWidth(); |
| 131 | + _updateHeight(); |
| 132 | + } |
| 133 | + |
| 134 | + void _updateField(dynamic value, ImplicitlyAnimatedValue animatedValue, Function initField) { |
| 135 | + if (animatedValue != null) |
| 136 | + animatedValue.value = value; |
| 137 | + else if (value != null) |
| 138 | + initField(); |
| 139 | + } |
| 140 | + |
| 141 | + void _updateConstraints() { |
| 142 | + _updateField(constraints, _constraints, () { |
| 143 | + _constraints = new ImplicitlyAnimatedValue<BoxConstraints>(new AnimatedBoxConstraintsValue(constraints), duration); |
| 144 | + watch(_constraints.performance); |
| 145 | + }); |
| 146 | + } |
| 147 | + |
| 148 | + void _updateDecoration() { |
| 149 | + _updateField(decoration, _decoration, () { |
| 150 | + _decoration = new ImplicitlyAnimatedValue<BoxDecoration>(new AnimatedBoxDecorationValue(decoration), duration); |
| 151 | + watch(_decoration.performance); |
| 152 | + }); |
| 153 | + } |
| 154 | + |
| 155 | + void _updateMargin() { |
| 156 | + _updateField(margin, _margin, () { |
| 157 | + _margin = new ImplicitlyAnimatedValue<EdgeDims>(new AnimatedEdgeDimsValue(margin), duration); |
| 158 | + watch(_margin.performance); |
| 159 | + }); |
| 160 | + } |
| 161 | + |
| 162 | + void _updatePadding() { |
| 163 | + _updateField(padding, _padding, () { |
| 164 | + _padding = new ImplicitlyAnimatedValue<EdgeDims>(new AnimatedEdgeDimsValue(padding), duration); |
| 165 | + watch(_padding.performance); |
| 166 | + }); |
| 167 | + } |
| 168 | + |
| 169 | + void _updateTransform() { |
| 170 | + _updateField(transform, _transform, () { |
| 171 | + _transform = new ImplicitlyAnimatedValue<Matrix4>(new AnimatedType<Matrix4>(transform), duration); |
| 172 | + watch(_transform.performance); |
| 173 | + }); |
| 174 | + } |
| 175 | + |
| 176 | + void _updateWidth() { |
| 177 | + _updateField(width, _width, () { |
| 178 | + _width = new ImplicitlyAnimatedValue<double>(new AnimatedType<double>(width), duration); |
| 179 | + watch(_width.performance); |
| 180 | + }); |
| 181 | + } |
| 182 | + |
| 183 | + void _updateHeight() { |
| 184 | + _updateField(height, _height, () { |
| 185 | + _height = new ImplicitlyAnimatedValue<double>( new AnimatedType<double>(height), duration); |
| 186 | + watch(_height.performance); |
| 187 | + }); |
| 188 | + } |
| 189 | + |
| 190 | + dynamic _getValue(dynamic value, ImplicitlyAnimatedValue animatedValue) { |
| 191 | + return animatedValue == null ? value : animatedValue.value; |
| 192 | + } |
| 193 | + |
| 194 | + Widget build() { |
| 195 | + return new Container( |
| 196 | + child: child, |
| 197 | + constraints: _getValue(constraints, _constraints), |
| 198 | + decoration: _getValue(decoration, _decoration), |
| 199 | + margin: _getValue(margin, _margin), |
| 200 | + padding: _getValue(padding, _padding), |
| 201 | + transform: _getValue(transform, _transform), |
| 202 | + width: _getValue(width, _width), |
| 203 | + height: _getValue(height, _height) |
| 204 | + ); |
| 205 | + } |
| 206 | +} |
0 commit comments