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