| OLD | NEW |
| (Empty) |
| 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/theme/shadows.dart'; | |
| 12 import 'package:sky/widgets/basic.dart'; | |
| 13 | |
| 14 // This class builds a Builder object from a collection of optionally- | |
| 15 // animated properties. Use syncFields to update the Builder's properties, | |
| 16 // which will optionally animate them using an AnimationPerformance. | |
| 17 class AnimationBuilder { | |
| 18 AnimatedType<double> opacity; | |
| 19 AnimatedType<Point> position; | |
| 20 AnimatedType<double> shadow; | |
| 21 AnimatedColor backgroundColor; | |
| 22 | |
| 23 // These don't animate, but are used to build the AnimationBuilder anyway. | |
| 24 double borderRadius; | |
| 25 Shape shape; | |
| 26 | |
| 27 Map<AnimatedVariable, AnimationPerformance> _variableToPerformance = | |
| 28 new Map<AnimatedVariable, AnimationPerformance>(); | |
| 29 | |
| 30 AnimationBuilder(); | |
| 31 | |
| 32 AnimationPerformance createPerformance(List<AnimatedType> variables, | |
| 33 {Duration duration}) { | |
| 34 AnimationPerformance performance = new AnimationPerformance() | |
| 35 ..duration = duration | |
| 36 ..variable = new AnimatedList(variables); | |
| 37 for (AnimatedVariable variable in variables) | |
| 38 _variableToPerformance[variable] = performance; | |
| 39 return performance; | |
| 40 } | |
| 41 | |
| 42 Widget build(Widget child) { | |
| 43 Widget current = child; | |
| 44 if (shadow != null || backgroundColor != null || | |
| 45 borderRadius != null || shape != null) { | |
| 46 current = new DecoratedBox( | |
| 47 decoration: new BoxDecoration( | |
| 48 borderRadius: borderRadius, | |
| 49 shape: shape, | |
| 50 boxShadow: shadow != null ? _computeShadow(shadow.value) : null, | |
| 51 backgroundColor: backgroundColor != null ? backgroundColor.value : nul
l), | |
| 52 child: current); | |
| 53 } | |
| 54 | |
| 55 if (position != null) { | |
| 56 Matrix4 transform = new Matrix4.identity(); | |
| 57 transform.translate(position.value.x, position.value.y); | |
| 58 current = new Transform(transform: transform, child: current); | |
| 59 } | |
| 60 | |
| 61 if (opacity != null) { | |
| 62 current = new Opacity(opacity: opacity.value, child: current); | |
| 63 } | |
| 64 | |
| 65 return current; | |
| 66 } | |
| 67 | |
| 68 void syncFields(AnimationBuilder source) { | |
| 69 _syncField(position, source.position); | |
| 70 _syncField(shadow, source.shadow); | |
| 71 _syncField(backgroundColor, source.backgroundColor); | |
| 72 | |
| 73 borderRadius = source.borderRadius; | |
| 74 shape = source.shape; | |
| 75 } | |
| 76 | |
| 77 void _syncField(AnimatedType variable, AnimatedType sourceVariable) { | |
| 78 if (variable == null) | |
| 79 return; // TODO(mpcomplete): Should we handle transition from null? | |
| 80 | |
| 81 AnimationPerformance performance = _variableToPerformance[variable]; | |
| 82 if (performance == null) { | |
| 83 // If there's no performance, no need to animate. | |
| 84 if (sourceVariable != null) | |
| 85 variable.value = sourceVariable.value; | |
| 86 return; | |
| 87 } | |
| 88 | |
| 89 if (variable.value != sourceVariable.value) { | |
| 90 variable | |
| 91 ..begin = variable.value | |
| 92 ..end = sourceVariable.value; | |
| 93 performance | |
| 94 ..progress = 0.0 | |
| 95 ..play(); | |
| 96 } | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 class AnimatedColor extends AnimatedType<Color> { | |
| 101 AnimatedColor(Color begin, {Color end, Curve curve: linear}) | |
| 102 : super(begin, end: end, curve: curve); | |
| 103 | |
| 104 void setFraction(double t) { | |
| 105 value = lerpColor(begin, end, t); | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 List<BoxShadow> _computeShadow(double level) { | |
| 110 if (level < 1.0) // shadows[1] is the first shadow | |
| 111 return null; | |
| 112 | |
| 113 int level1 = level.floor(); | |
| 114 int level2 = level.ceil(); | |
| 115 double t = level - level1.toDouble(); | |
| 116 | |
| 117 List<BoxShadow> shadow = new List<BoxShadow>(); | |
| 118 for (int i = 0; i < shadows[level1].length; ++i) | |
| 119 shadow.add(lerpBoxShadow(shadows[level1][i], shadows[level2][i], t)); | |
| 120 return shadow; | |
| 121 } | |
| OLD | NEW |