diff --git a/src/java/com/threerings/media/animation/Animation.java b/src/java/com/threerings/media/animation/Animation.java index 0228f5eb8..6598a04bb 100644 --- a/src/java/com/threerings/media/animation/Animation.java +++ b/src/java/com/threerings/media/animation/Animation.java @@ -1,5 +1,5 @@ // -// $Id: Animation.java,v 1.15 2004/11/11 23:51:40 mdb Exp $ +// $Id$ // // Narya library - tools for developing networked games // Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved diff --git a/src/java/com/threerings/media/animation/FadeAnimation.java b/src/java/com/threerings/media/animation/FadeAnimation.java index 7aadd69f4..424c593a8 100644 --- a/src/java/com/threerings/media/animation/FadeAnimation.java +++ b/src/java/com/threerings/media/animation/FadeAnimation.java @@ -27,6 +27,7 @@ import java.awt.Graphics2D; import java.awt.Rectangle; import com.threerings.media.Log; +import com.threerings.media.effects.FadeEffect; /** * An animation that displays an image fading from one alpha level to @@ -38,7 +39,6 @@ public abstract class FadeAnimation extends Animation /** * Constructs a fade animation. * - * @param image the image to animate. * @param bounds our bounds. * @param alpha the starting alpha. * @param step the alpha amount to step by each millisecond. @@ -48,49 +48,30 @@ public abstract class FadeAnimation extends Animation Rectangle bounds, float alpha, float step, float target) { super(bounds); - - // save things off - _startAlpha = _alpha = Math.max(alpha, 0.0f); - _step = step; - _target = Math.min(target, 1.0f); - - // create the initial composite - _comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, _alpha); + _effect = new FadeEffect(alpha, step, target); } // documentation inherited public void tick (long timestamp) { - // figure out the current alpha - long msecs = timestamp - _firstTick; - _alpha = _startAlpha + (msecs * _step); - if (_alpha < 0.0f) { - _alpha = 0.0f; - } else if (_alpha > 1.0f) { - _alpha = 1.0f; - } - - _comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, _alpha); - - // check whether we're done - _finished = ((_startAlpha < _target) ? (_alpha >= _target) : - (_alpha <= _target)); - - // dirty ourselves + _effect.tick(timestamp); + _finished = _effect.finished(); invalidate(); } // documentation inherited public void paint (Graphics2D gfx) { - Composite ocomp = gfx.getComposite(); - if (_comp == null) { - Log.warning("Fade anim has null composite [anim=" + this + "]."); - } else { - gfx.setComposite(_comp); - } + _effect.beforePaint(gfx); paintAnimation(gfx); - gfx.setComposite(ocomp); + _effect.afterPaint(gfx); + } + + // documentation inherited + protected void willStart (long tickStamp) + { + super.willStart(tickStamp); + _effect.init(tickStamp); } /** @@ -98,29 +79,6 @@ public abstract class FadeAnimation extends Animation */ protected abstract void paintAnimation (Graphics2D gfx); - // documentation inherited - protected void toString (StringBuffer buf) - { - super.toString(buf); - - buf.append(", alpha=").append(_alpha); - buf.append(", startAlpha=").append(_startAlpha); - buf.append(", step=").append(_step); - buf.append(", target=").append(_target); - } - - /** The composite used to render the image with the current alpha. */ - protected Composite _comp; - - /** The current alpha of the image. */ - protected float _alpha; - - /** The target alpha. */ - protected float _target; - - /** The alpha step per millisecond. */ - protected float _step; - - /** The starting alpha. */ - protected float _startAlpha; + /** This handles the main work of fading. */ + protected FadeEffect _effect; } diff --git a/src/java/com/threerings/media/effects/FadeEffect.java b/src/java/com/threerings/media/effects/FadeEffect.java new file mode 100644 index 000000000..9da8f4819 --- /dev/null +++ b/src/java/com/threerings/media/effects/FadeEffect.java @@ -0,0 +1,100 @@ +// +// $Id$ +// +// Narya library - tools for developing networked games +// Copyright (C) 2002-2005 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/narya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.media.effects; + +import java.awt.AlphaComposite; +import java.awt.Composite; +import java.awt.Graphics2D; + +/** + * Handles the math and timing of doing a fade effect in a sprite or animation. + */ +public class FadeEffect +{ + public FadeEffect (float alpha, float step, float target) + { + // save things off + _startAlpha = _alpha = Math.max(alpha, 0.0f); + _step = step; + _target = Math.min(target, 1.0f); + + // create the initial composite + _comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, _alpha); + } + + public void init (long tickStamp) + { + _initStamp = tickStamp; + } + + public boolean finished () + { + return (_startAlpha < _target) ? + (_alpha >= _target) : (_alpha <= _target); + } + + public void tick (long tickStamp) + { + // figure out the current alpha + long msecs = tickStamp - _initStamp; + _alpha = _startAlpha + (msecs * _step); + if (_alpha < 0.0f) { + _alpha = 0.0f; + } else if (_alpha > 1.0f) { + _alpha = 1.0f; + } + + _comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, _alpha); + } + + public void beforePaint (Graphics2D gfx) + { + _ocomp = gfx.getComposite(); + gfx.setComposite(_comp); + } + + public void afterPaint (Graphics2D gfx) + { + gfx.setComposite(_ocomp); + } + + /** The composite used to render the image with the current alpha. */ + protected Composite _comp; + + /** The composite in effect outside our faded render. */ + protected Composite _ocomp; + + /** The current alpha of the image. */ + protected float _alpha; + + /** The target alpha. */ + protected float _target; + + /** The alpha step per millisecond. */ + protected float _step; + + /** The starting alpha. */ + protected float _startAlpha; + + /** Time zero. */ + protected long _initStamp; +}