Factored FadeAnimation into something that can be extended to fade

different things in. Created FadeImage and FadeLabel derivations.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3425 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2005-03-24 04:18:24 +00:00
parent 6e947f0238
commit cd729458f4
3 changed files with 166 additions and 39 deletions
@@ -1,5 +1,5 @@
//
// $Id: FadeAnimation.java,v 1.8 2004/08/27 02:12:38 mdb Exp $
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
@@ -27,37 +27,32 @@ import java.awt.Graphics2D;
import java.awt.Rectangle;
import com.threerings.media.Log;
import com.threerings.media.image.Mirage;
/**
* An animation that displays an image fading from one alpha level to
* another in specified increments over time. The animation is finished
* when the specified target alpha is reached.
*/
public class FadeAnimation extends Animation
public abstract class FadeAnimation extends Animation
{
/**
* Constructs a fade animation.
*
* @param image the image to animate.
* @param x the image x-position.
* @param y the image y-position.
* @param bounds our bounds.
* @param alpha the starting alpha.
* @param step the alpha amount to step by each millisecond.
* @param target the target alpha level.
*/
public FadeAnimation (
Mirage image, int x, int y, float alpha, float step, float target)
protected FadeAnimation (
Rectangle bounds, float alpha, float step, float target)
{
super(new Rectangle(x, y, image.getWidth(), image.getHeight()));
super(bounds);
// save things off
_image = image;
_x = x;
_y = y;
_startAlpha = _alpha = alpha;
_startAlpha = _alpha = Math.max(alpha, 0.0f);
_step = step;
_target = target;
_target = Math.min(target, 1.0f);
// create the initial composite
_comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, _alpha);
@@ -66,13 +61,8 @@ public class FadeAnimation extends Animation
// documentation inherited
public void tick (long timestamp)
{
if (_start == 0) {
// initialize our starting time
_start = timestamp;
}
// figure out the current alpha
long msecs = timestamp - _start;
long msecs = timestamp - _firstTick;
_alpha = _startAlpha + (msecs * _step);
if (_alpha < 0.0f) {
_alpha = 0.0f;
@@ -90,14 +80,6 @@ public class FadeAnimation extends Animation
invalidate();
}
// documentation inherited
public void fastForward (long timeDelta)
{
if (_start > 0) {
_start += timeDelta;
}
}
// documentation inherited
public void paint (Graphics2D gfx)
{
@@ -107,17 +89,20 @@ public class FadeAnimation extends Animation
} else {
gfx.setComposite(_comp);
}
_image.paint(gfx, _x, _y);
paintAnimation(gfx);
gfx.setComposite(ocomp);
}
/**
* Here is where derived animations actually render their image.
*/
protected abstract void paintAnimation (Graphics2D gfx);
// documentation inherited
protected void toString (StringBuffer buf)
{
super.toString(buf);
buf.append(", x=").append(_x);
buf.append(", y=").append(_y);
buf.append(", alpha=").append(_alpha);
buf.append(", startAlpha=").append(_startAlpha);
buf.append(", step=").append(_step);
@@ -138,13 +123,4 @@ public class FadeAnimation extends Animation
/** The starting alpha. */
protected float _startAlpha;
/** The image position. */
protected int _x, _y;
/** The image to animate. */
protected Mirage _image;
/** The starting animation time. */
protected long _start;
}
@@ -0,0 +1,52 @@
//
// $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.animation;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import com.threerings.media.image.Mirage;
/**
* Fades an image in or out by varying the alpha level during rendering.
*/
public class FadeImageAnimation extends FadeAnimation
{
/**
* Creates an image fading animation.
*/
public FadeImageAnimation (Mirage image, int x, int y,
float alpha, float step, float target)
{
super(new Rectangle(x, y, image.getWidth(), image.getHeight()),
alpha, step, target);
_image = image;
}
// documentation inherited
protected void paintAnimation (Graphics2D gfx)
{
_image.paint(gfx, _bounds.x, _bounds.y);
}
protected Mirage _image;
}
@@ -0,0 +1,99 @@
//
// $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.animation;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import com.samskivert.swing.Label;
import com.samskivert.swing.util.SwingUtil;
import com.threerings.media.Log;
/**
* Does something extraordinary.
*/
public class FadeLabelAnimation extends FadeAnimation
{
/**
* Creates a label fading animation.
*/
public FadeLabelAnimation (Label label, int x, int y,
float alpha, float step, float target)
{
super(new Rectangle(x, y, 0, 0), alpha, step, target);
_label = label;
}
/**
* Indicates that our label should be rendered with antialiased text.
*/
public void setAntiAliased (boolean antiAliased)
{
_antiAliased = antiAliased;
}
// documentation inherited
protected void init ()
{
super.init();
// if our label is not yet laid out, do the deed
if (!_label.isLaidOut()) {
Graphics2D gfx = (Graphics2D)_mgr.getMediaPanel().getGraphics();
if (gfx != null) {
gfx.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
_antiAliased ?
RenderingHints.VALUE_ANTIALIAS_ON :
RenderingHints.VALUE_ANTIALIAS_OFF);
_label.layout(gfx);
gfx.dispose();
}
}
// size the bounds to fit our label
Dimension size = _label.getSize();
_bounds.width = size.width;
_bounds.height = size.height;
}
// documentation inherited
protected void paintAnimation (Graphics2D gfx)
{
Object ohints = null;
if (_antiAliased) {
ohints = SwingUtil.activateAntiAliasing(gfx);
}
_label.render(gfx, _bounds.x, _bounds.y);
if (_antiAliased) {
SwingUtil.restoreAntiAliasing(gfx, ohints);
}
}
/** The label we are rendering. */
protected Label _label;
/** Whether or not to use anti-aliased rendering. */
protected boolean _antiAliased;
}