Advertisement
Guest User

Untitled

a guest
Jan 30th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Graphics;
  8. using Squared.Game;
  9. using Squared.Render;
  10. using Squared.Render.Convenience;
  11. using Squared.Task;
  12. using Squared.Util;
  13.  
  14. namespace ThreefoldTrials.Framework {
  15. public struct Tween<T>
  16. where T : struct {
  17.  
  18. private static readonly BoundInterpolatorSource<T, Tween<T>> GetValue;
  19. private static readonly Dictionary<Interpolator<T>, object> Cache = new Dictionary<Interpolator<T>, object>();
  20.  
  21. public T From, To;
  22. public BoundInterpolator<T, Tween<T>> Interpolator;
  23. public readonly long StartedWhen, EndWhen;
  24.  
  25. static Tween () {
  26. GetValue = _GetValue;
  27. }
  28.  
  29. private static T _GetValue (ref Tween<T> tween, int index) {
  30. if (index == 0)
  31. return tween.From;
  32. else if (index == 1)
  33. return tween.To;
  34. else
  35. throw new ArgumentOutOfRangeException("index");
  36. }
  37.  
  38. public Tween (T value) {
  39. From = To = value;
  40. StartedWhen = EndWhen = 0;
  41. Interpolator = Interpolators<T>.GetBoundDefault<Tween<T>>();
  42. }
  43.  
  44. public Tween (
  45. T from, T to,
  46. long startWhen, long endWhen,
  47. BoundInterpolator<T, Tween<T>> interpolator = null
  48. ) {
  49. From = from;
  50. To = to;
  51. StartedWhen = startWhen;
  52. EndWhen = endWhen;
  53. Interpolator = interpolator ?? Interpolators<T>.GetBoundDefault<Tween<T>>();
  54. }
  55.  
  56. public static Tween<T> StartNow (
  57. T from, T to, long? delay, long duration,
  58. long? now = null, BoundInterpolator<T, Tween<T>> interpolator = null
  59. ) {
  60. var _now = now.HasValue ? now.Value : Time.Ticks;
  61. return new Tween<T>(
  62. from, to,
  63. _now + delay.GetValueOrDefault(0),
  64. _now + delay.GetValueOrDefault(0) + duration,
  65. interpolator
  66. );
  67. }
  68.  
  69. public static Tween<T> StartNow (
  70. T from, T to, float? delaySeconds, float durationSeconds,
  71. long? now = null, BoundInterpolator<T, Tween<T>> interpolator = null
  72. ) {
  73. return StartNow(
  74. from, to,
  75. TimeSpan.FromSeconds(delaySeconds.GetValueOrDefault(0)).Ticks,
  76. TimeSpan.FromSeconds(durationSeconds).Ticks,
  77. now, interpolator
  78. );
  79. }
  80.  
  81. private float GetProgress (long now) {
  82. if ((EndWhen <= StartedWhen) || (EndWhen <= 0))
  83. return 1f;
  84.  
  85. return Arithmetic.Clamp((float)((now - StartedWhen) / (double)(EndWhen - StartedWhen)), 0, 1);
  86. }
  87.  
  88. public T Get (long now) {
  89. var progress = GetProgress(now);
  90.  
  91. // Default-init / completed
  92. if (progress >= 1f)
  93. return To;
  94. else if (progress <= 0f)
  95. return From;
  96.  
  97. return Interpolator(GetValue, ref this, 0, progress);
  98. }
  99.  
  100. public bool IsOver (long now) {
  101. if (EndWhen <= 0)
  102. return false;
  103.  
  104. return GetProgress(now) >= 1;
  105. }
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement