15#include <egt/detail/meta.h>
16#include <egt/easing.h>
17#include <egt/geometry.h>
55template<
class Functor,
class T>
60 else if (percent > 1.0f)
64 percent = 1.0f - easing(1.0f - percent);
66 percent = easing(percent);
68 return start * (1.0f - percent) + end * percent;
87 virtual
void start() = 0;
95 virtual
bool next() = 0;
100 virtual
void stop() = 0;
105 virtual
void resume() = 0;
110 EGT_NODISCARD virtual
bool running()
const {
return m_running; }
125 m_callbacks.emplace_back(std::move(callback));
141 bool m_running{
false};
181 std::chrono::milliseconds duration,
207 void duration(std::chrono::milliseconds dur) { m_duration = dur; }
267 std::chrono::milliseconds m_duration{};
273 bool m_reverse{
false};
325 void add(
const std::shared_ptr<T>& animation)
327 add(std::dynamic_pointer_cast<detail::AnimationBase>(animation));
335 virtual void add(
const std::shared_ptr<detail::AnimationBase>& animation)
340 m_animations.push_back(animation);
342 auto weak_animation = std::weak_ptr<detail::AnimationBase>(animation);
343 animation->add_callback([
this, weak_animation](
EasingScalar)
345 auto animation = weak_animation.lock();
349 if (!animation->running())
360 if (!m_animations.empty())
361 m_animations.pop_back();
371 if (pos < m_animations.size())
372 m_animations.erase(m_animations.begin() + pos);
385 const auto i = std::find_if(m_animations.begin(), m_animations.end(),
386 [animation](
const auto & ptr)
388 return ptr.get() == animation;
390 if (i != m_animations.end())
392 m_animations.erase(i);
401 if (m_current >= m_animations.size())
404 m_animations[m_current]->stop();
406 if (++m_current >= m_animations.size())
409 m_animations[m_current]->start();
417 if (m_current >= m_animations.size())
425 m_animations[m_current]->start();
435 if (m_animations[m_current]->running())
438 if (++m_current >= m_animations.size())
452 m_animations[m_current]->start();
459 if (m_current >= m_animations.size())
462 m_animations[m_current]->stop();
469 if (m_current >= m_animations.size())
475 m_animations[m_current]->resume();
517 std::chrono::milliseconds duration,
578 std::chrono::milliseconds
duration = std::chrono::milliseconds(),
666 m_timer.on_timeout([
this]()
682 for (
const auto& c : m_callbacks)
Simple delay, useful to insert a delay in an AnimationSequence.
Definition animation.h:655
Timer m_timer
One-shot timer.
Definition animation.h:701
void resume() override
Resume the animation.
Definition animation.h:693
void start() override
Start the animation.
Definition animation.h:675
void stop() override
Stop the animation.
Definition animation.h:688
AnimationDelay(std::chrono::milliseconds duration) noexcept
Definition animation.h:663
bool next() override
Periodic call to the animation.
Definition animation.h:680
Series of animations as a single animation.
Definition animation.h:293
AnimationSequence(bool loop=false) noexcept
Definition animation.h:299
std::vector< std::shared_ptr< detail::AnimationBase > > AnimationArray
Helper type for an array of animations.
Definition animation.h:483
void resume() override
Resume the animation.
Definition animation.h:467
void start() override
Start the animation.
Definition animation.h:414
virtual void add(detail::AnimationBase &animation)
Add an animation to the animation sequence.
Definition animation.h:308
void removeAt(size_t pos)
Remove the sub animation at the given position from the sequence.
Definition animation.h:369
void stop() override
Stop the animation.
Definition animation.h:457
void remove(detail::AnimationBase *animation)
Remove the first occurence of a sub animation from the sequence.
Definition animation.h:380
virtual void add(const std::shared_ptr< detail::AnimationBase > &animation)
Add a sub animation to the sequence.
Definition animation.h:335
void add(const std::shared_ptr< T > &animation)
Add an animation to the animation sequence.
Definition animation.h:325
bool skip()
Skip to the next sub animation in the sequence.
Definition animation.h:399
bool next() override
Periodic call to the animation.
Definition animation.h:430
AnimationArray m_animations
The animations of the sequence.
Definition animation.h:486
void removeLast()
Remove the last sub animation from the sequence.
Definition animation.h:358
Animation class with configurable easing function.
Definition animation.h:168
void reverse(bool rev)
Definition animation.h:217
EGT_NODISCARD EasingScalar current() const
Get the current value.
Definition animation.h:237
void resume() override
Resume the animation from its current value.
void ending(EasingScalar end)
Definition animation.h:202
void starting(EasingScalar start)
Definition animation.h:192
void start() override
Start the animation from its start value.
void easing_func(EasingFunc func)
EGT_NODISCARD EasingScalar ending() const
Get the ending value.
Definition animation.h:197
void rounding(bool enable)
Should the value be rounded?
Definition animation.h:247
EGT_NODISCARD EasingScalar starting() const
Get the starting value.
Definition animation.h:187
void stop() override
Stop the animation.
Animation(EasingScalar start, EasingScalar end, const AnimationCallback &callback, std::chrono::milliseconds duration, EasingFunc func=easing_linear)
void duration(std::chrono::milliseconds dur)
Definition animation.h:207
std::chrono::time_point< std::chrono::steady_clock > m_intermediate_time
Absolute time used to track the animation time elapsed.
Definition animation.h:270
bool next() override
Periodic call to the animation.
Animation object with built in timer.
Definition animation.h:505
void resume() override
Resume the animation from its current value.
void start() override
Start the animation from its start value.
void interval(std::chrono::milliseconds duration)
Change the interval of the internal timer.
void stop() override
Stop the animation.
AutoAnimation(EasingScalar start, EasingScalar end, std::chrono::milliseconds duration, const EasingFunc &func=easing_linear, const AnimationCallback &callback=nullptr)
PeriodicTimer m_timer
Periodic timer used to run the animation.
Definition animation.h:548
AutoAnimation(std::chrono::milliseconds duration, const EasingFunc &func=easing_linear, const AnimationCallback &callback=nullptr)
Periodic timer.
Definition timer.h:281
Animates a property of a widget.
Definition animation.h:562
void invoke_handlers(T value)
Invoke handlers with the specified value.
Definition animation.h:619
PropertyAnimatorType(T start=T(), T end=T(), std::chrono::milliseconds duration=std::chrono::milliseconds(), const EasingFunc &func=easing_linear)
Definition animation.h:576
void clear_change_callbacks()
Clear all callbacks.
Definition animation.h:611
CallbackArray m_callbacks
Registered callbacks for the animation.
Definition animation.h:629
std::vector< PropertyCallback > CallbackArray
Property callback array type.
Definition animation.h:626
PropertyAnimatorType(std::chrono::milliseconds duration, const EasingFunc &func=easing_linear)
Definition animation.h:588
void on_change(PropertyCallback callback)
Register a callback handler for when the value changes.
Definition animation.h:605
std::function< void(T v)> PropertyCallback
Property callback type.
Definition animation.h:598
T Value
Alias for the value type of the animator.
Definition animation.h:568
Basic one shot timer.
Definition timer.h:70
Base class for an animation.
Definition animation.h:75
AnimationBase & operator=(const AnimationBase &)=delete
AnimationBase(const AnimationBase &)=delete
void clear_callbacks()
Clear all callbacks.
Definition animation.h:131
virtual ~AnimationBase() noexcept=default
AnimationBase(AnimationBase &&) noexcept=default
std::vector< AnimationCallback > m_callbacks
Registered callbacks for the animation.
Definition animation.h:144
void add_callback(AnimationCallback callback)
Register a callback for the animation.
Definition animation.h:122
EGT_API EasingScalar easing_linear(EasingScalar p)
<style>div.image img[src="linear.png"]{width:320px;}</style>
T interpolate(Functor &&easing, T percent, T start, T end, bool reverse=false)
Interpolate function used internally.
Definition animation.h:56
std::function< EasingScalar(EasingScalar percent)> EasingFunc
Easing function type.
Definition animation.h:41
std::function< void(EasingScalar value)> AnimationCallback
Animation callback type.
Definition animation.h:38
float EasingScalar
Easing function value type.
Definition animation.h:30
EGT framework namespace.
Definition animation.h:24