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