/Tweener.cs

http://github.com/dabrorius/MonoPunk · C# · 119 lines · 65 code · 11 blank · 43 comment · 18 complexity · 0d34886f52f7aec71c51fbc99262162d MD5 · raw file

  1. namespace Dabrorius.MonoPunk
  2. {
  3. /**
  4. * Updateable Tween container.
  5. */
  6. class Tweener
  7. {
  8. /**
  9. * Persistent Tween type, will stop when it finishes.
  10. */
  11. public const uint PERSIST = 0;
  12. /**
  13. * Looping Tween type, will restart immediately when it finishes.
  14. */
  15. public const uint LOPPING = 1;
  16. /**
  17. * Oneshot Tween type, will stop and remove itself from its core container when it finishes.
  18. */
  19. public const uint ONESHOT = 2;
  20. /**
  21. * If the Tweener should update.
  22. */
  23. public bool active = true;
  24. /**
  25. * If the Tweener should clear on removal. For Entities, this is when they are
  26. * removed from a World, and for World this is when the active World is switched.
  27. */
  28. public bool autoClear = false;
  29. /**
  30. * Constructor.
  31. */
  32. public Tweener()
  33. {
  34. }
  35. /**
  36. * Updates the Tween container.
  37. */
  38. public void update()
  39. {
  40. }
  41. /**
  42. * Adds a new Tween.
  43. * @param t The Tween to add.
  44. * @param start If the Tween should call start() immediately.
  45. * @return The added Tween.
  46. */
  47. public Tween addTween(Tween t, bool start = false)
  48. {
  49. if (t._parent != null) throw new System.ArgumentException("Cannot add a Tween object more than once.");
  50. t._parent = this;
  51. t._next = _tween;
  52. if (_tween != null) _tween._prev = t;
  53. _tween = t;
  54. if (start) _tween.start();
  55. return t;
  56. }
  57. /**
  58. * Removes a Tween.
  59. * @param t The Tween to remove.
  60. * @return The removed Tween.
  61. */
  62. public Tween removeTween(Tween t)
  63. {
  64. if (t._parent != this) throw new System.ArgumentException("Core object does not contain Tween.");
  65. if (t._next != null) t._next._prev = t._prev;
  66. if (t._prev != null) t._prev._next = t._next;
  67. else _tween = t._next;
  68. t._next = t._prev = null;
  69. t._parent = null;
  70. t.active = false;
  71. return t;
  72. }
  73. /**
  74. * Removes all Tweens.
  75. */
  76. public void clearTweens()
  77. {
  78. Tween t = _tween;
  79. Tween n;
  80. while (t != null)
  81. {
  82. n = t._next;
  83. removeTween(t);
  84. t = n;
  85. }
  86. }
  87. /**
  88. * Updates all contained tweens.
  89. */
  90. public void updateTweens()
  91. {
  92. Tween t = _tween;
  93. Tween n;
  94. while (t != null)
  95. {
  96. n = t._next;
  97. if (t.active)
  98. {
  99. t.update();
  100. if (t._finish) t.finish();
  101. }
  102. t = n;
  103. }
  104. }
  105. // List information.
  106. /** @private */ internal Tween _tween;
  107. }
  108. }