From 95b184cab9b3b4b81c1425a40d06c2de95075ef6 Mon Sep 17 00:00:00 2001 From: Tomas Mikula Date: Sun, 15 Feb 2015 03:02:27 -0500 Subject: [PATCH] Add convenient animation for Interpolatable types. --- .../org/reactfx/demo/AnimatedValDemo.java | 14 +++++++++-- .../java/org/reactfx/util/Interpolator.java | 5 ++++ .../src/main/java/org/reactfx/value/Val.java | 23 +++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/reactfx-demos/src/main/java/org/reactfx/demo/AnimatedValDemo.java b/reactfx-demos/src/main/java/org/reactfx/demo/AnimatedValDemo.java index 11c175a..a196425 100644 --- a/reactfx-demos/src/main/java/org/reactfx/demo/AnimatedValDemo.java +++ b/reactfx-demos/src/main/java/org/reactfx/demo/AnimatedValDemo.java @@ -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 center = Var.newSimpleVar(new Point2D(W/2, H/2)); Val animCenter = center.animate( (p1, p2) -> Duration.ofMillis((long) p1.distance(p2)), @@ -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 = Var.newSimpleVar(Color.BLUE); + Val 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)); diff --git a/reactfx/src/main/java/org/reactfx/util/Interpolator.java b/reactfx/src/main/java/org/reactfx/util/Interpolator.java index 68527f3..ab5badf 100644 --- a/reactfx/src/main/java/org/reactfx/util/Interpolator.java +++ b/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. @@ -52,4 +53,8 @@ public interface Interpolator { static final Interpolator EASE_OUT_LONG = (a, b, frac) -> EASE_OUT.interpolate(a.longValue(), b.longValue(), frac); + + static > Interpolator get() { + return (a, b, frac) -> a.interpolate(b, frac); + } } diff --git a/reactfx/src/main/java/org/reactfx/value/Val.java b/reactfx/src/main/java/org/reactfx/value/Val.java index 2de7f12..a7bafb2 100644 --- a/reactfx/src/main/java/org/reactfx/value/Val.java +++ b/reactfx/src/main/java/org/reactfx/value/Val.java @@ -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; @@ -464,6 +465,28 @@ static Val 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 > Val animate( + ObservableValue obs, + BiFunction 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 > Val animate( + ObservableValue obs, + Duration duration) { + return animate(obs, duration, Interpolator.get()); + } + static Val combine( ObservableValue src1,