1 /**
2  * @file lv_area.h
3  *
4  */
5 
6 #ifndef LV_AREA_H
7 #define LV_AREA_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 /*********************
14  *      INCLUDES
15  *********************/
16 #include "../lv_conf_internal.h"
17 #include <stdbool.h>
18 #include <stdint.h>
19 
20 /*********************
21  *      DEFINES
22  *********************/
23 
24 #if LV_USE_LARGE_COORD
25 typedef int32_t lv_coord_t;
26 #else
27 typedef int16_t lv_coord_t;
28 #endif
29 
30 /**********************
31  *      TYPEDEFS
32  **********************/
33 
34 /**
35  * Represents a point on the screen.
36  */
37 typedef struct {
38     lv_coord_t x;
39     lv_coord_t y;
40 } lv_point_t;
41 
42 /** Represents an area of the screen.*/
43 typedef struct {
44     lv_coord_t x1;
45     lv_coord_t y1;
46     lv_coord_t x2;
47     lv_coord_t y2;
48 } lv_area_t;
49 
50 /** Alignments*/
51 enum {
52     LV_ALIGN_DEFAULT = 0,
53     LV_ALIGN_TOP_LEFT,
54     LV_ALIGN_TOP_MID,
55     LV_ALIGN_TOP_RIGHT,
56     LV_ALIGN_BOTTOM_LEFT,
57     LV_ALIGN_BOTTOM_MID,
58     LV_ALIGN_BOTTOM_RIGHT,
59     LV_ALIGN_LEFT_MID,
60     LV_ALIGN_RIGHT_MID,
61     LV_ALIGN_CENTER,
62 
63     LV_ALIGN_OUT_TOP_LEFT,
64     LV_ALIGN_OUT_TOP_MID,
65     LV_ALIGN_OUT_TOP_RIGHT,
66     LV_ALIGN_OUT_BOTTOM_LEFT,
67     LV_ALIGN_OUT_BOTTOM_MID,
68     LV_ALIGN_OUT_BOTTOM_RIGHT,
69     LV_ALIGN_OUT_LEFT_TOP,
70     LV_ALIGN_OUT_LEFT_MID,
71     LV_ALIGN_OUT_LEFT_BOTTOM,
72     LV_ALIGN_OUT_RIGHT_TOP,
73     LV_ALIGN_OUT_RIGHT_MID,
74     LV_ALIGN_OUT_RIGHT_BOTTOM,
75 };
76 typedef uint8_t lv_align_t;
77 
78 enum {
79     LV_DIR_NONE     = 0x00,
80     LV_DIR_LEFT     = (1 << 0),
81     LV_DIR_RIGHT    = (1 << 1),
82     LV_DIR_TOP      = (1 << 2),
83     LV_DIR_BOTTOM   = (1 << 3),
84     LV_DIR_HOR      = LV_DIR_LEFT | LV_DIR_RIGHT,
85     LV_DIR_VER      = LV_DIR_TOP | LV_DIR_BOTTOM,
86     LV_DIR_ALL      = LV_DIR_HOR | LV_DIR_VER,
87 };
88 
89 typedef uint8_t lv_dir_t;
90 
91 /**********************
92  * GLOBAL PROTOTYPES
93  **********************/
94 
95 /**
96  * Initialize an area
97  * @param area_p pointer to an area
98  * @param x1 left coordinate of the area
99  * @param y1 top coordinate of the area
100  * @param x2 right coordinate of the area
101  * @param y2 bottom coordinate of the area
102  */
103 void lv_area_set(lv_area_t * area_p, lv_coord_t x1, lv_coord_t y1, lv_coord_t x2, lv_coord_t y2);
104 
105 /**
106  * Copy an area
107  * @param dest pointer to the destination area
108  * @param src pointer to the source area
109  */
lv_area_copy(lv_area_t * dest,const lv_area_t * src)110 inline static void lv_area_copy(lv_area_t * dest, const lv_area_t * src)
111 {
112     dest->x1 = src->x1;
113     dest->y1 = src->y1;
114     dest->x2 = src->x2;
115     dest->y2 = src->y2;
116 }
117 
118 /**
119  * Get the width of an area
120  * @param area_p pointer to an area
121  * @return the width of the area (if x1 == x2 -> width = 1)
122  */
lv_area_get_width(const lv_area_t * area_p)123 static inline lv_coord_t lv_area_get_width(const lv_area_t * area_p)
124 {
125     return (lv_coord_t)(area_p->x2 - area_p->x1 + 1);
126 }
127 
128 /**
129  * Get the height of an area
130  * @param area_p pointer to an area
131  * @return the height of the area (if y1 == y2 -> height = 1)
132  */
lv_area_get_height(const lv_area_t * area_p)133 static inline lv_coord_t lv_area_get_height(const lv_area_t * area_p)
134 {
135     return (lv_coord_t)(area_p->y2 - area_p->y1 + 1);
136 }
137 
138 /**
139  * Set the width of an area
140  * @param area_p pointer to an area
141  * @param w the new width of the area (w == 1 makes x1 == x2)
142  */
143 void lv_area_set_width(lv_area_t * area_p, lv_coord_t w);
144 
145 /**
146  * Set the height of an area
147  * @param area_p pointer to an area
148  * @param h the new height of the area (h == 1 makes y1 == y2)
149  */
150 void lv_area_set_height(lv_area_t * area_p, lv_coord_t h);
151 
152 /**
153  * Set the position of an area (width and height will be kept)
154  * @param area_p pointer to an area
155  * @param x the new x coordinate of the area
156  * @param y the new y coordinate of the area
157  */
158 void _lv_area_set_pos(lv_area_t * area_p, lv_coord_t x, lv_coord_t y);
159 
160 /**
161  * Return with area of an area (x * y)
162  * @param area_p pointer to an area
163  * @return size of area
164  */
165 uint32_t lv_area_get_size(const lv_area_t * area_p);
166 
167 void lv_area_increase(lv_area_t * area, lv_coord_t w_extra, lv_coord_t h_extra);
168 
169 void lv_area_move(lv_area_t * area, lv_coord_t x_ofs, lv_coord_t y_ofs);
170 
171 /**
172  * Get the common parts of two areas
173  * @param res_p pointer to an area, the result will be stored her
174  * @param a1_p pointer to the first area
175  * @param a2_p pointer to the second area
176  * @return false: the two area has NO common parts, res_p is invalid
177  */
178 bool _lv_area_intersect(lv_area_t * res_p, const lv_area_t * a1_p, const lv_area_t * a2_p);
179 
180 /**
181  * Get resulting sub areas after removing the common parts of two areas from the first area
182  * @param res_p pointer to an array of areas with a count of 4, the resulting areas will be stored here
183  * @param a1_p pointer to the first area
184  * @param a2_p pointer to the second area
185  * @return number of results (max 4) or -1 if no intersect
186  */
187 int8_t _lv_area_diff(lv_area_t * res_p, const lv_area_t * a1_p, const lv_area_t * a2_p);
188 
189 /**
190  * Join two areas into a third which involves the other two
191  * @param res_p pointer to an area, the result will be stored here
192  * @param a1_p pointer to the first area
193  * @param a2_p pointer to the second area
194  */
195 void _lv_area_join(lv_area_t * a_res_p, const lv_area_t * a1_p, const lv_area_t * a2_p);
196 
197 /**
198  * Check if a point is on an area
199  * @param a_p pointer to an area
200  * @param p_p pointer to a point
201  * @param radius radius of area (e.g. for rounded rectangle)
202  * @return false:the point is out of the area
203  */
204 bool _lv_area_is_point_on(const lv_area_t * a_p, const lv_point_t * p_p, lv_coord_t radius);
205 
206 /**
207  * Check if two area has common parts
208  * @param a1_p pointer to an area.
209  * @param a2_p pointer to an other area
210  * @return false: a1_p and a2_p has no common parts
211  */
212 bool _lv_area_is_on(const lv_area_t * a1_p, const lv_area_t * a2_p);
213 
214 /**
215  * Check if an area is fully on an other
216  * @param ain_p pointer to an area which could be in 'aholder_p'
217  * @param aholder_p pointer to an area which could involve 'ain_p'
218  * @param radius radius of `aholder_p` (e.g. for rounded rectangle)
219  * @return true: `ain_p` is fully inside `aholder_p`
220  */
221 bool _lv_area_is_in(const lv_area_t * ain_p, const lv_area_t * aholder_p, lv_coord_t radius);
222 
223 
224 /**
225  * Check if an area is fully out of an other
226  * @param aout_p pointer to an area which could be in 'aholder_p'
227  * @param aholder_p pointer to an area which could involve 'ain_p'
228  * @param radius radius of `aholder_p` (e.g. for rounded rectangle)
229  * @return true: `aout_p` is fully outside `aholder_p`
230  */
231 bool _lv_area_is_out(const lv_area_t * aout_p, const lv_area_t * aholder_p, lv_coord_t radius);
232 
233 /**
234  * Check if 2 area is the same
235  * @param a pointer to an area
236  * @param b pointer to another area
237  */
238 bool _lv_area_is_equal(const lv_area_t * a, const lv_area_t * b);
239 
240 /**
241  * Align an area to an other
242  * @param base an are where the other will be aligned
243  * @param to_align the area to align
244  * @param align `LV_ALIGN_...`
245  */
246 void lv_area_align(const lv_area_t * base, lv_area_t * to_align, lv_align_t align, lv_coord_t ofs_x, lv_coord_t ofs_y);
247 
248 void lv_point_transform(lv_point_t * p, int32_t angle, int32_t zoom, const lv_point_t * pivot);
249 
250 /**********************
251  *      MACROS
252  **********************/
253 
254 #if LV_USE_LARGE_COORD
255 #define _LV_COORD_TYPE_SHIFT    (29U)
256 #else
257 #define _LV_COORD_TYPE_SHIFT    (13U)
258 #endif
259 
260 #define _LV_COORD_TYPE_MASK     (3 << _LV_COORD_TYPE_SHIFT)
261 #define _LV_COORD_TYPE(x)       ((x) & _LV_COORD_TYPE_MASK)  /*Extract type specifiers*/
262 #define _LV_COORD_PLAIN(x)      ((x) & ~_LV_COORD_TYPE_MASK) /*Remove type specifiers*/
263 
264 #define _LV_COORD_TYPE_PX       (0 << _LV_COORD_TYPE_SHIFT)
265 #define _LV_COORD_TYPE_SPEC     (1 << _LV_COORD_TYPE_SHIFT)
266 #define _LV_COORD_TYPE_PX_NEG   (3 << _LV_COORD_TYPE_SHIFT)
267 
268 #define LV_COORD_IS_PX(x)       (_LV_COORD_TYPE(x) == _LV_COORD_TYPE_PX || \
269                                  _LV_COORD_TYPE(x) == _LV_COORD_TYPE_PX_NEG ? true : false)
270 #define LV_COORD_IS_SPEC(x)     (_LV_COORD_TYPE(x) == _LV_COORD_TYPE_SPEC ? true : false)
271 
272 #define LV_COORD_SET_SPEC(x)    ((x) | _LV_COORD_TYPE_SPEC)
273 
274 /*Special coordinates*/
275 #define LV_PCT(x)               (x < 0 ? LV_COORD_SET_SPEC(1000 - (x)) : LV_COORD_SET_SPEC(x))
276 #define LV_COORD_IS_PCT(x)      ((LV_COORD_IS_SPEC(x) && _LV_COORD_PLAIN(x) <= 2000) ? true : false)
277 #define LV_COORD_GET_PCT(x)     (_LV_COORD_PLAIN(x) > 1000 ? 1000 - _LV_COORD_PLAIN(x) : _LV_COORD_PLAIN(x))
278 #define LV_SIZE_CONTENT         LV_COORD_SET_SPEC(2001)
279 
280 LV_EXPORT_CONST_INT(LV_SIZE_CONTENT);
281 
282 /*Max coordinate value*/
283 #define LV_COORD_MAX            ((1 << _LV_COORD_TYPE_SHIFT) - 1)
284 #define LV_COORD_MIN            (-LV_COORD_MAX)
285 
286 LV_EXPORT_CONST_INT(LV_COORD_MAX);
287 LV_EXPORT_CONST_INT(LV_COORD_MIN);
288 
289 /**
290  * Convert a percentage value to `lv_coord_t`.
291  * Percentage values are stored in special range
292  * @param x the percentage (0..1000)
293  * @return a coordinate that stores the percentage
294  */
lv_pct(lv_coord_t x)295 static inline lv_coord_t lv_pct(lv_coord_t x)
296 {
297     return LV_PCT(x);
298 }
299 
300 #ifdef __cplusplus
301 } /*extern "C"*/
302 #endif
303 
304 #endif
305