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_ELEMENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_IMAGE_IMAGE_ANIMATOR_ELEMENT_H
18 
19 #include "core/animation/animator.h"
20 #include "core/animation/picture_animation.h"
21 #include "core/components/box/box_component.h"
22 #include "core/components/image/image_animator_component.h"
23 #include "core/components/image/image_component.h"
24 #include "core/components/page/page_element.h"
25 #include "core/pipeline/base/composed_element.h"
26 
27 namespace OHOS::Ace {
28 
29 class ImageAnimatorElement : public ComposedElement {
30     DECLARE_ACE_TYPE(ImageAnimatorElement, ComposedElement);
31 
32 public:
ImageAnimatorElement(const ComposeId & id)33     explicit ImageAnimatorElement(const ComposeId& id) : ComposedElement(id)
34     {
35         animator_ = CREATE_ANIMATOR();
36     }
37     ~ImageAnimatorElement() override;
38 
39     void Update() override;
40     void PerformBuild() override;
41 
GetStatus()42     Animator::Status GetStatus() const
43     {
44         return status_;
45     }
46 
GetDuration()47     int32_t GetDuration() const
48     {
49         return duration_;
50     }
51 
GetReverse()52     bool GetReverse() const
53     {
54         return isReverse_;
55     }
56 
GetFixedSize()57     bool GetFixedSize() const
58     {
59         return isFixedSize_;
60     }
61 
GetPreDecode()62     int32_t GetPreDecode() const
63     {
64         return preDecode_;
65     }
66 
GetFillMode()67     FillMode GetFillMode() const
68     {
69         return fillMode_;
70     }
71 
GetIteration()72     int32_t GetIteration() const
73     {
74         return iteration_;
75     }
76 
GetImages()77     std::vector<ImageProperties> GetImages() const
78     {
79         return images_;
80     }
81 
82     bool CanUpdate(const RefPtr<Component>& newComponent) override;
83 
84 protected:
85     RefPtr<Component> BuildChild() override;
86 
87 private:
88     void CreatePictureAnimation(int32_t size);
89     void PlayImageAnimator(int32_t index);
90     void UpdateFilterImages();
91     void UpdateImageSize(ImageProperties& imageProperties, const RefPtr<ImageComponent>& image);
92     void UpdatePreLoadImages(const RefPtr<BoxComponent>& box);
93     void CallAnimatorMethod(const std::string& method);
94     void UpdateCallbackAndFunc(const RefPtr<ImageAnimatorComponent>& imageAnimatorComponent);
95     Animator::Status GetAnimatorStatus() const;
96 
97     std::vector<ImageProperties> images_;
98     std::vector<ImageProperties> filterImages_;
99     RefPtr<Animator> animator_;
100     RefPtr<Component> childComponent_;
101     RefPtr<PictureAnimation<int32_t>> pictureAnimation_;
102     WeakPtr<PageElement> pageElement_;
103     Border border_;
104     Animator::Status status_ = Animator::Status::IDLE;
105     int32_t durationTotal_ = 0;
106     int32_t preDecode_ = 1;
107     int32_t callbackId_ = -1;
108     int32_t duration_ = 0; // Duration in millisecond.
109     int64_t stopCallbackId_ = -1;
110     int64_t repeatCallbackId_ = -1;
111     FillMode fillMode_ = FillMode::FORWARDS;
112     int32_t iteration_ = -1;
113     bool isPaused_ = false;
114     bool isReverse_ = false;
115     bool isFixedSize_ = true;
116     bool isSetDuration_ = false;
117     bool isResetBox_ = false;
118 };
119 
120 } // namespace OHOS::Ace
121 
122 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_IMAGE_IMAGE_ANIMATOR_ELEMENT_H
123