1 /*
2  * Copyright (c) 2021 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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_IMAGE_IMAGE_ANIMATOR_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_IMAGE_IMAGE_ANIMATOR_COMPONENT_H
18 
19 #include "core/components/declaration/image/image_animator_declaration.h"
20 #include "core/pipeline/base/component.h"
21 #include "core/pipeline/base/element.h"
22 
23 namespace OHOS::Ace {
24 
25 using AnimatorFunc = std::function<void(const std::string& method)>;
26 using AnimatorGetStatusFunc = std::function<Animator::Status()>;
27 
28 class ImageAnimatorController : public virtual AceType {
29     DECLARE_ACE_TYPE(ImageAnimatorController, AceType);
30 
31 public:
CallAnimationFunc(const std::string & method)32     void CallAnimationFunc(const std::string& method)
33     {
34         if (animatorFunc_) {
35             animatorFunc_(method);
36         }
37     }
38 
SetAnimationFunc(const AnimatorFunc & animatorFunc)39     void SetAnimationFunc(const AnimatorFunc& animatorFunc)
40     {
41         animatorFunc_ = animatorFunc;
42     }
43 
SetAnimatorGetStatusFunc(const AnimatorGetStatusFunc & animatorGetStatusFunc)44     void SetAnimatorGetStatusFunc(const AnimatorGetStatusFunc& animatorGetStatusFunc)
45     {
46         animatorGetStatusFunc_ = animatorGetStatusFunc;
47     }
48 
CallAnimatorGetStatusFunc()49     Animator::Status CallAnimatorGetStatusFunc()
50     {
51         if (animatorGetStatusFunc_) {
52             return animatorGetStatusFunc_();
53         }
54         return Animator::Status::IDLE;
55     }
56 
SetStartEvent(const EventMarker & startEvent)57     void SetStartEvent(const EventMarker& startEvent)
58     {
59         startEvent_ = startEvent;
60     }
61 
GetStartEvent()62     const EventMarker& GetStartEvent() const
63     {
64         return startEvent_;
65     }
66 
SetStopEvent(const EventMarker & stopEvent)67     void SetStopEvent(const EventMarker& stopEvent)
68     {
69         stopEvent_ = stopEvent;
70     }
71 
GetStopEvent()72     const EventMarker& GetStopEvent() const
73     {
74         return stopEvent_;
75     }
76 
SetPauseEvent(const EventMarker & pauseEvent)77     void SetPauseEvent(const EventMarker& pauseEvent)
78     {
79         pauseEvent_ = pauseEvent;
80     }
81 
GetPauseEvent()82     const EventMarker& GetPauseEvent() const
83     {
84         return pauseEvent_;
85     }
86 
SetResumeEvent(const EventMarker & resumeEvent)87     void SetResumeEvent(const EventMarker& resumeEvent)
88     {
89         resumeEvent_ = resumeEvent;
90     }
91 
GetResumeEvent()92     const EventMarker& GetResumeEvent() const
93     {
94         return resumeEvent_;
95     }
96 
SetRepeatEvent(const EventMarker & repeatEvent)97     void SetRepeatEvent(const EventMarker& repeatEvent)
98     {
99         repeatEvent_ = repeatEvent;
100     }
101 
GetRepeatEvent()102     const EventMarker& GetRepeatEvent() const
103     {
104         return repeatEvent_;
105     }
106 
SetCancelEvent(const EventMarker & cancelEvent)107     void SetCancelEvent(const EventMarker& cancelEvent)
108     {
109         cancelEvent_ = cancelEvent;
110     }
111 
GetCancelEvent()112     const EventMarker& GetCancelEvent() const
113     {
114         return cancelEvent_;
115     }
116 
117 private:
118     AnimatorFunc animatorFunc_;
119     AnimatorGetStatusFunc animatorGetStatusFunc_;
120 
121     EventMarker startEvent_;
122     EventMarker stopEvent_;
123     EventMarker pauseEvent_;
124     EventMarker resumeEvent_;
125     EventMarker repeatEvent_;
126     EventMarker cancelEvent_;
127 };
128 
129 class ACE_EXPORT ImageAnimatorComponent : public ComposedComponent {
130     DECLARE_ACE_TYPE(ImageAnimatorComponent, ComposedComponent);
131 
132 public:
133     explicit ImageAnimatorComponent(const std::string& name);
134     ~ImageAnimatorComponent() override = default;
135 
136     RefPtr<Element> CreateElement() override;
137 
138     static ComposeId GenerateComponentId();
139 
140     void SetFillMode(FillMode fillMode);
141     void SetIteration(int32_t iteration);
142     void SetDuration(int32_t duration);
143     void SetIsReverse(bool isReverse);
144     void SetIsFixedSize(bool isFixedSize);
145     void SetBorder(const Border& border);
146     void SetImageProperties(const std::vector<ImageProperties>& images);
147     void SetPreDecode(int32_t preDecode);
148     void SetStatus(Animator::Status status);
149 
150     FillMode GetFillMode() const;
151     int32_t GetIteration() const;
152     int32_t GetDuration() const;
153     int32_t GetPreDecode() const;
154     bool GetIsReverse() const;
155     bool GetIsFixedSize() const;
156     const Border& GetBorder() const;
157     const std::vector<ImageProperties>& GetImageProperties() const;
158     Animator::Status GetStatus() const;
159 
GetImageAnimatorController()160     const RefPtr<ImageAnimatorController>& GetImageAnimatorController() const
161     {
162         return imageAnimatorController_;
163     }
164 
165 private:
166     RefPtr<ImageAnimatorDeclaration> declaration_;
167     RefPtr<ImageAnimatorController> imageAnimatorController_;
168 };
169 
170 } // namespace OHOS::Ace
171 
172 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_IMAGE_IMAGE_ANIMATOR_COMPONENT_H
173