Tweeny  3
A Tweening library for modern C++
tween.h
1 /*
2  This file is part of the Tweeny library.
3 
4  Copyright (c) 2016-2018 Leonardo G. 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 
123  template<typename... Fs> tween<T, Ts...> & via(int index, Fs... fs);
124 
142  template<typename... Ds> tween<T, Ts...> & during(Ds... ds);
143 
164  const typename detail::tweentraits<T, Ts...>::valuesType & step(int32_t dt, bool suppressCallbacks = false);
165 
176  const typename detail::tweentraits<T, Ts...>::valuesType & step(uint32_t dt, bool suppressCallbacks = false);
177 
197  const typename detail::tweentraits<T, Ts...>::valuesType & step(float dp, bool suppressCallbacks = false);
198 
208  const typename detail::tweentraits<T, Ts...>::valuesType & seek(float p, bool suppressCallbacks = false);
209 
220  const typename detail::tweentraits<T, Ts...>::valuesType & seek(int32_t d, bool suppressCallbacks = false);
221 
232  const typename detail::tweentraits<T, Ts...>::valuesType & seek(uint32_t d, bool suppressCallbacks = false);
233 
265  tween<T, Ts...> & onStep(typename detail::tweentraits<T, Ts...>::callbackType callback);
266 
297  tween<T, Ts...> & onStep(typename detail::tweentraits<T, Ts...>::noValuesCallbackType callback);
298 
329  tween<T, Ts...> & onStep(typename detail::tweentraits<T, Ts...>::noTweenCallbackType callback);
330 
358  tween<T, Ts...> & onSeek(typename detail::tweentraits<T, Ts...>::callbackType callback);
359 
387  tween<T, Ts...> & onSeek(typename detail::tweentraits<T, Ts...>::noTweenCallbackType callback);
388 
416  tween<T, Ts...> & onSeek(typename detail::tweentraits<T, Ts...>::noValuesCallbackType callback);
417 
423  uint32_t duration() const;
424 
432  const typename detail::tweentraits<T, Ts...>::valuesType & peek() const;
433 
441  const typename detail::tweentraits<T, Ts...>::valuesType peek(float progress) const;
442 
443 
451  const typename detail::tweentraits<T, Ts...>::valuesType peek(uint32_t time) const;
452 
459  float progress() const;
460 
468  tween<T, Ts...> & forward();
469 
477  tween<T, Ts...> & backward();
478 
484  int direction() const;
485 
496  const typename detail::tweentraits<T, Ts...>::valuesType & jump(int32_t point, bool suppressCallbacks = false);
497 
503  uint16_t point() const;
504 
505  private /* member types */:
506  using traits = detail::tweentraits<T, Ts...>;
507 
508  private /* member variables */:
509  uint32_t total = 0; // total runtime
510  uint16_t currentPoint = 0; // current currentPoint
511  float currentProgress = 0; // current currentProgress
512  std::vector<detail::tweenpoint<T, Ts...>> points;
513  typename traits::valuesType current;
514  std::vector<typename traits::callbackType> onStepCallbacks;
515  std::vector<typename traits::callbackType> onSeekCallbacks;
516  int8_t currentDirection = 1;
517 
518  private:
519  /* member functions */
520  tween(T t, Ts... vs);
521  template<size_t I> void interpolate(float prog, unsigned point, typename traits::valuesType & values, detail::int2type<I>) const;
522  void interpolate(float prog, unsigned point, typename traits::valuesType & values, detail::int2type<0>) const;
523  void render(float p);
524  void dispatch(std::vector<typename traits::callbackType> & cbVector);
525  uint16_t pointAt(float progress) const;
526  };
527 
538  template<typename T>
539  class tween<T> {
540  public:
541  static tween<T> from(T t);
542 
543  public:
544  tween();
545  tween<T> & to(T t);
546  template<typename... Fs> tween<T> & via(Fs... fs);
547  template<typename... Fs> tween<T> & via(int index, Fs... fs);
548  template<typename... Ds> tween<T> & during(Ds... ds);
549  const T & step(int32_t dt, bool suppressCallbacks = false);
550  const T & step(uint32_t dt, bool suppressCallbacks = false);
551  const T & step(float dp, bool suppressCallbacks = false);
552  const T & seek(float p, bool suppressCallbacks = false);
553  const T & seek(int32_t d, bool suppressCallbacks = false);
554  const T & seek(uint32_t d, bool suppressCallbacks = false);
555  tween<T> & onStep(typename detail::tweentraits<T>::callbackType callback);
556  tween<T> & onStep(typename detail::tweentraits<T>::noValuesCallbackType callback);
557  tween<T> & onStep(typename detail::tweentraits<T>::noTweenCallbackType callback);
558  tween<T> & onSeek(typename detail::tweentraits<T>::callbackType callback);
559  tween<T> & onSeek(typename detail::tweentraits<T>::noValuesCallbackType callback);
560  tween<T> & onSeek(typename detail::tweentraits<T>::noTweenCallbackType callback);
561  const T & peek() const;
562  T peek(float progress) const;
563  T peek(uint32_t time) const;
564  uint32_t duration() const;
565  float progress() const;
566  tween<T> & forward();
567  tween<T> & backward();
568  int direction() const;
569  const T & jump(int32_t point, bool suppressCallbacks = false);
570  uint16_t point() const;
571 
572  private /* member types */:
573  using traits = detail::tweentraits<T>;
574 
575  private /* member variables */:
576  uint32_t total = 0; // total runtime
577  uint16_t currentPoint = 0; // current currentPoint
578  float currentProgress = 0; // current currentProgress
579  std::vector<detail::tweenpoint<T>> points;
580  T current;
581  std::vector<typename traits::callbackType> onStepCallbacks;
582  std::vector<typename traits::callbackType> onSeekCallbacks;
583  int8_t currentDirection = 1;
584 
585  private:
586  /* member functions */
587  tween(T t);
588  void interpolate(float prog, unsigned point, T & value) const;
589  void render(float p);
590  void dispatch(std::vector<typename traits::callbackType> & cbVector);
591  uint16_t pointAt(float progress) const;
592  };
593 }
594 
595 #include "tween.tcc"
596 #include "tweenone.tcc"
597 
598 #endif //TWEENY_TWEEN_H
Class specialization when a tween has a single value.
Definition: tween.h:539
uint32_t duration() const
Returns the total duration of this tween.
The tween class is the core class of tweeny. It controls the interpolation steps, easings and duratio...
Definition: tween.h:48
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...
tween()
Default constructor for a tween.
The tweeny namespace contains all symbols and names for the Tweeny library.
Definition: MANUAL.dox:1
const detail::tweentraits< T, Ts... >::valuesType & peek() const
Returns the current tween values.
tween< T, Ts... > & via(Fs... fs)
Specifies the easing function for the last added currentPoint.
uint16_t point() const
Returns the current tween point.
const detail::tweentraits< T, Ts... >::valuesType & jump(int32_t point, bool suppressCallbacks=false)
Jumps to a specific tween currentPoint.
tween< T, Ts... > & during(Ds... ds)
Specifies the duration, typically in milliseconds, for the tweening of values in last currentPoint...
float progress() const
Returns the current currentProgress of the interpolation.
static tween< T, Ts... > from(T t, Ts... vs)
Instantiates a tween from a starting currentPoint.
tween< T, Ts... > & onSeek(typename detail::tweentraits< T, Ts... >::callbackType callback)
Adds a callback for that will be called when seeking occurs.
tween< T, Ts... > & backward()
Sets the direction of this tween backward.
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(float p, bool suppressCallbacks=false)
Seeks to a specified currentPoint in time based on the currentProgress.
tween< T, Ts... > & forward()
Sets the direction of this tween forward.
int direction() const
Returns the current direction of this tween.
tween< T, Ts... > & to(T t, Ts... vs)
Adds a new currentPoint in this tweening.