From 7fdd8fee17970e3f74ab6a549c53c432e153b95a Mon Sep 17 00:00:00 2001 From: Charlie Groves Date: Wed, 11 Jul 2007 23:49:04 +0000 Subject: [PATCH] Add the ability to fade the sprite out without a path and fix fading out with a path so that it's actually fading out instead of fading in git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@272 ed5b42cb-e716-0410-a449-f6a68f950b19 --- .../media/sprite/FadableImageSprite.java | 48 ++++++++++++++++--- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/src/java/com/threerings/media/sprite/FadableImageSprite.java b/src/java/com/threerings/media/sprite/FadableImageSprite.java index e2df69cd..8e958234 100644 --- a/src/java/com/threerings/media/sprite/FadableImageSprite.java +++ b/src/java/com/threerings/media/sprite/FadableImageSprite.java @@ -22,8 +22,8 @@ package com.threerings.media.sprite; import java.awt.AlphaComposite; -import java.awt.Graphics2D; import java.awt.Composite; +import java.awt.Graphics2D; import com.threerings.media.util.Path; @@ -41,6 +41,19 @@ public class FadableImageSprite extends OrientableImageSprite _fadeDelay = delay; _fadeInDuration = duration; } + + /** + * Fades this sprite out over the specified duration after + * waiting for the specified delay. + */ + public void fadeOut (long delay, long duration) + { + setAlpha(1.0f); + + _fadeStamp = 0; + _fadeDelay = delay; + _fadeOutDuration = duration; + } /** * Puts this sprite on the specified path and fades it in over @@ -137,12 +150,33 @@ public class FadableImageSprite extends OrientableImageSprite } } - } else if (_fadeOutDuration != -1 && _pathStamp+_pathDuration-tickStamp - <= _fadeOutDuration) { - // fading out while moving - float alpha = (float)(_pathStamp+_pathDuration-tickStamp)/ - _fadeOutDuration; - setAlpha(alpha); + } else if (_fadeOutDuration != -1) { + if (_path != null) { + if (tickStamp - _pathStamp <= _fadeOutDuration) { + // fading out while moving + float alpha = 1f - (_pathStamp + _pathDuration - tickStamp) + / _fadeOutDuration; + setAlpha(alpha); + } + } else { + // fading out while stationary + if (_fadeStamp == 0) { + // store the time at which fade started + _fadeStamp = tickStamp; + } + if (tickStamp > _fadeStamp + _fadeDelay) { + // initial delay has passed + float alpha = 1f - (float)(tickStamp - _fadeStamp - _fadeDelay) / _fadeOutDuration; + if (alpha <= 0.0f) { + // fade-out complete + setAlpha(0.0f); + _fadeOutDuration = -1; + + } else { + setAlpha(alpha); + } + } + } } }