TweenObjects.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #ifndef UTIL3DS_TWEENABLEOBJECTS_H
  2. #define UTIL3DS_TWEENABLEOBJECTS_H
  3. #include <TweenEngine/Tweenable.h>
  4. #include <cpp3ds/Graphics/Transformable.hpp>
  5. #include <cpp3ds/Graphics/Sprite.hpp>
  6. #include <cpp3ds/Graphics/Text.hpp>
  7. #include <cpp3ds/Graphics/Shape.hpp>
  8. #include <cpp3ds/Graphics/RectangleShape.hpp>
  9. #include <cpp3ds/Graphics/CircleShape.hpp>
  10. #include <cpp3ds/Graphics/ConvexShape.hpp>
  11. #include "GUI/NinePatch.hpp"
  12. namespace util3ds {
  13. template <class T>
  14. class TweenTransformable: public T, public TweenEngine::Tweenable
  15. {
  16. public:
  17. static const int POSITION_X = 1;
  18. static const int POSITION_Y = 2;
  19. static const int POSITION_XY = 3;
  20. static const int ROTATION = 4;
  21. static const int SCALE_X = 5;
  22. static const int SCALE_Y = 6;
  23. static const int SCALE_XY = 7;
  24. static const int ORIGIN_X = 8;
  25. static const int ORIGIN_Y = 9;
  26. static const int ORIGIN_XY = 10;
  27. protected:
  28. virtual int getValues(int tweenType, float *returnValues)
  29. {
  30. switch (tweenType) {
  31. case POSITION_X: returnValues[0] = T::getPosition().x; return 1;
  32. case POSITION_Y: returnValues[0] = T::getPosition().y; return 1;
  33. case POSITION_XY:
  34. returnValues[0] = T::getPosition().x;
  35. returnValues[1] = T::getPosition().y;
  36. return 2;
  37. case ROTATION: returnValues[0] = T::getRotation(); return 1;
  38. case SCALE_X: returnValues[0] = T::getScale().x; return 1;
  39. case SCALE_Y: returnValues[0] = T::getScale().y; return 1;
  40. case SCALE_XY:
  41. returnValues[0] = T::getScale().x;
  42. returnValues[1] = T::getScale().y;
  43. return 2;
  44. case ORIGIN_X: returnValues[0] = T::getOrigin().x; return 1;
  45. case ORIGIN_Y: returnValues[0] = T::getOrigin().y; return 1;
  46. case ORIGIN_XY:
  47. returnValues[0] = T::getOrigin().x;
  48. returnValues[1] = T::getOrigin().y;
  49. return 2;
  50. default: return -1;
  51. }
  52. }
  53. virtual void setValues(int tweenType, float *newValues)
  54. {
  55. switch (tweenType) {
  56. case POSITION_X: T::setPosition(newValues[0], T::getPosition().y); break;
  57. case POSITION_Y: T::setPosition(T::getPosition().x, newValues[0]); break;
  58. case POSITION_XY: T::setPosition(newValues[0], newValues[1]); break;
  59. case ROTATION: T::setRotation(newValues[0]); break;
  60. case SCALE_X: T::setScale(newValues[0], T::getScale().y); break;
  61. case SCALE_Y: T::setScale(T::getScale().x, newValues[0]); break;
  62. case SCALE_XY: T::setScale(newValues[0], newValues[1]); break;
  63. case ORIGIN_X: T::setOrigin(newValues[0], T::getOrigin().y); break;
  64. case ORIGIN_Y: T::setOrigin(T::getOrigin().x, newValues[0]); break;
  65. case ORIGIN_XY: T::setOrigin(newValues[0], newValues[1]); break;
  66. default:
  67. break;
  68. }
  69. }
  70. };
  71. template <class T>
  72. class TweenShape: public TweenTransformable<T>
  73. {
  74. public:
  75. static const int FILL_COLOR_RGB = 11;
  76. static const int FILL_COLOR_ALPHA = 12;
  77. static const int OUTLINE_COLOR_RGB = 13;
  78. static const int OUTLINE_COLOR_ALPHA = 14;
  79. static const int OUTLINE_THICKNESS = 15;
  80. protected:
  81. virtual int getValues(int tweenType, float *returnValues)
  82. {
  83. switch (tweenType) {
  84. case FILL_COLOR_RGB: {
  85. cpp3ds::Color color = T::getFillColor();
  86. returnValues[0] = color.r;
  87. returnValues[1] = color.g;
  88. returnValues[2] = color.b;
  89. return 3;
  90. }
  91. case OUTLINE_COLOR_RGB: {
  92. cpp3ds::Color color = T::getOutlineColor();
  93. returnValues[0] = color.r;
  94. returnValues[1] = color.g;
  95. returnValues[2] = color.b;
  96. return 3;
  97. }
  98. case FILL_COLOR_ALPHA: returnValues[0] = T::getFillColor().a; return 1;
  99. case OUTLINE_COLOR_ALPHA: returnValues[0] = T::getOutlineColor().a; return 1;
  100. case OUTLINE_THICKNESS: returnValues[0] = T::getOutlineThickness(); return 1;
  101. default:
  102. return TweenTransformable<T>::getValues(tweenType, returnValues);
  103. }
  104. }
  105. virtual void setValues(int tweenType, float *newValues)
  106. {
  107. switch (tweenType) {
  108. case FILL_COLOR_RGB: {
  109. cpp3ds::Color color;
  110. color.r = std::max(std::min(newValues[0], 255.f), 0.f);
  111. color.g = std::max(std::min(newValues[1], 255.f), 0.f);
  112. color.b = std::max(std::min(newValues[2], 255.f), 0.f);
  113. color.a = T::getFillColor().a;
  114. T::setFillColor(color);
  115. break;
  116. }
  117. case FILL_COLOR_ALPHA: {
  118. cpp3ds::Color color = T::getFillColor();
  119. color.a = std::max(std::min(newValues[0], 255.f), 0.f);
  120. T::setFillColor(color);
  121. break;
  122. }
  123. case OUTLINE_COLOR_RGB: {
  124. cpp3ds::Color color;
  125. color.r = std::max(std::min(newValues[0], 255.f), 0.f);
  126. color.g = std::max(std::min(newValues[1], 255.f), 0.f);
  127. color.b = std::max(std::min(newValues[2], 255.f), 0.f);
  128. color.a = T::getOutlineColor().a;
  129. T::setOutlineColor(color);
  130. break;
  131. }
  132. case OUTLINE_COLOR_ALPHA: {
  133. cpp3ds::Color color = T::getOutlineColor();
  134. color.a = std::max(std::min(newValues[0], 255.f), 0.f);
  135. T::setOutlineColor(color);
  136. break;
  137. }
  138. case OUTLINE_THICKNESS: T::setOutlineThickness(newValues[0]); break;
  139. default:
  140. TweenTransformable<T>::setValues(tweenType, newValues);
  141. }
  142. }
  143. };
  144. template <class T>
  145. class TweenColorTransformable : public TweenTransformable<T>
  146. {
  147. public:
  148. static const int COLOR_RGB = 11;
  149. static const int COLOR_ALPHA = 12;
  150. protected:
  151. virtual int getValues(int tweenType, float *returnValues)
  152. {
  153. switch (tweenType) {
  154. case COLOR_RGB: {
  155. cpp3ds::Color color = T::getColor();
  156. returnValues[0] = color.r;
  157. returnValues[1] = color.g;
  158. returnValues[2] = color.b;
  159. return 3;
  160. }
  161. case COLOR_ALPHA: returnValues[0] = T::getColor().a; return 1;
  162. default:
  163. return TweenTransformable<T>::getValues(tweenType, returnValues);
  164. }
  165. }
  166. virtual void setValues(int tweenType, float *newValues)
  167. {
  168. switch (tweenType) {
  169. case COLOR_RGB: {
  170. cpp3ds::Color color;
  171. color.r = std::max(std::min(newValues[0], 255.f), 0.f);
  172. color.g = std::max(std::min(newValues[1], 255.f), 0.f);
  173. color.b = std::max(std::min(newValues[2], 255.f), 0.f);
  174. color.a = T::getColor().a;
  175. T::setColor(color);
  176. break;
  177. }
  178. case COLOR_ALPHA: {
  179. cpp3ds::Color color = T::getColor();
  180. color.a = std::max(std::min(newValues[0], 255.f), 0.f);
  181. T::setColor(color);
  182. break;
  183. }
  184. default:
  185. TweenTransformable<T>::setValues(tweenType, newValues);
  186. break;
  187. }
  188. }
  189. };
  190. typedef TweenColorTransformable<gui3ds::NinePatch> TweenNinePatch;
  191. typedef TweenColorTransformable<cpp3ds::Sprite> TweenSprite;
  192. typedef TweenShape<cpp3ds::Text> TweenText;
  193. typedef TweenShape<cpp3ds::RectangleShape> TweenRectangleShape;
  194. typedef TweenShape<cpp3ds::CircleShape> TweenCircleShape;
  195. typedef TweenShape<cpp3ds::ConvexShape> TweenConvexShape;
  196. } // namespace util3ds
  197. #endif // UTIL3DS_TWEENABLEOBJECTS_H