• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef META_SRC_STAGGERED_ANIMATION_STATE_H
17 #define META_SRC_STAGGERED_ANIMATION_STATE_H
18 
19 #include <algorithm>
20 #include <functional>
21 
22 #include <meta/api/property/property_event_handler.h>
23 #include <meta/base/interface_macros.h>
24 #include <meta/base/namespace.h>
25 #include <meta/ext/object_container.h>
26 #include <meta/interface/animation/intf_animation.h>
27 
28 #include "animation_state.h"
29 
META_BEGIN_NAMESPACE()30 META_BEGIN_NAMESPACE()
31 
32 namespace Internal {
33 
34 /**
35  * @brief The Base state class for animation containers.
36  */
37 class StaggeredAnimationState : public AnimationState {
38     using Super = AnimationState;
39 
40 public:
41     struct AnimationSegment {
42         IAnimation::Ptr animation_;                   // Animation in the segment
43         IAnimationController::WeakPtr controller_;    // The view animation was originally associated with (if any)
44         float startProgress_ {};                      // Progress [0..1] of the animation at start of the segment
45         float endProgress_ {};                        // Percentage [0..1] of the animation length in this segment
46         PropertyChangedEventHandler durationChanged_; // Handler for animation duration change
47         PropertyChangedEventHandler validChanged_;    // Handler for animation validity change
48     };
49 
50     using SegmentVector = BASE_NS::vector<AnimationSegment>;
51 
52 public:
53     META_NO_COPY_MOVE(StaggeredAnimationState)
54     StaggeredAnimationState() = default;
55     ~StaggeredAnimationState() override = default;
56 
57     IContainer& GetContainerInterface() noexcept
58     {
59         CORE_ASSERT(container_);
60         return *container_;
61     }
62     const IContainer& GetContainerInterface() const noexcept
63     {
64         CORE_ASSERT(container_);
65         return *container_;
66     }
67 
68     IEvent::Ptr OnChildrenChanged() const noexcept
69     {
70         return onChildrenChanged_;
71     }
72     bool IsValid() const;
73 
74     virtual void ChildrenChanged();
75 
76 protected: // AnimationState
77     bool Initialize(AnimationStateParams&& params) override;
78     void Uninitialize() override;
79     void UpdateTotalDuration() override;
80 
81 private:
82     void ChildAdded(const ChildChangedInfo& info);
83     void ChildRemoved(const ChildChangedInfo& info);
84     void ChildMoved(const ChildChangedInfo& info);
85     void RemoveChild(typename SegmentVector::iterator& item);
86 
87 protected:
88     SegmentVector& GetChildren() noexcept
89     {
90         return children_;
91     }
92     const SegmentVector& GetChildren() const noexcept
93     {
94         return children_;
95     }
96 
97     virtual IContainer::Ptr CreateContainer() const = 0;
98 
99 protected:
100     TimeSpan baseDuration_ { TimeSpan::Zero() };
101 
102 private:
103     IEvent::Ptr onChildrenChanged_;
104     IOnChanged::InterfaceTypePtr childrenChanged_;
105     EventHandler containerChanged_;
106 
107     IContainer::Ptr container_;
108     IAnimationController::WeakPtr controller_;
109     SegmentVector children_;
110 };
111 
112 /**
113  * @brief State class for parallel animations
114  */
115 class ParallelAnimationState : public StaggeredAnimationState {
116     using Super = StaggeredAnimationState;
117 
118 public:
119     AnyReturnValue Evaluate();
120 
121 protected:
122     IContainer::Ptr CreateContainer() const override;
123     void ChildrenChanged() override;
124     TimeSpan GetAnimationBaseDuration() const override;
125 };
126 
127 /**
128  * @brief State class for sequential animations
129  */
130 class SequentialAnimationState : public StaggeredAnimationState {
131     using Super = StaggeredAnimationState;
132 
133 public:
134     AnyReturnValue Evaluate();
135 
136 protected:
137     IContainer::Ptr CreateContainer() const override;
138     void ChildrenChanged() override;
139     TimeSpan GetAnimationBaseDuration() const override;
140 
141     struct ActiveAnimation {
142         const AnimationSegment* segment {};
143         int64_t index {};
144         explicit operator bool() const noexcept
145         {
146             return segment != nullptr;
147         }
148     };
149 
150     ActiveAnimation GetActiveAnimation(float progress, bool reverse) const;
151 };
152 
153 } // namespace Internal
154 
155 META_END_NAMESPACE()
156 
157 #endif // META_SRC_STAGGERED_ANIMATION_STATE_H
158