1 /*
   2  * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.animation;
  27 
  28 import javafx.beans.property.DoubleProperty;
  29 import javafx.beans.property.ObjectProperty;
  30 import javafx.beans.property.ObjectPropertyBase;
  31 import javafx.beans.property.SimpleDoubleProperty;
  32 import javafx.beans.property.SimpleObjectProperty;
  33 import javafx.scene.Node;
  34 import javafx.util.Duration;
  35 
  36 /**
  37  * This {@code Transition} creates a move/translate animation that spans its
  38  * {@link #duration}. This is done by updating the {@code translateX},
  39  * {@code translateY} and {@code translateZ} variables of the {@code node} at
  40  * regular interval.
  41  * <p>
  42  * It starts from the ({@code fromX}, {@code fromY}, {@code fromZ}) value if
  43  * provided else uses the {@code node}'s ({@code translateX}, {@code translateY}, {@code translateZ}) value.
  44  * <p>
  45  * It stops at the ({@code toX}, {@code toY}, {@code toZ}) value if provided
  46  * else it will use start value plus ({@code byX}, {@code byY}, {@code byZ})
  47  * value.
  48  * <p>
  49  * The ({@code toX}, {@code toY}, {@code toZ}) value takes precedence if both (
  50  * {@code toX}, {@code toY}, {@code toZ}) and ({@code byX}, {@code byY},
  51  * {@code byZ}) values are specified.
  52  *
  53  * <p>
  54  * Code Segment Example:
  55  * </p>
  56  *
  57  * <pre>
  58  * <code>
  59  * import javafx.scene.shape.*;
  60  * import javafx.animation.*;
  61  *
  62  * ...
  63  *
  64  *     Rectangle rect = new Rectangle (100, 40, 100, 100);
  65  *     rect.setArcHeight(50);
  66  *     rect.setArcWidth(50);
  67  *     rect.setFill(Color.VIOLET);
  68  *
  69  *     TranslateTransition tt = new TranslateTransition(Duration.millis(2000), rect);
  70  *     tt.setByX(200f);
  71  *     tt.setCycleCount(4f);
  72  *     tt.setAutoReverse(true);
  73  *
  74  *     tt.play();
  75  *
  76  * ...
  77  *
  78  * </code>
  79  * </pre>
  80  *
  81  * @see Transition
  82  * @see Animation
  83  *
  84  * @since JavaFX 2.0
  85  */
  86 public final class TranslateTransition extends Transition {
  87 
  88     private static final double EPSILON = 1e-12;
  89     private double startX;
  90     private double startY;
  91     private double startZ;
  92     private double deltaX;
  93     private double deltaY;
  94     private double deltaZ;
  95 
  96     /**
  97      * The target node of this {@code TranslateTransition}.
  98      * <p>
  99      * It is not possible to change the target {@code node} of a running
 100      * {@code TranslateTransition}. If the value of {@code node} is changed for
 101      * a running {@code TranslateTransition}, the animation has to be stopped
 102      * and started again to pick up the new value.
 103      */
 104     private ObjectProperty<Node> node;
 105     private static final Node DEFAULT_NODE = null;
 106 
 107     public final void setNode(Node value) {
 108         if ((node != null) || (value != null /* DEFAULT_NODE */)) {
 109             nodeProperty().set(value);
 110         }
 111     }
 112 
 113     public final Node getNode() {
 114         return (node == null)? DEFAULT_NODE : node.get();
 115     }
 116 
 117     public final ObjectProperty<Node> nodeProperty() {
 118         if (node == null) {
 119             node = new SimpleObjectProperty<Node>(this, "node", DEFAULT_NODE);
 120         }
 121         return node;
 122     }
 123 
 124     private Node cachedNode;
 125 
 126     /**
 127      * The duration of this {@code TranslateTransition}.
 128      * <p>
 129      * It is not possible to change the {@code duration} of a running
 130      * {@code TranslateTransition}. If the value of {@code duration} is changed
 131      * for a running {@code TranslateTransition}, the animation has to be
 132      * stopped and started again to pick up the new value.
 133      * <p>
 134      * Note: While the unit of {@code duration} is a millisecond, the
 135      * granularity depends on the underlying operating system and will in
 136      * general be larger. For example animations on desktop systems usually run
 137      * with a maximum of 60fps which gives a granularity of ~17 ms.
 138      *
 139      * Setting duration to value lower than {@link Duration#ZERO} will result
 140      * in {@link IllegalArgumentException}.
 141      *
 142      * @defaultValue 400ms
 143      */
 144     private ObjectProperty<Duration> duration;
 145     private static final Duration DEFAULT_DURATION = Duration.millis(400);
 146 
 147     public final void setDuration(Duration value) {
 148         if ((duration != null) || (!DEFAULT_DURATION.equals(value))) {
 149             durationProperty().set(value);
 150         }
 151     }
 152 
 153     public final Duration getDuration() {
 154         return (duration == null)? DEFAULT_DURATION : duration.get();
 155     }
 156 
 157     public final ObjectProperty<Duration> durationProperty() {
 158         if (duration == null) {
 159             duration = new ObjectPropertyBase<Duration>(DEFAULT_DURATION) {
 160 
 161                 @Override
 162                 public void invalidated() {
 163                     try {
 164                         setCycleDuration(getDuration());
 165                     } catch (IllegalArgumentException e) {
 166                         if (isBound()) {
 167                             unbind();
 168                         }
 169                         set(getCycleDuration());
 170                         throw e;
 171                     }
 172                 }
 173 
 174                 @Override
 175                 public Object getBean() {
 176                     return TranslateTransition.this;
 177                 }
 178 
 179                 @Override
 180                 public String getName() {
 181                     return "duration";
 182                 }
 183             };
 184         }
 185         return duration;
 186     }
 187 
 188     /**
 189      * Specifies the start X coordinate value of this
 190      * {@code TranslateTransition}.
 191      * <p>
 192      * It is not possible to change {@code fromX} of a running
 193      * {@code TranslateTransition}. If the value of {@code fromX} is changed for
 194      * a running {@code TranslateTransition}, the animation has to be stopped
 195      * and started again to pick up the new value.
 196      *
 197      * @defaultValue {@code Double.NaN}
 198      */
 199     private DoubleProperty fromX;
 200     private static final double DEFAULT_FROM_X = Double.NaN;
 201 
 202     public final void setFromX(double value) {
 203         if ((fromX != null) || (!Double.isNaN(value))) {
 204             fromXProperty().set(value);
 205         }
 206     }
 207 
 208     public final double getFromX() {
 209         return (fromX == null) ? DEFAULT_FROM_X : fromX.get();
 210     }
 211 
 212     public final DoubleProperty fromXProperty() {
 213         if (fromX == null) {
 214             fromX = new SimpleDoubleProperty(this, "fromX", DEFAULT_FROM_X);
 215         }
 216         return fromX;
 217     }
 218 
 219     /**
 220      * Specifies the start Y coordinate value of this
 221      * {@code TranslateTransition}.
 222      * <p>
 223      * It is not possible to change {@code fromY} of a running
 224      * {@code TranslateTransition}. If the value of {@code fromY} is changed for
 225      * a running {@code TranslateTransition}, the animation has to be stopped
 226      * and started again to pick up the new value.
 227      *
 228      * @defaultValue {@code Double.NaN}
 229      */
 230     private DoubleProperty fromY;
 231     private static final double DEFAULT_FROM_Y = Double.NaN;
 232 
 233     public final void setFromY(double value) {
 234         if ((fromY != null) || (!Double.isNaN(value))) {
 235             fromYProperty().set(value);
 236         }
 237     }
 238 
 239     public final double getFromY() {
 240         return (fromY == null)? DEFAULT_FROM_Y : fromY.get();
 241     }
 242 
 243     public final DoubleProperty fromYProperty() {
 244         if (fromY == null) {
 245             fromY = new SimpleDoubleProperty(this, "fromY", DEFAULT_FROM_Y);
 246         }
 247         return fromY;
 248     }
 249 
 250     /**
 251      * Specifies the start Z coordinate value of this
 252      * {@code TranslateTransition}.
 253      * <p>
 254      * It is not possible to change {@code fromZ} of a running
 255      * {@code TranslateTransition}. If the value of {@code fromZ} is changed for
 256      * a running {@code TranslateTransition}, the animation has to be stopped
 257      * and started again to pick up the new value.
 258      *
 259      * @defaultValue {@code Double.NaN}
 260      */
 261     private DoubleProperty fromZ;
 262     private static final double DEFAULT_FROM_Z = Double.NaN;
 263 
 264     public final void setFromZ(double value) {
 265         if ((fromZ != null) || (!Double.isNaN(value))) {
 266             fromZProperty().set(value);
 267         }
 268     }
 269 
 270     public final double getFromZ() {
 271         return (fromZ == null)? DEFAULT_FROM_Z : fromZ.get();
 272     }
 273 
 274     public final DoubleProperty fromZProperty() {
 275         if (fromZ == null) {
 276             fromZ = new SimpleDoubleProperty(this, "fromZ", DEFAULT_FROM_Z);
 277         }
 278         return fromZ;
 279     }
 280 
 281     /**
 282      * Specifies the stop X coordinate value of this {@code TranslateTransition}.
 283      * <p>
 284      * It is not possible to change {@code toX} of a running
 285      * {@code TranslateTransition}. If the value of {@code toX} is changed for a
 286      * running {@code TranslateTransition}, the animation has to be stopped and
 287      * started again to pick up the new value.
 288      *
 289      * @defaultValue {@code Double.NaN}
 290      */
 291     private DoubleProperty toX;
 292     private static final double DEFAULT_TO_X = Double.NaN;
 293 
 294     public final void setToX(double value) {
 295         if ((toX != null) || (!Double.isNaN(value))) {
 296             toXProperty().set(value);
 297         }
 298     }
 299 
 300     public final double getToX() {
 301         return (toX == null)? DEFAULT_TO_X : toX.get();
 302     }
 303 
 304     public final DoubleProperty toXProperty() {
 305         if (toX == null) {
 306             toX = new SimpleDoubleProperty(this, "toX", DEFAULT_TO_X);
 307         }
 308         return toX;
 309     }
 310 
 311     /**
 312      * Specifies the stop Y coordinate value of this {@code TranslateTransition}.
 313      * <p>
 314      * It is not possible to change {@code toY} of a running
 315      * {@code TranslateTransition}. If the value of {@code toY} is changed for a
 316      * running {@code TranslateTransition}, the animation has to be stopped and
 317      * started again to pick up the new value.
 318      *
 319      * @defaultValue {@code Double.NaN}
 320      */
 321     private DoubleProperty toY;
 322     private static final double DEFAULT_TO_Y = Double.NaN;
 323 
 324     public final void setToY(double value) {
 325         if ((toY != null) || (!Double.isNaN(value))) {
 326             toYProperty().set(value);
 327         }
 328     }
 329 
 330     public final double getToY() {
 331         return (toY == null)? DEFAULT_TO_Y : toY.get();
 332     }
 333 
 334     public final DoubleProperty toYProperty() {
 335         if (toY == null) {
 336             toY = new SimpleDoubleProperty(this, "toY", DEFAULT_TO_Y);
 337         }
 338         return toY;
 339     }
 340 
 341     /**
 342      * Specifies the stop Z coordinate value of this {@code TranslateTransition}.
 343      * <p>
 344      * It is not possible to change {@code toZ} of a running
 345      * {@code TranslateTransition}. If the value of {@code toZ} is changed for a
 346      * running {@code TranslateTransition}, the animation has to be stopped and
 347      * started again to pick up the new value.
 348      *
 349      * @defaultValue {@code Double.NaN}
 350      */
 351     private DoubleProperty toZ;
 352     private static final double DEFAULT_TO_Z = Double.NaN;
 353 
 354     public final void setToZ(double value) {
 355         if ((toZ != null) || (!Double.isNaN(value))) {
 356             toZProperty().set(value);
 357         }
 358     }
 359 
 360     public final double getToZ() {
 361         return (toZ == null)? DEFAULT_TO_Z : toZ.get();
 362     }
 363 
 364     public final DoubleProperty toZProperty() {
 365         if (toZ == null) {
 366             toZ = new SimpleDoubleProperty(this, "toZ", DEFAULT_TO_Z);
 367         }
 368         return toZ;
 369     }
 370 
 371     /**
 372      * Specifies the incremented stop X coordinate value, from the start, of
 373      * this {@code TranslateTransition}.
 374      * <p>
 375      * It is not possible to change {@code byX} of a running
 376      * {@code TranslateTransition}. If the value of {@code byX} is changed for a
 377      * running {@code TranslateTransition}, the animation has to be stopped and
 378      * started again to pick up the new value.
 379      */
 380     private DoubleProperty byX;
 381     private static final double DEFAULT_BY_X = 0.0;
 382 
 383     public final void setByX(double value) {
 384         if ((byX != null) || (Math.abs(value - DEFAULT_BY_X) > EPSILON)) {
 385             byXProperty().set(value);
 386         }
 387     }
 388 
 389     public final double getByX() {
 390         return (byX == null)? DEFAULT_BY_X : byX.get();
 391     }
 392 
 393     public final DoubleProperty byXProperty() {
 394         if (byX == null) {
 395             byX = new SimpleDoubleProperty(this, "byX", DEFAULT_BY_X);
 396         }
 397         return byX;
 398     }
 399 
 400     /**
 401      * Specifies the incremented stop Y coordinate value, from the start, of
 402      * this {@code TranslateTransition}.
 403      * <p>
 404      * It is not possible to change {@code byY} of a running
 405      * {@code TranslateTransition}. If the value of {@code byY} is changed for a
 406      * running {@code TranslateTransition}, the animation has to be stopped and
 407      * started again to pick up the new value.
 408      */
 409     private DoubleProperty byY;
 410     private static final double DEFAULT_BY_Y = 0.0;
 411 
 412     public final void setByY(double value) {
 413         if ((byY != null) || (Math.abs(value - DEFAULT_BY_Y) > EPSILON)) {
 414             byYProperty().set(value);
 415         }
 416     }
 417 
 418     public final double getByY() {
 419         return (byY == null)? DEFAULT_BY_Y : byY.get();
 420     }
 421 
 422     public final DoubleProperty byYProperty() {
 423         if (byY == null) {
 424             byY = new SimpleDoubleProperty(this, "byY", DEFAULT_BY_Y);
 425         }
 426         return byY;
 427     }
 428 
 429     /**
 430      * Specifies the incremented stop Z coordinate value, from the start, of
 431      * this {@code TranslateTransition}.
 432      * <p>
 433      * It is not possible to change {@code byZ} of a running
 434      * {@code TranslateTransition}. If the value of {@code byZ} is changed for a
 435      * running {@code TranslateTransition}, the animation has to be stopped and
 436      * started again to pick up the new value.
 437      */
 438     private DoubleProperty byZ;
 439     private static final double DEFAULT_BY_Z = 0.0;
 440 
 441     public final void setByZ(double value) {
 442         if ((byZ != null) || (Math.abs(value - DEFAULT_BY_Z) > EPSILON)) {
 443             byZProperty().set(value);
 444         }
 445     }
 446 
 447     public final double getByZ() {
 448         return (byZ == null)? DEFAULT_BY_Z : byZ.get();
 449     }
 450 
 451     public final DoubleProperty byZProperty() {
 452         if (byZ == null) {
 453             byZ = new SimpleDoubleProperty(this, "byZ", DEFAULT_BY_Z);
 454         }
 455         return byZ;
 456     }
 457 
 458     /**
 459      * The constructor of {@code TranslateTransition}
 460      *
 461      * @param duration
 462      *            The duration of the {@code TranslateTransition}
 463      * @param node
 464      *            The {@code node} which will be translated
 465      */
 466     public TranslateTransition(Duration duration, Node node) {
 467         setDuration(duration);
 468         setNode(node);
 469         setCycleDuration(duration);
 470     }
 471 
 472     /**
 473      * The constructor of {@code TranslateTransition}
 474      *
 475      * @param duration
 476      *            The duration of the {@code TranslateTransition}
 477      */
 478     public TranslateTransition(Duration duration) {
 479         this(duration, null);
 480     }
 481 
 482     /**
 483      * The constructor of {@code TranslateTransition}
 484      */
 485     public TranslateTransition() {
 486         this(DEFAULT_DURATION, null);
 487     }
 488 
 489     /**
 490      * {@inheritDoc}
 491      */
 492     @Override
 493     public void interpolate(double frac) {
 494         if (!Double.isNaN(startX)) {
 495             cachedNode.setTranslateX(startX + frac * deltaX);
 496         }
 497         if (!Double.isNaN(startY)) {
 498             cachedNode.setTranslateY(startY + frac * deltaY);
 499         }
 500         if (!Double.isNaN(startZ)) {
 501             cachedNode.setTranslateZ(startZ + frac * deltaZ);
 502         }
 503     }
 504 
 505     private Node getTargetNode() {
 506         final Node node = getNode();
 507         return (node != null)? node : getParentTargetNode();
 508     }
 509 
 510     @Override
 511     boolean startable(boolean forceSync) {
 512         return super.startable(forceSync)
 513                 && ((getTargetNode() != null) || (!forceSync && (cachedNode != null)));
 514     }
 515 
 516     @Override
 517     void sync(boolean forceSync) {
 518         super.sync(forceSync);
 519         if (forceSync || (cachedNode == null)) {
 520             cachedNode = getTargetNode();
 521 
 522             final double _fromX = getFromX();
 523             final double _fromY = getFromY();
 524             final double _fromZ = getFromZ();
 525 
 526             final double _toX = getToX();
 527             final double _toY = getToY();
 528             final double _toZ = getToZ();
 529 
 530             final double _byX = getByX();
 531             final double _byY = getByY();
 532             final double _byZ = getByZ();
 533 
 534             if (Double.isNaN(_fromX) && Double.isNaN(_toX) && (Math.abs(_byX) < EPSILON)) {
 535                 startX = Double.NaN;
 536             } else {
 537                 startX = (!Double.isNaN(_fromX)) ? _fromX : cachedNode.getTranslateX();
 538                 deltaX = (!Double.isNaN(_toX)) ? _toX - startX : _byX;
 539             }
 540 
 541             if (Double.isNaN(_fromY) && Double.isNaN(_toY) && (Math.abs(_byY) < EPSILON)) {
 542                 startY = Double.NaN;
 543             } else {
 544                 startY = (!Double.isNaN(_fromY)) ? _fromY : cachedNode.getTranslateY();
 545                 deltaY = (!Double.isNaN(_toY)) ? _toY - startY : getByY();
 546             }
 547 
 548             if (Double.isNaN(_fromZ) && Double.isNaN(_toZ) && (Math.abs(_byZ) < EPSILON)) {
 549                 startZ = Double.NaN;
 550             } else {
 551                 startZ = (!Double.isNaN(_fromZ)) ? _fromZ : cachedNode.getTranslateZ();
 552                 deltaZ = (!Double.isNaN(_toZ)) ? _toZ - startZ : getByZ();
 553             }
 554         }
 555     }
 556 
 557 }