💿🐜 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.

177 lines
4.9 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_lerp(const T& x, const T& y, float a);
  34. /**
  35. * Container which stores two states along with an interpolator, for quick and easy tweening.
  36. *
  37. * @tparam T Value type.
  38. * @tparam Interpolator Interpolator function or function object type.
  39. */
  40. template <class T, class Interpolator = typename std::function<std::remove_pointer<T>::type(const T&, const T&, float)>>
  41. class tween
  42. {
  43. public:
  44. typedef typename std::remove_pointer<T>::type value_type;
  45. typedef typename std::decay<Interpolator>::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_lerp<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_lerp<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_lerp<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(float a) const;
  83. /**
  84. * Returns the function or function object that is used to interpolate between states 0 and 1.
  85. */
  86. const interpolator_type& get_interpolator() const;
  87. /**
  88. * Sets state 0 = state 1.
  89. */
  90. void update();
  91. /**
  92. * Swaps state 0 and state 1.
  93. */
  94. void swap();
  95. private:
  96. interpolator_type interpolator;
  97. T state0;
  98. T state1;
  99. };
  100. template <class T>
  101. inline T tween_default_lerp(const T& x, const T& y, float a)
  102. {
  103. return x * (1.0f - a) + y * a;
  104. }
  105. template <class T, class Interpolator>
  106. tween<T, Interpolator>::tween(const T& value, const interpolator_type& interpolator):
  107. interpolator(interpolator),
  108. state0(value),
  109. state1(value)
  110. {}
  111. template <class T, class Interpolator>
  112. tween<T, Interpolator>::tween(const T& state0, const T& state1, const interpolator_type& interpolator):
  113. interpolator(interpolator),
  114. state0(state0),
  115. state1(state1)
  116. {}
  117. template <class T, class Interpolator>
  118. tween<T, Interpolator>::tween(const interpolator_type& interpolator):
  119. interpolator(interpolator)
  120. {}
  121. template <class T, class Interpolator>
  122. inline const T& tween<T, Interpolator>::operator[](int state) const
  123. {
  124. return (state <= 0) ? state0 : state1;
  125. }
  126. template <class T, class Interpolator>
  127. inline T& tween<T, Interpolator>::operator[](int state)
  128. {
  129. return (state <= 0) ? state0 : state1;
  130. }
  131. template <class T, class Interpolator>
  132. inline typename tween<T, Interpolator>::value_type tween<T, Interpolator>::interpolate(float a) const
  133. {
  134. return interpolator(state0, state1, a);
  135. }
  136. template <class T, class Interpolator>
  137. inline const typename tween<T, Interpolator>::interpolator_type& tween<T, Interpolator>::get_interpolator() const
  138. {
  139. return interpolator;
  140. }
  141. template <class T, class Interpolator>
  142. inline void tween<T, Interpolator>::update()
  143. {
  144. state0 = state1;
  145. }
  146. template <class T, class Interpolator>
  147. inline void tween<T, Interpolator>::swap()
  148. {
  149. std::swap(state0, state1);
  150. }
  151. #endif // ANTKEEPER_TWEEN_HPP