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