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

#include <unordered_map>

#include "base.h"
#include "application.h"
#include "timer.h"

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

    
    using IAnimationDelegate = ICancelable;
    using AnimationDelegatePtr = CancelablePtr;

    using AnimationStepHandlerPtr = FunctionPtr<void(AnimationDelegatePtr, double)>;
    using AnimationStartHandlerPtr = FunctionPtr<void(AnimationDelegatePtr)>;


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

        class AnimationManager : public Object
        {
        public:
            static AnimationManager* GetInstance()
            {
                return Application::GetInstance()->GetAnimationManager();
            }

        public:
            AnimationManager();
            AnimationManager(const AnimationManager& other) = delete;
            AnimationManager(AnimationManager&& other) = delete;
            AnimationManager& operator=(const AnimationManager& other) = delete;
            AnimationManager& operator=(AnimationManager&& other) = delete;
            ~AnimationManager() override;

            AnimationDelegatePtr CreateAnimation(
                String tag,
                AnimationTimeUnit duration,
                Vector<AnimationStepHandlerPtr> step_handlers,
                Vector<AnimationStartHandlerPtr> start_handlers,
                Vector<ActionPtr> finish_handlers,
                Vector<ActionPtr> cancel_handlers
            );
            void RemoveAnimation(const String& tag);

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

        private:
            std::unordered_map<String, AnimationPtr> animations_;
            std::shared_ptr<ICancelable> timer_;
            ActionPtr timer_action_;
        };
    }

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

        }

        String tag;
        AnimationTimeUnit duration;

        AnimationBuilder& AddStepHandler(AnimationStepHandlerPtr handler)
        {
            step_handlers_.push_back(std::move(handler));
            return *this;
        }

        AnimationBuilder& AddStartHandler(AnimationStartHandlerPtr handler)
        {
            start_handlers_.push_back(std::move(handler));
            return *this;
        }

        AnimationBuilder& AddFinishHandler(ActionPtr handler)
        {
            finish_handlers_.push_back(std::move(handler));
            return *this;
        }

        AnimationBuilder& AddCancelHandler(ActionPtr handler)
        {
            cancel_handlers_.push_back(std::move(handler));
            return *this;
        }

        AnimationDelegatePtr Start() const;

    private:
        Vector<AnimationStepHandlerPtr> step_handlers_;
        Vector<AnimationStartHandlerPtr> start_handlers_;
        Vector<ActionPtr> finish_handlers_;
        Vector<ActionPtr> cancel_handlers_;
    };
}