001: /*
002: * Copyright (c) 2001-2006 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.animation.animations;
032:
033: import java.awt.Color;
034:
035: import com.jgoodies.animation.AbstractAnimation;
036: import com.jgoodies.animation.AnimationFunction;
037: import com.jgoodies.animation.AnimationFunctions;
038: import com.jgoodies.animation.components.BasicTextLabel;
039:
040: /**
041: * A basic text animation that fades in a text, changes the x and y scaling,
042: * the position, and the space between glyphs.
043: *
044: * @author Karsten Lentzsch
045: * @version $Revision: 1.1 $
046: *
047: * @see com.jgoodies.animation.Animation
048: * @see BasicTextLabel
049: * @see AnimationFunction
050: * @see com.jgoodies.animation.AnimationFunctions
051: */
052: public final class BasicTextAnimation extends AbstractAnimation {
053:
054: private final BasicTextLabel label;
055: private final String text;
056: private final AnimationFunction colorFunction;
057: private final AnimationFunctions.FloatFunction offsetFunction;
058: private final AnimationFunctions.FloatFunction scaleXFunction;
059: private final AnimationFunctions.FloatFunction scaleYFunction;
060: private final AnimationFunctions.FloatFunction spaceFunction;
061:
062: private boolean offsetEnabled = false;
063:
064: // Instance Creation *****************************************************
065:
066: /**
067: * Constructs a text animation, that fades in a text, scales it and
068: * fades it out. Uses the given color and scaling functions.
069: *
070: * @param label the animation target component
071: * @param duration the animation duration
072: * @param text the text to fade in
073: * @param colorFunction the animation function for the color
074: * @param scaleXFunction the animation function for the horizontal scale
075: * @param scaleYFunction the animation function for the vertical scale
076: * @param spaceFunction the animation function for the glyph space
077: */
078: public BasicTextAnimation(BasicTextLabel label, long duration,
079: String text, AnimationFunction colorFunction,
080: AnimationFunction scaleXFunction,
081: AnimationFunction scaleYFunction,
082: AnimationFunction spaceFunction) {
083:
084: super (duration);
085: this .label = label;
086: this .text = text;
087: this .colorFunction = colorFunction != null ? colorFunction
088: : defaultFadeColorFunction(duration, Color.DARK_GRAY);
089: this .scaleXFunction = AnimationFunctions
090: .asFloat(scaleXFunction != null ? scaleXFunction
091: : AnimationFunctions.ONE);
092: this .scaleYFunction = AnimationFunctions
093: .asFloat(scaleYFunction != null ? scaleYFunction
094: : AnimationFunctions.ONE);
095: this .spaceFunction = AnimationFunctions
096: .asFloat(spaceFunction != null ? spaceFunction
097: : AnimationFunctions.ZERO);
098: this .offsetFunction = AnimationFunctions
099: .asFloat(defaultOffsetFunction());
100: }
101:
102: /**
103: * Creates and answers an animation, that provides a text fade-in and -out.
104: *
105: * @param label the animation target component
106: * @param duration the animation duration
107: * @param text the text to fade in
108: * @param baseColor the base color for the fade effect
109: * @return an animation with a default text fade
110: */
111: public static BasicTextAnimation defaultFade(BasicTextLabel label,
112: long duration, String text, Color baseColor) {
113:
114: return new BasicTextAnimation(label, duration, text,
115: cinemaFadeColorFunction(duration, baseColor),
116: //defaultFadeColorFunction(duration, baseColor),
117: null, null, null);
118: }
119:
120: /**
121: * Creates and answers an animation, that provides a text fade-in
122: * and -out and scales the text while fading out.
123: *
124: * @param label the animation target component
125: * @param duration the animation duration
126: * @param text the text to fade in
127: * @param baseColor the base color for the fade effect
128: * @return an animation with a default scaling text effect
129: */
130: public static BasicTextAnimation defaultScale(BasicTextLabel label,
131: long duration, String text, Color baseColor) {
132:
133: return new BasicTextAnimation(label, duration, text,
134: defaultScaleColorFunction(duration, baseColor),
135: defaultScaleFunction(duration),
136: defaultScaleFunction(duration), null);
137: }
138:
139: /**
140: * Creates and answers an animation, that provides a text fade-in
141: * and -out and increases the glyph spacing.
142: *
143: * @param label the animation target component
144: * @param duration the animation duration
145: * @param text the text to fade in
146: * @param baseColor the base color for the fade effect
147: * @return an animation with a default glyph spacing effect
148: */
149: public static BasicTextAnimation defaultSpace(BasicTextLabel label,
150: long duration, String text, Color baseColor) {
151:
152: return new BasicTextAnimation(label, duration, text,
153: defaultSpaceColorFunction(duration, baseColor), null,
154: null, defaultSpaceFunction(duration));
155: }
156:
157: /**
158: * Creates and answers the color animation function for the default fade.
159: *
160: * @param duration the animation duration
161: * @param baseColor the base color for the fade effect
162: * @return a Color-valued animation function for the default fade
163: */
164: public static AnimationFunction defaultFadeColorFunction(
165: long duration, Color baseColor) {
166: return AnimationFunctions.alphaColor(AnimationFunctions.linear(
167: duration, new Integer[] { new Integer(0),
168: new Integer(255), new Integer(255),
169: new Integer(0) }, new float[] { 0.0f, 0.3f,
170: 0.7f, 1.0f }), baseColor);
171: }
172:
173: /**
174: * Creates and answers the color animation function for the default fade.
175: *
176: * @param duration the animation duration
177: * @param baseColor the base color for the fade effect
178: * @return a Color-valued animation function for the default fade
179: */
180: public static AnimationFunction cinemaFadeColorFunction(
181: long duration, Color baseColor) {
182: return AnimationFunctions.alphaColor(AnimationFunctions.linear(
183: duration, new Integer[] { new Integer(0),
184: new Integer(255), new Integer(255),
185: new Integer(0) }, new float[] { 0.0f, 0.3f,
186: 0.85f, 1.0f }), baseColor);
187: }
188:
189: /**
190: * Creates and answers the animation function for the default scaling.
191: *
192: * @param duration the animation duration
193: * @param baseColor the base color for the fade effect
194: * @return a Color-valued animation function for the default scaling
195: */
196: public static AnimationFunction defaultScaleColorFunction(
197: long duration, Color baseColor) {
198: return AnimationFunctions.alphaColor(AnimationFunctions.linear(
199: duration, new Integer[] { new Integer(0),
200: new Integer(255), new Integer(255),
201: new Integer(0) }, new float[] { 0.0f, 0.2f,
202: 0.85f, 1.0f }), baseColor);
203: }
204:
205: /**
206: * Creates and answers the color animation function for
207: * the default spacing animation.
208: *
209: * @param duration the animation duration
210: * @param baseColor the base color for the fade effect
211: * @return a Color-valued animation function for the default spacing
212: */
213: public static AnimationFunction defaultSpaceColorFunction(
214: long duration, Color baseColor) {
215: return AnimationFunctions.alphaColor(AnimationFunctions.linear(
216: duration, new Integer[] { new Integer(0),
217: new Integer(255), new Integer(255),
218: new Integer(0) }, new float[] { 0.0f, 0.2f,
219: 0.8f, 1.0f }), baseColor);
220: }
221:
222: /**
223: * Returns the animation function for the default random position offset.
224: *
225: * @return an animation function for a default random offset
226: */
227: public static AnimationFunction defaultOffsetFunction() {
228: return AnimationFunctions.random(-2, 2, 0.5f);
229: }
230:
231: /**
232: * Creates and answers the default scaling animation function.
233: *
234: * @param duration the animation duration
235: * @return an animation function for the default scaling effect
236: */
237: public static AnimationFunction defaultScaleFunction(long duration) {
238: return AnimationFunctions.linear(duration, new Float[] {
239: new Float(1.0f), new Float(1.0f), new Float(1.8f) },
240: new float[] { 0.0f, 0.85f, 1.0f });
241: }
242:
243: /**
244: * Creates and answers the default spacing animation function.
245: *
246: * @param duration the animation duration
247: * @return an animation function for the default spacing effect
248: */
249: public static AnimationFunction defaultSpaceFunction(long duration) {
250: return AnimationFunctions.fromTo(duration, 0, 10);
251: }
252:
253: // ************************************************************************
254:
255: /**
256: * Applies the effect: sets color, spacing, scaling and offset,
257: * the latter only if enabled.
258: *
259: * @param time the render time
260: */
261: protected void applyEffect(long time) {
262: label.setText(time == 0 ? " " : text);
263: label.setColor((Color) colorFunction.valueAt(time));
264: label.setScaleX(scaleXFunction.valueAt(time));
265: label.setScaleY(scaleYFunction.valueAt(time));
266: label.setSpace(spaceFunction.valueAt(time));
267: if (isOffsetEnabled()) {
268: label.setOffsetX(offsetFunction.valueAt(time));
269: label.setOffsetY(offsetFunction.valueAt(time));
270: }
271: }
272:
273: /**
274: * Answers whether the random position offset is enabled.
275: *
276: * @return true indicates offset enabled, false disabled
277: */
278: public boolean isOffsetEnabled() {
279: return offsetEnabled;
280: }
281:
282: /**
283: * Enables or disables the random position offset.
284: *
285: * @param b the new enablement
286: */
287: public void setOffsetEnabled(boolean b) {
288: offsetEnabled = b;
289: }
290:
291: }
|