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_analog_clock.h
28  *
29  * @brief Declares an analog clock.
30  *
31  * @since 1.0
32  * @version 1.0
33  */
34 
35 #ifndef UI_ANALOG_CLOCK_H
36 #define UI_ANALOG_CLOCK_H
37 
38 #include "components/ui_abstract_clock.h"
39 
40 namespace OHOS {
41 class UIImageView;
42 /**
43  * @brief Provides the functions related to an analog clock.
44  *
45  * @see UIAbstractClock
46  * @since 1.0
47  * @version 1.0
48  */
49 class UIAnalogClock : public UIAbstractClock {
50 public:
51     /**
52      * @brief A default constructor used to create a <b>UIAnalogClock</b> instance.
53      *
54      * @since 1.0
55      * @version 1.0
56      */
57     UIAnalogClock();
58 
59     /**
60      * @brief A destructor used to delete the <b>UIAnalogClock</b> instance.
61      *
62      * @since 1.0
63      * @version 1.0
64      */
~UIAnalogClock()65     virtual ~UIAnalogClock() {}
66 
67     /**
68      * @brief Enumerates the clock hand types.
69      */
70     enum class HandType {
71         /** Hour hand */
72         HOUR_HAND,
73         /** Minute hand */
74         MINUTE_HAND,
75         /** Second hand */
76         SECOND_HAND,
77     };
78 
79     /**
80      * @brief Enumerates the drawing types of a clock hand.
81      */
82     enum class DrawType {
83         /** Using a line to draw a clock hand */
84         DRAW_LINE,
85         /** Using an image to draw a clock hand */
86         DRAW_IMAGE
87     };
88 
89     /**
90      * @brief Defines the basic attributes of the analog clock hands. This is an inner class of <b>UIAbstractClock</b>.
91      *
92      * @since 1.0
93      * @version 1.0
94      */
95     class Hand : public HeapBase {
96     public:
97         /**
98          * @brief A default constructor used to create a <b>Hand</b> instance.
99          *
100          * @since 1.0
101          * @version 1.0
102          */
Hand()103         Hand()
104             : center_{0, 0},
105               initAngle_(0),
106               preAngle_(0),
107               nextAngle_(0),
108               position_{0, 0},
109               imageInfo_{{0, 0, 0}},
110               color_{{0, 0, 0}},
111               width_(0),
112               height_(0),
113               opacity_(0),
114               drawtype_(DrawType::DRAW_IMAGE)
115         {
116         }
117 
118         /**
119          * @brief A destructor used to delete the <b>Hand</b> instance.
120          *
121          * @since 1.0
122          * @version 1.0
123          */
~Hand()124         virtual ~Hand() {}
125 
126         /**
127          * @brief Represents the rotation center of a clock hand.
128          */
129         Point center_;
130 
131         /**
132          * @brief Represents the initial clockwise rotation angle of a clock hand. The default value is <b>0</b>,
133          *        indicating that the hand direction is vertically upward.
134          */
135         uint16_t initAngle_;
136 
137         /**
138          * @brief Represents the latest rotation angle of this clock hand.
139          */
140         uint16_t preAngle_;
141 
142         /**
143          * @brief Represents the next rotation angle of this clock hand.
144          */
145         uint16_t nextAngle_;
146 
147         /**
148          * @brief Represents the position of a hand on this analog clock.
149          */
150         Point position_;
151 
152         /**
153          * @brief Represents the image information of this clock hand.
154          */
155         ImageInfo imageInfo_;
156 
157         /**
158          * @brief Represents the information about the rotation and translation of this clock hand.
159          */
160         TransformMap trans_;
161 
162         /**
163          * @brief Represents the new rectangle area after the rotation and translation.
164          */
165         Rect target_;
166 
167         /**
168          * @brief Represents the color of this clock hand.
169          */
170         ColorType color_;
171 
172         /**
173          * @brief Represents the width of this clock hand.
174          */
175         uint16_t width_;
176 
177         /**
178          * @brief Represents the height of this clock hand.
179          */
180         uint16_t height_;
181 
182         /**
183          * @brief Represents the opacity of this clock hand.
184          */
185         OpacityType opacity_;
186 
187         /**
188          * @brief Represents the drawing type of this clock hand.
189          */
190         DrawType drawtype_;
191     };
192 
193     /**
194      * @brief Obtains the view type.
195      *
196      * @return Returns <b>UI_ANALOG_CLOCK</b>, as defined in {@link UIViewType}.
197      * @since 1.0
198      * @version 1.0
199      */
GetViewType()200     UIViewType GetViewType() const override
201     {
202         return UI_ANALOG_CLOCK;
203     }
204 
205     /**
206      * @brief Sets the image used to draw a clock hand.
207      *
208      * @param type Indicates the clock hand type, as enumerated in {@link HandType}.
209      * @param img Indicates the image to set.
210      * @param position Indicates the start position of this image.
211      * @param center Indicates the rotation center of this clock hand.
212      * @since 1.0
213      * @version 1.0
214      */
215     void SetHandImage(HandType type, const UIImageView& img, Point position, Point center);
216 
217     /**
218      * @brief Sets the line used to draw a clock hand.
219      *
220      * @param type Indicates the clock hand type, as enumerated in {@link HandType}.
221      * @param position Indicates the position of the line endpoint close to the rotation center.
222      * @param center Indicates the rotation center of this clock hand.
223      * @param color Indicates the color of this line.
224      * @param width Indicates the width of this line when it is 12 o'clock.
225      * @param height Indicates the height of this line when it is 12 o'clock.
226      * @param opacity Indicates the opacity of this line.
227      * @since 1.0
228      * @version 1.0
229      */
230     void SetHandLine(HandType type,
231                      Point position,
232                      Point center,
233                      ColorType color,
234                      uint16_t width,
235                      uint16_t height,
236                      OpacityType opacity);
237 
238     /**
239      * @brief Obtains the rotation center of a specified clock hand.
240      *
241      * @param type Indicates the clock hand type, as enumerated in {@link HandType}.
242      * @return Returns the rotation center.
243      * @since 1.0
244      * @version 1.0
245      */
246     Point GetHandRotateCenter(HandType type) const;
247 
248     /**
249      * @brief Obtains the position of a specified clock hand.
250      *
251      * @param type Indicates the clock hand type, as enumerated in {@link HandType}.
252      * @return Returns the position of this specified clock hand.
253      * @since 1.0
254      * @version 1.0
255      */
256     Point GetHandPosition(HandType type) const;
257 
258     /**
259      * @brief Obtains the initial rotation angle of the specified clock hand.
260      *
261      * @param type Indicates the clock hand type, as enumerated in {@link HandType}.
262      * @return Returns the initial rotation angle of this specified clock hand.
263      * @since 1.0
264      * @version 1.0
265      */
266     uint16_t GetHandInitAngle(HandType type) const;
267 
268     /**
269      * @brief Obtains the current rotation angle of the specified clock hand.
270      *
271      * @param type Indicates the clock hand type, as enumerated in {@link HandType}.
272      * @return Returns the current rotation angle of this specified clock hand.
273      * @since 1.0
274      * @version 1.0
275      */
276     uint16_t GetHandCurrentAngle(HandType type) const;
277 
278     /**
279      * @brief Sets the initial time in the 24-hour format.
280      *
281      * @param hour Indicates the hour to set.
282      * @param minute Indicates the minute to set.
283      * @param second Indicates the second to set.
284      * @since 1.0
285      * @version 1.0
286      */
287     void SetInitTime24Hour(uint8_t hour, uint8_t minute, uint8_t second);
288 
289     /**
290      * @brief Sets the initial time in the 12-hour format.
291      *
292      * @param hour Indicates the hour to set.
293      * @param minute Indicates the minute to set.
294      * @param second Indicates the second to set.
295      * @param am Specifies whether it is in the morning. <b>true</b> indicates that it is in the morning,
296      *           and <b> false</b> indicates that it is in the afternoon.
297      * @since 1.0
298      * @version 1.0
299      */
300     void SetInitTime12Hour(uint8_t hour, uint8_t minute, uint8_t second, bool am);
301 
302     /**
303      * @brief Draws an analog clock.
304      *
305      * @param invalidatedArea Indicates the area to draw.
306      * @since 1.0
307      * @version 1.0
308      */
309     void OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override;
310 
311     /**
312      * @brief Performs the operations needed after the drawing.
313      *
314      * @param invalidatedArea Indicates the area to draw.
315      * @since 1.0
316      * @version 1.0
317      */
318     void OnPostDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override;
319 
320     /**
321      * @brief Sets the working mode for this analog clock.
322      *
323      * @param newMode Indicates the working mode to set. For details, see {@link WorkMode}.
324      * @since 1.0
325      * @version 1.0
326      */
327     void SetWorkMode(WorkMode newMode) override;
328 
329     /**
330      * @brief Get the working mode for this analog clock.
331      *
332      * @return Returns the working mode for this analog clock.
333      */
GetWorkMode()334     WorkMode GetWorkMode() const override
335     {
336         return mode_;
337     }
338 
339     /**
340      * @brief Updates the time of this analog clock.
341      *
342      * @param clockInit Specifies whether it is the first initialization. <b>true</b> indicates it is the
343      *                  first initialization, and <b> false</b> indicates the opposite case.
344      * @since 1.0
345      * @version 1.0
346      */
347     void UpdateClock(bool clockInit) override;
348 
349 private:
350     Hand hourHand_;
351     Hand minuteHand_;
352     Hand secondHand_;
353 
354     void DrawHand(BufferInfo& gfxDstBuffer, const Rect& current, const Rect& invalidatedArea, Hand& hand);
355     void DrawHandImage(BufferInfo& gfxDstBuffer, const Rect& current, const Rect& invalidatedArea, Hand& hand);
356     void DrawHandLine(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea, Hand& hand);
357     uint16_t ConvertHandValueToAngle(uint8_t handValue, uint8_t range, uint8_t secondHandValue, uint8_t ratio) const;
358     uint16_t ConvertHandValueToAngle(uint8_t handValue, uint8_t range) const;
359     void CalculateRedrawArea(const Rect& current, Hand& hand, bool clockInit);
360 };
361 } // namespace OHOS
362 #endif // UI_ANALOG_CLOCK_H
363