Split out fading sprite capability from the CardSprite class so it can be extended and used elsewhere.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3819 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Mike Thomas
2006-01-24 19:46:34 +00:00
parent 765fdc8edf
commit dfc8112277
2 changed files with 229 additions and 206 deletions
@@ -0,0 +1,203 @@
package com.threerings.media.sprite;
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Composite;
import com.threerings.media.util.Path;
public class FadableImageSprite extends OrientableImageSprite
{
/**
* Fades this sprite in over the specified duration after
* waiting for the specified delay.
*/
public void fadeIn (long delay, long duration)
{
setAlpha(0.0f);
_fadeStamp = 0;
_fadeDelay = delay;
_fadeInDuration = duration;
}
/**
* Puts this sprite on the specified path and fades it in over
* the specified duration.
*
* @param path the path to move along
* @param fadePortion the portion of time to spend fading in, from 0.0f
* (no time) to 1.0f (the entire time)
*/
public void moveAndFadeIn (Path path, long pathDuration, float fadePortion)
{
move(path);
setAlpha(0.0f);
_fadeInDuration = (long)(pathDuration*fadePortion);
}
/**
* Puts this sprite on the specified path and fades it out over
* the specified duration.
*
* @param path the path to move along
* @param pathDuration the duration of the path
* @param fadePortion the portion of time to spend fading out, from 0.0f
* (no time) to 1.0f (the entire time)
*/
public void moveAndFadeOut (Path path, long pathDuration, float fadePortion)
{
move(path);
setAlpha(1.0f);
_pathDuration = pathDuration;
_fadeOutDuration = (long)(pathDuration*fadePortion);
}
/**
* Puts this sprite on the specified path, fading it in over the specified
* duration at the beginning and fading it out at the end.
*
* @param path the path to move along
* @param pathDuration the duration of the path
* @param fadePortion the portion of time to spend fading in/out, from
* 0.0f (no time) to 1.0f (the entire time)
*/
public void moveAndFadeInAndOut (Path path, long pathDuration,
float fadePortion)
{
move(path);
setAlpha(0.0f);
_pathDuration = pathDuration;
_fadeInDuration = _fadeOutDuration = (long)(pathDuration*fadePortion);
}
// Documentation inherited.
public void tick (long tickStamp)
{
super.tick(tickStamp);
if (_fadeInDuration != -1) {
if (_path != null && (tickStamp-_pathStamp) <= _fadeInDuration) {
// fading in while moving
float alpha = (float)(tickStamp-_pathStamp)/_fadeInDuration;
if (alpha >= 1.0f) {
// fade-in complete
setAlpha(1.0f);
_fadeInDuration = -1;
} else {
setAlpha(alpha);
}
} else {
// fading in while stationary
if (_fadeStamp == 0) {
// store the time at which fade started
_fadeStamp = tickStamp;
}
if (tickStamp > _fadeStamp + _fadeDelay) {
// initial delay has passed
float alpha = (float)(tickStamp-_fadeStamp-_fadeDelay)/
_fadeInDuration;
if (alpha >= 1.0f) {
// fade-in complete
setAlpha(1.0f);
_fadeInDuration = -1;
} else {
setAlpha(alpha);
}
}
}
} else if (_fadeOutDuration != -1 && _pathStamp+_pathDuration-tickStamp
<= _fadeOutDuration) {
// fading out while moving
float alpha = (float)(_pathStamp+_pathDuration-tickStamp)/
_fadeOutDuration;
setAlpha(alpha);
}
}
// Documentation inherited.
public void pathCompleted (long timestamp)
{
super.pathCompleted(timestamp);
if (_fadeInDuration != -1) {
setAlpha(1.0f);
_fadeInDuration = -1;
} else if (_fadeOutDuration != -1) {
setAlpha(0.0f);
_fadeOutDuration = -1;
}
}
// Documentation inherited.
public void paint (Graphics2D gfx)
{
if (_alphaComposite.getAlpha() < 1.0f) {
Composite ocomp = gfx.getComposite();
gfx.setComposite(_alphaComposite);
super.paint(gfx);
gfx.setComposite(ocomp);
} else {
super.paint(gfx);
}
}
/**
* Sets the alpha value of this sprite.
*/
public void setAlpha (float alpha)
{
if (alpha < 0.0f) {
alpha = 0.0f;
} else if (alpha > 1.0f) {
alpha = 1.0f;
}
if (alpha != _alphaComposite.getAlpha()) {
_alphaComposite =
AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
if (_mgr != null) {
_mgr.getRegionManager().invalidateRegion(_bounds);
}
}
}
/**
* Returns the alpha value of this sprite.
*/
public float getAlpha ()
{
return _alphaComposite.getAlpha();
}
/** The alpha composite. */
protected AlphaComposite _alphaComposite =
AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f);
/** If fading in, the fade-in duration (otherwise -1). */
protected long _fadeInDuration = -1;
/** If fading in without moving, the fade-in delay. */
protected long _fadeDelay;
/** The time at which fading started. */
protected long _fadeStamp = -1;
/** If fading out, the fade-out duration (otherwise -1). */
protected long _fadeOutDuration = -1;
/** If fading out, the path duration. */
protected long _pathDuration;
}
@@ -21,13 +21,11 @@
package com.threerings.parlor.card.client; package com.threerings.parlor.card.client;
import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.geom.AffineTransform; import java.awt.geom.AffineTransform;
import com.threerings.media.image.Mirage; import com.threerings.media.image.Mirage;
import com.threerings.media.sprite.OrientableImageSprite; import com.threerings.media.sprite.FadableImageSprite;
import com.threerings.media.util.Path; import com.threerings.media.util.Path;
import com.threerings.parlor.card.data.Card; import com.threerings.parlor.card.data.Card;
@@ -35,7 +33,7 @@ import com.threerings.parlor.card.data.Card;
/** /**
* A sprite representing a playing card. * A sprite representing a playing card.
*/ */
public class CardSprite extends OrientableImageSprite public class CardSprite extends FadableImageSprite
implements Comparable implements Comparable
{ {
/** /**
@@ -134,34 +132,6 @@ public class CardSprite extends OrientableImageSprite
return _draggable; return _draggable;
} }
/**
* Sets the alpha value of this sprite.
*/
public void setAlpha (float alpha)
{
if (alpha < 0.0f) {
alpha = 0.0f;
} else if (alpha > 1.0f) {
alpha = 1.0f;
}
if (alpha != _alphaComposite.getAlpha()) {
_alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
alpha);
if (_mgr != null) {
_mgr.getRegionManager().invalidateRegion(_bounds);
}
}
}
/**
* Returns the alpha value of this sprite.
*/
public float getAlpha ()
{
return _alphaComposite.getAlpha();
}
/** /**
* Flip the card from its current displayed card to the specified card. * Flip the card from its current displayed card to the specified card.
*/ */
@@ -174,75 +144,6 @@ public class CardSprite extends OrientableImageSprite
_scaleFactor = 1.0; _scaleFactor = 1.0;
} }
/**
* Fades this sprite in over the specified duration after
* waiting for the specified delay.
*/
public void fadeIn (long delay, long duration)
{
setAlpha(0.0f);
_fadeStamp = 0;
_fadeDelay = delay;
_fadeInDuration = duration;
}
/**
* Puts this sprite on the specified path and fades it in over
* the specified duration.
*
* @param path the path to move along
* @param fadePortion the portion of time to spend fading in, from 0.0f (no time)
* to 1.0f (the entire time)
*/
public void moveAndFadeIn (Path path, long pathDuration, float fadePortion)
{
move(path);
setAlpha(0.0f);
_fadeInDuration = (long)(pathDuration*fadePortion);
}
/**
* Puts this sprite on the specified path and fades it out over
* the specified duration.
*
* @param path the path to move along
* @param pathDuration the duration of the path
* @param fadePortion the portion of time to spend fading out, from 0.0f (no time)
* to 1.0f (the entire time)
*/
public void moveAndFadeOut (Path path, long pathDuration, float fadePortion)
{
move(path);
setAlpha(1.0f);
_pathDuration = pathDuration;
_fadeOutDuration = (long)(pathDuration*fadePortion);
}
/**
* Puts this sprite on the specified path, fading it in over the specified
* duration at the beginning and fading it out at the end.
*
* @param path the path to move along
* @param pathDuration the duration of the path
* @param fadePortion the portion of time to spend fading in/out, from
* 0.0f (no time) to 1.0f (the entire time)
*/
public void moveAndFadeInAndOut (Path path, long pathDuration,
float fadePortion)
{
move(path);
setAlpha(0.0f);
_pathDuration = pathDuration;
_fadeInDuration = _fadeOutDuration = (long)(pathDuration*fadePortion);
}
// Documentation inherited. // Documentation inherited.
public void tick (long tickStamp) public void tick (long tickStamp)
{ {
@@ -280,60 +181,6 @@ public class CardSprite extends OrientableImageSprite
} }
} }
if (_fadeInDuration != -1) {
if (_path != null && (tickStamp-_pathStamp) <= _fadeInDuration) {
// fading in while moving
float alpha = (float)(tickStamp-_pathStamp)/_fadeInDuration;
if (alpha >= 1.0f) {
// fade-in complete
setAlpha(1.0f);
_fadeInDuration = -1;
} else {
setAlpha(alpha);
}
} else {
// fading in while stationary
if (_fadeStamp == 0) {
// store the time at which fade started
_fadeStamp = tickStamp;
}
if (tickStamp > _fadeStamp + _fadeDelay) {
// initial delay has passed
float alpha = (float)(tickStamp-_fadeStamp-_fadeDelay)/_fadeInDuration;
if (alpha >= 1.0f) {
// fade-in complete
setAlpha(1.0f);
_fadeInDuration = -1;
} else {
setAlpha(alpha);
}
}
}
} else if(_fadeOutDuration != -1 && _pathStamp+_pathDuration-tickStamp
<= _fadeOutDuration) {
// fading out while moving
float alpha = (float)(_pathStamp+_pathDuration-tickStamp)/_fadeOutDuration;
setAlpha(alpha);
}
}
// Documentation inherited.
public void pathCompleted (long timestamp)
{
super.pathCompleted(timestamp);
if (_fadeInDuration != -1) {
setAlpha(1.0f);
_fadeInDuration = -1;
} else if(_fadeOutDuration != -1) {
setAlpha(0.0f);
_fadeOutDuration = -1;
}
} }
// Documentation inherited. // Documentation inherited.
@@ -351,15 +198,7 @@ public class CardSprite extends OrientableImageSprite
gfx.translate(-xtrans, 0); gfx.translate(-xtrans, 0);
} }
if (_alphaComposite.getAlpha() < 1.0f) {
Composite ocomp = gfx.getComposite();
gfx.setComposite(_alphaComposite);
super.paint(gfx); super.paint(gfx);
gfx.setComposite(ocomp);
} else {
super.paint(gfx);
}
gfx.setTransform(otrans); gfx.setTransform(otrans);
} }
@@ -399,10 +238,6 @@ public class CardSprite extends OrientableImageSprite
/** Whether or not the user can drag the card around the board. */ /** Whether or not the user can drag the card around the board. */
protected boolean _draggable; protected boolean _draggable;
/** The alpha composite. */
protected AlphaComposite _alphaComposite =
AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f);
/** The horizontal scale factor used while flipping the card. */ /** The horizontal scale factor used while flipping the card. */
protected double _scaleFactor = 1.0; protected double _scaleFactor = 1.0;
@@ -414,19 +249,4 @@ public class CardSprite extends OrientableImageSprite
/** The card which will be revealed when we're done flipping. */ /** The card which will be revealed when we're done flipping. */
protected Card _flipCard; protected Card _flipCard;
/** If fading in, the fade-in duration (otherwise -1). */
protected long _fadeInDuration = -1;
/** If fading in without moving, the fade-in delay. */
protected long _fadeDelay;
/** The time at which fading started. */
protected long _fadeStamp = -1;
/** If fading out, the fade-out duration (otherwise -1). */
protected long _fadeOutDuration = -1;
/** If fading out, the path duration. */
protected long _pathDuration;
} }