1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
/**
* Copyright (c) 2006, Sun Microsystems, Inc
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the TimingFramework project nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.jdesktop.animation.timing.triggers;
import org.jdesktop.animation.timing.Animator;
/**
* This abstract class should be overridden by any class wanting to
* implement a new Trigger. The subclass will define the events to trigger
* off of and any listeners to handle those events. That subclass will call
* either {@link #fire()} or {@link #fire(TriggerEvent)} to start the
* animator based on an event that occurred.
* <p>
* Subclasses should call one of the constructors in Trigger, according to
* whether they want Trigger to discern between different TriggerEvents
* and whether they want Trigger to auto-reverse the animation based on
* opposite TriggerEvents.
* <p>
* Subclasses should call one of the <code>fire</code> methods based on
* whether they want Trigger to perform any event logic or simply start
* the animation.
*
* @author Chet
*/
public abstract class Trigger {
private boolean disarmed = false;
private Animator animator, reverseAnimator;
private TriggerEvent triggerEvent;
private boolean autoReverse = false;
/**
* Creates a Trigger that will start the animator when {@link #fire()}
* is called. Subclasses call this method to set up a simple Trigger
* that will be started by calling {@link #fire()}, and will have
* no dependency upon the specific {@link TriggerEvent} that must have
* occurred to start the animator.
* @param animator the Animator that will start when the Trigger
* is fired
*/
protected Trigger(Animator animator) {
this(animator, null);
}
/**
* Creates a Trigger that will start the animator when
* {@link #fire(TriggerEvent)} is called with an event that equals
* triggerEvent.
* @param animator the Animator that will start when the Trigger
* is fired
* @param triggerEvent the TriggerEvent that must occur for this
* Trigger to fire
*/
protected Trigger(Animator animator, TriggerEvent triggerEvent) {
this(animator, triggerEvent, false);
}
/**
* Creates a Trigger that will start the animator when
* {@link #fire(TriggerEvent)} is called with an event that equals
* triggerEvent. Also, automatically stops and reverses animator when
* opposite event occurs, and stops reversing animator likewise
* when triggerEvent occurs.
* @param animator the Animator that will start when the Trigger
* is fired
* @param triggerEvent the TriggerEvent that must occur for this
* Trigger to fire
* @param autoReverse flag to determine whether the animator should
* stop and reverse based on opposite triggerEvents.
* @see TriggerEvent#getOppositeEvent()
*/
protected Trigger(Animator animator, TriggerEvent triggerEvent,
boolean autoReverse) {
this.animator = animator;
this.triggerEvent = triggerEvent;
this.autoReverse = autoReverse;
}
/**
* This method disables this Trigger and effectively noop's any actions
* that would otherwise occur
*/
public void disarm() {
disarmed = true;
}
/**
* Called by subclasses to start the animator if currentEvent equals
* the event that the Trigger is based upon. Also, if the Trigger is
* set to autoReverse, stops and reverses the animator running in the
* opposite direction as appropriate.
* @param currentEvent the {@link TriggerEvent} that just occurred, which
* will be compared with the TriggerEvent used to construct this Trigger
* and determine whether the animator should be started or reversed
*/
protected void fire(TriggerEvent currentEvent) {
if (disarmed) {
return;
}
if (currentEvent == triggerEvent) {
// event occurred; fire the animation
if (autoReverse) {
if (animator.isRunning()) {
float f = animator.getTimingFraction();
animator.stop();
animator.setStartFraction(f);
} else {
animator.setStartFraction(0f);
}
}
if (animator.isRunning()) {
animator.stop();
}
animator.setStartDirection(Animator.Direction.FORWARD);
fire();
} else if (triggerEvent != null &&
currentEvent == triggerEvent.getOppositeEvent()) {
// Opposite event occurred - run reverse anim if autoReverse
if (autoReverse) {
if (animator.isRunning()) {
float f = animator.getTimingFraction();
animator.stop();
animator.setStartFraction(f);
} else {
animator.setStartFraction(1f -
animator.getStartFraction());
}
animator.setStartDirection(Animator.Direction.BACKWARD);
fire();
}
}
}
/**
* Utility method called by subclasses to start the animator. This variant
* assumes that there need be no check of the TriggerEvent that fired,
* which is useful for subclasses with simple events.
*/
protected void fire() {
if (disarmed) {
return;
}
if (animator.isRunning()) {
animator.stop();
}
animator.start();
}
}
|