• 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/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 
28 class ACE_EXPORT ImageAnimatorPattern : public Pattern {
29     DECLARE_ACE_TYPE(ImageAnimatorPattern, Pattern);
30 
31 public:
32     ImageAnimatorPattern();
~ImageAnimatorPattern()33     ~ImageAnimatorPattern() override
34     {
35         animator_ = 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 override;
62 
SetImages(std::vector<ImageProperties> && images)63     void SetImages(std::vector<ImageProperties>&& images)
64     {
65         if (images_.size() == images.size()) {
66             return;
67         }
68         images_ = std::move(images);
69         durationTotal_ = 0;
70         for (const auto& childImage : images_) {
71             if (!childImage.src.empty() && childImage.duration > 0) {
72                 durationTotal_ += childImage.duration;
73             }
74         }
75         imagesChangedFlag_ = true;
76     }
77 
SetStatus(Animator::Status status)78     void SetStatus(Animator::Status status)
79     {
80         status_ = status;
81     }
82 
SetDuration(int32_t duration)83     void SetDuration(int32_t duration)
84     {
85         auto finalDuration = durationTotal_ > 0 ? durationTotal_ : duration;
86         if (animator_->GetDuration() == finalDuration) {
87             animator_->RemoveRepeatListener(repeatCallbackId_);
88             return;
89         }
90         if (animator_->GetStatus() == Animator::Status::IDLE || animator_->GetStatus() == Animator::Status::STOPPED) {
91             animator_->SetDuration(finalDuration);
92             animator_->RemoveRepeatListener(repeatCallbackId_);
93             return;
94         }
95         // if animator is running or paused, duration will work next time
96         animator_->RemoveRepeatListener(repeatCallbackId_);
97         repeatCallbackId_ = animator_->AddRepeatListener([weak = WeakClaim(this), finalDuration]() {
98             auto imageAnimator = weak.Upgrade();
99             CHECK_NULL_VOID(imageAnimator);
100             imageAnimator->animator_->SetDuration(finalDuration);
101         });
102     }
103 
SetIteration(int32_t iteration)104     void SetIteration(int32_t iteration)
105     {
106         animator_->SetIteration(iteration);
107     }
108 
SetFillMode(FillMode fillMode)109     void SetFillMode(FillMode fillMode)
110     {
111         animator_->SetFillMode(fillMode);
112     }
113 
SetPreDecode(int32_t preDecode)114     void SetPreDecode(int32_t preDecode) {}
115 
SetIsReverse(bool isReverse)116     void SetIsReverse(bool isReverse)
117     {
118         isReverse_ = isReverse;
119     }
120 
SetFixedSize(bool fixedSize)121     void SetFixedSize(bool fixedSize)
122     {
123         fixedSize_ = fixedSize;
124     }
125 
OnInActive()126     void OnInActive() override
127     {
128         if (status_ == Animator::Status::RUNNING) {
129             animator_->Pause();
130         }
131     }
132 
OnActive()133     void OnActive() override
134     {
135         if (status_ == Animator::Status::RUNNING && animator_->GetStatus() != Animator::Status::RUNNING) {
136             isReverse_ ? animator_->Backward() : animator_->Forward();
137         }
138     }
139 
140 private:
141     RefPtr<PictureAnimation<int32_t>> CreatePictureAnimation(int32_t size);
142     void UpdateEventCallback();
143     std::string ImagesToString() const;
144     void AdaptSelfSize();
145     void SetShowingIndex(int32_t index);
146     void UpdateShowingImageInfo(const RefPtr<FrameNode>& imageFrameNode, int32_t index);
147     void UpdateCacheImageInfo(CacheImageStruct& cacheImage, int32_t index);
148     std::list<CacheImageStruct>::iterator FindCacheImageNode(const std::string& src);
149     int32_t GetNextIndex(int32_t preIndex);
150     void GenerateCachedImages();
151     void AddImageLoadSuccessEvent(const RefPtr<FrameNode>& imageFrameNode);
152     static bool IsShowingSrc(const RefPtr<FrameNode>& imageFrameNode, const std::string& src);
153     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& wrapper, const DirtySwapConfig& config) override;
154 
155     RefPtr<Animator> animator_;
156     std::vector<ImageProperties> images_;
157     std::list<CacheImageStruct> cacheImages_;
158     Animator::Status status_ = Animator::Status::IDLE;
159     int32_t durationTotal_ = 0;
160     int32_t nowImageIndex_ = 0;
161     uint64_t repeatCallbackId_ = 0;
162     bool isReverse_ = false;
163     bool fixedSize_ = true;
164 
165     bool imagesChangedFlag_ = false;
166     bool firstUpdateEvent_ = true;
167     bool isLayouted_ = false;
168 };
169 
170 } // namespace OHOS::Ace::NG
171 
172 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_IMAGE_ANIMATOR_IMAGE_ANIMATOR_PATTERN_H
173