1 /*
2  * Copyright (c) 2020-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 #include "components/ui_image_animator.h"
17 
18 namespace OHOS {
UIImageAnimatorView()19 UIImageAnimatorView::UIImageAnimatorView()
20     : imageSrc_(nullptr),
21       imageNum_(0),
22       tickOfUpdate_(1),
23       timeOfUpdate_(DEFAULT_TASK_PERIOD),
24       timeOfPause_(0),
25       tickOfPause_(0),
26       repeatTimes_(1),
27       imageAnimator_(&imageAnimatorCallback_, this, 0, true),
28       listener_(nullptr),
29       reverse_(false),
30       repeat_(true),
31       sizeFixed_(false),
32       fillMode_(true)
33 {
34 }
35 
~UIImageAnimatorView()36 UIImageAnimatorView::~UIImageAnimatorView() {}
37 
Callback(UIView * view)38 void UIImageAnimatorView::ImageAnimatorCallback::Callback(UIView* view)
39 {
40     if (view == nullptr) {
41         return;
42     }
43     UIImageAnimatorView* imageAnimatorView = static_cast<UIImageAnimatorView*>(view);
44 
45     imageSrc_ = imageAnimatorView->GetImageAnimatorSrc();
46     imageNum_ = imageAnimatorView->GetImageAnimatorImageNum();
47     if ((imageSrc_ == nullptr) || (imageNum_ == 0) || (imageAnimatorView->tickOfUpdate_ == 0)) {
48         return;
49     }
50 
51     if (!imageAnimatorView->IsRepeat() && (repeat_ == imageAnimatorView->GetRepeatTimes())) {
52         imageAnimatorView->Stop();
53         return;
54     }
55 
56     tickNum_++;
57 
58     if (loop_ != imageNum_) {
59         if (tickNum_ < imageAnimatorView->tickOfUpdate_) {
60             return;
61         }
62     } else {
63         if (imageAnimatorView->tickOfPause_ != 0) {
64             if (tickNum_ < imageAnimatorView->tickOfPause_) {
65                 return;
66             }
67         } else {
68             if (tickNum_ < imageAnimatorView->tickOfUpdate_) {
69                 return;
70             }
71         }
72         repeat_++;
73         loop_ = 0;
74         if (!imageAnimatorView->IsRepeat() && (repeat_ == imageAnimatorView->GetRepeatTimes())) {
75             imageAnimatorView->Stop();
76             return;
77         }
78     }
79     imageAnimatorView->UpdateImage(drawingImage_, loop_);
80     tickNum_ = 0;
81 }
82 
UpdateImage(uint8_t & drawingImage,uint8_t & loop)83 void UIImageAnimatorView::UpdateImage(uint8_t& drawingImage, uint8_t& loop)
84 {
85     Invalidate();
86     drawingImage = reverse_ ? (imageNum_ - loop - 1) : loop;
87     if (drawingImage >= imageNum_) {
88         return;
89     }
90 
91     ImageAnimatorInfo* imageAnimatorInfo = &(imageSrc_[drawingImage]);
92     if (imageAnimatorInfo->imageType == IMG_SRC_FILE_PATH) {
93         SetSrc(imageAnimatorInfo->imagePath);
94     } else if (imageAnimatorInfo->imageType == IMG_SRC_IMAGE_INFO) {
95         SetSrc(imageAnimatorInfo->imageInfo);
96     }
97     if (!sizeFixed_) {
98         SetPosition(imageAnimatorInfo->pos.x, imageAnimatorInfo->pos.y);
99         SetWidth(imageAnimatorInfo->width);
100         SetHeight(imageAnimatorInfo->height);
101     }
102     Invalidate();
103     loop++;
104 }
105 
SetImageAnimatorSrc(const ImageAnimatorInfo imageAnimatorInfoSrc[],uint8_t imageNum)106 void UIImageAnimatorView::SetImageAnimatorSrc(const ImageAnimatorInfo imageAnimatorInfoSrc[], uint8_t imageNum)
107 {
108     SetImageAnimatorSrc(imageAnimatorInfoSrc, imageNum, timeOfUpdate_);
109 }
110 
SetImageAnimatorSrc(const ImageAnimatorInfo imageAnimatorInfoSrc[],uint8_t imageNum,uint16_t timeOfUpdate)111 void UIImageAnimatorView::SetImageAnimatorSrc(const ImageAnimatorInfo imageAnimatorInfoSrc[],
112                                               uint8_t imageNum,
113                                               uint16_t timeOfUpdate)
114 {
115     imageSrc_ = const_cast<ImageAnimatorInfo*>(imageAnimatorInfoSrc);
116     imageNum_ = imageNum;
117     timeOfUpdate_ = timeOfUpdate;
118     tickOfUpdate_ = GetTickByTime(timeOfUpdate);
119     return;
120 }
121 
GetImageAnimatorSrc() const122 const ImageAnimatorInfo* UIImageAnimatorView::GetImageAnimatorSrc() const
123 {
124     return imageSrc_;
125 }
126 
GetImageAnimatorImageNum() const127 uint8_t UIImageAnimatorView::GetImageAnimatorImageNum() const
128 {
129     return imageNum_;
130 }
131 
SetTimeOfUpdate(uint16_t timeOfUpdate)132 void UIImageAnimatorView::SetTimeOfUpdate(uint16_t timeOfUpdate)
133 {
134     timeOfUpdate_ = timeOfUpdate;
135     tickOfUpdate_ = GetTickByTime(timeOfUpdate);
136 }
137 
GetTimeOfUpdate() const138 uint16_t UIImageAnimatorView::GetTimeOfUpdate() const
139 {
140     return timeOfUpdate_;
141 }
142 
SetTimeOfPause(uint16_t timeOfPause)143 void UIImageAnimatorView::SetTimeOfPause(uint16_t timeOfPause)
144 {
145     timeOfPause_ = timeOfPause;
146     tickOfPause_ = GetTickByTime(timeOfPause);
147 }
148 
GetTimeOfPause() const149 uint16_t UIImageAnimatorView::GetTimeOfPause() const
150 {
151     return timeOfPause_;
152 }
153 
Start()154 void UIImageAnimatorView::Start()
155 {
156     Reset(false);
157     imageAnimator_.Start();
158 }
159 
Reset(bool fillMode)160 void UIImageAnimatorView::Reset(bool fillMode)
161 {
162     if ((imageSrc_ == nullptr) || (imageNum_ == 0)) {
163         return;
164     }
165 
166     Invalidate();
167     uint8_t drawingImage;
168     if (fillMode) {
169         drawingImage = reverse_ ? 0 : (imageNum_ - 1);
170     } else {
171         drawingImage = reverse_ ? (imageNum_ - 1) : 0;
172     }
173     ImageAnimatorInfo* imageAnimatorInfo = &(imageSrc_[drawingImage]);
174     if (imageAnimatorInfo->imageType == IMG_SRC_FILE_PATH) {
175         SetSrc(imageAnimatorInfo->imagePath);
176     } else if (imageAnimatorInfo->imageType == IMG_SRC_IMAGE_INFO) {
177         SetSrc(imageAnimatorInfo->imageInfo);
178     }
179     if (!sizeFixed_) {
180         SetPosition(imageAnimatorInfo->pos.x, imageAnimatorInfo->pos.y);
181         SetWidth(imageAnimatorInfo->width);
182         SetHeight(imageAnimatorInfo->height);
183     }
184     Invalidate();
185     imageAnimatorCallback_.Reset();
186 }
187 
Stop()188 void UIImageAnimatorView::Stop()
189 {
190     if (imageAnimator_.GetState() == Animator::STOP) {
191         return;
192     }
193 
194     imageAnimator_.Stop();
195     Reset(fillMode_);
196     if (listener_ != nullptr) {
197         listener_->OnAnimatorStop(*this);
198     }
199 }
200 
Pause()201 void UIImageAnimatorView::Pause()
202 {
203     imageAnimator_.Pause();
204 }
205 
Resume()206 void UIImageAnimatorView::Resume()
207 {
208     imageAnimator_.Resume();
209 }
210 
GetTickByTime(uint16_t time) const211 uint8_t UIImageAnimatorView::GetTickByTime(uint16_t time) const
212 {
213     uint8_t tick;
214     if ((time > 0) && (time <= DEFAULT_TASK_PERIOD)) {
215         tick = 1;
216     } else {
217         tick = time / DEFAULT_TASK_PERIOD;
218     }
219     return tick;
220 }
221 } // namespace OHOS
222