Tween.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "../../Include/RmlUi/Core/Tween.h"
  29. #include "../../Include/RmlUi/Core/Math.h"
  30. #include <utility>
  31. namespace Rml {
  32. namespace TweenFunctions {
  33. /**
  34. Tweening functions below.
  35. Partly based on http://libclaw.sourceforge.net/tweeners.html
  36. */
  37. static inline float square(float t)
  38. {
  39. return t * t;
  40. }
  41. static float back(float t)
  42. {
  43. return t * t * (2.70158f * t - 1.70158f);
  44. }
  45. static float bounce(float t)
  46. {
  47. if (t > 1.f - 1.f / 2.75f)
  48. return 1.f - 7.5625f * square(1.f - t);
  49. else if (t > 1.f - 2.f / 2.75f)
  50. return 1.0f - (7.5625f * square(1.f - t - 1.5f / 2.75f) + 0.75f);
  51. else if (t > 1.f - 2.5f / 2.75f)
  52. return 1.0f - (7.5625f * square(1.f - t - 2.25f / 2.75f) + 0.9375f);
  53. return 1.0f - (7.5625f * square(1.f - t - 2.625f / 2.75f) + 0.984375f);
  54. }
  55. static float circular(float t)
  56. {
  57. return 1.f - Math::SquareRoot(1.f - t * t);
  58. }
  59. static float cubic(float t)
  60. {
  61. return t * t * t;
  62. }
  63. static float elastic(float t)
  64. {
  65. if (t == 0)
  66. return t;
  67. if (t == 1)
  68. return t;
  69. return -Math::Exp(7.24f * (t - 1.f)) * Math::Sin((t - 1.1f) * 2.f * Math::RMLUI_PI / 0.4f);
  70. }
  71. static float exponential(float t)
  72. {
  73. if (t == 0)
  74. return t;
  75. if (t == 1)
  76. return t;
  77. return Math::Exp(7.24f * (t - 1.f));
  78. }
  79. static float linear(float t)
  80. {
  81. return t;
  82. }
  83. static float quadratic(float t)
  84. {
  85. return t * t;
  86. }
  87. static float quartic(float t)
  88. {
  89. return t * t * t * t;
  90. }
  91. static float quintic(float t)
  92. {
  93. return t * t * t * t * t;
  94. }
  95. static float sine(float t)
  96. {
  97. return 1.f - Math::Cos(t * Math::RMLUI_PI * 0.5f);
  98. }
  99. } // namespace TweenFunctions
  100. Tween::Tween(Type type, Direction direction)
  101. {
  102. if (direction & In)
  103. type_in = type;
  104. if (direction & Out)
  105. type_out = type;
  106. }
  107. Tween::Tween(Type type_in, Type type_out) : type_in(type_in), type_out(type_out) {}
  108. Tween::Tween(CallbackFnc callback, Direction direction) : callback(callback)
  109. {
  110. if (direction & In)
  111. type_in = Callback;
  112. if (direction & Out)
  113. type_out = Callback;
  114. }
  115. float Tween::operator()(float t) const
  116. {
  117. if (type_in != None && type_out == None)
  118. {
  119. return in(t);
  120. }
  121. if (type_in == None && type_out != None)
  122. {
  123. return out(t);
  124. }
  125. if (type_in != None && type_out != None)
  126. {
  127. return in_out(t);
  128. }
  129. return t;
  130. }
  131. void Tween::reverse()
  132. {
  133. std::swap(type_in, type_out);
  134. }
  135. bool Tween::operator==(const Tween& other) const
  136. {
  137. return type_in == other.type_in && type_out == other.type_out && callback == other.callback;
  138. }
  139. bool Tween::operator!=(const Tween& other) const
  140. {
  141. return !(*this == other);
  142. }
  143. String Tween::to_string() const
  144. {
  145. static const Array<String, size_t(Count)> type_str = {
  146. {"none", "back", "bounce", "circular", "cubic", "elastic", "exponential", "linear", "quadratic", "quartic", "quintic", "sine", "callback"}};
  147. if (size_t(type_in) < type_str.size() && size_t(type_out) < type_str.size())
  148. {
  149. if (type_in == None && type_out == None)
  150. {
  151. return "none";
  152. }
  153. else if (type_in == type_out)
  154. {
  155. return type_str[size_t(type_in)] + String("-in-out");
  156. }
  157. else if (type_in == None)
  158. {
  159. return type_str[size_t(type_out)] + String("-out");
  160. }
  161. else if (type_out == None)
  162. {
  163. return type_str[size_t(type_in)] + String("-in");
  164. }
  165. else if (type_in != type_out)
  166. {
  167. return type_str[size_t(type_in)] + String("-in-") + type_str[size_t(type_out)] + String("-out");
  168. }
  169. }
  170. return "unknown";
  171. }
  172. float Tween::tween(Type type, float t) const
  173. {
  174. using namespace TweenFunctions;
  175. switch (type)
  176. {
  177. case Back: return back(t);
  178. case Bounce: return bounce(t);
  179. case Circular: return circular(t);
  180. case Cubic: return cubic(t);
  181. case Elastic: return elastic(t);
  182. case Exponential: return exponential(t);
  183. case Linear: return linear(t);
  184. case Quadratic: return quadratic(t);
  185. case Quartic: return quartic(t);
  186. case Quintic: return quintic(t);
  187. case Sine: return sine(t);
  188. case Callback:
  189. if (callback)
  190. return (*callback)(t);
  191. break;
  192. default: break;
  193. }
  194. return t;
  195. }
  196. float Tween::in(float t) const
  197. {
  198. return tween(type_in, t);
  199. }
  200. float Tween::out(float t) const
  201. {
  202. return 1.0f - tween(type_out, 1.0f - t);
  203. }
  204. float Tween::in_out(float t) const
  205. {
  206. if (t < 0.5f)
  207. return tween(type_in, 2.0f * t) * 0.5f;
  208. else
  209. return 0.5f + out(2.0f * t - 1.0f) * 0.5f;
  210. }
  211. } // namespace Rml