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  * Join two areas into a third which involves the other two
182  * @param res_p pointer to an area, the result will be stored here
183  * @param a1_p pointer to the first area
184  * @param a2_p pointer to the second area
185  */
186 void _lv_area_join(lv_area_t * a_res_p, const lv_area_t * a1_p, const lv_area_t * a2_p);
187 
188 /**
189  * Check if a point is on an area
190  * @param a_p pointer to an area
191  * @param p_p pointer to a point
192  * @param radius radius of area (e.g. for rounded rectangle)
193  * @return false:the point is out of the area
194  */
195 bool _lv_area_is_point_on(const lv_area_t * a_p, const lv_point_t * p_p, lv_coord_t radius);
196 
197 /**
198  * Check if two area has common parts
199  * @param a1_p pointer to an area.
200  * @param a2_p pointer to an other area
201  * @return false: a1_p and a2_p has no common parts
202  */
203 bool _lv_area_is_on(const lv_area_t * a1_p, const lv_area_t * a2_p);
204 
205 /**
206  * Check if an area is fully on an other
207  * @param ain_p pointer to an area which could be in 'aholder_p'
208  * @param aholder_p pointer to an area which could involve 'ain_p'
209  * @param radius radius of `aholder_p` (e.g. for rounded rectangle)
210  * @return true: `ain_p` is fully inside `aholder_p`
211  */
212 bool _lv_area_is_in(const lv_area_t * ain_p, const lv_area_t * aholder_p, lv_coord_t radius);
213 
214 
215 /**
216  * Check if an area is fully out of an other
217  * @param aout_p pointer to an area which could be in 'aholder_p'
218  * @param aholder_p pointer to an area which could involve 'ain_p'
219  * @param radius radius of `aholder_p` (e.g. for rounded rectangle)
220  * @return true: `aout_p` is fully outside `aholder_p`
221  */
222 bool _lv_area_is_out(const lv_area_t * aout_p, const lv_area_t * aholder_p, lv_coord_t radius);
223 
224 /**
225  * Check if 2 area is the same
226  * @param a pointer to an area
227  * @param b pointer to another area
228  */
229 bool _lv_area_is_equal(const lv_area_t * a, const lv_area_t * b);
230 
231 /**
232  * Align an area to an other
233  * @param base an are where the other will be aligned
234  * @param to_align the area to align
235  * @param align `LV_ALIGN_...`
236  */
237 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);
238 
239 /**********************
240  *      MACROS
241  **********************/
242 
243 #if LV_USE_LARGE_COORD
244 #define _LV_COORD_TYPE_SHIFT    (29U)
245 #else
246 #define _LV_COORD_TYPE_SHIFT    (13U)
247 #endif
248 
249 #define _LV_COORD_TYPE_MASK     (3 << _LV_COORD_TYPE_SHIFT)
250 #define _LV_COORD_TYPE(x)       ((x) & _LV_COORD_TYPE_MASK)  /*Extract type specifiers*/
251 #define _LV_COORD_PLAIN(x)      ((x) & ~_LV_COORD_TYPE_MASK) /*Remove type specifiers*/
252 
253 #define _LV_COORD_TYPE_PX       (0 << _LV_COORD_TYPE_SHIFT)
254 #define _LV_COORD_TYPE_SPEC     (1 << _LV_COORD_TYPE_SHIFT)
255 #define _LV_COORD_TYPE_PX_NEG   (3 << _LV_COORD_TYPE_SHIFT)
256 
257 #define LV_COORD_IS_PX(x)       (_LV_COORD_TYPE(x) == _LV_COORD_TYPE_PX || \
258                                  _LV_COORD_TYPE(x) == _LV_COORD_TYPE_PX_NEG ? true : false)
259 #define LV_COORD_IS_SPEC(x)     (_LV_COORD_TYPE(x) == _LV_COORD_TYPE_SPEC ? true : false)
260 
261 #define LV_COORD_SET_SPEC(x)    ((x) | _LV_COORD_TYPE_SPEC)
262 
263 /*Special coordinates*/
264 #define LV_PCT(x)               (x < 0 ? LV_COORD_SET_SPEC(1000 - (x)) : LV_COORD_SET_SPEC(x))
265 #define LV_COORD_IS_PCT(x)      ((LV_COORD_IS_SPEC(x) && _LV_COORD_PLAIN(x) <= 2000) ? true : false)
266 #define LV_COORD_GET_PCT(x)     (_LV_COORD_PLAIN(x) > 1000 ? 1000 - _LV_COORD_PLAIN(x) : _LV_COORD_PLAIN(x))
267 #define LV_SIZE_CONTENT         LV_COORD_SET_SPEC(2001)
268 
269 LV_EXPORT_CONST_INT(LV_SIZE_CONTENT);
270 
271 /*Max coordinate value*/
272 #define LV_COORD_MAX            ((1 << _LV_COORD_TYPE_SHIFT) - 1)
273 #define LV_COORD_MIN            (-LV_COORD_MAX)
274 
275 LV_EXPORT_CONST_INT(LV_COORD_MAX);
276 LV_EXPORT_CONST_INT(LV_COORD_MIN);
277 
278 /**
279  * Convert a percentage value to `lv_coord_t`.
280  * Percentage values are stored in special range
281  * @param x the percentage (0..1000)
282  * @return a coordinate that stores the percentage
283  */
lv_pct(lv_coord_t x)284 static inline lv_coord_t lv_pct(lv_coord_t x)
285 {
286     return LV_PCT(x);
287 }
288 
289 #ifdef __cplusplus
290 } /*extern "C"*/
291 #endif
292 
293 #endif
294