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_texture_mapper.h
28  *
29  * @brief Defines the attributes and functions of a texture mapper.
30  *
31  * @since 1.0
32  * @version 1.0
33  */
34 
35 #ifndef GRAPHIC_LITE_UI_TEXTURE_MAPPER_H
36 #define GRAPHIC_LITE_UI_TEXTURE_MAPPER_H
37 
38 #include "components/ui_image_view.h"
39 #include "animator/animator.h"
40 #include "animator/easing_equation.h"
41 
42 namespace OHOS {
43 /**
44  * @brief Rotates and scales images.
45  *
46  * @see UIImageView
47  * @since 1.0
48  * @version 1.0
49  */
50 class UITextureMapper : public UIImageView {
51 public:
52     /**
53      * @brief A constructor used to create a <b>UITextureMapper</b> instance.
54      *
55      * @since 1.0
56      * @version 1.0
57      */
58     UITextureMapper();
59 
60     /**
61      * @brief A destructor used to delete the <b>UITextureMapper</b> instance.
62      *
63      * @since 1.0
64      * @version 1.0
65      */
66     virtual ~UITextureMapper();
67 
68     /**
69      * @brief Obtains the view type.
70      *
71      * @return Returns the view type, as defined in {@link UIViewType}.
72      * @since 1.0
73      * @version 1.0
74      */
GetViewType()75     UIViewType GetViewType() const override
76     {
77         return UI_TEXTURE_MAPPER;
78     }
79 
80     /**
81      * @brief Starts this animator.
82      *
83      * @since 1.0
84      * @version 1.0
85      */
86     void Start();
87 
88     /**
89      * @brief Sets the start angle of rotation for this image.
90      *
91      * By default, the image starts to rotate from the current angle of this stopped animator.
92      *
93      * @param start Indicates the start angle to set. The default value is <b>0</b>.
94      * @since 1.0
95      * @version 1.0
96      */
SetRotateStart(int16_t start)97     void SetRotateStart(int16_t start)
98     {
99         rotateCur_ = start;
100     }
101 
102     /**
103      * @brief Sets the end angle of rotation for this image.
104      *
105      * @param end Indicates the end angle of rotation to set. The default value is <b>0</b>.
106      * @since 1.0
107      * @version 1.0
108      */
SetRotateEnd(int16_t end)109     void SetRotateEnd(int16_t end)
110     {
111         rotateEnd_ = end;
112     }
113 
114     /**
115      * @brief Sets the start ratio for scaling this image.
116      *
117      * By default, the image starts to scale from the current ratio of this stopped animator. \n
118      * The X and Y directions are scaled at the same ratio. Single-direction scaling is not supported. \n
119      *
120      * @param start Indicates the start ratio to set. The default value is <b>1</b>.
121      * @since 1.0
122      * @version 1.0
123      */
SetScaleStart(float start)124     void SetScaleStart(float start)
125     {
126         scaleCur_ = static_cast<int16_t>(FloatToInt64(start));
127     }
128 
129     /**
130      * @brief Sets the end ratio for scaling this image.
131      *
132      * @param end Indicates the end ratio to set. The default value is <b>1</b>.
133      * @since 1.0
134      * @version 1.0
135      */
SetScaleEnd(float end)136     void SetScaleEnd(float end)
137     {
138         scaleEnd_ = static_cast<int16_t>(FloatToInt64(end));
139     }
140 
141     /**
142      * @brief Sets the duration for this animator, in milliseconds.
143      *
144      * The durations of image scaling and rotation are the same and cannot be set separately.
145      *
146      * @param durationTime Indicates the duration to set.
147      * @since 1.0
148      * @version 1.0
149      */
SetDurationTime(uint16_t durationTime)150     void SetDurationTime(uint16_t durationTime)
151     {
152         animator_.SetTime(durationTime + delayTime_);
153     }
154 
155     /**
156      * @brief Sets the delay time for this animator, in milliseconds.
157      *
158      * The delays of image scaling and rotation are the same and cannot be set separately.
159      *
160      * @param delayTime Indicates the delay time to set.
161      * @since 1.0
162      * @version 1.0
163      */
SetDelayTime(uint16_t delayTime)164     void SetDelayTime(uint16_t delayTime)
165     {
166         animator_.SetTime(animator_.GetTime() - delayTime_ + delayTime);
167         delayTime_ = delayTime;
168     }
169 
170     /**
171      * @brief Sets the easing function for this animator.
172      *
173      * The easing functions for image scaling and rotation are the same and cannot be set separately.
174      *
175      * @param easingFunc Indicates the easing function to set. The animation moves at a constant velocity by default.
176      *                   For details, see {@link EasingEquation}.
177      * @since 1.0
178      * @version 1.0
179      */
SetEasingFunc(EasingFunc easingFunc)180     void SetEasingFunc(EasingFunc easingFunc)
181     {
182         easingFunc_ = easingFunc;
183     }
184 
185     /**
186      * @brief Cancels this animator.
187      *
188      * After being cancelled, the animator will stop in the current playback state.
189      *
190      * @since 1.0
191      * @version 1.0
192      */
193     void Cancel();
194 
195     /**
196      * @brief Displays the original image.
197      *
198      * The configured parameters are reserved and will not be reset.
199      *
200      * @since 1.0
201      * @version 1.0
202      */
203     void Reset();
204 
205     /**
206      * @brief Sets the coordinates of the rotation and scaling pivots for this image.
207      *
208      * The coordinates represent the position relative to this image. For example, setting the x-coordinate and
209      * y-coordinate of the rotation or scaling center to the half of the image width and height respectively
210      * means the rotation or scaling is going to be performed around the center of this image.
211      *
212      * @param x Indicates the x-coordinate to set.
213      * @param y Indicates the y-coordinate to set.
214      * @since 1.0
215      * @version 1.0
216      */
SetPivot(float x,float y)217     void SetPivot(float x, float y)
218     {
219         pivot_.x_ = x;
220         pivot_.y_ = y;
221     }
222 
223     /**
224      * @brief Represents a listener that contains a callback to be invoked when this animator stops.
225      *
226      * @since 1.0
227      * @version 1.0
228      */
229     class AnimatorStopListener : public HeapBase {
230     public:
231         /**
232          * @brief A destructor used to delete an <b>AnimatorStopListener</b> instance.
233          *
234          * @since 1.0
235          * @version 1.0
236          */
~AnimatorStopListener()237         virtual ~AnimatorStopListener() {}
238 
239         /**
240          * @brief Called when this animator stops. This is a pure virtual function, which needs
241          *        your inheritance and implementation.
242          *
243          * @param view Indicates the instance of this view.
244          * @since 1.0
245          * @version 1.0
246          */
247         virtual void OnAnimatorStop(UIView& view) = 0;
248     };
249 
250     /**
251      * @brief Sets the listener for the stop of this animator.
252      *
253      * @param listener Indicates the listener to set. For details, see {@link AnimatorStopListener}.
254      * @since 1.0
255      * @version 1.0
256      */
SetAnimatorStopListener(AnimatorStopListener * listener)257     void SetAnimatorStopListener(AnimatorStopListener* listener)
258     {
259         listener_ = listener;
260     }
261 
262 private:
263     class TextureMapperAnimatorCallback : public AnimatorCallback {
264     public:
~TextureMapperAnimatorCallback()265         virtual ~TextureMapperAnimatorCallback() {}
266 
267         void Callback(UIView* view) override;
268 
269         void OnStop(UIView& view) override;
270     };
271 
272     static constexpr int16_t SCALE_CONVERTION = 256;
273 
274     void Callback();
275 
276     TextureMapperAnimatorCallback animatorCallback_;
277     Animator animator_;
278     AnimatorStopListener* listener_;
279 
280     Vector2<float> pivot_;
281     int16_t rotateCur_;
282     int16_t rotateStart_;
283     int16_t rotateEnd_;
284     int16_t scaleCur_;
285     int16_t scaleStart_;
286     int16_t scaleEnd_;
287     uint16_t delayTime_;
288     EasingFunc easingFunc_;
289 };
290 } // namespace OHOS
291 #endif // GRAPHIC_LITE_UI_TEXTURE_MAPPER_H
292