aboutsummaryrefslogtreecommitdiff
path: root/CruUI/ui/animations/animation.cpp
blob: 26b7d5fcc7106272573d686d8266770bd9536cb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "animation.h"

#include <cassert>
#include <utility>

namespace cru::ui::animations
{
    constexpr int frame_rate = 60;
    constexpr double frame_step_time = 1.0 / frame_rate;

    AnimationManager::AnimationManager()
        : timer_action_(new Action<>([this]()
    {
        for (auto& animation : animations_)
        {
            if (animation.second->Step(frame_step_time))
                InvokeLater([=]
            {
                RemoveAnimation(animation.second); //TODO!!!
            });
        }
    }))
    {

    }

    AnimationManager::~AnimationManager()
    {
        for (auto& animation : animations_)
            delete animation.second;

        if (timer_)
            timer_->Cancel();
    }

    void AnimationManager::AddAnimation(Animation* animation)
    {
        if (animations_.empty())
            timer_ = SetInterval(frame_step_time, timer_action_);

        const auto find_result = animations_.find(animation->GetTag());
        if (find_result != animations_.cend())
            find_result->second->Cancel();
        animations_.insert_or_assign(animation->GetTag(), animation);
    }

    void AnimationManager::RemoveAnimation(Animation* animation)
    {
        const auto find_result = animations_.find(animation->GetTag());
        if (find_result != animations_.cend())
        {
            delete find_result->second;
            animations_.erase(find_result);
        }

        if (animations_.empty())
        {
            assert(timer_);
            timer_->Cancel();
            timer_ = nullptr;
        }
    }

    Animation::Animation(String tag, const double duration,
        const Vector<std::shared_ptr<Action<Animation*, double>>>& step_handlers,
        const Vector<std::shared_ptr<Action<Animation*>>>& start_handlers,
        const Vector<std::shared_ptr<Action<Animation*>>>& finish_handlers,
        const Vector<std::shared_ptr<Action<Animation*>>>& cancel_handlers
    ) : tag_(std::move(tag)), duration_(duration), step_handlers_(step_handlers),
        start_handlers_(start_handlers), finish_handlers_(finish_handlers), cancel_handlers_(cancel_handlers)
    {
        AnimationManager::GetInstance()->AddAnimation(this);
    }

    bool Animation::Step(const double time)
    {
        current_time_ += time;
        if (current_time_ > duration_)
        {
            for (auto& handler : step_handlers_)
                (*handler)(this, 1);
            for (auto& handler : finish_handlers_)
                (*handler)(this);
            return true;
        }
        else
        {
            for (auto& handler : step_handlers_)
                (*handler)(this, current_time_ / duration_);
            return false;
        }
    }

    void Animation::Cancel()
    {
        for (auto& handler : cancel_handlers_)
            (*handler)(this);
        InvokeLater([this]
        {
            AnimationManager::GetInstance()->RemoveAnimation(this); //TODO!!!
        });
    }
}