Antkeeper  0.0.1
tween.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 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 
20 #ifndef ANTKEEPER_TWEEN_HPP
21 #define ANTKEEPER_TWEEN_HPP
22 
23 #include <algorithm>
24 #include <functional>
25 #include <stdexcept>
26 #include <type_traits>
27 
34 template <class T, class S = float>
35 class tween
36 {
37 public:
38  static_assert(std::is_scalar<S>::value);
39 
40  typedef T value_type;
41  typedef S scalar_type;
42  typedef typename std::decay<std::function<value_type(const value_type&, const value_type&, scalar_type)>>::type interpolator_type;
43 
44 private:
46  static value_type interpolator_error(const value_type&, const value_type&, scalar_type);
47 
48 public:
49 
57  tween(const value_type& state0, const value_type& state1, interpolator_type interpolator = tween<T, S>::interpolator_error);
58 
65  explicit tween(const value_type& value, interpolator_type interpolator = tween<T, S>::interpolator_error);
66 
70  tween();
71 
78  const value_type& operator[](int i) const;
79 
82 
85 
93 
99  void set_interpolator(const interpolator_type& interpolator);
100 
105 
109  void update();
110 
114  void swap();
115 
116 private:
117  value_type states[2];
118  interpolator_type interpolator;
119 };
120 
121 template <class T, class S>
122 typename tween<T, S>::value_type tween<T, S>::interpolator_error(const value_type&, const value_type&, scalar_type)
123 {
124  throw std::runtime_error("tween interpolator not set");
125 }
126 
127 template <class T, class S>
128 tween<T, S>::tween(const value_type& value, interpolator_type interpolator):
129  states{value, value},
130  interpolator(interpolator)
131 {}
132 
133 template <class T, class S>
134 tween<T, S>::tween(const value_type& state0, const value_type& state1, interpolator_type interpolator):
135  states{state0, state1},
136  interpolator(interpolator)
137 {}
138 
139 template <class T, class S>
141  interpolator(nullptr)
142 {}
143 
144 template <class T, class S>
145 inline const typename tween<T, S>::value_type& tween<T, S>::operator[](int i) const
146 {
147  return states[i];
148 }
149 
150 template <class T, class S>
152 {
153  return states[i];
154 }
155 
156 template <class T, class S>
158 {
159  return interpolate(a);
160 }
161 
162 template <class T, class S>
164 {
165  return interpolator(states[0], states[1], a);
166 }
167 
168 template <class T, class S>
169 inline void tween<T, S>::set_interpolator(const interpolator_type& interpolator)
170 {
171  this->interpolator = interpolator;
172 }
173 
174 template <class T, class S>
176 {
177  return interpolator;
178 }
179 
180 template <class T, class S>
181 inline void tween<T, S>::update()
182 {
183  states[0] = states[1];
184 }
185 
186 template <class T, class S>
187 inline void tween<T, S>::swap()
188 {
189  std::swap(states[0], states[1]);
190 }
191 
192 #endif // ANTKEEPER_TWEEN_HPP
Container which stores two states along with an interpolator, for quick and easy tween<T,...
Definition: tween.hpp:36
void update()
Sets state 0 = state 1.
Definition: tween.hpp:181
tween(const value_type &state0, const value_type &state1, interpolator_type interpolator=tween< T, S >::interpolator_error)
Creates a tween.
Definition: tween.hpp:134
S scalar_type
Definition: tween.hpp:41
std::decay< std::function< value_type(const value_type &, const value_type &, scalar_type)> >::type interpolator_type
Definition: tween.hpp:42
value_type interpolate(scalar_type a) const
Returns an interpolated state between state 0 and state 1.
Definition: tween.hpp:163
tween()
Creates a tween.
Definition: tween.hpp:140
value_type operator[](scalar_type a) const
S>::interpolate(scalar_type) const
Definition: tween.hpp:157
void swap()
Swaps state 0 and state 1.
Definition: tween.hpp:187
tween(const value_type &value, interpolator_type interpolator=tween< T, S >::interpolator_error)
Creates a tween.
Definition: tween.hpp:128
void set_interpolator(const interpolator_type &interpolator)
Sets the function used to interpolate between states 0 and 1.
Definition: tween.hpp:169
T value_type
Definition: tween.hpp:38
const interpolator_type & get_interpolator() const
Returns the function used to interpolate between states 0 and 1.
Definition: tween.hpp:175
value_type & operator[](int i)
S>::operator[](int) const
Definition: tween.hpp:151
const value_type & operator[](int i) const
Returns a reference to the specified tween state.
Definition: tween.hpp:145