Tween.cpp 5.4 KB

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