Ejemplo n.º 1
0
        /// <summary>
        /// An <c>CommandDuration</c> runs over a duration of time.
        /// </summary>
        /// <param name="command">
        /// The command to execute. Must be non-null.
        /// </param>
        /// <param name="duration">
        /// The duration of time, in seconds, to apply the command over.
        /// Must be greater than or equal to 0.
        /// </param>
        /// <param name="ease">
        /// An easing function to apply to the <c>t</c> parameter of an
        /// <c>CommandDuration</c> delegate. If null, linear easing is used.
        /// </param>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <exception cref="System.ArgumentOutOfRange"></exception>
        public static CommandDelegate Duration(CommandDuration command, double duration, CommandEase ease = null)
        {
            CheckArgumentNonNull(command);
            CheckDurationGreaterThanOrEqualToZero(duration);
            if (duration == 0.0)
            {
                // Sometimes it is convenient to create duration commands with
                // a time of zero, so we have a special case.
                return((ref double deltaTime) =>
                {
                    var t = 1.0;
                    if (ease != null)
                    {
                        t = ease(t);
                    }
                    command(t);
                    return true;
                });
            }

            var elapsedTime = 0.0;

            return((ref double deltaTime) =>
            {
                elapsedTime += deltaTime;
                deltaTime = 0.0;
                var t = (elapsedTime / duration);
                t = t < 0.0 ? 0.0 : (t > 1.0 ? 1.0 : t);
                if (ease != null)
                {
                    t = ease(t);
                }
                command(t);
                var finished = elapsedTime >= duration;
                if (!finished)
                {
                    return false;
                }

                deltaTime = elapsedTime - duration;
                elapsedTime = 0.0;
                return true;
            });
        }
Ejemplo n.º 2
0
        /// <summary>
        /// An <c>CommandDuration</c> runs over a duration of time.
        /// </summary>
        /// <param name="command">
        /// The command to execute. Must be non-null.
        /// </param>
        /// <param name="duration">
        /// The duration of time, in seconds, to apply the command over. 
        /// Must be greater than or equal to 0.
        /// </param>
        /// <param name="ease">
        /// An easing function to apply to the <c>t</c> parameter of an
        /// <c>CommandDuration</c> delegate. If null, linear easing is used.
        /// </param>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <exception cref="System.ArgumentOutOfRange"></exception>
        public static CommandDelegate Duration(CommandDuration command, double duration, CommandEase ease = null)
        {
            CheckArgumentNonNull(command);
            CheckDurationGreaterThanOrEqualToZero(duration);
            if (duration == 0.0) {
            // Sometimes it is convenient to create duration commands with
            // a time of zero, so we have a special case.
            return (ref double deltaTime) => {
                double t = 1.0;
                if (ease != null) { t = ease(t); }
                command(t);
                return true;
            };
            }

            double elapsedTime = 0.0;

            return (ref double deltaTime) => {
            elapsedTime += deltaTime;
            deltaTime = 0.0;
            double t = (elapsedTime / duration);
            t = t < 0.0 ? 0.0 : (t > 1.0 ? 1.0 : t);
            if (ease != null) { t = ease(t); }
            command(t);
            bool finished = elapsedTime >= duration;
            if (finished) {
                deltaTime = elapsedTime - duration;
                elapsedTime = 0.0;
            }
            return finished;
            };
        }