Tween.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 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. // Tweening functions below.
  34. // Partly based on http://libclaw.sourceforge.net/tweeners.html
  35. static inline float square(float t) {
  36. return t * t;
  37. }
  38. static float back(float t)
  39. {
  40. return t * t * (2.70158f * t - 1.70158f);
  41. }
  42. static float bounce(float t)
  43. {
  44. if (t > 1.f - 1.f / 2.75f)
  45. return 1.f - 7.5625f * square(1.f - t);
  46. else if (t > 1.f - 2.f / 2.75f)
  47. return 1.0f - (7.5625f * square(1.f - t - 1.5f / 2.75f) + 0.75f);
  48. else if (t > 1.f - 2.5f / 2.75f)
  49. return 1.0f - (7.5625f * square(1.f - t - 2.25f / 2.75f) + 0.9375f);
  50. return 1.0f - (7.5625f * square(1.f - t - 2.625f / 2.75f) + 0.984375f);
  51. }
  52. static float circular(float t)
  53. {
  54. return 1.f - Math::SquareRoot(1.f - t * t);
  55. }
  56. static float cubic(float t)
  57. {
  58. return t * t * t;
  59. }
  60. static float elastic(float t)
  61. {
  62. if (t == 0) return t;
  63. if (t == 1) return t;
  64. return -Math::Exp(7.24f * (t - 1.f)) * Math::Sin((t - 1.1f) * 2.f * Math::RMLUI_PI / 0.4f);
  65. }
  66. static float exponential(float t)
  67. {
  68. if (t == 0) return t;
  69. if (t == 1) return t;
  70. return Math::Exp(7.24f * (t - 1.f));
  71. }
  72. static float linear(float t)
  73. {
  74. return t;
  75. }
  76. static float quadratic(float t)
  77. {
  78. return t * t;
  79. }
  80. static float quartic(float t)
  81. {
  82. return t * t * t * t;
  83. }
  84. static float quintic(float t)
  85. {
  86. return t * t * t * t * t;
  87. }
  88. static float sine(float t)
  89. {
  90. return 1.f - Math::Cos(t * Math::RMLUI_PI * 0.5f);
  91. }
  92. } // namespace TweenFunctions
  93. Tween::Tween(Type type, Direction direction) {
  94. if (direction & In) type_in = type;
  95. if (direction & Out) type_out = type;
  96. }
  97. Tween::Tween(Type type_in, Type type_out) : type_in(type_in), type_out(type_out) {}
  98. Tween::Tween(CallbackFnc callback, Direction direction) : callback(callback) {
  99. if (direction & In) type_in = Callback;
  100. if (direction & Out) type_out = Callback;
  101. }
  102. float Tween::operator()(float t) const
  103. {
  104. if (type_in != None && type_out == None)
  105. {
  106. return in(t);
  107. }
  108. if (type_in == None && type_out != None)
  109. {
  110. return out(t);
  111. }
  112. if (type_in != None && type_out != None)
  113. {
  114. return in_out(t);
  115. }
  116. return t;
  117. }
  118. void Tween::reverse()
  119. {
  120. std::swap(type_in, type_out);
  121. }
  122. bool Tween::operator==(const Tween& other) const
  123. {
  124. return type_in == other.type_in && type_out == other.type_out && callback == other.callback;
  125. }
  126. bool Tween::operator!=(const Tween& other) const
  127. {
  128. return !(*this == other);
  129. }
  130. String Tween::to_string() const
  131. {
  132. static const Array<String, size_t(Count)> type_str = { { "none", "back", "bounce", "circular", "cubic", "elastic", "exponential", "linear", "quadratic", "quartic", "quintic", "sine", "callback" } };
  133. if (size_t(type_in) < type_str.size() && size_t(type_out) < type_str.size())
  134. {
  135. if (type_in == None && type_out == None)
  136. {
  137. return "none";
  138. }
  139. else if (type_in == type_out)
  140. {
  141. return type_str[size_t(type_in)] + String("-in-out");
  142. }
  143. else if (type_in == None)
  144. {
  145. return type_str[size_t(type_out)] + String("-out");
  146. }
  147. else if (type_out == None)
  148. {
  149. return type_str[size_t(type_in)] + String("-in");
  150. }
  151. else if (type_in != type_out)
  152. {
  153. return type_str[size_t(type_in)] + String("-in-") + type_str[size_t(type_out)] + String("-out");
  154. }
  155. }
  156. return "unknown";
  157. }
  158. float Tween::tween(Type type, float t) const
  159. {
  160. using namespace TweenFunctions;
  161. switch (type)
  162. {
  163. case Back:
  164. return back(t);
  165. case Bounce:
  166. return bounce(t);
  167. case Circular:
  168. return circular(t);
  169. case Cubic:
  170. return cubic(t);
  171. case Elastic:
  172. return elastic(t);
  173. case Exponential:
  174. return exponential(t);
  175. case Linear:
  176. return linear(t);
  177. case Quadratic:
  178. return quadratic(t);
  179. case Quartic:
  180. return quartic(t);
  181. case Quintic:
  182. return quintic(t);
  183. case Sine:
  184. return sine(t);
  185. case Callback:
  186. if (callback)
  187. return (*callback)(t);
  188. break;
  189. default:
  190. break;
  191. }
  192. return t;
  193. }
  194. float Tween::in(float t) const
  195. {
  196. return tween(type_in, t);
  197. }
  198. float Tween::out(float t) const
  199. {
  200. return 1.0f - tween(type_out, 1.0f - t);
  201. }
  202. float Tween::in_out(float t) const
  203. {
  204. if (t < 0.5f)
  205. return tween(type_in, 2.0f * t) * 0.5f;
  206. else
  207. return 0.5f + out(2.0f * t - 1.0f) * 0.5f;
  208. }
  209. } // namespace Rml