Break the code for completing a fade into a method so subclasses can override it and do what they will when it finishes.

git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@284 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Charlie Groves
2007-08-10 22:00:50 +00:00
parent 069fe85c70
commit e003d97535
@@ -30,8 +30,7 @@ import com.threerings.media.util.Path;
public class FadableImageSprite extends OrientableImageSprite public class FadableImageSprite extends OrientableImageSprite
{ {
/** /**
* Fades this sprite in over the specified duration after * Fades this sprite in over the specified duration after waiting for the specified delay.
* waiting for the specified delay.
*/ */
public void fadeIn (long delay, long duration) public void fadeIn (long delay, long duration)
{ {
@@ -41,10 +40,9 @@ public class FadableImageSprite extends OrientableImageSprite
_fadeDelay = delay; _fadeDelay = delay;
_fadeInDuration = duration; _fadeInDuration = duration;
} }
/** /**
* Fades this sprite out over the specified duration after * Fades this sprite out over the specified duration after waiting for the specified delay.
* waiting for the specified delay.
*/ */
public void fadeOut (long delay, long duration) public void fadeOut (long delay, long duration)
{ {
@@ -56,12 +54,11 @@ public class FadableImageSprite extends OrientableImageSprite
} }
/** /**
* Puts this sprite on the specified path and fades it in over * Puts this sprite on the specified path and fades it in over the specified duration.
* the specified duration. *
*
* @param path the path to move along * @param path the path to move along
* @param fadePortion the portion of time to spend fading in, from 0.0f * @param fadePortion the portion of time to spend fading in, from 0.0f (no time) to 1.0f (the
* (no time) to 1.0f (the entire time) * entire time)
*/ */
public void moveAndFadeIn (Path path, long pathDuration, float fadePortion) public void moveAndFadeIn (Path path, long pathDuration, float fadePortion)
{ {
@@ -69,17 +66,16 @@ public class FadableImageSprite extends OrientableImageSprite
setAlpha(0.0f); setAlpha(0.0f);
_fadeInDuration = (long)(pathDuration*fadePortion); _fadeInDuration = (long)(pathDuration * fadePortion);
} }
/** /**
* Puts this sprite on the specified path and fades it out over * Puts this sprite on the specified path and fades it out over the specified duration.
* the specified duration. *
*
* @param path the path to move along * @param path the path to move along
* @param pathDuration the duration of the path * @param pathDuration the duration of the path
* @param fadePortion the portion of time to spend fading out, from 0.0f * @param fadePortion the portion of time to spend fading out, from 0.0f (no time) to 1.0f
* (no time) to 1.0f (the entire time) * (the entire time)
*/ */
public void moveAndFadeOut (Path path, long pathDuration, float fadePortion) public void moveAndFadeOut (Path path, long pathDuration, float fadePortion)
{ {
@@ -89,28 +85,27 @@ public class FadableImageSprite extends OrientableImageSprite
_fadeStamp = 0; _fadeStamp = 0;
_pathDuration = pathDuration; _pathDuration = pathDuration;
_fadeOutDuration = (long)(pathDuration*fadePortion); _fadeOutDuration = (long)(pathDuration * fadePortion);
_fadeDelay = _pathDuration - _fadeOutDuration; _fadeDelay = _pathDuration - _fadeOutDuration;
} }
/** /**
* Puts this sprite on the specified path, fading it in over the specified * Puts this sprite on the specified path, fading it in over the specified duration at the
* duration at the beginning and fading it out at the end. * beginning and fading it out at the end.
* *
* @param path the path to move along * @param path the path to move along
* @param pathDuration the duration of the path * @param pathDuration the duration of the path
* @param fadePortion the portion of time to spend fading in/out, from * @param fadePortion the portion of time to spend fading in/out, from 0.0f (no time) to 1.0f
* 0.0f (no time) to 1.0f (the entire time) * (the entire time)
*/ */
public void moveAndFadeInAndOut (Path path, long pathDuration, public void moveAndFadeInAndOut (Path path, long pathDuration, float fadePortion)
float fadePortion)
{ {
move(path); move(path);
setAlpha(0.0f); setAlpha(0.0f);
_pathDuration = pathDuration; _pathDuration = pathDuration;
_fadeInDuration = _fadeOutDuration = (long)(pathDuration*fadePortion); _fadeInDuration = _fadeOutDuration = (long)(pathDuration * fadePortion);
} }
// Documentation inherited. // Documentation inherited.
@@ -119,14 +114,11 @@ public class FadableImageSprite extends OrientableImageSprite
super.tick(tickStamp); super.tick(tickStamp);
if (_fadeInDuration != -1) { if (_fadeInDuration != -1) {
if (_path != null && (tickStamp-_pathStamp) <= _fadeInDuration) { if (_path != null && (tickStamp - _pathStamp) <= _fadeInDuration) {
// fading in while moving // fading in while moving
float alpha = (float)(tickStamp-_pathStamp)/_fadeInDuration; float alpha = (float)(tickStamp - _pathStamp) / _fadeInDuration;
if (alpha >= 1.0f) { if (alpha >= 1.0f) {
// fade-in complete completeFadeIn();
setAlpha(1.0f);
_fadeInDuration = -1;
} else { } else {
setAlpha(alpha); setAlpha(alpha);
} }
@@ -139,12 +131,9 @@ public class FadableImageSprite extends OrientableImageSprite
} }
if (tickStamp > _fadeStamp + _fadeDelay) { if (tickStamp > _fadeStamp + _fadeDelay) {
// initial delay has passed // initial delay has passed
float alpha = (float)(tickStamp-_fadeStamp-_fadeDelay) / _fadeInDuration; float alpha = (float)(tickStamp - _fadeStamp - _fadeDelay) / _fadeInDuration;
if (alpha >= 1.0f) { if (alpha >= 1.0f) {
// fade-in complete completeFadeIn();
setAlpha(1.0f);
_fadeInDuration = -1;
} else { } else {
setAlpha(alpha); setAlpha(alpha);
} }
@@ -161,10 +150,7 @@ public class FadableImageSprite extends OrientableImageSprite
// initial delay has passed // initial delay has passed
float alpha = 1f - (float)(tickStamp - _fadeStamp - _fadeDelay) / _fadeOutDuration; float alpha = 1f - (float)(tickStamp - _fadeStamp - _fadeDelay) / _fadeOutDuration;
if (alpha <= 0.0f) { if (alpha <= 0.0f) {
// fade-out complete completeFadeOut();
setAlpha(0.0f);
_fadeOutDuration = -1;
} else { } else {
setAlpha(alpha); setAlpha(alpha);
} }
@@ -176,17 +162,27 @@ public class FadableImageSprite extends OrientableImageSprite
public void pathCompleted (long timestamp) public void pathCompleted (long timestamp)
{ {
super.pathCompleted(timestamp); super.pathCompleted(timestamp);
if (_fadeInDuration != -1) { if (_fadeInDuration != -1) {
setAlpha(1.0f); completeFadeIn();
_fadeInDuration = -1;
} else if (_fadeOutDuration != -1) { } else if (_fadeOutDuration != -1) {
setAlpha(0.0f); completeFadeOut();
_fadeOutDuration = -1;
} }
} }
/** Completes the process of fading in. */
protected void completeFadeIn ()
{
setAlpha(1.0f);
_fadeInDuration = -1;
}
/** Completes the process of fading out. */
protected void completeFadeOut ()
{
setAlpha(0.0f);
_fadeOutDuration = -1;
}
// Documentation inherited. // Documentation inherited.
public void paint (Graphics2D gfx) public void paint (Graphics2D gfx)
{ {
@@ -213,8 +209,7 @@ public class FadableImageSprite extends OrientableImageSprite
alpha = 1.0f; alpha = 1.0f;
} }
if (alpha != _alphaComposite.getAlpha()) { if (alpha != _alphaComposite.getAlpha()) {
_alphaComposite = _alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
if (_mgr != null) { if (_mgr != null) {
_mgr.getRegionManager().invalidateRegion(_bounds); _mgr.getRegionManager().invalidateRegion(_bounds);
} }