1 /**
2 * @file lv_line.c
3 *
4 */
5
6 /*********************
7 * INCLUDES
8 *********************/
9 #include "lv_line.h"
10
11 #if LV_USE_LINE != 0
12 #include "../lv_draw/lv_draw.h"
13 #include "../lv_misc/lv_math.h"
14 #include <stdbool.h>
15 #include <stdint.h>
16 #include <string.h>
17
18 /*********************
19 * DEFINES
20 *********************/
21
22 /**********************
23 * TYPEDEFS
24 **********************/
25
26 /**********************
27 * STATIC PROTOTYPES
28 **********************/
29 static bool lv_line_design(lv_obj_t * line, const lv_area_t * mask, lv_design_mode_t mode);
30 static lv_res_t lv_line_signal(lv_obj_t * line, lv_signal_t sign, void * param);
31
32 /**********************
33 * STATIC VARIABLES
34 **********************/
35 static lv_signal_cb_t ancestor_signal;
36
37 /**********************
38 * MACROS
39 **********************/
40
41 /**********************
42 * GLOBAL FUNCTIONS
43 **********************/
44
45 /**
46 * Create a line objects
47 * @param par pointer to an object, it will be the parent of the new line
48 * @return pointer to the created line
49 */
lv_line_create(lv_obj_t * par,const lv_obj_t * copy)50 lv_obj_t * lv_line_create(lv_obj_t * par, const lv_obj_t * copy)
51 {
52 LV_LOG_TRACE("line create started");
53
54 /*Create a basic object*/
55 lv_obj_t * new_line = lv_obj_create(par, copy);
56 lv_mem_assert(new_line);
57 if(new_line == NULL) return NULL;
58
59 if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_line);
60
61 /*Extend the basic object to line object*/
62 lv_line_ext_t * ext = lv_obj_allocate_ext_attr(new_line, sizeof(lv_line_ext_t));
63 lv_mem_assert(ext);
64 if(ext == NULL) return NULL;
65
66 ext->point_num = 0;
67 ext->point_array = NULL;
68 ext->auto_size = 1;
69 ext->y_inv = 0;
70
71 lv_obj_set_design_cb(new_line, lv_line_design);
72 lv_obj_set_signal_cb(new_line, lv_line_signal);
73
74 /*Init the new line*/
75 if(copy == NULL) {
76 lv_obj_set_size(new_line, LV_DPI,
77 LV_DPI); /*Auto size is enables, but set default size until no points are added*/
78 lv_obj_set_style(new_line, NULL); /*Inherit parent's style*/
79 lv_obj_set_click(new_line, false);
80 }
81 /*Copy an existing object*/
82 else {
83 lv_line_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
84 lv_line_set_auto_size(new_line, lv_line_get_auto_size(copy));
85 lv_line_set_y_invert(new_line, lv_line_get_y_invert(copy));
86 lv_line_set_auto_size(new_line, lv_line_get_auto_size(copy));
87 lv_line_set_points(new_line, copy_ext->point_array, copy_ext->point_num);
88 /*Refresh the style with new signal function*/
89 lv_obj_refresh_style(new_line);
90 }
91
92 LV_LOG_INFO("line created");
93
94 return new_line;
95 }
96
97 /*=====================
98 * Setter functions
99 *====================*/
100
101 /**
102 * Set an array of points. The line object will connect these points.
103 * @param line pointer to a line object
104 * @param point_a an array of points. Only the address is saved,
105 * so the array can NOT be a local variable which will be destroyed
106 * @param point_num number of points in 'point_a'
107 */
lv_line_set_points(lv_obj_t * line,const lv_point_t point_a[],uint16_t point_num)108 void lv_line_set_points(lv_obj_t * line, const lv_point_t point_a[], uint16_t point_num)
109 {
110 lv_line_ext_t * ext = lv_obj_get_ext_attr(line);
111 ext->point_array = point_a;
112 ext->point_num = point_num;
113
114 if(point_num > 0 && ext->auto_size != 0) {
115 uint16_t i;
116 lv_coord_t xmax = LV_COORD_MIN;
117 lv_coord_t ymax = LV_COORD_MIN;
118 for(i = 0; i < point_num; i++) {
119 xmax = LV_MATH_MAX(point_a[i].x, xmax);
120 ymax = LV_MATH_MAX(point_a[i].y, ymax);
121 }
122
123 const lv_style_t * style = lv_line_get_style(line, LV_LINE_STYLE_MAIN);
124 lv_obj_set_size(line, xmax + style->line.width, ymax + style->line.width);
125 }
126
127 lv_obj_invalidate(line);
128 }
129
130 /**
131 * Enable (or disable) the auto-size option. The size of the object will fit to its points.
132 * (set width to x max and height to y max)
133 * @param line pointer to a line object
134 * @param en true: auto size is enabled, false: auto size is disabled
135 */
lv_line_set_auto_size(lv_obj_t * line,bool en)136 void lv_line_set_auto_size(lv_obj_t * line, bool en)
137 {
138 lv_line_ext_t * ext = lv_obj_get_ext_attr(line);
139 if(ext->auto_size == en) return;
140
141 ext->auto_size = en == false ? 0 : 1;
142
143 /*Refresh the object*/
144 if(en) lv_line_set_points(line, ext->point_array, ext->point_num);
145 }
146
147 /**
148 * Enable (or disable) the y coordinate inversion.
149 * If enabled then y will be subtracted from the height of the object,
150 * therefore the y=0 coordinate will be on the bottom.
151 * @param line pointer to a line object
152 * @param en true: enable the y inversion, false:disable the y inversion
153 */
lv_line_set_y_invert(lv_obj_t * line,bool en)154 void lv_line_set_y_invert(lv_obj_t * line, bool en)
155 {
156 lv_line_ext_t * ext = lv_obj_get_ext_attr(line);
157 if(ext->y_inv == en) return;
158
159 ext->y_inv = en == false ? 0 : 1;
160
161 lv_obj_invalidate(line);
162 }
163
164 /*=====================
165 * Getter functions
166 *====================*/
167
168 /**
169 * Get the auto size attribute
170 * @param line pointer to a line object
171 * @return true: auto size is enabled, false: disabled
172 */
lv_line_get_auto_size(const lv_obj_t * line)173 bool lv_line_get_auto_size(const lv_obj_t * line)
174 {
175 lv_line_ext_t * ext = lv_obj_get_ext_attr(line);
176
177 return ext->auto_size == 0 ? false : true;
178 }
179
180 /**
181 * Get the y inversion attribute
182 * @param line pointer to a line object
183 * @return true: y inversion is enabled, false: disabled
184 */
lv_line_get_y_invert(const lv_obj_t * line)185 bool lv_line_get_y_invert(const lv_obj_t * line)
186 {
187 lv_line_ext_t * ext = lv_obj_get_ext_attr(line);
188
189 return ext->y_inv == 0 ? false : true;
190 }
191
192 /**********************
193 * STATIC FUNCTIONS
194 **********************/
195
196 /**
197 * Handle the drawing related tasks of the lines
198 * @param line pointer to an object
199 * @param mask the object will be drawn only in this area
200 * @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area
201 * (return 'true' if yes)
202 * LV_DESIGN_DRAW: draw the object (always return 'true')
203 * LV_DESIGN_DRAW_POST: drawing after every children are drawn
204 * @param return true/false, depends on 'mode'
205 */
lv_line_design(lv_obj_t * line,const lv_area_t * mask,lv_design_mode_t mode)206 static bool lv_line_design(lv_obj_t * line, const lv_area_t * mask, lv_design_mode_t mode)
207 {
208 /*A line never covers an area*/
209 if(mode == LV_DESIGN_COVER_CHK)
210 return false;
211 else if(mode == LV_DESIGN_DRAW_MAIN) {
212 lv_line_ext_t * ext = lv_obj_get_ext_attr(line);
213
214 if(ext->point_num == 0 || ext->point_array == NULL) return false;
215
216 const lv_style_t * style = lv_obj_get_style(line);
217 lv_opa_t opa_scale = lv_obj_get_opa_scale(line);
218 lv_area_t area;
219 lv_obj_get_coords(line, &area);
220 lv_coord_t x_ofs = area.x1;
221 lv_coord_t y_ofs = area.y1;
222 lv_point_t p1;
223 lv_point_t p2;
224 lv_coord_t h = lv_obj_get_height(line);
225 uint16_t i;
226
227 lv_style_t circle_style_tmp; /*If rounded...*/
228 lv_style_copy(&circle_style_tmp, style);
229 circle_style_tmp.body.radius = LV_RADIUS_CIRCLE;
230 circle_style_tmp.body.main_color = style->line.color;
231 circle_style_tmp.body.grad_color = style->line.color;
232 circle_style_tmp.body.opa = style->line.opa;
233 lv_area_t circle_area;
234
235 /*Read all points and draw the lines*/
236 for(i = 0; i < ext->point_num - 1; i++) {
237
238 p1.x = ext->point_array[i].x + x_ofs;
239 p2.x = ext->point_array[i + 1].x + x_ofs;
240
241 if(ext->y_inv == 0) {
242 p1.y = ext->point_array[i].y + y_ofs;
243 p2.y = ext->point_array[i + 1].y + y_ofs;
244 } else {
245 p1.y = h - ext->point_array[i].y + y_ofs;
246 p2.y = h - ext->point_array[i + 1].y + y_ofs;
247 }
248 lv_draw_line(&p1, &p2, mask, style, opa_scale);
249
250 /*Draw circle on the joints if enabled*/
251 if(style->line.rounded) {
252 circle_area.x1 = p1.x - ((style->line.width - 1) >> 1) - ((style->line.width - 1) & 0x1);
253 circle_area.y1 = p1.y - ((style->line.width - 1) >> 1) - ((style->line.width - 1) & 0x1);
254 circle_area.x2 = p1.x + ((style->line.width - 1) >> 1);
255 circle_area.y2 = p1.y + ((style->line.width - 1) >> 1);
256 lv_draw_rect(&circle_area, mask, &circle_style_tmp, opa_scale);
257 }
258 }
259
260 /*Draw circle on the last point too if enabled*/
261 if(style->line.rounded) {
262 circle_area.x1 = p2.x - ((style->line.width - 1) >> 1) - ((style->line.width - 1) & 0x1);
263 circle_area.y1 = p2.y - ((style->line.width - 1) >> 1) - ((style->line.width - 1) & 0x1);
264 circle_area.x2 = p2.x + ((style->line.width - 1) >> 1);
265 circle_area.y2 = p2.y + ((style->line.width - 1) >> 1);
266 lv_draw_rect(&circle_area, mask, &circle_style_tmp, opa_scale);
267 }
268 }
269 return true;
270 }
271
272 /**
273 * Signal function of the line
274 * @param line pointer to a line object
275 * @param sign a signal type from lv_signal_t enum
276 * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
277 */
lv_line_signal(lv_obj_t * line,lv_signal_t sign,void * param)278 static lv_res_t lv_line_signal(lv_obj_t * line, lv_signal_t sign, void * param)
279 {
280 lv_res_t res;
281
282 /* Include the ancient signal function */
283 res = ancestor_signal(line, sign, param);
284 if(res != LV_RES_OK) return res;
285
286 if(sign == LV_SIGNAL_GET_TYPE) {
287 lv_obj_type_t * buf = param;
288 uint8_t i;
289 for(i = 0; i < LV_MAX_ANCESTOR_NUM - 1; i++) { /*Find the last set data*/
290 if(buf->type[i] == NULL) break;
291 }
292 buf->type[i] = "lv_line";
293 } else if(sign == LV_SIGNAL_REFR_EXT_DRAW_PAD) {
294 const lv_style_t * style = lv_line_get_style(line, LV_LINE_STYLE_MAIN);
295 if(line->ext_draw_pad < style->line.width) line->ext_draw_pad = style->line.width;
296 }
297
298 return res;
299 }
300 #endif
301