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:
@@ -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
|
// Narya library - tools for developing networked games
|
||||||
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
|
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
|
||||||
@@ -27,37 +27,32 @@ import java.awt.Graphics2D;
|
|||||||
import java.awt.Rectangle;
|
import java.awt.Rectangle;
|
||||||
|
|
||||||
import com.threerings.media.Log;
|
import com.threerings.media.Log;
|
||||||
import com.threerings.media.image.Mirage;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An animation that displays an image fading from one alpha level to
|
* An animation that displays an image fading from one alpha level to
|
||||||
* another in specified increments over time. The animation is finished
|
* another in specified increments over time. The animation is finished
|
||||||
* when the specified target alpha is reached.
|
* when the specified target alpha is reached.
|
||||||
*/
|
*/
|
||||||
public class FadeAnimation extends Animation
|
public abstract class FadeAnimation extends Animation
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Constructs a fade animation.
|
* Constructs a fade animation.
|
||||||
*
|
*
|
||||||
* @param image the image to animate.
|
* @param image the image to animate.
|
||||||
* @param x the image x-position.
|
* @param bounds our bounds.
|
||||||
* @param y the image y-position.
|
|
||||||
* @param alpha the starting alpha.
|
* @param alpha the starting alpha.
|
||||||
* @param step the alpha amount to step by each millisecond.
|
* @param step the alpha amount to step by each millisecond.
|
||||||
* @param target the target alpha level.
|
* @param target the target alpha level.
|
||||||
*/
|
*/
|
||||||
public FadeAnimation (
|
protected FadeAnimation (
|
||||||
Mirage image, int x, int y, float alpha, float step, float target)
|
Rectangle bounds, float alpha, float step, float target)
|
||||||
{
|
{
|
||||||
super(new Rectangle(x, y, image.getWidth(), image.getHeight()));
|
super(bounds);
|
||||||
|
|
||||||
// save things off
|
// save things off
|
||||||
_image = image;
|
_startAlpha = _alpha = Math.max(alpha, 0.0f);
|
||||||
_x = x;
|
|
||||||
_y = y;
|
|
||||||
_startAlpha = _alpha = alpha;
|
|
||||||
_step = step;
|
_step = step;
|
||||||
_target = target;
|
_target = Math.min(target, 1.0f);
|
||||||
|
|
||||||
// create the initial composite
|
// create the initial composite
|
||||||
_comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, _alpha);
|
_comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, _alpha);
|
||||||
@@ -66,13 +61,8 @@ public class FadeAnimation extends Animation
|
|||||||
// documentation inherited
|
// documentation inherited
|
||||||
public void tick (long timestamp)
|
public void tick (long timestamp)
|
||||||
{
|
{
|
||||||
if (_start == 0) {
|
|
||||||
// initialize our starting time
|
|
||||||
_start = timestamp;
|
|
||||||
}
|
|
||||||
|
|
||||||
// figure out the current alpha
|
// figure out the current alpha
|
||||||
long msecs = timestamp - _start;
|
long msecs = timestamp - _firstTick;
|
||||||
_alpha = _startAlpha + (msecs * _step);
|
_alpha = _startAlpha + (msecs * _step);
|
||||||
if (_alpha < 0.0f) {
|
if (_alpha < 0.0f) {
|
||||||
_alpha = 0.0f;
|
_alpha = 0.0f;
|
||||||
@@ -90,14 +80,6 @@ public class FadeAnimation extends Animation
|
|||||||
invalidate();
|
invalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
// documentation inherited
|
|
||||||
public void fastForward (long timeDelta)
|
|
||||||
{
|
|
||||||
if (_start > 0) {
|
|
||||||
_start += timeDelta;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// documentation inherited
|
// documentation inherited
|
||||||
public void paint (Graphics2D gfx)
|
public void paint (Graphics2D gfx)
|
||||||
{
|
{
|
||||||
@@ -107,17 +89,20 @@ public class FadeAnimation extends Animation
|
|||||||
} else {
|
} else {
|
||||||
gfx.setComposite(_comp);
|
gfx.setComposite(_comp);
|
||||||
}
|
}
|
||||||
_image.paint(gfx, _x, _y);
|
paintAnimation(gfx);
|
||||||
gfx.setComposite(ocomp);
|
gfx.setComposite(ocomp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Here is where derived animations actually render their image.
|
||||||
|
*/
|
||||||
|
protected abstract void paintAnimation (Graphics2D gfx);
|
||||||
|
|
||||||
// documentation inherited
|
// documentation inherited
|
||||||
protected void toString (StringBuffer buf)
|
protected void toString (StringBuffer buf)
|
||||||
{
|
{
|
||||||
super.toString(buf);
|
super.toString(buf);
|
||||||
|
|
||||||
buf.append(", x=").append(_x);
|
|
||||||
buf.append(", y=").append(_y);
|
|
||||||
buf.append(", alpha=").append(_alpha);
|
buf.append(", alpha=").append(_alpha);
|
||||||
buf.append(", startAlpha=").append(_startAlpha);
|
buf.append(", startAlpha=").append(_startAlpha);
|
||||||
buf.append(", step=").append(_step);
|
buf.append(", step=").append(_step);
|
||||||
@@ -138,13 +123,4 @@ public class FadeAnimation extends Animation
|
|||||||
|
|
||||||
/** The starting alpha. */
|
/** The starting alpha. */
|
||||||
protected float _startAlpha;
|
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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user