💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
5.0 KiB

  1. /*
  2. * Copyright (C) 2020 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_TWEEN_HPP
  20. #define ANTKEEPER_TWEEN_HPP
  21. #include <algorithm>
  22. #include <functional>
  23. #include <type_traits>
  24. /**
  25. * Linearly interpolates between two values.
  26. *
  27. * @param x Start of the range in which to interpolate.
  28. * @param y End of the range in which to interpolate.
  29. * @param a Value used to interpolate between @p x and @p y.
  30. * @return Interpolated value.
  31. */
  32. template <class T>
  33. T tween_default_interpolator(const T& x, const T& y, double a);
  34. /**
  35. * Container which stores two states along with an interpolator, for quick and easy tweening.
  36. *
  37. * @tparam T Value type.
  38. */
  39. template <class T>
  40. class tween
  41. {
  42. public:
  43. //typedef typename std::remove_pointer<T>::type value_type;
  44. typedef T value_type;
  45. typedef typename std::decay<std::function<value_type(const T&, const T&, double)>>::type interpolator_type;
  46. /**
  47. * Creates a tween.
  48. *
  49. * @param state0 Initial value of state 0.
  50. * @param state1 Initial value of state 1.
  51. * @param interpolator Function or function object that will be used to interpolate between states 0 and 1.
  52. */
  53. explicit tween(const T& state0, const T& state1, const interpolator_type& interpolator = tween_default_interpolator<T>);
  54. /**
  55. * Creates a tween.
  56. *
  57. * @param value Initial value of states 0 and 1.
  58. * @param interpolator Function or function object that will be used to interpolate between states 0 and 1.
  59. */
  60. explicit tween(const T& value, const interpolator_type& interpolator = tween_default_interpolator<T>);
  61. /**
  62. * Creates a tween.
  63. *
  64. * @param interpolator Function or function object that will be used to interpolate between states 0 and 1.
  65. */
  66. explicit tween(const interpolator_type& interpolator = tween_default_interpolator<T>);
  67. /**
  68. * Returns a reference to the specified tween state.
  69. *
  70. * @param state Index of a tween state. Should be either `0` or `1`.
  71. * @return Reference to the specified tween state.
  72. */
  73. const T& operator[](int state) const;
  74. /// @copydoc tween::operator[](int) const
  75. T& operator[](int state);
  76. /**
  77. * Returns an interpolated state between state 0 and state 1.
  78. *
  79. * @param a Interpolation factor. Should be on `[0.0, 1.0]`.
  80. * @return Interpolated state.
  81. */
  82. value_type interpolate(double a) const;
  83. /**
  84. * Sets the function object used to interpolate between states 0 and 1.
  85. *
  86. * @param interpolator Interpolator function object.
  87. */
  88. void set_interpolator(const interpolator_type& interpolator);
  89. /**
  90. * Returns the function or function object that is used to interpolate between states 0 and 1.
  91. */
  92. const interpolator_type& get_interpolator() const;
  93. /**
  94. * Sets state 0 = state 1.
  95. */
  96. void update();
  97. /**
  98. * Swaps state 0 and state 1.
  99. */
  100. void swap();
  101. private:
  102. interpolator_type interpolator;
  103. T state0;
  104. T state1;
  105. };
  106. template <class T>
  107. inline T tween_default_interpolator(const T& x, const T& y, double a)
  108. {
  109. //return x * (1.0 - a) + y * a;
  110. return y;
  111. }
  112. template <>
  113. inline float tween_default_interpolator<float>(const float& x, const float& y, double a)
  114. {
  115. return (y - x) * a + x;
  116. }
  117. template <class T>
  118. tween<T>::tween(const T& value, const interpolator_type& interpolator):
  119. interpolator(interpolator),
  120. state0(value),
  121. state1(value)
  122. {}
  123. template <class T>
  124. tween<T>::tween(const T& state0, const T& state1, const interpolator_type& interpolator):
  125. interpolator(interpolator),
  126. state0(state0),
  127. state1(state1)
  128. {}
  129. template <class T>
  130. tween<T>::tween(const interpolator_type& interpolator):
  131. interpolator(interpolator)
  132. {}
  133. template <class T>
  134. inline const T& tween<T>::operator[](int state) const
  135. {
  136. return (state <= 0) ? state0 : state1;
  137. }
  138. template <class T>
  139. inline T& tween<T>::operator[](int state)
  140. {
  141. return (state <= 0) ? state0 : state1;
  142. }
  143. template <class T>
  144. inline typename tween<T>::value_type tween<T>::interpolate(double a) const
  145. {
  146. return interpolator(state0, state1, a);
  147. }
  148. template <class T>
  149. inline void tween<T>::set_interpolator(const interpolator_type& interpolator)
  150. {
  151. this->interpolator = interpolator;
  152. }
  153. template <class T>
  154. inline const typename tween<T>::interpolator_type& tween<T>::get_interpolator() const
  155. {
  156. return interpolator;
  157. }
  158. template <class T>
  159. inline void tween<T>::update()
  160. {
  161. state0 = state1;
  162. }
  163. template <class T>
  164. inline void tween<T>::swap()
  165. {
  166. std::swap(state0, state1);
  167. }
  168. #endif // ANTKEEPER_TWEEN_HPP