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 /**
17  * @addtogroup UI_Components
18  * @{
19  *
20  * @brief Defines UI components such as buttons, texts, images, lists, and progress bars.
21  *
22  * @since 1.0
23  * @version 1.0
24  */
25 
26 /**
27  * @file ui_image_animator.h
28  *
29  * @brief Defines the attributes and functions of an image animator.
30  *
31  * @since 1.0
32  * @version 1.0
33  */
34 
35 #ifndef GRAPHIC_LITE_UI_IMAGE_ANIMATOR_H
36 #define GRAPHIC_LITE_UI_IMAGE_ANIMATOR_H
37 
38 #include "animator/animator.h"
39 #include "components/ui_image_view.h"
40 #include "gfx_utils/graphic_types.h"
41 
42 namespace OHOS {
43 /**
44  * @brief Provides information about the images.
45  */
46 struct ImageAnimatorInfo {
47     union {
48         /** Image path */
49         const char* imagePath;
50         /** Pointer to the {@link ImageInfo} structure */
51         const ImageInfo* imageInfo;
52     };
53     /** Image start position, relative to this view */
54     Point pos;
55     /** Image width */
56     int16_t width;
57     /** Image height */
58     int16_t height;
59     /** Image type */
60     ImageType imageType;
61 };
62 
63 /**
64  * @brief Represents an image animator.
65  *
66  * Images are switched at a specified interval to form an animator.
67  *
68  * @see UIImageView
69  * @since 1.0
70  * @version 1.0
71  */
72 class UIImageAnimatorView : public UIImageView {
73 public:
74     /**
75      * @brief A constructor used to create a <b>UIImageAnimatorView</b> instance.
76      *
77      * @since 1.0
78      * @version 1.0
79      */
80     UIImageAnimatorView();
81 
82     /**
83      * @brief A destructor used to delete the <b>UIImageAnimatorView</b> instance.
84      *
85      * @since 1.0
86      * @version 1.0
87      */
88     virtual ~UIImageAnimatorView();
89 
90     /**
91      * @brief Obtains the view type.
92      *
93      * @return Returns the view type, as defined in {@link UIViewType}.
94      * @since 1.0
95      * @version 1.0
96      */
GetViewType()97     UIViewType GetViewType() const override
98     {
99         return UI_IMAGE_ANIMATOR_VIEW;
100     }
101 
102     /**
103      * @brief Sets the images and speed for this animator.
104      *
105      * @param imageSrc Indicates the pointer to the {@link ImageAnimatorInfo} array for storing the configured
106      *                 image information. The memory cannot be released before this animator stops.
107      * @param imageNum Indicates the number of images.
108      * @param timeOfUpdate Indicates the interval for updating images, in milliseconds. The default value is <b>1</b>.
109      * @see GetTimeOfUpdate | GetImageAnimatorSrc | GetImageAnimatorImageNum
110      * @since 1.0
111      * @version 1.0
112      */
113     void SetImageAnimatorSrc(const ImageAnimatorInfo imageSrc[], uint8_t imageNum, uint16_t timeOfUpdate);
114 
115     /**
116      * @brief Sets the images for this animator.
117      *
118      * @param imageSrc Indicates the pointer to the {@link ImageAnimatorInfo} array for storing the configured
119      *                 image information. The memory cannot be released before this animator stops.
120      * @param imageNum Indicates the number of images.
121      * @see GetImageAnimatorSrc | GetImageAnimatorImageNum
122      * @since 1.0
123      * @version 1.0
124      */
125     void SetImageAnimatorSrc(const ImageAnimatorInfo imageSrc[], uint8_t imageNum);
126 
127     /**
128      * @brief Sets the speed for this animator.
129      *
130      * @param timeOfUpdate Indicates the interval for updating images, in milliseconds.
131      * @see GetTimeOfUpdate
132      * @since 1.0
133      * @version 1.0
134      */
135     void SetTimeOfUpdate(uint16_t timeOfUpdate);
136 
137     /**
138      * @brief Obtains the speed of this animator.
139      *
140      * @return Returns the interval for updating images, in milliseconds.
141      * @see SetTimeOfUpdate
142      * @since 1.0
143      * @version 1.0
144      */
145     uint16_t GetTimeOfUpdate() const;
146 
147     /**
148      * @brief Sets the interval between two playbacks of an infinitely repeated animator.
149      *
150      * @param timeOfPause Indicates the interval between two playbacks of an infinitely repeated animator, in
151      *                    milliseconds. The default value is <b>0</b>, indicating that there is no interval.
152      * @see GetTimeOfPause
153      * @since 1.0
154      * @version 1.0
155      */
156     void SetTimeOfPause(uint16_t timeOfPause);
157 
158     /**
159      * @brief Obtains the interval between two playbacks of an infinitely repeated animator.
160      *
161      * @return Returns the interval between two playbacks, in milliseconds.
162      * @see SetTimeOfPause
163      * @since 1.0
164      * @version 1.0
165      */
166     uint16_t GetTimeOfPause() const;
167 
168     /**
169      * @brief Obtains the image array of this frame animator.
170      *
171      * @return Returns a pointer to the {@link ImageAnimatorInfo} array if the operation is successful;
172      *         returns <b>nullptr</b> otherwise.
173      * @since 1.0
174      * @version 1.0
175      */
176     const ImageAnimatorInfo* GetImageAnimatorSrc() const;
177 
178     /**
179      * @brief Obtains the number of images.
180      *
181      * @return Returns the number of images.
182      * @since 1.0
183      * @version 1.0
184      */
185     uint8_t GetImageAnimatorImageNum() const;
186 
187     /**
188      * @brief Sets whether the image size is fixed to the view size.
189      *
190      * @param fixed Specifies whether the image size is fixed to the view size. The value <b>true</b> indicates
191      *              the size of the image is the same as that of the view, and <b>false</b> indicates the
192      *              position and size of this image need to be set in the {@link ImageAnimatorInfo} structure.
193      * @see IsSizeFixed
194      * @since 1.0
195      * @version 1.0
196      */
SetSizeFixed(bool fixed)197     void SetSizeFixed(bool fixed)
198     {
199         sizeFixed_ = fixed;
200     }
201 
202     /**
203      * @brief Checks whether the image size is fixed to the view size.
204      *
205      * @return Returns <b>true</b> if the image size is fixed to the view size;
206      *         returns <b>false</b> if the image size is set independently.
207      * @see SetSizeFixed
208      * @since 1.0
209      * @version 1.0
210      */
IsSizeFixed()211     bool IsSizeFixed() const
212     {
213         return sizeFixed_;
214     }
215 
216     /**
217      * @brief Sets whether to play this animator for infinite times.
218      *
219      * @param repeat Specifies whether the animator is played for infinite times. <b>true</b> (default value)
220      *               indicates the animator is played for infinite times, and <b>false</b> indicates the animator
221      *               is played a specified number of times.
222      * @see IsRepeat
223      * @since 1.0
224      * @version 1.0
225      */
SetRepeat(bool repeat)226     void SetRepeat(bool repeat)
227     {
228         repeat_ = repeat;
229     }
230 
231     /**
232      * @brief Checks whether this animator is played for infinite times.
233      *
234      * @return Returns <b>true</b> if this animator is played for infinite times, returns <b>false</b> if the
235      *         animator is played a specified number of times.
236      * @see SetRepeat
237      * @since 1.0
238      * @version 1.0
239      */
IsRepeat()240     bool IsRepeat() const
241     {
242         return repeat_;
243     }
244 
245     /**
246      * @brief Sets the playback times for this animator.
247      *
248      * This function takes effect only when <b>repeat</b> is set to <b>false</b> in {@link SetRepeat}.
249      *
250      * @param times Indicates the playback times to set. The default value is <b>1</b>.
251      * @see GetRepeatTimes
252      * @since 1.0
253      * @version 1.0
254      */
SetRepeatTimes(uint32_t times)255     void SetRepeatTimes(uint32_t times)
256     {
257         repeatTimes_ = times;
258     }
259 
260     /**
261      * @brief Obtains the playback times.
262      *
263      * @return Returns the playback times.
264      * @see SetRepeatTimes
265      * @since 1.0
266      * @version 1.0
267      */
GetRepeatTimes()268     uint32_t GetRepeatTimes() const
269     {
270         return repeatTimes_;
271     }
272 
273     /**
274      * @brief Obtains the current state of this animator.
275      *
276      * @return Returns the current state, which can be {@link START}, {@link STOP}, or {@link PAUSE}.
277      *         For details, see {@link Animator}.
278      * @since 1.0
279      * @version 1.0
280      */
GetState()281     uint8_t GetState() const
282     {
283         return imageAnimator_.GetState();
284     }
285 
286     /**
287      * @brief Sets the playback sequence for this animator.
288      *
289      * @param reverse Indicates the playback sequence to set. <b>true</b> indicates a reverse playback
290      *                and <b>false</b> (default value) indicates a forward playback.
291      * @see IsReverse
292      * @since 1.0
293      * @version 1.0
294      */
SetReverse(bool reverse)295     void SetReverse(bool reverse)
296     {
297         reverse_ = reverse;
298     }
299 
300     /**
301      * @brief Obtains the playback sequence of this animator.
302      *
303      * @return Returns <b>true</b> if a reverse playback is performed;
304      *         returns <b>false</b> if a forward playback is performed.
305      * @see SetReverse
306      * @since 1.0
307      * @version 1.0
308      */
IsReverse()309     bool IsReverse() const
310     {
311         return reverse_;
312     }
313 
314     /**
315      * @brief Sets the attribute value for the end frame of this animator.
316      *
317      * @param fillMode Indicates the attribute value for the end frame of this animator.
318      *                 When a forward playback is performed, the value <b>true</b> indicates setting the end frame of
319      *                 this animator to the last one of the image set, and <b>false</b> indicates setting the end frame
320      *                 of this animator to the first one of the image set. When a reverse playback is performed,
321      *                 the value <b>true</b> indicates setting the end frame of this animator to the first one of
322      *                 the image set, and <b>false</b> indicates setting the end frame of this animator to the last one
323      *                 of the image set. The default value is <b>true</b>.
324      * @see GetFillMode
325      * @since 3.0
326      * @version 5.0
327      */
SetFillMode(bool fillMode)328     void SetFillMode(bool fillMode)
329     {
330         fillMode_ = fillMode;
331     }
332 
333     /**
334      * @brief Obtains the attribute value of the last frame of this animator.
335      *
336      * @return Returns <b>true</b> if the last frame of this animator is the last one of the image set when
337      *         a forward playback is performed, or if the last frame of this animator is the first one of the image set
338      *         when a reverse playback is performed;
339      *         returns <b>false</b> if the last frame of this animator is the first one of the image set when
340      *         a forward playback is performed, or if the last frame of this animator is the last one of the image set
341      *         when a reverse playback is performed.
342      *
343      * @see SetFillMode
344      * @since 3.0
345      * @version 5.0
346      */
GetFillMode()347     bool GetFillMode() const
348     {
349         return fillMode_;
350     }
351 
352     /**
353      * @brief Starts this animator.
354      *
355      * The forward playback starts from the first image and the reverse playback starts from the last image.
356      *
357      * @see Stop
358      * @since 1.0
359      * @version 1.0
360      */
361     void Start();
362 
363     /**
364      * @brief Stops this animator.
365      *
366      * @see Start
367      * @since 1.0
368      * @version 1.0
369      */
370     void Stop();
371 
372     /**
373      * @brief Pauses this animator at the current image.
374      *
375      * @see Resume
376      * @since 1.0
377      * @version 1.0
378      */
379     void Pause();
380 
381     /**
382      * @brief Resumes this animator from the current image.
383      *
384      * @see Pause
385      * @since 1.0
386      * @version 1.0
387      */
388     void Resume();
389 
390     /**
391      * @brief Represents a listener that contains a callback to be invoked when this animator stops.
392      *
393      * @since 1.0
394      * @version 1.0
395      */
396     class AnimatorStopListener : public HeapBase {
397     public:
398         /**
399          * @brief A destructor used to delete an <b>AnimatorStopListener</b> instance.
400          *
401          * @since 1.0
402          * @version 1.0
403          */
~AnimatorStopListener()404         virtual ~AnimatorStopListener() {}
405 
406         /**
407          * @brief Called when this animator stops.
408          *
409          * You need to inherit from the <b> AnimatorStopListener</b> class and implement this function.
410          *
411          * @param view Indicates the instance of this view.
412          * @since 1.0
413          * @version 1.0
414          */
OnAnimatorStop(UIView & view)415         virtual void OnAnimatorStop(UIView& view) {}
416     };
417 
418     /**
419      * @brief Sets the listener for the stop of this animator.
420      *
421      * @param listener Indicates the listener to set. For details, see {@link AnimatorStopListener}.
422      * @since 1.0
423      * @version 1.0
424      */
SetAnimatorStopListener(AnimatorStopListener * listener)425     void SetAnimatorStopListener(AnimatorStopListener* listener)
426     {
427         listener_ = listener;
428     }
429 
430 protected:
431     class ImageAnimatorCallback : public AnimatorCallback {
432     public:
ImageAnimatorCallback()433         ImageAnimatorCallback() : tickNum_(0), loop_(1), drawingImage_(0), repeat_(0), imageSrc_(nullptr), imageNum_(0)
434         {
435         }
436 
~ImageAnimatorCallback()437         virtual ~ImageAnimatorCallback() {}
438 
439         void Callback(UIView* view) override;
440 
Reset()441         void Reset()
442         {
443             tickNum_ = 0;
444             loop_ = 1;
445             repeat_ = 0;
446         }
447 
448     protected:
449         uint16_t tickNum_;
450         uint8_t loop_;
451         uint8_t drawingImage_;
452         uint32_t repeat_;
453         const ImageAnimatorInfo* imageSrc_;
454         uint8_t imageNum_;
455     };
456 
457     void Reset(bool fillMode);
458     void UpdateImage(uint8_t& drawingImage, uint8_t& loop);
459     uint8_t GetTickByTime(uint16_t time) const;
460     ImageAnimatorInfo* imageSrc_;
461     uint8_t imageNum_;
462     uint8_t tickOfUpdate_;
463     uint16_t timeOfUpdate_;
464     uint16_t timeOfPause_;
465     uint8_t tickOfPause_;
466     uint32_t repeatTimes_;
467     Animator imageAnimator_;
468     ImageAnimatorCallback imageAnimatorCallback_;
469     AnimatorStopListener* listener_;
470     bool reverse_;
471     bool repeat_;
472     bool sizeFixed_;
473     bool fillMode_;
474 };
475 } // namespace OHOS
476 #endif // GRAPHIC_LITE_UI_IMAGE_ANIMATOR_H
477