Allow the start or end alpha levels to be outside the normal range so that
useful delays can be built into the beginning or end of the animation. Detect bogus parameters in the constructor and throw an exception to save developer head-scratching time when the animation doesn't actually do any fading. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3560 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -32,24 +32,32 @@ public class FadeEffect
|
|||||||
{
|
{
|
||||||
public FadeEffect (float alpha, float step, float target)
|
public FadeEffect (float alpha, float step, float target)
|
||||||
{
|
{
|
||||||
|
if ((step == 0f) || (alpha > target && step > 0f) ||
|
||||||
|
(alpha < target && step < 0f)) {
|
||||||
|
throw new IllegalArgumentException("Step specified is illegal: " +
|
||||||
|
"Fade would never finish (start=" + alpha + ", step=" + step +
|
||||||
|
", target=" + target + ")");
|
||||||
|
}
|
||||||
|
|
||||||
// save things off
|
// save things off
|
||||||
_startAlpha = _alpha = Math.max(alpha, 0.0f);
|
_startAlpha = alpha;
|
||||||
_step = step;
|
_step = step;
|
||||||
_target = Math.min(target, 1.0f);
|
_target = target;
|
||||||
|
|
||||||
// create the initial composite
|
// create the initial composite
|
||||||
|
_alpha = Math.max(0f, Math.min(1f, _startAlpha));
|
||||||
_comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, _alpha);
|
_comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, _alpha);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void init (long tickStamp)
|
public void init (long tickStamp)
|
||||||
{
|
{
|
||||||
|
_finished = false;
|
||||||
_initStamp = tickStamp;
|
_initStamp = tickStamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean finished ()
|
public boolean finished ()
|
||||||
{
|
{
|
||||||
return (_startAlpha < _target) ?
|
return _finished;
|
||||||
(_alpha >= _target) : (_alpha <= _target);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public float getAlpha ()
|
public float getAlpha ()
|
||||||
@@ -62,6 +70,8 @@ public class FadeEffect
|
|||||||
// figure out the current alpha
|
// figure out the current alpha
|
||||||
long msecs = tickStamp - _initStamp;
|
long msecs = tickStamp - _initStamp;
|
||||||
float alpha = _startAlpha + (msecs * _step);
|
float alpha = _startAlpha + (msecs * _step);
|
||||||
|
_finished = (_startAlpha < _target) ? (alpha >= _target)
|
||||||
|
: (alpha <= _target);
|
||||||
if (alpha < 0.0f) {
|
if (alpha < 0.0f) {
|
||||||
alpha = 0.0f;
|
alpha = 0.0f;
|
||||||
} else if (alpha > 1.0f) {
|
} else if (alpha > 1.0f) {
|
||||||
@@ -74,7 +84,8 @@ public class FadeEffect
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
// return false, unless we're finished
|
||||||
|
return _finished;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void beforePaint (Graphics2D gfx)
|
public void beforePaint (Graphics2D gfx)
|
||||||
@@ -108,4 +119,7 @@ public class FadeEffect
|
|||||||
|
|
||||||
/** Time zero. */
|
/** Time zero. */
|
||||||
protected long _initStamp;
|
protected long _initStamp;
|
||||||
|
|
||||||
|
/** True when we're finished. */
|
||||||
|
protected boolean _finished;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user