Skip to content

Commit

Permalink
Add convenient animation for Interpolatable types.
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasMikula committed Feb 15, 2015
1 parent b369264 commit 95b184c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
14 changes: 12 additions & 2 deletions reactfx-demos/src/main/java/org/reactfx/demo/AnimatedValDemo.java
Expand Up @@ -20,9 +20,10 @@ public class AnimatedValDemo extends Application {

@Override
public void start(Stage primaryStage) {
Circle circle = new Circle(30.0, Color.BLUE);
Circle circle = new Circle(30.0);
Pane canvas = new Pane(circle);

// animate circle position
Var<Point2D> center = Var.newSimpleVar(new Point2D(W/2, H/2));
Val<Point2D> animCenter = center.animate(
(p1, p2) -> Duration.ofMillis((long) p1.distance(p2)),
Expand All @@ -31,12 +32,21 @@ public void start(Stage primaryStage) {
circle.centerXProperty().bind(animCenter.map(Point2D::getX));
circle.centerYProperty().bind(animCenter.map(Point2D::getY));

Random random = new Random();
// animate circle color
Var<Color> color = Var.newSimpleVar(Color.BLUE);
Val<Color> animColor = Val.animate(color, Duration.ofMillis(500));
circle.fillProperty().bind(animColor);

// on click, move to random position and transition to random color
Random random = new Random();
circle.setOnMouseClicked(click -> {
center.setValue(new Point2D(
random.nextInt(W),
random.nextInt(H)));
color.setValue(Color.rgb(
random.nextInt(240),
random.nextInt(240),
random.nextInt(240)));
});

primaryStage.setScene(new Scene(canvas, W, H));
Expand Down
5 changes: 5 additions & 0 deletions reactfx/src/main/java/org/reactfx/util/Interpolator.java
@@ -1,6 +1,7 @@
package org.reactfx.util;

import static javafx.animation.Interpolator.*;
import javafx.animation.Interpolatable;

/**
* Interpolates values between two boundary values.
Expand Down Expand Up @@ -52,4 +53,8 @@ public interface Interpolator<T> {

static final Interpolator<Long> EASE_OUT_LONG =
(a, b, frac) -> EASE_OUT.interpolate(a.longValue(), b.longValue(), frac);

static <T extends Interpolatable<T>> Interpolator<T> get() {
return (a, b, frac) -> a.interpolate(b, frac);
}
}
23 changes: 23 additions & 0 deletions reactfx/src/main/java/org/reactfx/value/Val.java
Expand Up @@ -10,6 +10,7 @@
import java.util.function.Predicate;
import java.util.function.Supplier;

import javafx.animation.Interpolatable;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.property.Property;
Expand Down Expand Up @@ -464,6 +465,28 @@ static <T> Val<T> animate(
return animate(obs, (a, b) -> duration, interpolator);
}

/**
* Like {@link #animate(ObservableValue, BiFunction, Interpolator)}, but
* uses the interpolation defined by the {@linkplain Interpolatable} type
* {@code T}.
*/
static <T extends Interpolatable<T>> Val<T> animate(
ObservableValue<T> obs,
BiFunction<? super T, ? super T, Duration> duration) {
return animate(obs, duration, Interpolator.get());
}

/**
* Like {@link #animate(ObservableValue, Duration, Interpolator)}, but
* uses the interpolation defined by the {@linkplain Interpolatable} type
* {@code T}.
*/
static <T extends Interpolatable<T>> Val<T> animate(
ObservableValue<T> obs,
Duration duration) {
return animate(obs, duration, Interpolator.get());
}


static <A, B, R> Val<R> combine(
ObservableValue<A> src1,
Expand Down

0 comments on commit 95b184c

Please sign in to comment.