Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added wait() method to delay movement at the specified point #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions include/tween.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,24 @@ namespace tweeny {
*/
template<typename... Ds> tween<T, Ts...> & during(Ds... ds);

/**
* @brief Specifies the duration, typically in milliseconds, for the delay at the last point.
*
* You can either specify a single duration for all values or give every value its own duration. Value types
* must be convertible to the uint16_t type.
*
* **Example**:
*
* @code
* // Specify waiting at the first point (0, 0) for 100 milliseconds and then continue to (200, 300).
* auto tween = tweeny::from(0, 0).wait(100).to(200, 300).during(100, 500);
* @endcode
*
* @param ds Duration values
* @returns *this
*/
template<typename... Ds> tween<T, Ts...>& wait(Ds... ds);

/**
* @brief Steps the animation by the designated delta amount.
*
Expand Down Expand Up @@ -579,6 +597,7 @@ namespace tweeny {
void render(float p);
void dispatch(std::vector<typename traits::callbackType> & cbVector);
uint16_t pointAt(float progress) const;
void calculateTotal();
};

/**
Expand All @@ -605,7 +624,8 @@ namespace tweeny {
template<typename... Fs> tween<T> & via(const std::string & easing, Fs... fs); ///< @sa tween::via
template<typename... Fs> tween<T> & via(const char * easing, Fs... fs); ///< @sa tween::via
template<typename... Ds> tween<T> & during(Ds... ds); ///< @sa tween::during
const T & step(int32_t dt, bool suppressCallbacks = false); ///< @sa tween::step(int32_t dt, bool suppressCallbacks)
template<typename... Ds> tween<T> & wait(Ds... ds); ///< @sa tween::wait
const T & step(int32_t dt, bool suppressCallbacks = false); ///< @sa tween::step(int32_t dt, bool suppressCallbacks)
const T & step(uint32_t dt, bool suppressCallbacks = false); ///< @sa tween::step(uint32_t dt, bool suppressCallbacks)
const T & step(float dp, bool suppressCallbacks = false); ///< @sa tween::step(float dp, bool suppressCallbacks)
const T & seek(float p, bool suppressCallbacks = false); ///< @sa tween::seek(float p, bool suppressCallbacks)
Expand Down Expand Up @@ -648,7 +668,8 @@ namespace tweeny {
void render(float p);
void dispatch(std::vector<typename traits::callbackType> & cbVector);
uint16_t pointAt(float progress) const;
};
void calculateTotal();
};
}

#include "tween.tcc"
Expand Down
25 changes: 20 additions & 5 deletions include/tween.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,20 @@ namespace tweeny {
template<typename T, typename... Ts>
template<typename... Ds>
inline tween<T, Ts...> & tween<T, Ts...>::during(Ds... ds) {
total = 0;
points.at(points.size() - 2).during(ds...);
for (detail::tweenpoint<T, Ts...> & p : points) {
total += p.duration();
p.stacked = total;
}
calculateTotal();
return *this;
}

template<typename T, typename... Ts>
template<typename... Ds>
inline tween<T, Ts...>& tween<T, Ts...>::wait(Ds... ds) {
points.emplace_back(points.back());
points.at(points.size() - 2).during(ds...);
calculateTotal();
return *this;
}

template<typename T, typename... Ts>
inline const typename detail::tweentraits<T, Ts...>::valuesType & tween<T, Ts...>::step(int32_t dt, bool suppress) {
return step(static_cast<float>(dt)/static_cast<float>(total), suppress);
Expand Down Expand Up @@ -212,6 +217,16 @@ namespace tweeny {
interpolate(prog, point, values, detail::int2type<I-1>{ });
}

template<typename T, typename... Ts>
inline void tween<T, Ts...>::calculateTotal()
{
total = 0;
for(detail::tweenpoint<T, Ts...>& p : points) {
total += p.duration();
p.stacked = total;
}
}

template<typename T, typename... Ts>
inline void tween<T, Ts...>::interpolate(float prog, unsigned point, typename traits::valuesType & values, detail::int2type<0>) const {
auto & p = points.at(point);
Expand Down
27 changes: 21 additions & 6 deletions include/tweenone.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,20 @@ namespace tweeny {
template<typename T>
template<typename... Ds>
inline tween<T> & tween<T>::during(Ds... ds) {
total = 0;
points.at(points.size() - 2).during(ds...);
for (detail::tweenpoint<T> & p : points) {
total += p.duration();
p.stacked = total;
}
return *this;
calculateTotal();
return *this;
}

template<typename T>
template<typename... Ds>
inline tween<T>& tween<T>::wait(Ds... ds) {
points.emplace_back(points.back());
points.at(points.size() - 2).during(ds...);
calculateTotal();
return *this;
}

template<typename T>
inline const T & tween<T>::step(int32_t dt, bool suppress) {
return step(static_cast<float>(dt)/static_cast<float>(total), suppress);
Expand Down Expand Up @@ -207,6 +212,16 @@ namespace tweeny {
value = easing(pointTotal, std::get<0>(p.values), std::get<0>(points.at(point+1).values));
}

template<typename T>
inline void tween<T>::calculateTotal()
{
total = 0;
for(detail::tweenpoint<T>& p : points) {
total += p.duration();
p.stacked = total;
}
}

template<typename T>
inline void tween<T>::render(float p) {
currentPoint = pointAt(p);
Expand Down