-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9a363ba
commit 4053012
Showing
5 changed files
with
416 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
reactfx-demos/src/main/java/org/reactfx/demo/AnimatedValDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.reactfx.demo; | ||
|
||
import java.time.Duration; | ||
import java.util.Random; | ||
|
||
import javafx.application.Application; | ||
import javafx.geometry.Point2D; | ||
import javafx.scene.Scene; | ||
import javafx.scene.layout.Pane; | ||
import javafx.scene.paint.Color; | ||
import javafx.scene.shape.Circle; | ||
import javafx.stage.Stage; | ||
|
||
import org.reactfx.value.Val; | ||
import org.reactfx.value.Var; | ||
|
||
public class AnimatedValDemo extends Application { | ||
private static final int W = 600; | ||
private static final int H = 600; | ||
|
||
@Override | ||
public void start(Stage primaryStage) { | ||
Circle circle = new Circle(30.0, Color.BLUE); | ||
Pane canvas = new Pane(circle); | ||
|
||
Var<Point2D> center = Var.newSimpleVar(new Point2D(W/2, H/2)); | ||
Val<Point2D> animCenter = center.animate( | ||
(p1, p2) -> Duration.ofMillis((long) p1.distance(p2)), | ||
(p1, p2, frac) -> p1.multiply(1.0-frac).add(p2.multiply(frac))); | ||
|
||
circle.centerXProperty().bind(animCenter.map(Point2D::getX)); | ||
circle.centerYProperty().bind(animCenter.map(Point2D::getY)); | ||
|
||
Random random = new Random(); | ||
|
||
circle.setOnMouseClicked(click -> { | ||
center.setValue(new Point2D( | ||
random.nextInt(W), | ||
random.nextInt(H))); | ||
}); | ||
|
||
primaryStage.setScene(new Scene(canvas, W, H)); | ||
primaryStage.show(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
launch(args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.reactfx.util; | ||
|
||
import static javafx.animation.Interpolator.*; | ||
|
||
/** | ||
* Interpolates values between two boundary values. | ||
* | ||
* <p>This is a simpler and more flexible interface than the class | ||
* {@link javafx.animation.Interpolator}. Simpler, because it only interpolates | ||
* values of one type, {@code T}. More flexible, because the values to | ||
* interpolate don't have to be numbers nor implement | ||
* {@linkplain javafx.animation.Interpolatable}. | ||
* | ||
* @param <T> type of the values to interpolate | ||
*/ | ||
@FunctionalInterface | ||
public interface Interpolator<T> { | ||
T interpolate(T start, T end, double fraction); | ||
|
||
static final Interpolator<Double> LINEAR_DOUBLE = | ||
(a, b, frac) -> LINEAR.interpolate(a.doubleValue(), b.doubleValue(), frac); | ||
|
||
static final Interpolator<Integer> LINEAR_INTEGER = | ||
(a, b, frac) -> LINEAR.interpolate(a.intValue(), b.intValue(), frac); | ||
|
||
static final Interpolator<Long> LINEAR_LONG = | ||
(a, b, frac) -> LINEAR.interpolate(a.longValue(), b.longValue(), frac); | ||
|
||
static final Interpolator<Double> EASE_BOTH_DOUBLE = | ||
(a, b, frac) -> EASE_BOTH.interpolate(a.doubleValue(), b.doubleValue(), frac); | ||
|
||
static final Interpolator<Integer> EASE_BOTH_INTEGER = | ||
(a, b, frac) -> EASE_BOTH.interpolate(a.intValue(), b.intValue(), frac); | ||
|
||
static final Interpolator<Long> EASE_BOTH_LONG = | ||
(a, b, frac) -> EASE_BOTH.interpolate(a.longValue(), b.longValue(), frac); | ||
|
||
static final Interpolator<Double> EASE_IN_DOUBLE = | ||
(a, b, frac) -> EASE_IN.interpolate(a.doubleValue(), b.doubleValue(), frac); | ||
|
||
static final Interpolator<Integer> EASE_IN_INTEGER = | ||
(a, b, frac) -> EASE_IN.interpolate(a.intValue(), b.intValue(), frac); | ||
|
||
static final Interpolator<Long> EASE_IN_LONG = | ||
(a, b, frac) -> EASE_IN.interpolate(a.longValue(), b.longValue(), frac); | ||
|
||
static final Interpolator<Double> EASE_OUT_DOUBLE = | ||
(a, b, frac) -> EASE_OUT.interpolate(a.doubleValue(), b.doubleValue(), frac); | ||
|
||
static final Interpolator<Integer> EASE_OUT_INTEGER = | ||
(a, b, frac) -> EASE_OUT.interpolate(a.intValue(), b.intValue(), frac); | ||
|
||
static final Interpolator<Long> EASE_OUT_LONG = | ||
(a, b, frac) -> EASE_OUT.interpolate(a.longValue(), b.longValue(), frac); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.reactfx.value; | ||
|
||
import java.time.Duration; | ||
import java.util.function.BiFunction; | ||
|
||
import javafx.animation.Transition; | ||
import javafx.beans.value.ObservableValue; | ||
|
||
import org.reactfx.Subscription; | ||
import org.reactfx.util.Interpolator; | ||
|
||
class AnimatedVal<T> extends ValBase<T> { | ||
private final class FractionTransition extends Transition { | ||
|
||
@Override | ||
protected void interpolate(double frac) { | ||
fraction = frac; | ||
invalidate(); | ||
} | ||
|
||
void setDuration(Duration d) { | ||
setCycleDuration(javafx.util.Duration.millis(d.toMillis())); | ||
} | ||
} | ||
|
||
private final ObservableValue<T> src; | ||
private final BiFunction<? super T, ? super T, Duration> duration; | ||
private final Interpolator<T> interpolator; | ||
private final FractionTransition transition = new FractionTransition(); | ||
|
||
private double fraction = 1.0; | ||
private T oldValue = null; | ||
|
||
AnimatedVal( | ||
ObservableValue<T> src, | ||
BiFunction<? super T, ? super T, Duration> duration, | ||
Interpolator<T> interpolator) { | ||
this.src = src; | ||
this.duration = duration; | ||
this.interpolator = interpolator; | ||
} | ||
|
||
@Override | ||
protected Subscription connect() { | ||
oldValue = src.getValue(); | ||
return Val.observeChanges(src, (obs, oldVal, newVal) -> { | ||
oldValue = getValue(); | ||
Duration d = duration.apply(oldValue, newVal); | ||
transition.setDuration(d); | ||
transition.playFromStart(); | ||
}); | ||
} | ||
|
||
@Override | ||
protected T computeValue() { | ||
return fraction == 1.0 | ||
? src.getValue() | ||
: interpolator.interpolate(oldValue, src.getValue(), fraction); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.