Tweeny  3.2.0
A Tweening library for modern C++
tween.h
1 /*
2  This file is part of the Tweeny library.
3 
4  Copyright (c) 2016-2021 Leonardo Guilherme Lucena de Freitas
5  Copyright (c) 2016 Guilherme R. Costa
6 
7  Permission is hereby granted, free of charge, to any person obtaining a copy of
8  this software and associated documentation files (the "Software"), to deal in
9  the Software without restriction, including without limitation the rights to
10  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11  the Software, and to permit persons to whom the Software is furnished to do so,
12  subject to the following conditions:
13 
14  The above copyright notice and this permission notice shall be included in all
15  copies or substantial portions of the Software.
16 
17  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24 
30 #ifndef TWEENY_TWEEN_H
31 #define TWEENY_TWEEN_H
32 
33 #include <tuple>
34 #include <vector>
35 #include <functional>
36 
37 #include "tweentraits.h"
38 #include "tweenpoint.h"
39 
40 namespace tweeny {
47  template<typename T, typename... Ts>
48  class tween {
49  public:
57  static tween<T, Ts...> from(T t, Ts... vs);
58 
59  public:
66  tween();
67 
83  tween<T, Ts...> & to(T t, Ts... vs);
84 
109  template<typename... Fs> tween<T, Ts...> & via(Fs... fs);
110 
111 
130  template<typename... Fs> tween<T, Ts...> & via(easing::enumerated enumerated, Fs... fs);
131 
147  template<typename... Fs> tween<T, Ts...> & via(const std::string & easing, Fs... fs);
148 
164  template<typename... Fs> tween<T, Ts...> & via(const char * easing, Fs... fs);
165 
178  template<typename... Fs> tween<T, Ts...> & via(int index, Fs... fs);
179 
197  template<typename... Ds> tween<T, Ts...> & during(Ds... ds);
198 
219  const typename detail::tweentraits<T, Ts...>::valuesType & step(int32_t dt, bool suppressCallbacks = false);
220 
231  const typename detail::tweentraits<T, Ts...>::valuesType & step(uint32_t dt, bool suppressCallbacks = false);
232 
252  const typename detail::tweentraits<T, Ts...>::valuesType & step(float dp, bool suppressCallbacks = false);
253 
263  const typename detail::tweentraits<T, Ts...>::valuesType & seek(float p, bool suppressCallbacks = false);
264 
275  const typename detail::tweentraits<T, Ts...>::valuesType & seek(int32_t d, bool suppressCallbacks = false);
276 
287  const typename detail::tweentraits<T, Ts...>::valuesType & seek(uint32_t d, bool suppressCallbacks = false);
288 
320  tween<T, Ts...> & onStep(typename detail::tweentraits<T, Ts...>::callbackType callback);
321 
352  tween<T, Ts...> & onStep(typename detail::tweentraits<T, Ts...>::noValuesCallbackType callback);
353 
384  tween<T, Ts...> & onStep(typename detail::tweentraits<T, Ts...>::noTweenCallbackType callback);
385 
413  tween<T, Ts...> & onSeek(typename detail::tweentraits<T, Ts...>::callbackType callback);
414 
442  tween<T, Ts...> & onSeek(typename detail::tweentraits<T, Ts...>::noTweenCallbackType callback);
443 
471  tween<T, Ts...> & onSeek(typename detail::tweentraits<T, Ts...>::noValuesCallbackType callback);
472 
478  uint32_t duration() const;
479 
487  const typename detail::tweentraits<T, Ts...>::valuesType & peek() const;
488 
496  const typename detail::tweentraits<T, Ts...>::valuesType peek(float progress) const;
497 
498 
506  const typename detail::tweentraits<T, Ts...>::valuesType peek(uint32_t time) const;
507 
514  float progress() const;
515 
523  tween<T, Ts...> & forward();
524 
532  tween<T, Ts...> & backward();
533 
539  int direction() const;
540 
551  const typename detail::tweentraits<T, Ts...>::valuesType & jump(size_t point, bool suppressCallbacks = false);
552 
558  uint16_t point() const;
559 
560  private /* member types */:
561  using traits = detail::tweentraits<T, Ts...>;
562 
563  private /* member variables */:
564  uint32_t total = 0; // total runtime
565  uint16_t currentPoint = 0; // current point
566  float currentProgress = 0; // current progress
567  std::vector<detail::tweenpoint<T, Ts...>> points;
568  typename traits::valuesType current;
569  std::vector<typename traits::callbackType> onStepCallbacks;
570  std::vector<typename traits::callbackType> onSeekCallbacks;
571  int8_t currentDirection = 1;
572 
573  private:
574  /* member functions */
575  tween(T t, Ts... vs);
576  template<size_t I> void interpolate(float prog, unsigned point, typename traits::valuesType & values, detail::int2type<I>) const;
577  void interpolate(float prog, unsigned point, typename traits::valuesType & values, detail::int2type<0>) const;
578  void render(float p);
579  void dispatch(std::vector<typename traits::callbackType> & cbVector);
580  uint16_t pointAt(float progress) const;
581  };
582 
593  template<typename T>
594  class tween<T> {
595  public:
596  static tween<T> from(T t);
597 
598  public:
599  tween();
600  tween<T> & to(T t);
601  template<typename... Fs> tween<T> & via(Fs... fs);
602  template<typename... Fs> tween<T> & via(int index, Fs... fs);
603  template<typename... Fs> tween<T> & via(tweeny::easing::enumerated enumerated, Fs... fs);
604  template<typename... Fs> tween<T> & via(const std::string & easing, Fs... fs);
605  template<typename... Fs> tween<T> & via(const char * easing, Fs... fs);
606  template<typename... Ds> tween<T> & during(Ds... ds);
607  const T & step(int32_t dt, bool suppressCallbacks = false);
608  const T & step(uint32_t dt, bool suppressCallbacks = false);
609  const T & step(float dp, bool suppressCallbacks = false);
610  const T & seek(float p, bool suppressCallbacks = false);
611  const T & seek(int32_t d, bool suppressCallbacks = false);
612  const T & seek(uint32_t d, bool suppressCallbacks = false);
613  tween<T> & onStep(typename detail::tweentraits<T>::callbackType callback);
614  tween<T> & onStep(typename detail::tweentraits<T>::noValuesCallbackType callback);
615  tween<T> & onStep(typename detail::tweentraits<T>::noTweenCallbackType callback);
616  tween<T> & onSeek(typename detail::tweentraits<T>::callbackType callback);
617  tween<T> & onSeek(typename detail::tweentraits<T>::noValuesCallbackType callback);
618  tween<T> & onSeek(typename detail::tweentraits<T>::noTweenCallbackType callback);
619  const T & peek() const;
620  T peek(float progress) const;
621  T peek(uint32_t time) const;
622  uint32_t duration() const;
623  float progress() const;
626  int direction() const;
627  const T & jump(size_t point, bool suppressCallbacks = false);
628  uint16_t point() const;
629 
630  private /* member types */:
631  using traits = detail::tweentraits<T>;
632 
633  private /* member variables */:
634  uint32_t total = 0; // total runtime
635  uint16_t currentPoint = 0; // current point
636  float currentProgress = 0; // current progress
637  std::vector<detail::tweenpoint<T>> points;
638  T current;
639  std::vector<typename traits::callbackType> onStepCallbacks;
640  std::vector<typename traits::callbackType> onSeekCallbacks;
641  int8_t currentDirection = 1;
642 
643  private:
644  /* member functions */
645  tween(T t);
646  void interpolate(float prog, unsigned point, T & value) const;
647  void render(float p);
648  void dispatch(std::vector<typename traits::callbackType> & cbVector);
649  uint16_t pointAt(float progress) const;
650  };
651 }
652 
653 #include "tween.tcc"
654 #include "tweenone.tcc"
655 
656 #endif //TWEENY_TWEEN_H
enumerated
Enumerates all easings to aid in runtime when adding easins to a tween using tween::via.
Definition: easing.h:138
The easing class holds all the bundled easings.
Definition: easing.h:130
tween< T > & onStep(typename detail::tweentraits< T >::callbackType callback)
tween< T > & to(T t)
int direction() const
const T & step(int32_t dt, bool suppressCallbacks=false)
const T & seek(uint32_t d, bool suppressCallbacks=false)
uint16_t point() const
tween< T > & onSeek(typename detail::tweentraits< T >::noValuesCallbackType callback)
tween< T > & via(Fs... fs)
tween< T > & via(tweeny::easing::enumerated enumerated, Fs... fs)
T peek(float progress) const
tween< T > & via(const char *easing, Fs... fs)
const T & step(uint32_t dt, bool suppressCallbacks=false)
T peek(uint32_t time) const
tween< T > & during(Ds... ds)
tween< T > & via(const std::string &easing, Fs... fs)
tween< T > & via(int index, Fs... fs)
tween< T > & onStep(typename detail::tweentraits< T >::noTweenCallbackType callback)
uint32_t duration() const
const T & seek(int32_t d, bool suppressCallbacks=false)
tween< T > & backward()
tween< T > & onSeek(typename detail::tweentraits< T >::noTweenCallbackType callback)
const T & seek(float p, bool suppressCallbacks=false)
tween< T > & onSeek(typename detail::tweentraits< T >::callbackType callback)
const T & peek() const
tween< T > & onStep(typename detail::tweentraits< T >::noValuesCallbackType callback)
const T & step(float dp, bool suppressCallbacks=false)
const T & jump(size_t point, bool suppressCallbacks=false)
float progress() const
tween< T > & forward()
Class specialization when a tween has a single value.
Definition: tween.h:594
const detail::tweentraits< T, Ts... >::valuesType & seek(float p, bool suppressCallbacks=false)
Seeks to a specified point in time based on the currentProgress.
tween< T, Ts... > & onStep(typename detail::tweentraits< T, Ts... >::noValuesCallbackType callback)
Adds a callback that will be called when stepping occurs, accepting only the tween.
const detail::tweentraits< T, Ts... >::valuesType peek(float progress) const
Calculates and returns the tween values at a given progress.
tween< T, Ts... > & onSeek(typename detail::tweentraits< T, Ts... >::callbackType callback)
Adds a callback for that will be called when seeking occurs.
int direction() const
Returns the current direction of this tween.
tween< T, Ts... > & forward()
Sets the direction of this tween forward.
const detail::tweentraits< T, Ts... >::valuesType & step(int32_t dt, bool suppressCallbacks=false)
Steps the animation by the designated delta amount.
const detail::tweentraits< T, Ts... >::valuesType & seek(int32_t d, bool suppressCallbacks=false)
Seeks to a specified point in time.
tween< T, Ts... > & onSeek(typename detail::tweentraits< T, Ts... >::noValuesCallbackType callback)
Adds a callback for that will be called when seeking occurs, accepting only the tween.
uint16_t point() const
Returns the current tween point.
tween< T, Ts... > & to(T t, Ts... vs)
Adds a new point in this tweening.
const detail::tweentraits< T, Ts... >::valuesType & step(uint32_t dt, bool suppressCallbacks=false)
Steps the animation by the designated delta amount.
const detail::tweentraits< T, Ts... >::valuesType & jump(size_t point, bool suppressCallbacks=false)
Jumps to a specific tween point.
tween< T, Ts... > & onSeek(typename detail::tweentraits< T, Ts... >::noTweenCallbackType callback)
Adds a callback for that will be called when seeking occurs, accepting only the tween values.
tween()
Default constructor for a tween.
tween< T, Ts... > & during(Ds... ds)
Specifies the easing function for the last added point, accepting an easing name as a const char * va...
tween< T, Ts... > & onStep(typename detail::tweentraits< T, Ts... >::noTweenCallbackType callback)
Adds a callback that will be called when stepping occurs, accepting only the tween values.
tween< T, Ts... > & backward()
Sets the direction of this tween backward.
const detail::tweentraits< T, Ts... >::valuesType & seek(uint32_t d, bool suppressCallbacks=false)
Seeks to a specified point in time.
const detail::tweentraits< T, Ts... >::valuesType & step(float dp, bool suppressCallbacks=false)
Steps the animation by the designated percentage amount.
tween< T, Ts... > & via(Fs... fs)
Specifies the easing function for the last added point.
tween< T, Ts... > & onStep(typename detail::tweentraits< T, Ts... >::callbackType callback)
Adds a callback that will be called when stepping occurs, accepting both the tween and its values.
uint32_t duration() const
Returns the total duration of this tween.
static tween< T, Ts... > from(T t, Ts... vs)
Instantiates a tween from a starting point.
const detail::tweentraits< T, Ts... >::valuesType & peek() const
Returns the current tween values.
const detail::tweentraits< T, Ts... >::valuesType peek(uint32_t time) const
Calculates and return the tween values at a given time.
float progress() const
Returns the current currentProgress of the interpolation.
The tween class is the core class of tweeny. It controls the interpolation steps, easings and duratio...
Definition: tween.h:48
The tweeny namespace contains all symbols and names for the Tweeny library.
Definition: MANUAL.dox:1