From 965b44d25320c4f2bc1a18d8629eadc3793773e6 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Mon, 6 Feb 2006 01:40:44 +0000 Subject: [PATCH] Updated standard source header. Created a time function mechanism that can be used to easily plug in different methods of smoothly varying a value over time. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3834 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- lib/SOURCE_HEADER | 2 +- .../jme/effect/FadeInOutEffect.java | 35 ++++--- .../jme/util/LinearTimeFunction.java | 42 +++++++++ .../com/threerings/jme/util/TimeFunction.java | 93 +++++++++++++++++++ 4 files changed, 157 insertions(+), 15 deletions(-) create mode 100644 src/java/com/threerings/jme/util/LinearTimeFunction.java create mode 100644 src/java/com/threerings/jme/util/TimeFunction.java diff --git a/lib/SOURCE_HEADER b/lib/SOURCE_HEADER index cda049f99..2faec9a24 100644 --- a/lib/SOURCE_HEADER +++ b/lib/SOURCE_HEADER @@ -2,7 +2,7 @@ // $Id$ // // Narya library - tools for developing networked games -// Copyright (C) 2002-2005 Three Rings Design, Inc., All Rights Reserved +// Copyright (C) 2002-2006 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 diff --git a/src/java/com/threerings/jme/effect/FadeInOutEffect.java b/src/java/com/threerings/jme/effect/FadeInOutEffect.java index 56c6d29a0..ee6435e85 100644 --- a/src/java/com/threerings/jme/effect/FadeInOutEffect.java +++ b/src/java/com/threerings/jme/effect/FadeInOutEffect.java @@ -29,6 +29,9 @@ import com.jme.scene.shape.Quad; import com.jme.scene.state.AlphaState; import com.jme.system.DisplaySystem; +import com.threerings.jme.util.LinearTimeFunction; +import com.threerings.jme.util.TimeFunction; + /** * Fades the screen in from a solid color or out to a solid color. */ @@ -36,13 +39,19 @@ public class FadeInOutEffect extends Quad { public FadeInOutEffect (ColorRGBA color, float startAlpha, float endAlpha, float duration, boolean overUI) + { + this(color, new LinearTimeFunction(startAlpha, endAlpha, duration), + overUI); + } + + public FadeInOutEffect (ColorRGBA color, TimeFunction alphaFunc, + boolean overUI) { super("FadeInOut"); - _color = new ColorRGBA(color.r, color.g, color.b, startAlpha); - _alpha = startAlpha; - _deltaAlpha = (endAlpha-startAlpha); - _duration = duration; + _color = new ColorRGBA( + color.r, color.g, color.b, alphaFunc.getValue(0)); + _alphaFunc = alphaFunc; // we need to render in the ortho queue setRenderQueueMode(Renderer.QUEUE_ORTHO); @@ -76,15 +85,14 @@ public class FadeInOutEffect extends Quad public void updateGeometricState (float time, boolean initiator) { super.updateGeometricState(time, initiator); + if (_paused) { + return; + } - if (!_paused && _elapsed < _duration) { - _elapsed += time; - float alpha = (_alpha + _deltaAlpha * _elapsed / _duration); - _color.a = Math.min(1f, Math.max(0f, alpha)); - - if (_elapsed >= _duration) { - fadeComplete(); - } + float alpha = _alphaFunc.getValue(time); + _color.a = Math.min(1f, Math.max(0f, alpha)); + if (_alphaFunc.isComplete()) { + fadeComplete(); } } @@ -96,7 +104,6 @@ public class FadeInOutEffect extends Quad } protected ColorRGBA _color; - protected float _alpha, _deltaAlpha; - protected float _duration, _elapsed; + protected TimeFunction _alphaFunc; protected boolean _paused; } diff --git a/src/java/com/threerings/jme/util/LinearTimeFunction.java b/src/java/com/threerings/jme/util/LinearTimeFunction.java new file mode 100644 index 000000000..d29ccd7ad --- /dev/null +++ b/src/java/com/threerings/jme/util/LinearTimeFunction.java @@ -0,0 +1,42 @@ +// +// $Id: LinearTimeFunction.java 3122 2004-09-18 22:57:08Z mdb $ +// +// Narya library - tools for developing networked games +// Copyright (C) 2002-2006 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.jme.util; + +/** + * Varies a value linearly with time. + */ +public class LinearTimeFunction extends TimeFunction +{ + public LinearTimeFunction (float start, float end, float duration) + { + super(start, end, duration); + _range = (end - start); + } + + // documentation inherited + protected float computeValue () + { + return (_elapsed * _range / _duration) + _start; + } + + protected float _range; +} diff --git a/src/java/com/threerings/jme/util/TimeFunction.java b/src/java/com/threerings/jme/util/TimeFunction.java new file mode 100644 index 000000000..e3547b260 --- /dev/null +++ b/src/java/com/threerings/jme/util/TimeFunction.java @@ -0,0 +1,93 @@ +// +// $Id: TimeFunction.java 3122 2004-09-18 22:57:08Z mdb $ +// +// Narya library - tools for developing networked games +// Copyright (C) 2002-2006 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.jme.util; + +/** + * Used to vary a value over time where time is provided at discrete increments + * (on the frame tick) and the value is computed appropriately. This can be + * used for fades and other effects where different functions (linear, ease-in + * ease-out, etc.) should be easy to plug in. + */ +public abstract class TimeFunction +{ + /** + * Every time function varies a value from some starting value to some + * ending value over some duration. The way in which it varies + * (linearly, for example) is up to the derived class. + * + * @param start the starting value. + * @param end the ending value. + * @param duration the duration in seconds. + */ + public TimeFunction (float start, float end, float duration) + { + _start = start; + _end = end; + _duration = duration; + } + + /** + * Returns the current value given the supplied elapsed time. The value + * will be bounded to the originally supplied starting and ending values at + * times 0 (and below) and {@link #_duration} (and above) respectively. + * + * @param deltaTime the amount of time that has elapsed since the last call + * to this method. + */ + public float getValue (float deltaTime) + { + _elapsed += deltaTime; + + if (_elapsed <= 0) { + return _start; + } else if (_elapsed >= _duration) { + return _end; + } else { + return computeValue(); + } + } + + /** + * Returns true if this function has proceeded the full length of its + * duration. This should be called after a call to {@link #getValue} has + * been made to update our internal elapsed time. + */ + public boolean isComplete () + { + return _elapsed >= _duration; + } + + /** + * This must be implemented by our derived class to compute our value given + * the currently stored {@link #_elapsed} time. + */ + protected abstract float computeValue (); + + /** Our starting and ending values. */ + protected float _start, _end; + + /** The number of milliseconds over which we vary our value. */ + protected float _duration; + + /** The number of seconds that have elapsed since we started. */ + protected float _elapsed; +}