From 4033d0811315553e2d4233f9c28b29bb791c9b30 Mon Sep 17 00:00:00 2001 From: Ted V Date: Thu, 27 Jan 2005 21:17:59 +0000 Subject: [PATCH] Most of the class has been rewritten. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3317 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../media/animation/ScaleAnimation.java | 146 +++++++++++------- 1 file changed, 86 insertions(+), 60 deletions(-) diff --git a/src/java/com/threerings/media/animation/ScaleAnimation.java b/src/java/com/threerings/media/animation/ScaleAnimation.java index 04a071c40..35879b7a5 100644 --- a/src/java/com/threerings/media/animation/ScaleAnimation.java +++ b/src/java/com/threerings/media/animation/ScaleAnimation.java @@ -6,64 +6,91 @@ package com.threerings.media.animation; import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Composite; -import java.awt.Graphics2D; import java.awt.Image; +import java.awt.Graphics2D; +import java.awt.Point; +import java.awt.Rectangle; import java.awt.Transparency; +import com.threerings.media.image.Mirage; import com.threerings.media.sprite.Sprite; import com.threerings.media.sprite.SpriteManager; import com.threerings.media.util.LinearTimeFunction; import com.threerings.media.util.TimeFunction; /** - * Washes all non-transparent pixels in a sprite with a particular color - * (by compositing them with the solid color with progressively higher - * alpha values) and then back again. + * Animates an image changing size about its center point. */ public class ScaleAnimation extends Animation { /** - * Creates a gleam animation with the supplied sprite. The sprite will - * be faded to the specified color and then back again. The sprite may - * be already added to the supplied sprite manager or not, but when - * the animation is complete, it will have been added. + * Creates a scale animation with the supplied image. If the image's + * size would ever be 0 or less, it is not drawn. * - * @param fadeIn if true, the sprite itself will be faded in as we - * fade up to the gleam color and the gleam color will fade out, - * leaving just the sprite imagery. + * @param image The image to paint. + * + * @param center The screen coordinates of the pixel upon which the + * image's center should always be rendered. + * + * @param startScale The amount to scale the image when it is rendered + * at time 0. + * + * @param endScale The amount to scale the image at the final frame + * of animation. + * + * @param duration The time in milliseconds the anim takes to complete. */ - public ScaleAnimation (SpriteManager spmgr, Sprite sprite, Color color, - int upmillis, int downmillis, boolean fadeIn) + public ScaleAnimation (Mirage image, Point center, + float startScale, float endScale, int duration) { - super(sprite.getBounds()); - _spmgr = spmgr; - _sprite = sprite; - _color = color; - _upfunc = new LinearTimeFunction(0, 750, upmillis); - _downfunc = new LinearTimeFunction(750, 0, downmillis); - _fadeIn = fadeIn; + super(getBounds(image, center, Math.max(startScale, endScale))); + + // Save inputted variables + _image = image; + _center = new Point(center); + _startScale = startScale; + _endScale = endScale; + _duration = duration; + + // Hack the LinearTimeFunction to use fixed point rationals + // + // FIXME: This class doesn't seem to be saving me a lot of + // work, since I have to repackage the outputs into floats + // anyway. Find some way to make the LinearTimeFunction do + // more of this work for us, or write a new class that does. + // Maybe IntLinearTimeFunction and FloatLinearTimeFunction + // classes would be useful. + _scaleFunc = new LinearTimeFunction(0, 10000, duration); + } + + /** + * Java wants the first call in a constructor to be super() + * if it exists at all, so we have to trick it with this function. + * + * Oh, and this function computes how big the bounding box needs + * to be. + */ + public static Rectangle getBounds (Mirage image, Point center, float scale) + { + int width = (int) (image.getWidth() * scale); + int height = (int) (image.getWidth() * scale); + + return new Rectangle(center.x - width/2, center.y - height/2, + width, height); } // documentation inherited - public void tick (long timestamp) + public void tick (long tickStamp) { - int alpha; - if (_upfunc != null) { - if ((alpha = _upfunc.getValue(timestamp)) == 750) { - _upfunc = null; - } - } else if (_downfunc != null) { - if ((alpha = _downfunc.getValue(timestamp)) == 0) { - _downfunc = null; - } - } else { - _finished = true; - _spmgr.addSprite(_sprite); - return; - } + // Compute the new scaling value + float weight = _scaleFunc.getValue(tickStamp) / 10000.0f; + float scale = ((1.0f - weight) * _startScale) + + (( weight) * _endScale); - if (_alpha != alpha) { - _alpha = alpha; + // Update the animation if the scaling changes + if (_scale != scale) + { + _scale = scale; invalidate(); } } @@ -71,16 +98,14 @@ public class ScaleAnimation extends Animation // documentation inherited public void fastForward (long timeDelta) { - if (_upfunc != null) { - _upfunc.fastForward(timeDelta); - } else if (_downfunc != null) { - _downfunc.fastForward(timeDelta); - } + _scaleFunc.fastForward(timeDelta); } // documentation inherited public void paint (Graphics2D gfx) { + //XXX Write this + /* // TODO: recreate our off image if the sprite bounds changed; we // also need to change the bounds of our animation which might // require some jockeying (especially if we shrink) @@ -124,27 +149,28 @@ public class ScaleAnimation extends Animation // now alpha composite our mask atop the sprite gfx.drawImage(_offimg, _sprite.getX(), _sprite.getY(), null); gfx.setComposite(ocomp); + */ } - // documentation inherited - protected void willStart (long tickStamp) - { - super.willStart(tickStamp); + /** The image to scale. */ + protected Mirage _image; - // remove the sprite we're fiddling with from the manager; we'll - // add it back when we're done - if (_spmgr.isManaged(_sprite)) { - _spmgr.removeSprite(_sprite); - } - } + /** The center pixel to render the image around. */ + protected Point _center; - protected SpriteManager _spmgr; - protected Sprite _sprite; - protected Color _color; - protected Image _offimg; - protected boolean _fadeIn; + /** The amount of time the animation should last. */ + //XXX Is this needed? + protected long _duration; - protected TimeFunction _upfunc; - protected TimeFunction _downfunc; - protected int _alpha = -1; + /** The amount to scale the image at the start of the animation. */ + protected float _startScale; + + /** The amount to scale the image at the end of the animation. */ + protected float _endScale; + + /** The current amount of scaling to render. */ + protected float _scale; + + /** Computes the image scaling to use at the specified time. */ + protected TimeFunction _scaleFunc; }