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 IsAtomicNode()51 bool IsAtomicNode() const override 52 { 53 return true; 54 } 55 CreateEventHub()56 RefPtr<EventHub> CreateEventHub() override 57 { 58 return MakeRefPtr<ImageAnimatorEventHub>(); 59 } 60 61 void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override; 62 SetImages(std::vector<ImageProperties> && images)63 void SetImages(std::vector<ImageProperties>&& images) 64 { 65 if (images_.size() == images.size() && images_ == images) { 66 isImagesSame_ = true; 67 return; 68 } 69 images_ = std::move(images); 70 durationTotal_ = 0; 71 for (const auto& childImage : images_) { 72 if ((!childImage.src.empty() || childImage.pixelMap != nullptr) && childImage.duration > 0) { 73 durationTotal_ += childImage.duration; 74 } 75 } 76 imagesChangedFlag_ = true; 77 } 78 SetStatus(ControlledAnimator::ControlStatus status)79 void SetStatus(ControlledAnimator::ControlStatus status) 80 { 81 status_ = status; 82 } 83 SetFillMode(FillMode fillMode)84 void SetFillMode(FillMode fillMode) 85 { 86 controlledAnimator_->SetFillMode(fillMode); 87 } 88 SetPreDecode(int32_t preDecode)89 void SetPreDecode(int32_t preDecode) {} 90 SetIsReverse(bool isReverse)91 void SetIsReverse(bool isReverse) 92 { 93 isReverse_ = isReverse; 94 } 95 SetFixedSize(bool fixedSize)96 void SetFixedSize(bool fixedSize) 97 { 98 fixedSize_ = fixedSize; 99 } 100 OnInActive()101 void OnInActive() override 102 { 103 if (status_ == ControlledAnimator::ControlStatus::RUNNING) { 104 controlledAnimator_->Pause(); 105 } 106 } 107 OnActive()108 void OnActive() override 109 { 110 if (status_ == ControlledAnimator::ControlStatus::RUNNING && 111 controlledAnimator_->GetControlStatus() != ControlledAnimator::ControlStatus::RUNNING) { 112 isReverse_ ? controlledAnimator_->Backward() : controlledAnimator_->Forward(); 113 } 114 } 115 116 void SetDuration(int32_t duration); 117 void SetIteration(int32_t iteration); GetIteration()118 int32_t GetIteration() 119 { 120 return iteration_; 121 } 122 IsReverse()123 bool IsReverse() { 124 return isReverse_; 125 } 126 GetDuration()127 int32_t GetDuration() { 128 return controlledAnimator_->GetDuration(); 129 } 130 GetStatus()131 ControlledAnimator::ControlStatus GetStatus() { 132 return status_; 133 } 134 IsFixedSize()135 bool IsFixedSize() { 136 return fixedSize_; 137 } 138 GetFillMode()139 FillMode GetFillMode() { 140 return controlledAnimator_->GetFillMode(); 141 } 142 GetImagesSize()143 int32_t GetImagesSize() 144 { 145 return static_cast<int32_t>(images_.size()); 146 } 147 CheckIfNeedVisibleAreaChange()148 bool CheckIfNeedVisibleAreaChange() 149 { 150 return isAutoMonitorInvisibleArea_; 151 } 152 SetAutoMonitorInvisibleArea(bool isAutoMonitorInvisibleArea)153 void SetAutoMonitorInvisibleArea(bool isAutoMonitorInvisibleArea) 154 { 155 isAutoMonitorInvisibleArea_ = isAutoMonitorInvisibleArea; 156 } 157 158 private: 159 std::vector<PictureInfo> CreatePictureAnimation(int32_t size); 160 void UpdateEventCallback(); 161 std::string ImagesToString() const; 162 void AdaptSelfSize(); 163 void SetShowingIndex(int32_t index); 164 void DisablePreAnimatedImageAnimation(uint32_t index); 165 void ControlAnimatedImageAnimation(const RefPtr<FrameNode>& imageFrameNode, bool play); 166 void EnableFirstAnimatedImageAnimation(); 167 void UpdateShowingImageInfo(const RefPtr<FrameNode>& imageFrameNode, int32_t index); 168 void UpdateCacheImageInfo(CacheImageStruct& cacheImage, int32_t index); 169 std::list<CacheImageStruct>::iterator FindCacheImageNode(const std::string& src); 170 std::list<CacheImageStruct>::iterator FindCacheImageNode(const RefPtr<PixelMap>& src); 171 int32_t GetNextIndex(int32_t preIndex); 172 void GenerateCachedImages(); 173 void AddImageLoadSuccessEvent(const RefPtr<FrameNode>& imageFrameNode); 174 static bool IsShowingSrc(const RefPtr<FrameNode>& imageFrameNode, const std::string& src); 175 static bool IsShowingSrc(const RefPtr<FrameNode>& imageFrameNode, const RefPtr<PixelMap>& src); 176 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& wrapper, const DirtySwapConfig& config) override; 177 bool IsFormRender(); 178 void UpdateFormDurationByRemainder(); 179 void ResetFormAnimationStartTime(); 180 void ResetFormAnimationFlag(); 181 void RunAnimatorByStatus(int32_t index); 182 void UpdateBorderRadius(); 183 void RegisterVisibleAreaChange(); 184 void OnVisibleAreaChange(bool visible = true, double ratio = 0.0); 185 186 int32_t iteration_ = 1; 187 RefPtr<ControlledAnimator> controlledAnimator_; 188 std::vector<ImageProperties> images_; 189 std::list<CacheImageStruct> cacheImages_; 190 ControlledAnimator::ControlStatus status_ = ControlledAnimator::ControlStatus::IDLE; 191 int32_t durationTotal_ = 0; 192 int32_t nowImageIndex_ = 0; 193 bool isReverse_ = false; 194 bool fixedSize_ = true; 195 196 bool isImagesSame_ = false; 197 bool imagesChangedFlag_ = false; 198 bool firstUpdateEvent_ = true; 199 bool isLayouted_ = false; 200 int64_t formAnimationStartTime_ = 0; 201 int32_t formAnimationRemainder_ = 0; 202 bool isFormAnimationStart_ = true; 203 bool isFormAnimationEnd_ = false; 204 bool isAutoMonitorInvisibleArea_ = false; // Controls whether the system's onVisibleAreaChange callback is used to 205 // manage the play and stop behavior of ImageAnimator. 206 }; 207 208 } // namespace OHOS::Ace::NG 209 210 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_IMAGE_ANIMATOR_IMAGE_ANIMATOR_PATTERN_H 211