Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/libs/lvgl/lv_objx/lv_line.c
1476 views
1
/**
2
* @file lv_line.c
3
*
4
*/
5
6
/*********************
7
* INCLUDES
8
*********************/
9
#include "lv_line.h"
10
11
#if USE_LV_LINE != 0
12
#include "../lv_draw/lv_draw.h"
13
#include "../lv_misc/lv_math.h"
14
#include <stdint.h>
15
#include <string.h>
16
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_func_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
*/
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_func(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_func(new_line, lv_line_design);
72
lv_obj_set_signal_func(new_line, lv_line_signal);
73
74
/*Init the new line*/
75
if(copy == NULL) {
76
lv_obj_set_size(new_line, LV_DPI, LV_DPI); /*Auto size is enables, but set default size until no points are added*/
77
lv_obj_set_style(new_line, NULL); /*Inherit parent's style*/
78
lv_obj_set_click(new_line, false);
79
}
80
/*Copy an existing object*/
81
else {
82
lv_line_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
83
lv_line_set_auto_size(new_line, lv_line_get_auto_size(copy));
84
lv_line_set_y_invert(new_line, lv_line_get_y_invert(copy));
85
lv_line_set_auto_size(new_line, lv_line_get_auto_size(copy));
86
lv_line_set_points(new_line, copy_ext->point_array, copy_ext->point_num);
87
/*Refresh the style with new signal function*/
88
lv_obj_refresh_style(new_line);
89
}
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
*/
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
lv_style_t * style = lv_line_get_style(line);
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
*/
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
*/
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
*/
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
*/
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
*/
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) return false;
210
else if(mode == LV_DESIGN_DRAW_MAIN) {
211
lv_line_ext_t * ext = lv_obj_get_ext_attr(line);
212
213
if(ext->point_num == 0 || ext->point_array == NULL) return false;
214
215
lv_style_t * style = lv_obj_get_style(line);
216
lv_opa_t opa_scale = lv_obj_get_opa_scale(line);
217
lv_area_t area;
218
lv_obj_get_coords(line, &area);
219
lv_coord_t x_ofs = area.x1;
220
lv_coord_t y_ofs = area.y1;
221
lv_point_t p1;
222
lv_point_t p2;
223
lv_coord_t h = lv_obj_get_height(line);
224
uint16_t i;
225
226
lv_style_t circle_style; /*If rounded...*/
227
lv_style_copy(&circle_style, style);
228
circle_style.body.radius = LV_RADIUS_CIRCLE;
229
circle_style.body.main_color = style->line.color;
230
circle_style.body.grad_color = style->line.color;
231
circle_style.body.opa = style->line.opa;
232
lv_area_t circle_area;
233
234
/*Read all points and draw the lines*/
235
for(i = 0; i < ext->point_num - 1; i++) {
236
237
p1.x = ext->point_array[i].x + x_ofs;
238
p2.x = ext->point_array[i + 1].x + x_ofs;
239
240
if(ext->y_inv == 0) {
241
p1.y = ext->point_array[i].y + y_ofs;
242
p2.y = ext->point_array[i + 1].y + y_ofs;
243
} else {
244
p1.y = h - ext->point_array[i].y + y_ofs;
245
p2.y = h - ext->point_array[i + 1].y + y_ofs;
246
}
247
lv_draw_line(&p1, &p2, mask, style, opa_scale);
248
249
/*Draw circle on the joints if enabled*/
250
if(style->line.rounded) {
251
circle_area.x1 = p1.x - ((style->line.width - 1) >> 1) - ((style->line.width - 1) & 0x1);
252
circle_area.y1 = p1.y - ((style->line.width - 1) >> 1) - ((style->line.width - 1) & 0x1);
253
circle_area.x2 = p1.x + ((style->line.width - 1) >> 1);
254
circle_area.y2 = p1.y + ((style->line.width - 1) >> 1);
255
lv_draw_rect(&circle_area, mask, &circle_style, opa_scale);
256
}
257
}
258
259
/*Draw circle on the last point too if enabled*/
260
if(style->line.rounded) {
261
circle_area.x1 = p2.x - ((style->line.width - 1) >> 1) - ((style->line.width - 1) & 0x1);
262
circle_area.y1 = p2.y - ((style->line.width - 1) >> 1) - ((style->line.width - 1) & 0x1);
263
circle_area.x2 = p2.x + ((style->line.width - 1) >> 1);
264
circle_area.y2 = p2.y + ((style->line.width - 1) >> 1);
265
lv_draw_rect(&circle_area, mask, &circle_style, opa_scale);
266
}
267
}
268
return true;
269
}
270
271
/**
272
* Signal function of the line
273
* @param line pointer to a line object
274
* @param sign a signal type from lv_signal_t enum
275
* @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
276
*/
277
static lv_res_t lv_line_signal(lv_obj_t * line, lv_signal_t sign, void * param)
278
{
279
lv_res_t res;
280
281
/* Include the ancient signal function */
282
res = ancestor_signal(line, sign, param);
283
if(res != LV_RES_OK) return res;
284
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_SIZE) {
294
lv_style_t * style = lv_line_get_style(line);
295
if(line->ext_size < style->line.width) line->ext_size = style->line.width;
296
}
297
298
299
return res;
300
}
301
#endif
302
303