Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sky/sdk/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ dart_pkg("sky") {
"CHANGELOG.md",
"bin/init.dart",
"lib/animation/animated_simulation.dart",
"lib/animation/animated_value.dart",
"lib/animation/animation_performance.dart",
"lib/animation/curves.dart",
"lib/animation/forces.dart",
Expand Down
6 changes: 3 additions & 3 deletions sky/sdk/example/stocks/lib/stock_home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// found in the LICENSE file.

import 'package:sky/editing/input.dart';
import 'package:sky/animation/animation_performance.dart';
import 'package:sky/animation/animated_value.dart';
import 'package:sky/widgets/animated_component.dart';
import 'package:sky/widgets/animation_builder.dart';
import 'package:sky/theme/colors.dart' as colors;
Expand Down Expand Up @@ -280,10 +280,10 @@ class StockHome extends AnimatedComponent {
void _handleStockPurchased() {
setState(() {
_snackbarTransform = new AnimationBuilder()
..position = new AnimatedType<Point>(const Point(0.0, 45.0), end: Point.origin);
..position = new AnimatedValue<Point>(const Point(0.0, 45.0), end: Point.origin);
var performance = _snackbarTransform.createPerformance(
[_snackbarTransform.position], duration: _kSnackbarSlideDuration);
watch(performance);
watch(performance); // TODO(mpcomplete): need to unwatch
performance.play();
});
}
Expand Down
79 changes: 79 additions & 0 deletions sky/sdk/lib/animation/animated_value.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import "dart:sky";

import 'package:sky/animation/curves.dart';
import 'package:sky/base/lerp.dart';

abstract class AnimatedVariable {
void setProgress(double t);
String toString();
}

class Interval {
final double start;
final double end;

double adjustTime(double t) {
return ((t - start) / (end - start)).clamp(0.0, 1.0);
}

Interval(this.start, this.end) {
assert(start >= 0.0);
assert(start <= 1.0);
assert(end >= 0.0);
assert(end <= 1.0);
}
}

class AnimatedValue<T extends dynamic> extends AnimatedVariable {
AnimatedValue(this.begin, { this.end, this.interval, this.curve: linear }) {
value = begin;
}

T value;
T begin;
T end;
Interval interval;
Curve curve;

void setProgress(double t) {
if (end != null) {
double adjustedTime = interval == null ? t : interval.adjustTime(t);
if (adjustedTime == 1.0) {
value = end;
} else {
// TODO(mpcomplete): Reverse the timeline and curve.
value = begin + (end - begin) * curve.transform(adjustedTime);
}
}
}

String toString() => 'AnimatedValue(begin=$begin, end=$end, value=$value)';
}

class AnimatedList extends AnimatedVariable {
List<AnimatedVariable> variables;
Interval interval;

AnimatedList(this.variables, { this.interval });

void setProgress(double t) {
double adjustedTime = interval == null ? t : interval.adjustTime(t);
for (AnimatedVariable variable in variables)
variable.setProgress(adjustedTime);
}

String toString() => 'AnimatedList([$variables])';
}

class AnimatedColor extends AnimatedValue<Color> {
AnimatedColor(Color begin, { Color end, Curve curve: linear })
: super(begin, end: end, curve: curve);

void setProgress(double t) {
value = lerpColor(begin, end, t);
}
}
69 changes: 3 additions & 66 deletions sky/sdk/lib/animation/animation_performance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,9 @@

import 'dart:async';

import 'package:sky/animation/timeline.dart';
import 'package:sky/animation/curves.dart';
import 'package:sky/animation/animated_value.dart';
import 'package:sky/animation/forces.dart';

abstract class AnimatedVariable {
void setFraction(double t);
String toString();
}

class Interval {
final double start;
final double end;

double adjustTime(double t) {
return ((t - start) / (end - start)).clamp(0.0, 1.0);
}

Interval(this.start, this.end) {
assert(start >= 0.0);
assert(start <= 1.0);
assert(end >= 0.0);
assert(end <= 1.0);
}
}

class AnimatedType<T extends dynamic> extends AnimatedVariable {
AnimatedType(this.begin, { this.end, this.interval, this.curve: linear }) {
value = begin;
}

T value;
T begin;
T end;
Interval interval;
Curve curve;

void setFraction(double t) {
if (end != null) {
double adjustedTime = interval == null ? t : interval.adjustTime(t);
if (adjustedTime == 1.0) {
value = end;
} else {
// TODO(mpcomplete): Reverse the timeline and curve.
value = begin + (end - begin) * curve.transform(adjustedTime);
}
}
}

String toString() => 'AnimatedType(begin=$begin, end=$end, value=$value)';
}

class AnimatedList extends AnimatedVariable {
List<AnimatedVariable> variables;
Interval interval;

AnimatedList(this.variables, { this.interval });

void setFraction(double t) {
double adjustedTime = interval == null ? t : interval.adjustTime(t);
for (AnimatedVariable variable in variables)
variable.setFraction(adjustedTime);
}

String toString() => 'AnimatedList([$variables])';
}
import 'package:sky/animation/timeline.dart';

// This class manages a "performance" - a collection of values that change
// based on a timeline. For example, a performance may handle an animation
Expand All @@ -82,7 +20,6 @@ class AnimationPerformance {
_timeline = new Timeline(_tick);
}

// TODO(mpcomplete): make this a list, or composable somehow.
AnimatedVariable variable;
// TODO(mpcomplete): duration should be on a director.
Duration duration;
Expand Down Expand Up @@ -142,7 +79,7 @@ class AnimationPerformance {
}

void _tick(double t) {
variable.setFraction(t);
variable.setProgress(t);
_notifyListeners();
}
}
21 changes: 11 additions & 10 deletions sky/sdk/lib/widgets/animated_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@

import 'package:vector_math/vector_math.dart';

import 'package:sky/animation/animated_value.dart';
import 'package:sky/animation/animation_performance.dart';
import 'package:sky/animation/curves.dart';
import 'package:sky/base/lerp.dart';
import 'package:sky/painting/box_painter.dart';
import 'package:sky/widgets/basic.dart';
import 'package:sky/widgets/animated_component.dart';

class AnimatedBoxConstraintsValue extends AnimatedType<BoxConstraints> {
class AnimatedBoxConstraintsValue extends AnimatedValue<BoxConstraints> {
AnimatedBoxConstraintsValue(BoxConstraints begin, { BoxConstraints end, Curve curve: linear })
: super(begin, end: end, curve: curve);

void setFraction(double t) {
void setProgress(double t) {
// TODO(abarth): We should lerp the BoxConstraints.
value = end;
}
}

class AnimatedBoxDecorationValue extends AnimatedType<BoxDecoration> {
class AnimatedBoxDecorationValue extends AnimatedValue<BoxDecoration> {
AnimatedBoxDecorationValue(BoxDecoration begin, { BoxDecoration end, Curve curve: linear })
: super(begin, end: end, curve: curve);

void setFraction(double t) {
void setProgress(double t) {
if (t == 1.0) {
value = end;
return;
Expand All @@ -34,11 +35,11 @@ class AnimatedBoxDecorationValue extends AnimatedType<BoxDecoration> {
}
}

class AnimatedEdgeDimsValue extends AnimatedType<EdgeDims> {
class AnimatedEdgeDimsValue extends AnimatedValue<EdgeDims> {
AnimatedEdgeDimsValue(EdgeDims begin, { EdgeDims end, Curve curve: linear })
: super(begin, end: end, curve: curve);

void setFraction(double t) {
void setProgress(double t) {
if (t == 1.0) {
value = end;
return;
Expand All @@ -54,7 +55,7 @@ class AnimatedEdgeDimsValue extends AnimatedType<EdgeDims> {

class ImplicitlyAnimatedValue<T> {
final AnimationPerformance performance = new AnimationPerformance();
final AnimatedType<T> _variable;
final AnimatedValue<T> _variable;

ImplicitlyAnimatedValue(this._variable, Duration duration) {
performance
Expand Down Expand Up @@ -168,21 +169,21 @@ class AnimatedContainer extends AnimatedComponent {

void _updateTransform() {
_updateField(transform, _transform, () {
_transform = new ImplicitlyAnimatedValue<Matrix4>(new AnimatedType<Matrix4>(transform), duration);
_transform = new ImplicitlyAnimatedValue<Matrix4>(new AnimatedValue<Matrix4>(transform), duration);
watch(_transform.performance);
});
}

void _updateWidth() {
_updateField(width, _width, () {
_width = new ImplicitlyAnimatedValue<double>(new AnimatedType<double>(width), duration);
_width = new ImplicitlyAnimatedValue<double>(new AnimatedValue<double>(width), duration);
watch(_width.performance);
});
}

void _updateHeight() {
_updateField(height, _height, () {
_height = new ImplicitlyAnimatedValue<double>( new AnimatedType<double>(height), duration);
_height = new ImplicitlyAnimatedValue<double>( new AnimatedValue<double>(height), duration);
watch(_height.performance);
});
}
Expand Down
24 changes: 7 additions & 17 deletions sky/sdk/lib/widgets/animation_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

import 'package:vector_math/vector_math.dart';

import 'package:sky/animation/animated_value.dart';
import 'package:sky/animation/animation_performance.dart';
import 'package:sky/animation/curves.dart';
import 'package:sky/base/lerp.dart';
import 'package:sky/painting/box_painter.dart';
import 'package:sky/theme/shadows.dart';
import 'package:sky/widgets/basic.dart';
Expand All @@ -18,9 +17,9 @@ class AnimationBuilder {

AnimationBuilder();

AnimatedType<double> opacity;
AnimatedType<Point> position;
AnimatedType<double> shadow;
AnimatedValue<double> opacity;
AnimatedValue<Point> position;
AnimatedValue<double> shadow;
AnimatedColor backgroundColor;

// These don't animate, but are used to build the AnimationBuilder anyway.
Expand All @@ -30,7 +29,7 @@ class AnimationBuilder {
Map<AnimatedVariable, AnimationPerformance> _variableToPerformance =
new Map<AnimatedVariable, AnimationPerformance>();

AnimationPerformance createPerformance(List<AnimatedType> variables,
AnimationPerformance createPerformance(List<AnimatedValue> variables,
{ Duration duration }) {
AnimationPerformance performance = new AnimationPerformance()
..duration = duration
Expand Down Expand Up @@ -67,7 +66,7 @@ class AnimationBuilder {
}

void updateFields({
AnimatedType<double> shadow,
AnimatedValue<double> shadow,
AnimatedColor backgroundColor,
double borderRadius,
Shape shape
Expand All @@ -78,7 +77,7 @@ class AnimationBuilder {
this.shape = shape;
}

void _updateField(AnimatedType variable, AnimatedType sourceVariable) {
void _updateField(AnimatedValue variable, AnimatedValue sourceVariable) {
if (variable == null)
return; // TODO(mpcomplete): Should we handle transition from null?

Expand All @@ -101,15 +100,6 @@ class AnimationBuilder {
}
}

class AnimatedColor extends AnimatedType<Color> {
AnimatedColor(Color begin, { Color end, Curve curve: linear })
: super(begin, end: end, curve: curve);

void setFraction(double t) {
value = lerpColor(begin, end, t);
}
}

List<BoxShadow> _computeShadow(double level) {
if (level < 1.0) // shadows[1] is the first shadow
return null;
Expand Down
9 changes: 5 additions & 4 deletions sky/sdk/lib/widgets/dismissable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'dart:sky' as sky;

import 'package:sky/animation/animated_value.dart';
import 'package:sky/animation/animation_performance.dart';
import 'package:sky/widgets/animated_component.dart';
import 'package:sky/widgets/basic.dart';
Expand All @@ -30,17 +31,17 @@ class Dismissable extends AnimatedComponent {
Widget child;
DismissedCallback onDismissed;

AnimatedType<Point> _position;
AnimatedType<double> _opacity;
AnimatedValue<Point> _position;
AnimatedValue<double> _opacity;
AnimationPerformance _performance;

double _width;
double _dragX = 0.0;
bool _dragUnderway = false;

void initState() {
_position = new AnimatedType<Point>(Point.origin);
_opacity = new AnimatedType<double>(1.0, end: 0.0);
_position = new AnimatedValue<Point>(Point.origin);
_opacity = new AnimatedValue<double>(1.0, end: 0.0);
_performance = new AnimationPerformance()
..duration = _kCardDismissFadeout
..variable = new AnimatedList([_position, _opacity])
Expand Down
Loading