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 #ifndef META_SRC_ANIMATION_CONTROLLER_H
16 #define META_SRC_ANIMATION_CONTROLLER_H
17 
18 #include <shared_mutex>
19 
20 #include <base/containers/vector.h>
21 
22 #include <meta/ext/attachment/attachment.h>
23 #include <meta/interface/animation/builtin_animations.h>
24 #include <meta/interface/animation/intf_animation.h>
25 #include <meta/interface/intf_attachment.h>
26 #include <meta/interface/intf_clock.h>
27 #include <meta/interface/serialization/intf_serializable.h>
28 
META_BEGIN_NAMESPACE()29 META_BEGIN_NAMESPACE()
30 
31 class AnimationController final : public META_NS::AttachmentFwd<AnimationController, ClassId::AnimationController,
32                                       IAnimationController, ISerializable, IImportFinalize> {
33 private:
34     META_IMPLEMENT_INTERFACE_READONLY_PROPERTY(IAnimationController, uint32_t, Count)
35     META_IMPLEMENT_INTERFACE_READONLY_PROPERTY(IAnimationController, uint32_t, RunningCount)
36 
37 protected:
38     bool AttachTo(const META_NS::IAttach::Ptr& target, const META_NS::IObject::Ptr& dataContext) override;
39     bool DetachFrom(const META_NS::IAttach::Ptr& target) override;
40 
41 private: // IAnimationController
42     BASE_NS::vector<IAnimation::WeakPtr> GetAnimations() const override;
43     BASE_NS::vector<IAnimation::WeakPtr> GetRunning() const override;
44     bool AddAnimation(const IAnimation::Ptr& animation) override;
45     bool RemoveAnimation(const IAnimation::Ptr& animation) override;
46     void Clear() override;
47     StepInfo Step(const IClock::ConstPtr& clock) override;
48     bool Build(const IMetadata::Ptr& data) override;
49 
50     ReturnError Export(IExportContext&) const override;
51     ReturnError Import(IImportContext&) override;
52     ReturnError Finalize(IImportFunctions&) override;
53 
54 private:
55     void UpdateAnimations();
56     void UpdateRunningHandler(const IAnimation::Ptr& animation, bool addHandler);
57 
58     IOnChanged::InterfaceTypePtr updateCallback_;             // UpdateAnimations() callback
59     mutable BASE_NS::vector<IAnimation::WeakPtr> animations_; // All animations
60     mutable BASE_NS::vector<IAnimation::WeakPtr> running_;    // Currently running animations
61     mutable std::shared_mutex mutex_;
62 };
63 
64 META_END_NAMESPACE()
65 
66 #endif
67