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