aboutsummaryrefslogtreecommitdiff
path: root/src/ui/animations/animation.hpp
blob: f25e46995566ce054c20173ab52780c3f7c4bd00 (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
#pragma once

#include <unordered_map>

#include "base.hpp"
#include "timer.hpp"

namespace cru::ui::animations
{
    using AnimationTimeUnit = FloatSecond;

    struct IAnimationDelegate : virtual Interface
    {
        virtual void Cancel() = 0;
    };

    using AnimationDelegatePtr = std::shared_ptr<IAnimationDelegate>;

    using AnimationStepHandler = std::function<void(AnimationDelegatePtr, double)>;
    using AnimationStartHandler = std::function<void(AnimationDelegatePtr)>;
    using AnimationFinishHandler = std::function<void()>;
    using AnimationCancelHandler = std::function<void()>;

    namespace details
    {
        class Animation;
        using AnimationPtr = std::unique_ptr<Animation>;

        class AnimationInfo
        {
        public:
            AnimationInfo(String tag, const AnimationTimeUnit duration)
                : tag(std::move(tag)),
                  duration(duration)
            {

            }
            AnimationInfo(const AnimationInfo& other) = default;
            AnimationInfo(AnimationInfo&& other) = default;
            AnimationInfo& operator=(const AnimationInfo& other) = default;
            AnimationInfo& operator=(AnimationInfo&& other) = default;
            ~AnimationInfo() = default;

            String tag;
            AnimationTimeUnit duration;
            std::vector<AnimationStepHandler> step_handlers{};
            std::vector<AnimationStartHandler> start_handlers{};
            std::vector<AnimationFinishHandler> finish_handlers{};
            std::vector<AnimationCancelHandler> cancel_handlers{};
        };

        class AnimationManager : public Object
        {
        public:
            static AnimationManager* GetInstance();
        private:
            AnimationManager();
        public:
            AnimationManager(const AnimationManager& other) = delete;
            AnimationManager(AnimationManager&& other) = delete;
            AnimationManager& operator=(const AnimationManager& other) = delete;
            AnimationManager& operator=(AnimationManager&& other) = delete;
            ~AnimationManager() override;

            AnimationDelegatePtr CreateAnimation(AnimationInfo info);
            void RemoveAnimation(const String& tag);

        private:
            void SetTimer();
            void KillTimer();

        private:
            std::unordered_map<String, AnimationPtr> animations_;
            std::optional<TimerTask> timer_;
        };
    }

    class AnimationBuilder : public Object
    {
    public:
        AnimationBuilder(String tag, const AnimationTimeUnit duration)
            : info_(std::move(tag), duration)
        {

        }

        AnimationBuilder& AddStepHandler(const AnimationStepHandler& handler)
        {
            CheckValid();
            info_.step_handlers.push_back(handler);
            return *this;
        }

        AnimationBuilder& AddStartHandler(const AnimationStartHandler& handler)
        {
            CheckValid();
            info_.start_handlers.push_back(handler);
            return *this;
        }

        AnimationBuilder& AddFinishHandler(const AnimationFinishHandler& handler)
        {
            CheckValid();
            info_.finish_handlers.push_back(handler);
            return *this;
        }

        AnimationBuilder& AddCancelHandler(const AnimationCancelHandler& handler)
        {
            CheckValid();
            info_.cancel_handlers.push_back(handler);
            return *this;
        }

        AnimationDelegatePtr Start();

    private:
        void CheckValid() const
        {
            if (!valid_)
                throw std::runtime_error("The animation builder is invalid.");
        }

        bool valid_ = true;
        details::AnimationInfo info_;
    };
}