Spec-Zone .ru
спецификации, руководства, описания, API
|
001/* 002 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. Oracle designates this 008 * particular file as subject to the "Classpath" exception as provided 009 * by Oracle in the LICENSE file that accompanied this code. 010 * 011 * This code is distributed in the hope that it will be useful, but WITHOUT 012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 014 * version 2 for more details (a copy is included in the LICENSE file that 015 * accompanied this code). 016 * 017 * You should have received a copy of the GNU General Public License version 018 * 2 along with this work; if not, write to the Free Software Foundation, 019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 020 * 021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 022 * or visit www.oracle.com if you need additional information or have any 023 * questions. 024 */ 025 026package javafx.animation; 027 028import javafx.beans.property.DoubleProperty; 029import javafx.beans.property.ObjectProperty; 030import javafx.beans.property.ObjectPropertyBase; 031import javafx.beans.property.SimpleDoubleProperty; 032import javafx.beans.property.SimpleObjectProperty; 033import javafx.scene.Node; 034import javafx.util.Duration; 035 036/** 037 * This {@code Transition} creates a scale animation that spans its 038 * {@link #duration}. This is done by updating the {@code scaleX}, 039 * {@code scaleY} and {@code scaleZ} variables of the {@code node} at regular 040 * interval. 041 * <p> 042 * It starts from the ({@code fromX}, {@code fromY}, {@code fromZ}) value if 043 * provided else uses the {@code node}'s ({@code scaleX}, {@code scaleY}, 044 * {@code scaleZ}) value. 045 * <p> 046 * It stops at the ({@code toX}, {@code toY}, {@code toZ}) value if provided 047 * else it will use start value plus ({@code byX}, {@code byY}, {@code byZ}) 048 * value. 049 * <p> 050 * The ({@code toX}, {@code toY}, {@code toZ}) value takes precedence if both ( 051 * {@code toX}, {@code toY}, {@code toZ}) and ({@code byX}, {@code byY}, 052 * {@code byZ}) values are specified. 053 * 054 * <p> 055 * Code Segment Example: 056 * </p> 057 * 058 * <pre> 059 * <code> 060 * import javafx.scene.shape.*; 061 * import javafx.animation.transition.*; 062 * 063 * ... 064 * 065 * Rectangle rect = new Rectangle (100, 40, 100, 100); 066 * rect.setArcHeight(50); 067 * rect.setArcWidth(50); 068 * rect.setFill(Color.VIOLET); 069 * 070 * ScaleTransition st = new ScaleTransition(Duration.millis(2000), rect); 071 * st.setByX(1.5f); 072 * st.setByY(1.5f); 073 * st.setCycleCount(4f); 074 * st.setAutoReverse(true); 075 * 076 * st.play(); 077 * 078 * ... 079 * 080 * </code> 081 * </pre> 082 * 083 * @see Transition 084 * @see Animation 085 * 086 */ 087public final class ScaleTransition extends Transition { 088 089 private static final double EPSILON = 1e-12; 090 private double startX; 091 private double startY; 092 private double startZ; 093 private double deltaX; 094 private double deltaY; 095 private double deltaZ; 096 097 /** 098 * The target node of this {@code ScaleTransition}. 099 * <p> 100 * It is not possible to change the target {@code node} of a running 101 * {@code ScaleTransition}. If the value of {@code node} is changed for a 102 * running {@code ScaleTransition}, the animation has to be stopped and 103 * started again to pick up the new value. 104 */ 105 private ObjectProperty<Node> node; 106 private static final Node DEFAULT_NODE = null; 107 108 public final void setNode(Node value) { 109 if ((node != null) || (value != null /* DEFAULT_NODE */)) { 110 nodeProperty().set(value); 111 } 112 } 113 114 public final Node getNode() { 115 return (node == null)? DEFAULT_NODE : node.get(); 116 } 117 118 public final ObjectProperty<Node> nodeProperty() { 119 if (node == null) { 120 node = new SimpleObjectProperty<Node>(this, "node", DEFAULT_NODE); 121 } 122 return node; 123 } 124 125 private Node cachedNode; 126 127 /** 128 * The duration of this {@code ScaleTransition}. 129 * <p> 130 * It is not possible to change the {@code duration} of a running 131 * {@code ScaleTransition}. If the value of {@code duration} is changed for 132 * a running {@code ScaleTransition}, the animation has to be stopped and 133 * started again to pick up the new value. 134 * <p> 135 * Note: While the unit of {@code duration} is a millisecond, the 136 * granularity depends on the underlying operating system and will in 137 * general be larger. For example animations on desktop systems usually run 138 * with a maximum of 60fps which gives a granularity of ~17 ms. 139 * 140 * Setting duration to value lower than {@link Duration#ZERO} will result 141 * in {@link IllegalArgumentException}. 142 * 143 * @defaultValue 400ms 144 */ 145 private ObjectProperty<Duration> duration; 146 private static final Duration DEFAULT_DURATION = Duration.millis(400); 147 148 public final void setDuration(Duration value) { 149 if ((duration != null) || (!DEFAULT_DURATION.equals(value))) { 150 durationProperty().set(value); 151 } 152 } 153 154 public final Duration getDuration() { 155 return (duration == null)? DEFAULT_DURATION : duration.get(); 156 } 157 158 public final ObjectProperty<Duration> durationProperty() { 159 if (duration == null) { 160 duration = new ObjectPropertyBase<Duration>(DEFAULT_DURATION) { 161 162 @Override 163 public void invalidated() { 164 try { 165 setCycleDuration(getDuration()); 166 } catch (IllegalArgumentException e) { 167 if (isBound()) { 168 unbind(); 169 } 170 set(getCycleDuration()); 171 throw e; 172 } 173 } 174 175 @Override 176 public Object getBean() { 177 return ScaleTransition.this; 178 } 179 180 @Override 181 public String getName() { 182 return "duration"; 183 } 184 }; 185 } 186 return duration; 187 } 188 189 /** 190 * Specifies the start X scale value of this {@code ScaleTransition}. 191 * <p> 192 * It is not possible to change {@code fromX} of a running 193 * {@code ScaleTransition}. If the value of {@code fromX} is changed for a 194 * running {@code ScaleTransition}, the animation has to be stopped and 195 * 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 scale value of this {@code ScaleTransition}. 221 * <p> 222 * It is not possible to change {@code fromY} of a running 223 * {@code ScaleTransition}. If the value of {@code fromY} is changed for a 224 * running {@code ScaleTransition}, the animation has to be stopped and 225 * started again to pick up the new value. 226 * 227 * @defaultValue {@code Double.NaN} 228 */ 229 private DoubleProperty fromY; 230 private static final double DEFAULT_FROM_Y = Double.NaN; 231 232 public final void setFromY(double value) { 233 if ((fromY != null) || (!Double.isNaN(value))) { 234 fromYProperty().set(value); 235 } 236 } 237 238 public final double getFromY() { 239 return (fromY == null)? DEFAULT_FROM_Y : fromY.get(); 240 } 241 242 public final DoubleProperty fromYProperty() { 243 if (fromY == null) { 244 fromY = new SimpleDoubleProperty(this, "fromY", DEFAULT_FROM_Y); 245 } 246 return fromY; 247 } 248 249 /** 250 * Specifies the start Z scale value of this {@code ScaleTransition}. 251 * <p> 252 * It is not possible to change {@code fromZ} of a running 253 * {@code ScaleTransition}. If the value of {@code fromZ} is changed for a 254 * running {@code ScaleTransition}, the animation has to be stopped and 255 * started again to pick up the new value. 256 * 257 * @defaultValue {@code Double.NaN} 258 */ 259 private DoubleProperty fromZ; 260 private static final double DEFAULT_FROM_Z = Double.NaN; 261 262 public final void setFromZ(double value) { 263 if ((fromZ != null) || (!Double.isNaN(value))) { 264 fromZProperty().set(value); 265 } 266 } 267 268 public final double getFromZ() { 269 return (fromZ == null)? DEFAULT_FROM_Z : fromZ.get(); 270 } 271 272 public final DoubleProperty fromZProperty() { 273 if (fromZ == null) { 274 fromZ = new SimpleDoubleProperty(this, "fromZ", DEFAULT_FROM_Z); 275 } 276 return fromZ; 277 } 278 279 /** 280 * Specifies the stop X scale value of this {@code ScaleTransition}. 281 * <p> 282 * It is not possible to change {@code toX} of a running 283 * {@code ScaleTransition}. If the value of {@code toX} is changed for a 284 * running {@code ScaleTransition}, the animation has to be stopped and 285 * started again to pick up the new value. 286 * 287 * @defaultValue {@code Double.NaN} 288 */ 289 private DoubleProperty toX; 290 private static final double DEFAULT_TO_X = Double.NaN; 291 292 public final void setToX(double value) { 293 if ((toX != null) || (!Double.isNaN(value))) { 294 toXProperty().set(value); 295 } 296 } 297 298 public final double getToX() { 299 return (toX == null)? DEFAULT_TO_X : toX.get(); 300 } 301 302 public final DoubleProperty toXProperty() { 303 if (toX == null) { 304 toX = new SimpleDoubleProperty(this, "toX", DEFAULT_TO_X); 305 } 306 return toX; 307 } 308 309 /** 310 * The stop Y scale value of this {@code ScaleTransition}. 311 * <p> 312 * It is not possible to change {@code toY} of a running 313 * {@code ScaleTransition}. If the value of {@code toY} is changed for a 314 * running {@code ScaleTransition}, the animation has to be stopped and 315 * started again to pick up the new value. 316 * 317 * @defaultValue {@code Double.NaN} 318 */ 319 private DoubleProperty toY; 320 private static final double DEFAULT_TO_Y = Double.NaN; 321 322 public final void setToY(double value) { 323 if ((toY != null) || (!Double.isNaN(value))) { 324 toYProperty().set(value); 325 } 326 } 327 328 public final double getToY() { 329 return (toY == null)? DEFAULT_TO_Y : toY.get(); 330 } 331 332 public final DoubleProperty toYProperty() { 333 if (toY == null) { 334 toY = new SimpleDoubleProperty(this, "toY", DEFAULT_TO_Y); 335 } 336 return toY; 337 } 338 339 /** 340 * The stop Z scale value of this {@code ScaleTransition}. 341 * <p> 342 * It is not possible to change {@code toZ} of a running 343 * {@code ScaleTransition}. If the value of {@code toZ} is changed for a 344 * running {@code ScaleTransition}, the animation has to be stopped and 345 * started again to pick up the new value. 346 * 347 * @defaultValue {@code Double.NaN} 348 */ 349 private DoubleProperty toZ; 350 private static final double DEFAULT_TO_Z = Double.NaN; 351 352 public final void setToZ(double value) { 353 if ((toZ != null) || (!Double.isNaN(value))) { 354 toZProperty().set(value); 355 } 356 } 357 358 public final double getToZ() { 359 return (toZ == null)? DEFAULT_TO_Z : toZ.get(); 360 } 361 362 public final DoubleProperty toZProperty() { 363 if (toZ == null) { 364 toZ = new SimpleDoubleProperty(this, "toZ", DEFAULT_TO_Z); 365 } 366 return toZ; 367 } 368 369 /** 370 * Specifies the incremented stop X scale value, from the start, of this 371 * {@code ScaleTransition}. 372 * <p> 373 * It is not possible to change {@code byX} of a running 374 * {@code ScaleTransition}. If the value of {@code byX} is changed for a 375 * running {@code ScaleTransition}, the animation has to be stopped and 376 * started again to pick up the new value. 377 */ 378 private DoubleProperty byX; 379 private static final double DEFAULT_BY_X = 0.0; 380 381 public final void setByX(double value) { 382 if ((byX != null) || (Math.abs(value - DEFAULT_BY_X) > EPSILON)) { 383 byXProperty().set(value); 384 } 385 } 386 387 public final double getByX() { 388 return (byX == null)? DEFAULT_BY_X : byX.get(); 389 } 390 391 public final DoubleProperty byXProperty() { 392 if (byX == null) { 393 byX = new SimpleDoubleProperty(this, "byX", DEFAULT_BY_X); 394 } 395 return byX; 396 } 397 398 /** 399 * Specifies the incremented stop Y scale value, from the start, of this 400 * {@code ScaleTransition}. 401 * <p> 402 * It is not possible to change {@code byY} of a running 403 * {@code ScaleTransition}. If the value of {@code byY} is changed for a 404 * running {@code ScaleTransition}, the animation has to be stopped and 405 * started again to pick up the new value. 406 */ 407 private DoubleProperty byY; 408 private static final double DEFAULT_BY_Y = 0.0; 409 410 public final void setByY(double value) { 411 if ((byY != null) || (Math.abs(value - DEFAULT_BY_Y) > EPSILON)) { 412 byYProperty().set(value); 413 } 414 } 415 416 public final double getByY() { 417 return (byY == null)? DEFAULT_BY_Y : byY.get(); 418 } 419 420 public final DoubleProperty byYProperty() { 421 if (byY == null) { 422 byY = new SimpleDoubleProperty(this, "byY", DEFAULT_BY_Y); 423 } 424 return byY; 425 } 426 427 /** 428 * Specifies the incremented stop Z scale value, from the start, of this 429 * {@code ScaleTransition}. 430 * <p> 431 * It is not possible to change {@code byZ} of a running 432 * {@code ScaleTransition}. If the value of {@code byZ} is changed for a 433 * running {@code ScaleTransition}, the animation has to be stopped and 434 * started again to pick up the new value. 435 */ 436 private DoubleProperty byZ; 437 private static final double DEFAULT_BY_Z = 0.0; 438 439 public final void setByZ(double value) { 440 if ((byZ != null) || (Math.abs(value - DEFAULT_BY_Z) > EPSILON)) { 441 byZProperty().set(value); 442 } 443 } 444 445 public final double getByZ() { 446 return (byZ == null)? DEFAULT_BY_Z : byZ.get(); 447 } 448 449 public final DoubleProperty byZProperty() { 450 if (byZ == null) { 451 byZ = new SimpleDoubleProperty(this, "byZ", DEFAULT_BY_Z); 452 } 453 return byZ; 454 } 455 456 /** 457 * The constructor of {@code ScaleTransition} 458 * 459 * @param duration 460 * The duration of the {@code ScaleTransition} 461 * @param node 462 * The {@code node} which will be scaled 463 */ 464 public ScaleTransition(Duration duration, Node node) { 465 setDuration(duration); 466 setNode(node); 467 setCycleDuration(duration); 468 } 469 470 /** 471 * The constructor of {@code ScaleTransition} 472 * 473 * @param duration 474 * The duration of the {@code ScaleTransition} 475 */ 476 public ScaleTransition(Duration duration) { 477 this(duration, null); 478 } 479 480 /** 481 * The constructor of {@code ScaleTransition} 482 */ 483 public ScaleTransition() { 484 this(DEFAULT_DURATION, null); 485 } 486 487 /** 488 * {@inheritDoc} 489 */ 490 @Override 491 public void interpolate(double frac) { 492 if (!Double.isNaN(startX)) { 493 cachedNode.setScaleX(startX + frac * deltaX); 494 } 495 if (!Double.isNaN(startY)) { 496 cachedNode.setScaleY(startY + frac * deltaY); 497 } 498 if (!Double.isNaN(startZ)) { 499 cachedNode.setScaleZ(startZ + frac * deltaZ); 500 } 501 } 502 503 private Node getTargetNode() { 504 final Node node = getNode(); 505 return (node != null) ? node : getParentTargetNode(); 506 } 507 508 @Override 509 boolean impl_startable(boolean forceSync) { 510 return super.impl_startable(forceSync) 511 && ((getTargetNode() != null) || (!forceSync && (cachedNode != null))); 512 } 513 514 @Override 515 void impl_sync(boolean forceSync) { 516 super.impl_sync(forceSync); 517 if (forceSync || (cachedNode == null)) { 518 cachedNode = getTargetNode(); 519 520 final double _fromX = getFromX(); 521 final double _fromY = getFromY(); 522 final double _fromZ = getFromZ(); 523 524 final double _toX = getToX(); 525 final double _toY = getToY(); 526 final double _toZ = getToZ(); 527 528 final double _byX = getByX(); 529 final double _byY = getByY(); 530 final double _byZ = getByZ(); 531 532 if (Double.isNaN(_fromX) && Double.isNaN(_toX) && (Math.abs(_byX) < EPSILON)) { 533 startX = Double.NaN; 534 } else { 535 startX = (!Double.isNaN(_fromX)) ? _fromX : cachedNode.getScaleX(); 536 deltaX = (!Double.isNaN(_toX)) ? _toX - startX : getByX(); 537 } 538 539 if (Double.isNaN(_fromY) && Double.isNaN(_toY) && (Math.abs(_byY) < EPSILON)) { 540 startY = Double.NaN; 541 } else { 542 startY = (!Double.isNaN(_fromY)) ? _fromY : cachedNode.getScaleY(); 543 deltaY = (!Double.isNaN(_toY)) ? _toY - startY : getByY(); 544 } 545 546 if (Double.isNaN(_fromZ) && Double.isNaN(_toZ) && (Math.abs(_byZ) < EPSILON)) { 547 startZ = Double.NaN; 548 } else { 549 startZ = (!Double.isNaN(_fromZ)) ? _fromZ : cachedNode.getScaleZ(); 550 deltaZ = (!Double.isNaN(_toZ)) ? _toZ - startZ : getByZ(); 551 } 552 } 553 } 554 555}