aboutsummaryrefslogtreecommitdiff
path: root/src/ui/animations/animation.cpp
blob: ca0fe8bc8957ea5b7e33896416ef43a6aea341f3 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "animation.h"

#include <utility>

namespace cru::ui::animations
{
    namespace details
    {
        class AnimationDelegateImpl;
        constexpr double frame_rate = 60;
        constexpr AnimationTimeUnit frame_step_time = AnimationTimeUnit(1) / frame_rate;

        
        class AnimationDelegateImpl : public virtual IAnimationDelegate
        {
        public:
            explicit AnimationDelegateImpl(String tag)
                : tag_(std::move(tag))
            {

            }
            AnimationDelegateImpl(const AnimationDelegateImpl& other) = delete;
            AnimationDelegateImpl(AnimationDelegateImpl&& other) = delete;
            AnimationDelegateImpl& operator=(const AnimationDelegateImpl& other) = delete;
            AnimationDelegateImpl& operator=(AnimationDelegateImpl&& other) = delete;
            ~AnimationDelegateImpl() override = default;

            void Cancel() override
            {
                AnimationManager::GetInstance()->RemoveAnimation(tag_);
            }

        private:
            String tag_;
        };


        class Animation : public Object
        {
        public:
            Animation(AnimationInfo info, AnimationDelegatePtr delegate)
                : info_(std::move(info)), delegate_(std::move(delegate))
            {
                
            }

            Animation(const Animation& other) = delete;
            Animation(Animation&& other) = delete;
            Animation& operator=(const Animation& other) = delete;
            Animation& operator=(Animation&& other) = delete;
            ~Animation() override;


            // If finish or invalid, return false.
            bool Step(AnimationTimeUnit time);

            String GetTag() const
            {
                return info_.tag;
            }

        private:
            const AnimationInfo info_;
            const AnimationDelegatePtr delegate_;

            AnimationTimeUnit current_time_ = AnimationTimeUnit::zero();
        };

        AnimationManager::AnimationManager()
        {

        }

        AnimationManager::~AnimationManager()
        {
            KillTimer();
        }

        AnimationDelegatePtr AnimationManager::CreateAnimation(AnimationInfo info)
        {
            if (animations_.empty())
                SetTimer();

            const auto tag = info.tag;
            auto delegate = std::make_shared<AnimationDelegateImpl>(tag);
            animations_[tag] = std::make_unique<Animation>(std::move(info), delegate);

            return delegate;
        }

        void AnimationManager::RemoveAnimation(const String& tag)
        {
            const auto find_result = animations_.find(tag);
            if (find_result != animations_.cend())
                animations_.erase(find_result);

            if (animations_.empty())
                KillTimer();
        }

        void AnimationManager::SetTimer()
        {
            if (!timer_.has_value())
                timer_ = SetInterval(std::chrono::duration_cast<std::chrono::milliseconds>(frame_step_time), [this]()
                {
                    auto i = animations_.cbegin();
                    while (i != animations_.cend())
                    {
                        auto current_i = i++;
                        if (current_i->second->Step(frame_step_time))
                            animations_.erase(current_i);
                    }

                    if (animations_.empty())
                        KillTimer();
                });
        }

        void AnimationManager::KillTimer()
        {
            if (timer_.has_value())
            {
                timer_.value().Cancel();
                timer_ = std::nullopt;
            }
        }

        Animation::~Animation()
        {
            if (current_time_ < info_.duration)
                for (const auto& handler : info_.cancel_handlers)
                    handler();
        }

        bool Animation::Step(const AnimationTimeUnit time)
        {
            current_time_ += time;
            if (current_time_ > info_.duration)
            {
                for (const auto& handler : info_.step_handlers)
                    handler(delegate_, 1);
                for (const auto& handler : info_.finish_handlers)
                    handler();
                return true;
            }
            else
            {
                for (const auto& handler : info_.step_handlers)
                    handler(delegate_, current_time_ / info_.duration);
                return false;
            }
        }

    }

    AnimationDelegatePtr AnimationBuilder::Start()
    {
        CheckValid();
        valid_ = false;
        return details::AnimationManager::GetInstance()->CreateAnimation(std::move(info_));
    }
}