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 "lv_types.h"
18 #include "lv_math.h"
19
20 /*********************
21 * DEFINES
22 *********************/
23
24 /**********************
25 * TYPEDEFS
26 **********************/
27
28 /**
29 * Represents a point on the screen.
30 */
31 typedef struct {
32 int32_t x;
33 int32_t y;
34 } lv_point_t;
35
36 typedef struct {
37 lv_value_precise_t x;
38 lv_value_precise_t y;
39 } lv_point_precise_t;
40
41 /** Represents an area of the screen.*/
42 typedef struct {
43 int32_t x1;
44 int32_t y1;
45 int32_t x2;
46 int32_t y2;
47 } lv_area_t;
48
49 /** Alignments*/
50
51 typedef 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 } lv_align_t;
76
77 typedef enum {
78 LV_DIR_NONE = 0x00,
79 LV_DIR_LEFT = (1 << 0),
80 LV_DIR_RIGHT = (1 << 1),
81 LV_DIR_TOP = (1 << 2),
82 LV_DIR_BOTTOM = (1 << 3),
83 LV_DIR_HOR = LV_DIR_LEFT | LV_DIR_RIGHT,
84 LV_DIR_VER = LV_DIR_TOP | LV_DIR_BOTTOM,
85 LV_DIR_ALL = LV_DIR_HOR | LV_DIR_VER,
86 } lv_dir_t;
87
88 /**********************
89 * GLOBAL PROTOTYPES
90 **********************/
91
92 /**
93 * Initialize an area
94 * @param area_p pointer to an area
95 * @param x1 left coordinate of the area
96 * @param y1 top coordinate of the area
97 * @param x2 right coordinate of the area
98 * @param y2 bottom coordinate of the area
99 */
100 void lv_area_set(lv_area_t * area_p, int32_t x1, int32_t y1, int32_t x2, int32_t y2);
101
102 /**
103 * Copy an area
104 * @param dest pointer to the destination area
105 * @param src pointer to the source area
106 */
lv_area_copy(lv_area_t * dest,const lv_area_t * src)107 inline static void lv_area_copy(lv_area_t * dest, const lv_area_t * src)
108 {
109 dest->x1 = src->x1;
110 dest->y1 = src->y1;
111 dest->x2 = src->x2;
112 dest->y2 = src->y2;
113 }
114
115 /**
116 * Get the width of an area
117 * @param area_p pointer to an area
118 * @return the width of the area (if x1 == x2 -> width = 1)
119 */
120 int32_t lv_area_get_width(const lv_area_t * area_p);
121
122 /**
123 * Get the height of an area
124 * @param area_p pointer to an area
125 * @return the height of the area (if y1 == y2 -> height = 1)
126 */
127 int32_t lv_area_get_height(const lv_area_t * area_p);
128
129 /**
130 * Set the width of an area
131 * @param area_p pointer to an area
132 * @param w the new width of the area (w == 1 makes x1 == x2)
133 */
134 void lv_area_set_width(lv_area_t * area_p, int32_t w);
135
136 /**
137 * Set the height of an area
138 * @param area_p pointer to an area
139 * @param h the new height of the area (h == 1 makes y1 == y2)
140 */
141 void lv_area_set_height(lv_area_t * area_p, int32_t h);
142
143 /**
144 * Return with area of an area (x * y)
145 * @param area_p pointer to an area
146 * @return size of area
147 */
148 uint32_t lv_area_get_size(const lv_area_t * area_p);
149
150 void lv_area_increase(lv_area_t * area, int32_t w_extra, int32_t h_extra);
151
152 void lv_area_move(lv_area_t * area, int32_t x_ofs, int32_t y_ofs);
153
154 /**
155 * Align an area to another
156 * @param base an area where the other will be aligned
157 * @param to_align the area to align
158 * @param align `LV_ALIGN_...`
159 * @param ofs_x X offset
160 * @param ofs_y Y offset
161 */
162 void lv_area_align(const lv_area_t * base, lv_area_t * to_align, lv_align_t align, int32_t ofs_x, int32_t ofs_y);
163
164 /**
165 * Transform a point
166 * @param point pointer to a point
167 * @param angle angle with 0.1 resolutions (123 means 12.3°)
168 * @param scale_x horizontal zoom, 256 means 100%
169 * @param scale_y vertical zoom, 256 means 100%
170 * @param pivot pointer to the pivot point of the transformation
171 * @param zoom_first true: zoom first and rotate after that; else: opposite order
172 */
173 void lv_point_transform(lv_point_t * point, int32_t angle, int32_t scale_x, int32_t scale_y, const lv_point_t * pivot,
174 bool zoom_first);
175
176 /**
177 * Transform an array of points
178 * @param points pointer to an array of points
179 * @param count number of points in the array
180 * @param angle angle with 0.1 resolutions (123 means 12.3°)
181 * @param scale_x horizontal zoom, 256 means 100%
182 * @param scale_y vertical zoom, 256 means 100%
183 * @param pivot pointer to the pivot point of the transformation
184 * @param zoom_first true: zoom first and rotate after that; else: opposite order
185 */
186 void lv_point_array_transform(lv_point_t * points, size_t count, int32_t angle, int32_t scale_x, int32_t scale_y,
187 const lv_point_t * pivot,
188 bool zoom_first);
189
190 lv_point_t lv_point_from_precise(const lv_point_precise_t * p);
191
192 lv_point_precise_t lv_point_to_precise(const lv_point_t * p);
193
194 void lv_point_set(lv_point_t * p, int32_t x, int32_t y);
195
196 void lv_point_precise_set(lv_point_precise_t * p, lv_value_precise_t x, lv_value_precise_t y);
197
198 void lv_point_swap(lv_point_t * p1, lv_point_t * p2);
199
200 void lv_point_precise_swap(lv_point_precise_t * p1, lv_point_precise_t * p2);
201
202 /**********************
203 * MACROS
204 **********************/
205
206 #define LV_COORD_TYPE_SHIFT (29U)
207
208 #define LV_COORD_TYPE_MASK (3 << LV_COORD_TYPE_SHIFT)
209 #define LV_COORD_TYPE(x) ((x) & LV_COORD_TYPE_MASK) /*Extract type specifiers*/
210 #define LV_COORD_PLAIN(x) ((x) & ~LV_COORD_TYPE_MASK) /*Remove type specifiers*/
211
212 #define LV_COORD_TYPE_PX (0 << LV_COORD_TYPE_SHIFT)
213 #define LV_COORD_TYPE_SPEC (1 << LV_COORD_TYPE_SHIFT)
214 #define LV_COORD_TYPE_PX_NEG (3 << LV_COORD_TYPE_SHIFT)
215
216 #define LV_COORD_IS_PX(x) (LV_COORD_TYPE(x) == LV_COORD_TYPE_PX || LV_COORD_TYPE(x) == LV_COORD_TYPE_PX_NEG)
217 #define LV_COORD_IS_SPEC(x) (LV_COORD_TYPE(x) == LV_COORD_TYPE_SPEC)
218
219 #define LV_COORD_SET_SPEC(x) ((x) | LV_COORD_TYPE_SPEC)
220
221 /** Max coordinate value */
222 #define LV_COORD_MAX ((1 << LV_COORD_TYPE_SHIFT) - 1)
223 #define LV_COORD_MIN (-LV_COORD_MAX)
224
225 /*Special coordinates*/
226 #define LV_SIZE_CONTENT LV_COORD_SET_SPEC(LV_COORD_MAX)
227 #define LV_PCT_STORED_MAX (LV_COORD_MAX - 1)
228 #if LV_PCT_STORED_MAX % 2 != 0
229 #error LV_PCT_STORED_MAX should be an even number
230 #endif
231 #define LV_PCT_POS_MAX (LV_PCT_STORED_MAX / 2)
232 #define LV_PCT(x) (LV_COORD_SET_SPEC(((x) < 0 ? (LV_PCT_POS_MAX - LV_MAX((x), -LV_PCT_POS_MAX)) : LV_MIN((x), LV_PCT_POS_MAX))))
233 #define LV_COORD_IS_PCT(x) ((LV_COORD_IS_SPEC(x) && LV_COORD_PLAIN(x) <= LV_PCT_STORED_MAX))
234 #define LV_COORD_GET_PCT(x) (LV_COORD_PLAIN(x) > LV_PCT_POS_MAX ? LV_PCT_POS_MAX - LV_COORD_PLAIN(x) : LV_COORD_PLAIN(x))
235
236 LV_EXPORT_CONST_INT(LV_COORD_MAX);
237 LV_EXPORT_CONST_INT(LV_COORD_MIN);
238 LV_EXPORT_CONST_INT(LV_SIZE_CONTENT);
239
240 /**
241 * Convert a percentage value to `int32_t`.
242 * Percentage values are stored in special range
243 * @param x the percentage (0..1000)
244 * @return a coordinate that stores the percentage
245 */
246 int32_t lv_pct(int32_t x);
247
248 int32_t lv_pct_to_px(int32_t v, int32_t base);
249
250 #ifdef __cplusplus
251 } /*extern "C"*/
252 #endif
253
254 #endif
255