Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Squared.Game;
- using Squared.Render;
- using Squared.Render.Convenience;
- using Squared.Task;
- using Squared.Util;
- namespace ThreefoldTrials.Framework {
- public struct Tween<T>
- where T : struct {
- private static readonly BoundInterpolatorSource<T, Tween<T>> GetValue;
- private static readonly Dictionary<Interpolator<T>, object> Cache = new Dictionary<Interpolator<T>, object>();
- public T From, To;
- public BoundInterpolator<T, Tween<T>> Interpolator;
- public readonly long StartedWhen, EndWhen;
- static Tween () {
- GetValue = _GetValue;
- }
- private static T _GetValue (ref Tween<T> tween, int index) {
- if (index == 0)
- return tween.From;
- else if (index == 1)
- return tween.To;
- else
- throw new ArgumentOutOfRangeException("index");
- }
- public Tween (T value) {
- From = To = value;
- StartedWhen = EndWhen = 0;
- Interpolator = Interpolators<T>.GetBoundDefault<Tween<T>>();
- }
- public Tween (
- T from, T to,
- long startWhen, long endWhen,
- BoundInterpolator<T, Tween<T>> interpolator = null
- ) {
- From = from;
- To = to;
- StartedWhen = startWhen;
- EndWhen = endWhen;
- Interpolator = interpolator ?? Interpolators<T>.GetBoundDefault<Tween<T>>();
- }
- public static Tween<T> StartNow (
- T from, T to, long? delay, long duration,
- long? now = null, BoundInterpolator<T, Tween<T>> interpolator = null
- ) {
- var _now = now.HasValue ? now.Value : Time.Ticks;
- return new Tween<T>(
- from, to,
- _now + delay.GetValueOrDefault(0),
- _now + delay.GetValueOrDefault(0) + duration,
- interpolator
- );
- }
- public static Tween<T> StartNow (
- T from, T to, float? delaySeconds, float durationSeconds,
- long? now = null, BoundInterpolator<T, Tween<T>> interpolator = null
- ) {
- return StartNow(
- from, to,
- TimeSpan.FromSeconds(delaySeconds.GetValueOrDefault(0)).Ticks,
- TimeSpan.FromSeconds(durationSeconds).Ticks,
- now, interpolator
- );
- }
- private float GetProgress (long now) {
- if ((EndWhen <= StartedWhen) || (EndWhen <= 0))
- return 1f;
- return Arithmetic.Clamp((float)((now - StartedWhen) / (double)(EndWhen - StartedWhen)), 0, 1);
- }
- public T Get (long now) {
- var progress = GetProgress(now);
- // Default-init / completed
- if (progress >= 1f)
- return To;
- else if (progress <= 0f)
- return From;
- return Interpolator(GetValue, ref this, 0, progress);
- }
- public bool IsOver (long now) {
- if (EndWhen <= 0)
- return false;
- return GetProgress(now) >= 1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement