Most of the class has been rewritten.
git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3317 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -6,64 +6,91 @@ package com.threerings.media.animation;
|
|||||||
import java.awt.AlphaComposite;
|
import java.awt.AlphaComposite;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.Composite;
|
import java.awt.Composite;
|
||||||
import java.awt.Graphics2D;
|
|
||||||
import java.awt.Image;
|
import java.awt.Image;
|
||||||
|
import java.awt.Graphics2D;
|
||||||
|
import java.awt.Point;
|
||||||
|
import java.awt.Rectangle;
|
||||||
import java.awt.Transparency;
|
import java.awt.Transparency;
|
||||||
|
|
||||||
|
import com.threerings.media.image.Mirage;
|
||||||
import com.threerings.media.sprite.Sprite;
|
import com.threerings.media.sprite.Sprite;
|
||||||
import com.threerings.media.sprite.SpriteManager;
|
import com.threerings.media.sprite.SpriteManager;
|
||||||
import com.threerings.media.util.LinearTimeFunction;
|
import com.threerings.media.util.LinearTimeFunction;
|
||||||
import com.threerings.media.util.TimeFunction;
|
import com.threerings.media.util.TimeFunction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Washes all non-transparent pixels in a sprite with a particular color
|
* Animates an image changing size about its center point.
|
||||||
* (by compositing them with the solid color with progressively higher
|
|
||||||
* alpha values) and then back again.
|
|
||||||
*/
|
*/
|
||||||
public class ScaleAnimation extends Animation
|
public class ScaleAnimation extends Animation
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Creates a gleam animation with the supplied sprite. The sprite will
|
* Creates a scale animation with the supplied image. If the image's
|
||||||
* be faded to the specified color and then back again. The sprite may
|
* size would ever be 0 or less, it is not drawn.
|
||||||
* be already added to the supplied sprite manager or not, but when
|
|
||||||
* the animation is complete, it will have been added.
|
|
||||||
*
|
*
|
||||||
* @param fadeIn if true, the sprite itself will be faded in as we
|
* @param image The image to paint.
|
||||||
* fade up to the gleam color and the gleam color will fade out,
|
*
|
||||||
* leaving just the sprite imagery.
|
* @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,
|
public ScaleAnimation (Mirage image, Point center,
|
||||||
int upmillis, int downmillis, boolean fadeIn)
|
float startScale, float endScale, int duration)
|
||||||
{
|
{
|
||||||
super(sprite.getBounds());
|
super(getBounds(image, center, Math.max(startScale, endScale)));
|
||||||
_spmgr = spmgr;
|
|
||||||
_sprite = sprite;
|
// Save inputted variables
|
||||||
_color = color;
|
_image = image;
|
||||||
_upfunc = new LinearTimeFunction(0, 750, upmillis);
|
_center = new Point(center);
|
||||||
_downfunc = new LinearTimeFunction(750, 0, downmillis);
|
_startScale = startScale;
|
||||||
_fadeIn = fadeIn;
|
_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
|
// documentation inherited
|
||||||
public void tick (long timestamp)
|
public void tick (long tickStamp)
|
||||||
{
|
{
|
||||||
int alpha;
|
// Compute the new scaling value
|
||||||
if (_upfunc != null) {
|
float weight = _scaleFunc.getValue(tickStamp) / 10000.0f;
|
||||||
if ((alpha = _upfunc.getValue(timestamp)) == 750) {
|
float scale = ((1.0f - weight) * _startScale) +
|
||||||
_upfunc = null;
|
(( weight) * _endScale);
|
||||||
}
|
|
||||||
} else if (_downfunc != null) {
|
|
||||||
if ((alpha = _downfunc.getValue(timestamp)) == 0) {
|
|
||||||
_downfunc = null;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
_finished = true;
|
|
||||||
_spmgr.addSprite(_sprite);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_alpha != alpha) {
|
// Update the animation if the scaling changes
|
||||||
_alpha = alpha;
|
if (_scale != scale)
|
||||||
|
{
|
||||||
|
_scale = scale;
|
||||||
invalidate();
|
invalidate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -71,16 +98,14 @@ public class ScaleAnimation extends Animation
|
|||||||
// documentation inherited
|
// documentation inherited
|
||||||
public void fastForward (long timeDelta)
|
public void fastForward (long timeDelta)
|
||||||
{
|
{
|
||||||
if (_upfunc != null) {
|
_scaleFunc.fastForward(timeDelta);
|
||||||
_upfunc.fastForward(timeDelta);
|
|
||||||
} else if (_downfunc != null) {
|
|
||||||
_downfunc.fastForward(timeDelta);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// documentation inherited
|
// documentation inherited
|
||||||
public void paint (Graphics2D gfx)
|
public void paint (Graphics2D gfx)
|
||||||
{
|
{
|
||||||
|
//XXX Write this
|
||||||
|
/*
|
||||||
// TODO: recreate our off image if the sprite bounds changed; we
|
// TODO: recreate our off image if the sprite bounds changed; we
|
||||||
// also need to change the bounds of our animation which might
|
// also need to change the bounds of our animation which might
|
||||||
// require some jockeying (especially if we shrink)
|
// require some jockeying (especially if we shrink)
|
||||||
@@ -124,27 +149,28 @@ public class ScaleAnimation extends Animation
|
|||||||
// now alpha composite our mask atop the sprite
|
// now alpha composite our mask atop the sprite
|
||||||
gfx.drawImage(_offimg, _sprite.getX(), _sprite.getY(), null);
|
gfx.drawImage(_offimg, _sprite.getX(), _sprite.getY(), null);
|
||||||
gfx.setComposite(ocomp);
|
gfx.setComposite(ocomp);
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
// documentation inherited
|
/** The image to scale. */
|
||||||
protected void willStart (long tickStamp)
|
protected Mirage _image;
|
||||||
{
|
|
||||||
super.willStart(tickStamp);
|
|
||||||
|
|
||||||
// remove the sprite we're fiddling with from the manager; we'll
|
/** The center pixel to render the image around. */
|
||||||
// add it back when we're done
|
protected Point _center;
|
||||||
if (_spmgr.isManaged(_sprite)) {
|
|
||||||
_spmgr.removeSprite(_sprite);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected SpriteManager _spmgr;
|
/** The amount of time the animation should last. */
|
||||||
protected Sprite _sprite;
|
//XXX Is this needed?
|
||||||
protected Color _color;
|
protected long _duration;
|
||||||
protected Image _offimg;
|
|
||||||
protected boolean _fadeIn;
|
|
||||||
|
|
||||||
protected TimeFunction _upfunc;
|
/** The amount to scale the image at the start of the animation. */
|
||||||
protected TimeFunction _downfunc;
|
protected float _startScale;
|
||||||
protected int _alpha = -1;
|
|
||||||
|
/** 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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user