• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_NG_PATTERNS_IMAGE_ANIMATOR_IMAGE_ANIMATOR_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_IMAGE_ANIMATOR_IMAGE_ANIMATOR_PATTERN_H
18 
19 #include "core/components/declaration/image/image_animator_declaration.h"
20 #include "core/components_ng/base/frame_node.h"
21 #include "core/components_ng/pattern/image_animator/controlled_animator.h"
22 #include "core/components_ng/pattern/image_animator/image_animator_event_hub.h"
23 #include "core/components_ng/pattern/pattern.h"
24 
25 namespace OHOS::Ace::NG {
26 class InspectorFilter;
27 
28 class ACE_EXPORT ImageAnimatorPattern : public Pattern {
29     DECLARE_ACE_TYPE(ImageAnimatorPattern, Pattern);
30 
31 public:
32     ImageAnimatorPattern();
~ImageAnimatorPattern()33     ~ImageAnimatorPattern() override
34     {
35         controlledAnimator_ = nullptr;
36     }
37 
38     struct CacheImageStruct {
39         CacheImageStruct() = default;
CacheImageStructCacheImageStruct40         CacheImageStruct(const RefPtr<FrameNode>& imageNode) : imageNode(imageNode) {}
41         virtual ~CacheImageStruct() = default;
42         RefPtr<FrameNode> imageNode;
43         int32_t index = 0;
44         bool isLoaded = false;
45     };
46 
47     void OnModifyDone() override;
48 
49     void OnAttachToFrameNode() override;
50 
51     void OnAttachToMainTree() override;
52     void OnAttachToFrameNodeMultiThread();
53     void OnAttachToMainTreeMultiThread();
54 
IsAtomicNode()55     bool IsAtomicNode() const override
56     {
57         return true;
58     }
59 
CreateEventHub()60     RefPtr<EventHub> CreateEventHub() override
61     {
62         return MakeRefPtr<ImageAnimatorEventHub>();
63     }
64 
65     void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override;
66 
SetImages(std::vector<ImageProperties> && images)67     void SetImages(std::vector<ImageProperties>&& images)
68     {
69         if (images_.size() == images.size() && images_ == images) {
70             isImagesSame_ = true;
71             return;
72         }
73         images_ = std::move(images);
74         durationTotal_ = 0;
75         for (auto& childImage : images_) {
76             if (childImage.duration < 0) {
77                 childImage.duration = 0;
78             }
79             if ((!childImage.src.empty() || childImage.pixelMap != nullptr) && childImage.duration > 0) {
80                 durationTotal_ += childImage.duration;
81             }
82         }
83         imagesChangedFlag_ = true;
84     }
85 
SetStatus(ControlledAnimator::ControlStatus status)86     void SetStatus(ControlledAnimator::ControlStatus status)
87     {
88         status_ = status;
89     }
90 
SetFillMode(FillMode fillMode)91     void SetFillMode(FillMode fillMode)
92     {
93         controlledAnimator_->SetFillMode(fillMode);
94     }
95 
SetPreDecode(int32_t preDecode)96     void SetPreDecode(int32_t preDecode) {}
97 
SetIsReverse(bool isReverse)98     void SetIsReverse(bool isReverse)
99     {
100         isReverse_ = isReverse;
101     }
102 
SetFixedSize(bool fixedSize)103     void SetFixedSize(bool fixedSize)
104     {
105         fixedSize_ = fixedSize;
106     }
107 
OnInActive()108     void OnInActive() override
109     {
110         if (status_ == ControlledAnimator::ControlStatus::RUNNING) {
111             controlledAnimator_->Pause();
112         }
113     }
114 
OnActive()115     void OnActive() override
116     {
117         if (status_ == ControlledAnimator::ControlStatus::RUNNING &&
118             controlledAnimator_->GetControlStatus() != ControlledAnimator::ControlStatus::RUNNING) {
119             isReverse_ ? controlledAnimator_->Backward() : controlledAnimator_->Forward();
120         }
121     }
122 
123     void SetDuration(int32_t duration);
124     void SetIteration(int32_t iteration);
GetIteration()125     int32_t GetIteration()
126     {
127         return iteration_;
128     }
129 
IsReverse()130     bool IsReverse() {
131         return isReverse_;
132     }
133 
GetDuration()134     int32_t GetDuration() {
135         return controlledAnimator_->GetDuration();
136     }
137 
GetStatus()138     ControlledAnimator::ControlStatus GetStatus() {
139         return status_;
140     }
141 
IsFixedSize()142     bool IsFixedSize() {
143         return fixedSize_;
144     }
145 
GetFillMode()146     FillMode GetFillMode() {
147         return controlledAnimator_->GetFillMode();
148     }
149 
GetImagesSize()150     int32_t GetImagesSize()
151     {
152         return static_cast<int32_t>(images_.size());
153     }
154 
CheckIfNeedVisibleAreaChange()155     bool CheckIfNeedVisibleAreaChange()
156     {
157         return isAutoMonitorInvisibleArea_;
158     }
159 
SetAutoMonitorInvisibleArea(bool isAutoMonitorInvisibleArea)160     void SetAutoMonitorInvisibleArea(bool isAutoMonitorInvisibleArea)
161     {
162         isAutoMonitorInvisibleArea_ = isAutoMonitorInvisibleArea;
163     }
164 
IsEnableMatchParent()165     bool IsEnableMatchParent() override
166     {
167         return true;
168     }
169 
IsEnableFix()170     bool IsEnableFix() override
171     {
172         return true;
173     }
174 
175 private:
176     std::vector<PictureInfo> CreatePictureAnimation(int32_t size);
177     void UpdateEventCallback();
178     std::string ImagesToString() const;
179     void CheckClearUserDefinedSize(const RefPtr<LayoutProperty>& layoutProperty);
180     void AdaptSelfSize();
181     void SetShowingIndex(int32_t index);
182     void DisablePreAnimatedImageAnimation(uint32_t index);
183     void ControlAnimatedImageAnimation(const RefPtr<FrameNode>& imageFrameNode, bool play);
184     void EnableFirstAnimatedImageAnimation();
185     void UpdateShowingImageInfo(const RefPtr<FrameNode>& imageFrameNode, int32_t index);
186     void UpdateCacheImageInfo(CacheImageStruct& cacheImage, int32_t index);
187     std::list<CacheImageStruct>::iterator FindCacheImageNode(const std::string& src);
188     std::list<CacheImageStruct>::iterator FindCacheImageNode(const RefPtr<PixelMap>& src);
189     int32_t GetNextIndex(int32_t preIndex);
190     void GenerateCachedImages();
191     void AddImageLoadSuccessEvent(const RefPtr<FrameNode>& imageFrameNode);
192     static bool IsShowingSrc(const RefPtr<FrameNode>& imageFrameNode, const std::string& src);
193     static bool IsShowingSrc(const RefPtr<FrameNode>& imageFrameNode, const RefPtr<PixelMap>& src);
194     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& wrapper, const DirtySwapConfig& config) override;
195     bool IsFormRender();
196     void UpdateFormDurationByRemainder();
197     void ResetFormAnimationStartTime();
198     void ResetFormAnimationFlag();
199     void RunAnimatorByStatus(int32_t index);
200     void ShowIndex(int32_t index);
201     void UpdateBorderRadius();
202     void RegisterVisibleAreaChange();
203     void OnVisibleAreaChange(bool visible = true, double ratio = 0.0);
204 
205     int32_t iteration_ = 1;
206     RefPtr<ControlledAnimator> controlledAnimator_;
207     std::vector<ImageProperties> images_;
208     std::list<CacheImageStruct> cacheImages_;
209     ControlledAnimator::ControlStatus status_ = ControlledAnimator::ControlStatus::IDLE;
210     int32_t durationTotal_ = 0;
211     int32_t nowImageIndex_ = 0;
212     bool isReverse_ = false;
213     bool fixedSize_ = true;
214 
215     bool isImagesSame_ = false;
216     bool imagesChangedFlag_ = false;
217     bool firstUpdateEvent_ = true;
218     bool showingIndexByStoppedOrPaused_ = false;
219     bool isLayouted_ = false;
220     int64_t formAnimationStartTime_ = 0;
221     int32_t formAnimationRemainder_ = 0;
222     bool isFormAnimationStart_ = true;
223     bool isFormAnimationEnd_ = false;
224     bool isAutoMonitorInvisibleArea_ = false; // Controls whether the system's onVisibleAreaChange callback is used to
225                                               // manage the play and stop behavior of ImageAnimator.
226 };
227 
228 } // namespace OHOS::Ace::NG
229 
230 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_IMAGE_ANIMATOR_IMAGE_ANIMATOR_PATTERN_H
231