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/animation/animator.h" 20 #include "core/animation/picture_animation.h" 21 #include "core/components/declaration/image/image_animator_declaration.h" 22 #include "core/components_ng/base/frame_node.h" 23 #include "core/components_ng/pattern/image_animator/image_animator_event_hub.h" 24 #include "core/components_ng/pattern/pattern.h" 25 26 namespace OHOS::Ace::NG { 27 class InspectorFilter; 28 29 class ACE_EXPORT ImageAnimatorPattern : public Pattern { 30 DECLARE_ACE_TYPE(ImageAnimatorPattern, Pattern); 31 32 public: 33 ImageAnimatorPattern(); ~ImageAnimatorPattern()34 ~ImageAnimatorPattern() override 35 { 36 animator_ = nullptr; 37 } 38 39 struct CacheImageStruct { 40 CacheImageStruct() = default; CacheImageStructCacheImageStruct41 CacheImageStruct(const RefPtr<FrameNode>& imageNode) : imageNode(imageNode) {} 42 virtual ~CacheImageStruct() = default; 43 RefPtr<FrameNode> imageNode; 44 int32_t index = 0; 45 bool isLoaded = false; 46 }; 47 48 void OnModifyDone() override; 49 50 void OnAttachToFrameNode() override; 51 IsAtomicNode()52 bool IsAtomicNode() const override 53 { 54 return true; 55 } 56 CreateEventHub()57 RefPtr<EventHub> CreateEventHub() override 58 { 59 return MakeRefPtr<ImageAnimatorEventHub>(); 60 } 61 62 void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override; 63 SetImages(std::vector<ImageProperties> && images)64 void SetImages(std::vector<ImageProperties>&& images) 65 { 66 images_ = std::move(images); 67 durationTotal_ = 0; 68 for (const auto& childImage : images_) { 69 if ((!childImage.src.empty() || childImage.pixelMap != nullptr) && childImage.duration > 0) { 70 durationTotal_ += childImage.duration; 71 } 72 } 73 imagesChangedFlag_ = true; 74 } 75 SetStatus(Animator::Status status)76 void SetStatus(Animator::Status status) 77 { 78 status_ = status; 79 } 80 SetFillMode(FillMode fillMode)81 void SetFillMode(FillMode fillMode) 82 { 83 animator_->SetFillMode(fillMode); 84 } 85 SetPreDecode(int32_t preDecode)86 void SetPreDecode(int32_t preDecode) {} 87 SetIsReverse(bool isReverse)88 void SetIsReverse(bool isReverse) 89 { 90 isReverse_ = isReverse; 91 } 92 SetFixedSize(bool fixedSize)93 void SetFixedSize(bool fixedSize) 94 { 95 fixedSize_ = fixedSize; 96 } 97 OnInActive()98 void OnInActive() override 99 { 100 if (status_ == Animator::Status::RUNNING) { 101 animator_->Pause(); 102 } 103 } 104 OnActive()105 void OnActive() override 106 { 107 if (status_ == Animator::Status::RUNNING && animator_->GetStatus() != Animator::Status::RUNNING) { 108 isReverse_ ? animator_->Backward() : animator_->Forward(); 109 } 110 } 111 112 void SetDuration(int32_t duration); 113 void SetIteration(int32_t iteration); GetIteration()114 int32_t GetIteration() 115 { 116 return iteration_; 117 } 118 IsReverse()119 bool IsReverse() { 120 return isReverse_; 121 } 122 GetDuration()123 int32_t GetDuration() { 124 return animator_->GetDuration(); 125 } 126 GetStatus()127 Animator::Status GetStatus() { 128 return status_; 129 } 130 IsFixedSize()131 bool IsFixedSize() { 132 return fixedSize_; 133 } 134 GetFillMode()135 FillMode GetFillMode() { 136 return animator_->GetFillMode(); 137 } 138 GetImagesSize()139 int32_t GetImagesSize() { 140 return static_cast<int32_t>(images_.size()); 141 } 142 143 private: 144 RefPtr<PictureAnimation<int32_t>> CreatePictureAnimation(int32_t size); 145 void UpdateEventCallback(); 146 std::string ImagesToString() const; 147 void AdaptSelfSize(); 148 void SetShowingIndex(int32_t index); 149 void DisablePreAnimatedImageAnimation(uint32_t index); 150 void ControlAnimatedImageAnimation(const RefPtr<FrameNode>& imageFrameNode, bool play); 151 void EnableFirstAnimatedImageAnimation(); 152 void UpdateShowingImageInfo(const RefPtr<FrameNode>& imageFrameNode, int32_t index); 153 void UpdateCacheImageInfo(CacheImageStruct& cacheImage, int32_t index); 154 std::list<CacheImageStruct>::iterator FindCacheImageNode(const std::string& src); 155 std::list<CacheImageStruct>::iterator FindCacheImageNode(const RefPtr<PixelMap>& src); 156 int32_t GetNextIndex(int32_t preIndex); 157 void GenerateCachedImages(); 158 void AddImageLoadSuccessEvent(const RefPtr<FrameNode>& imageFrameNode); 159 static bool IsShowingSrc(const RefPtr<FrameNode>& imageFrameNode, const std::string& src); 160 static bool IsShowingSrc(const RefPtr<FrameNode>& imageFrameNode, const RefPtr<PixelMap>& src); 161 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& wrapper, const DirtySwapConfig& config) override; 162 bool IsFormRender(); 163 void UpdateFormDurationByRemainder(); 164 void ResetFormAnimationStartTime(); 165 void ResetFormAnimationFlag(); 166 void RunAnimatorByStatus(int32_t index); 167 168 int32_t iteration_ = 1; 169 RefPtr<Animator> animator_; 170 std::vector<ImageProperties> images_; 171 std::list<CacheImageStruct> cacheImages_; 172 Animator::Status status_ = Animator::Status::IDLE; 173 int32_t durationTotal_ = 0; 174 int32_t nowImageIndex_ = 0; 175 uint64_t repeatCallbackId_ = 0; 176 bool isReverse_ = false; 177 bool fixedSize_ = true; 178 179 bool imagesChangedFlag_ = false; 180 bool firstUpdateEvent_ = true; 181 bool isLayouted_ = false; 182 int64_t formAnimationStartTime_ = 0; 183 int32_t formAnimationRemainder_ = 0; 184 bool isFormAnimationStart_ = true; 185 bool isFormAnimationEnd_ = false; 186 }; 187 188 } // namespace OHOS::Ace::NG 189 190 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_IMAGE_ANIMATOR_IMAGE_ANIMATOR_PATTERN_H 191