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 <string.h>
17 #include <stdbool.h>
18 #include <stdint.h>
19 #ifdef LV_CONF_INCLUDE_SIMPLE
20 #include "lv_conf.h"
21 #else
22 #include "../../lv_conf.h"
23 #endif
24 
25 /*********************
26  *      DEFINES
27  *********************/
28 /*To avoid overflow don't let the max ranges (reduce with 1000) */
29 #define LV_COORD_MAX ((lv_coord_t)((uint32_t)((uint32_t)1 << (8 * sizeof(lv_coord_t) - 1)) - 1000))
30 #define LV_COORD_MIN (-LV_COORD_MAX)
31 
32 /**********************
33  *      TYPEDEFS
34  **********************/
35 
36 /**
37  * Represents a point on the screen.
38  */
39 typedef struct
40 {
41     lv_coord_t x;
42     lv_coord_t y;
43 } lv_point_t;
44 
45 /** Represents an area of the screen. */
46 typedef struct
47 {
48     lv_coord_t x1;
49     lv_coord_t y1;
50     lv_coord_t x2;
51     lv_coord_t y2;
52 } lv_area_t;
53 
54 /**********************
55  * GLOBAL PROTOTYPES
56  **********************/
57 
58 /**
59  * Initialize an area
60  * @param area_p pointer to an area
61  * @param x1 left coordinate of the area
62  * @param y1 top coordinate of the area
63  * @param x2 right coordinate of the area
64  * @param y2 bottom coordinate of the area
65  */
66 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);
67 
68 /**
69  * Copy an area
70  * @param dest pointer to the destination area
71  * @param src pointer to the source area
72  */
lv_area_copy(lv_area_t * dest,const lv_area_t * src)73 inline static void lv_area_copy(lv_area_t * dest, const lv_area_t * src)
74 {
75     memcpy(dest, src, sizeof(lv_area_t));
76 }
77 
78 /**
79  * Get the width of an area
80  * @param area_p pointer to an area
81  * @return the width of the area (if x1 == x2 -> width = 1)
82  */
lv_area_get_width(const lv_area_t * area_p)83 static inline lv_coord_t lv_area_get_width(const lv_area_t * area_p)
84 {
85     return area_p->x2 - area_p->x1 + 1;
86 }
87 
88 /**
89  * Get the height of an area
90  * @param area_p pointer to an area
91  * @return the height of the area (if y1 == y2 -> height = 1)
92  */
lv_area_get_height(const lv_area_t * area_p)93 static inline lv_coord_t lv_area_get_height(const lv_area_t * area_p)
94 {
95     return area_p->y2 - area_p->y1 + 1;
96 }
97 
98 /**
99  * Set the width of an area
100  * @param area_p pointer to an area
101  * @param w the new width of the area (w == 1 makes x1 == x2)
102  */
103 void lv_area_set_width(lv_area_t * area_p, lv_coord_t w);
104 
105 /**
106  * Set the height of an area
107  * @param area_p pointer to an area
108  * @param h the new height of the area (h == 1 makes y1 == y2)
109  */
110 void lv_area_set_height(lv_area_t * area_p, lv_coord_t h);
111 
112 /**
113  * Set the position of an area (width and height will be kept)
114  * @param area_p pointer to an area
115  * @param x the new x coordinate of the area
116  * @param y the new y coordinate of the area
117  */
118 void lv_area_set_pos(lv_area_t * area_p, lv_coord_t x, lv_coord_t y);
119 
120 /**
121  * Return with area of an area (x * y)
122  * @param area_p pointer to an area
123  * @return size of area
124  */
125 uint32_t lv_area_get_size(const lv_area_t * area_p);
126 
127 /**
128  * Get the common parts of two areas
129  * @param res_p pointer to an area, the result will be stored her
130  * @param a1_p pointer to the first area
131  * @param a2_p pointer to the second area
132  * @return false: the two area has NO common parts, res_p is invalid
133  */
134 bool lv_area_intersect(lv_area_t * res_p, const lv_area_t * a1_p, const lv_area_t * a2_p);
135 
136 /**
137  * Join two areas into a third which involves the other two
138  * @param res_p pointer to an area, the result will be stored here
139  * @param a1_p pointer to the first area
140  * @param a2_p pointer to the second area
141  */
142 void lv_area_join(lv_area_t * a_res_p, const lv_area_t * a1_p, const lv_area_t * a2_p);
143 
144 /**
145  * Check if a point is on an area
146  * @param a_p pointer to an area
147  * @param p_p pointer to a point
148  * @return false:the point is out of the area
149  */
150 bool lv_area_is_point_on(const lv_area_t * a_p, const lv_point_t * p_p);
151 
152 /**
153  * Check if two area has common parts
154  * @param a1_p pointer to an area.
155  * @param a2_p pointer to an other area
156  * @return false: a1_p and a2_p has no common parts
157  */
158 bool lv_area_is_on(const lv_area_t * a1_p, const lv_area_t * a2_p);
159 
160 /**
161  * Check if an area is fully on an other
162  * @param ain_p pointer to an area which could be on aholder_p
163  * @param aholder pointer to an area which could involve ain_p
164  * @return
165  */
166 bool lv_area_is_in(const lv_area_t * ain_p, const lv_area_t * aholder_p);
167 
168 /**********************
169  *      MACROS
170  **********************/
171 
172 #ifdef __cplusplus
173 } /* extern "C" */
174 #endif
175 
176 #endif
177