PolyTween.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyGlobals.h"
  21. #include "PolyVector3.h"
  22. #include "PolyEventDispatcher.h"
  23. namespace Polycode {
  24. class BezierCurve;
  25. class Timer;
  26. class Quaternion;
  27. class QuaternionCurve;
  28. /**
  29. * Tween animation class. This class lets you tween a floating point value over a period of time with different easing types.
  30. */
  31. class _PolyExport Tween : public EventDispatcher {
  32. public:
  33. /**
  34. * Constructor.
  35. * @param target Pointer to the Number to tween
  36. * @param easeType Easing type. See the static members of this class for the different types of easing you can use.
  37. * @param startVal Starting value of the number at tween's start.
  38. * @param endVal The value to tween the number to.
  39. * @param time The duration of the tween.
  40. * @param repeat If true, this tween will repeat over and over.
  41. */
  42. Tween(Number *target, int easeType, Number startVal, Number endVal, Number time, bool repeat=false, bool deleteOnComplete=false, Number waitTime = 0.0);
  43. virtual ~Tween();
  44. void updateTween(Number elapsed);
  45. Number interpolateTween();
  46. virtual void updateCustomTween() {}
  47. void doOnComplete();
  48. /**
  49. * Pauses and resumes the tween.
  50. * @param pauseVal If true, pauses the tweem, if false, resumes it.
  51. */
  52. void Pause(bool pauseVal);
  53. /**
  54. * Resets the tween to starting position.
  55. */
  56. void Reset();
  57. static const int EASE_NONE = 0;
  58. static const int EASE_IN_QUAD = 1;
  59. static const int EASE_OUT_QUAD = 2;
  60. static const int EASE_INOUT_QUAD = 3;
  61. static const int EASE_IN_CUBIC= 4;
  62. static const int EASE_OUT_CUBIC= 5;
  63. static const int EASE_INOUT_CUBIC= 6;
  64. static const int EASE_IN_QUART= 7;
  65. static const int EASE_OUT_QUART= 8;
  66. static const int EASE_INOUT_QUART= 9;
  67. static const int EASE_IN_QUINT= 10;
  68. static const int EASE_OUT_QUINT= 11;
  69. static const int EASE_INOUT_QUINT= 12;
  70. static const int EASE_IN_SINE= 13;
  71. static const int EASE_OUT_SINE= 14;
  72. static const int EASE_INOUT_SINE= 15;
  73. static const int EASE_IN_EXPO= 16;
  74. static const int EASE_OUT_EXPO= 17;
  75. static const int EASE_INOUT_EXPO= 18;
  76. static const int EASE_IN_CIRC= 19;
  77. static const int EASE_OUT_CIRC= 20;
  78. static const int EASE_INOUT_CIRC= 21;
  79. static const int EASE_IN_BOUNCE= 22;
  80. static const int EASE_OUT_BOUNCE = 23;
  81. static const int EASE_INOUT_BOUNCE = 24;
  82. bool isComplete();
  83. bool repeat;
  84. bool deleteOnComplete;
  85. /*
  86. * Set a speed multiplier for the tween
  87. * @param speed Speed multiplier.
  88. */
  89. void setSpeed(Number speed);
  90. bool paused;
  91. protected:
  92. Number waitTime;
  93. int easeType;
  94. bool complete;
  95. Number endVal;
  96. Number cVal;
  97. Number startVal;
  98. Number actEndTime;
  99. Number endTime;
  100. Number *targetVal;
  101. Number localTargetVal;
  102. Number tweenTime;
  103. };
  104. /**
  105. * Tweens a position along a bezier path. This class automatically animates a 3d position over a 3d bezier curve. You can use it for 2d curves as well, of course, just ignore the z!
  106. */
  107. class _PolyExport BezierPathTween : public Tween {
  108. public:
  109. /**
  110. * Constructor.
  111. * @param target Target vector to animate.
  112. * @param curve The curve to animate along.
  113. * @param easeType Easing type (@see Tween)
  114. * @param time The duration of the tween.
  115. * @param repeat If true, this tween will repeat over and over.
  116. */
  117. BezierPathTween(Vector3 *target, BezierCurve *curve, int easeType, Number time, bool repeat=false);
  118. ~BezierPathTween();
  119. void updateCustomTween();
  120. protected:
  121. Number pathValue;
  122. Tween *pathTween;
  123. BezierCurve *curve;
  124. Vector3 *target;
  125. };
  126. class _PolyExport QuaternionTween : public Tween {
  127. public:
  128. QuaternionTween(Quaternion *target, BezierCurve *wCurve, BezierCurve *xCurve, BezierCurve *yCurve,
  129. BezierCurve *zCurve, int easeType, Number time, bool repeat=false);
  130. ~QuaternionTween();
  131. void updateCustomTween();
  132. protected:
  133. Number pathValue;
  134. Tween *pathTween;
  135. QuaternionCurve *quatCurve;
  136. Quaternion *target;
  137. };
  138. }