Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 import 'package:vector_math/vector_math.dart'; | 5 import 'package:vector_math/vector_math.dart'; |
| 6 | 6 |
| 7 import 'package:sky/animation/animation_performance.dart'; | 7 import 'package:sky/animation/animation_performance.dart'; |
| 8 import 'package:sky/animation/curves.dart'; | 8 import 'package:sky/animation/curves.dart'; |
| 9 import 'package:sky/base/lerp.dart'; | 9 import 'package:sky/base/lerp.dart'; |
| 10 import 'package:sky/painting/box_painter.dart'; | 10 import 'package:sky/painting/box_painter.dart'; |
| 11 import 'package:sky/theme/shadows.dart'; | 11 import 'package:sky/theme/shadows.dart'; |
| 12 import 'package:sky/widgets/basic.dart'; | 12 import 'package:sky/widgets/basic.dart'; |
| 13 | 13 |
| 14 // This class builds a Container object from a collection of optionally- | 14 // This class builds a Container object from a collection of optionally- |
| 15 // animated properties. Use syncFields to update the Container's properties, | 15 // animated properties. Use syncFields to update the Container's properties, |
| 16 // which will optionally animate them using an AnimationPerformance. | 16 // which will optionally animate them using an AnimationPerformance. |
| 17 class AnimationBuilder { | 17 class AnimationBuilder { |
| 18 | |
| 19 AnimationBuilder(); | |
| 20 | |
| 18 AnimatedType<double> opacity; | 21 AnimatedType<double> opacity; |
| 19 AnimatedType<Point> position; | 22 AnimatedType<Point> position; |
| 20 AnimatedType<double> shadow; | 23 AnimatedType<double> shadow; |
| 21 AnimatedColor backgroundColor; | 24 AnimatedColor backgroundColor; |
| 22 | 25 |
| 23 // These don't animate, but are used to build the AnimationBuilder anyway. | 26 // These don't animate, but are used to build the AnimationBuilder anyway. |
| 24 double borderRadius; | 27 double borderRadius; |
| 25 Shape shape; | 28 Shape shape; |
| 26 | 29 |
| 27 Map<AnimatedVariable, AnimationPerformance> _variableToPerformance = | 30 Map<AnimatedVariable, AnimationPerformance> _variableToPerformance = |
| 28 new Map<AnimatedVariable, AnimationPerformance>(); | 31 new Map<AnimatedVariable, AnimationPerformance>(); |
| 29 | 32 |
| 30 AnimationBuilder(); | |
| 31 | |
| 32 AnimationPerformance createPerformance(List<AnimatedType> variables, | 33 AnimationPerformance createPerformance(List<AnimatedType> variables, |
| 33 {Duration duration}) { | 34 { Duration duration }) { |
| 34 AnimationPerformance performance = new AnimationPerformance() | 35 AnimationPerformance performance = new AnimationPerformance() |
| 35 ..duration = duration | 36 ..duration = duration |
| 36 ..variable = new AnimatedList(variables); | 37 ..variable = new AnimatedList(variables); |
| 37 for (AnimatedVariable variable in variables) | 38 for (AnimatedVariable variable in variables) |
| 38 _variableToPerformance[variable] = performance; | 39 _variableToPerformance[variable] = performance; |
| 39 return performance; | 40 return performance; |
| 40 } | 41 } |
| 41 | 42 |
| 42 Widget build(Widget child) { | 43 Widget build(Widget child) { |
| 43 Widget current = child; | 44 Widget current = child; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 58 current = new Transform(transform: transform, child: current); | 59 current = new Transform(transform: transform, child: current); |
| 59 } | 60 } |
| 60 | 61 |
| 61 if (opacity != null) { | 62 if (opacity != null) { |
| 62 current = new Opacity(opacity: opacity.value, child: current); | 63 current = new Opacity(opacity: opacity.value, child: current); |
| 63 } | 64 } |
| 64 | 65 |
| 65 return current; | 66 return current; |
| 66 } | 67 } |
| 67 | 68 |
| 68 void updateFields({ AnimatedType<double> shadow, | 69 void updateFields({ |
| 69 AnimatedColor backgroundColor, | 70 AnimatedType<double> shadow, |
| 70 double borderRadius, | 71 AnimatedColor backgroundColor, |
| 71 Shape shape }) { | 72 double borderRadius, |
| 73 Shape shape | |
| 74 }) { | |
| 72 _updateField(this.shadow, shadow); | 75 _updateField(this.shadow, shadow); |
| 73 _updateField(this.backgroundColor, backgroundColor); | 76 _updateField(this.backgroundColor, backgroundColor); |
| 74 this.borderRadius = borderRadius; | 77 this.borderRadius = borderRadius; |
| 75 this.shape = shape; | 78 this.shape = shape; |
| 76 } | 79 } |
| 77 | 80 |
| 78 void _updateField(AnimatedType variable, AnimatedType sourceVariable) { | 81 void _updateField(AnimatedType variable, AnimatedType sourceVariable) { |
| 79 if (variable == null) | 82 if (variable == null) |
| 80 return; // TODO(mpcomplete): Should we handle transition from null? | 83 return; // TODO(mpcomplete): Should we handle transition from null? |
|
Matt Perry
2015/07/13 17:32:55
google C++ style is to have 2 spaces before in-lin
| |
| 81 | 84 |
| 82 AnimationPerformance performance = _variableToPerformance[variable]; | 85 AnimationPerformance performance = _variableToPerformance[variable]; |
| 83 if (performance == null) { | 86 if (performance == null) { |
| 84 // If there's no performance, no need to animate. | 87 // If there's no performance, no need to animate. |
| 85 if (sourceVariable != null) | 88 if (sourceVariable != null) |
| 86 variable.value = sourceVariable.value; | 89 variable.value = sourceVariable.value; |
| 87 return; | 90 return; |
| 88 } | 91 } |
| 89 | 92 |
| 90 if (variable.value != sourceVariable.value) { | 93 if (variable.value != sourceVariable.value) { |
| 91 variable | 94 variable |
| 92 ..begin = variable.value | 95 ..begin = variable.value |
| 93 ..end = sourceVariable.value; | 96 ..end = sourceVariable.value; |
| 94 performance | 97 performance |
| 95 ..progress = 0.0 | 98 ..progress = 0.0 |
| 96 ..play(); | 99 ..play(); |
| 97 } | 100 } |
| 98 } | 101 } |
| 99 } | 102 } |
| 100 | 103 |
| 101 class AnimatedColor extends AnimatedType<Color> { | 104 class AnimatedColor extends AnimatedType<Color> { |
| 102 AnimatedColor(Color begin, {Color end, Curve curve: linear}) | 105 AnimatedColor(Color begin, { Color end, Curve curve: linear }) |
| 103 : super(begin, end: end, curve: curve); | 106 : super(begin, end: end, curve: curve); |
| 104 | 107 |
| 105 void setFraction(double t) { | 108 void setFraction(double t) { |
| 106 value = lerpColor(begin, end, t); | 109 value = lerpColor(begin, end, t); |
| 107 } | 110 } |
| 108 } | 111 } |
| 109 | 112 |
| 110 List<BoxShadow> _computeShadow(double level) { | 113 List<BoxShadow> _computeShadow(double level) { |
| 111 if (level < 1.0) // shadows[1] is the first shadow | 114 if (level < 1.0) // shadows[1] is the first shadow |
| 112 return null; | 115 return null; |
| 113 | 116 |
| 114 int level1 = level.floor(); | 117 int level1 = level.floor(); |
| 115 int level2 = level.ceil(); | 118 int level2 = level.ceil(); |
| 116 double t = level - level1.toDouble(); | 119 double t = level - level1.toDouble(); |
| 117 | 120 |
| 118 List<BoxShadow> shadow = new List<BoxShadow>(); | 121 List<BoxShadow> shadow = new List<BoxShadow>(); |
| 119 for (int i = 0; i < shadows[level1].length; ++i) | 122 for (int i = 0; i < shadows[level1].length; ++i) |
| 120 shadow.add(lerpBoxShadow(shadows[level1][i], shadows[level2][i], t)); | 123 shadow.add(lerpBoxShadow(shadows[level1][i], shadows[level2][i], t)); |
| 121 return shadow; | 124 return shadow; |
| 122 } | 125 } |
| OLD | NEW |