1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-21 06:53:01 +08:00
lvgl/src/lv_objx/lv_line.c

291 lines
8.5 KiB
C
Raw Normal View History

2016-06-08 07:25:08 +02:00
/**
* @file lv_line.c
*
*/
/*********************
* INCLUDES
*********************/
#include "lv_line.h"
2016-06-08 07:25:08 +02:00
#if LV_USE_LINE != 0
2019-09-24 16:30:38 +02:00
#include "../lv_core/lv_debug.h"
2016-06-08 07:25:08 +02:00
#include "../lv_draw/lv_draw.h"
2017-11-23 20:42:14 +01:00
#include "../lv_misc/lv_math.h"
2016-06-08 07:25:08 +02:00
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
/*********************
* DEFINES
*********************/
#define LV_OBJX_NAME "lv_line"
2016-06-08 07:25:08 +02:00
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
2019-09-06 19:53:39 +02:00
static lv_design_res_t lv_line_design(lv_obj_t * line, const lv_area_t * clip_area, lv_design_mode_t mode);
2017-11-13 16:11:05 +01:00
static lv_res_t lv_line_signal(lv_obj_t * line, lv_signal_t sign, void * param);
2016-06-08 07:25:08 +02:00
/**********************
* STATIC VARIABLES
**********************/
static lv_signal_cb_t ancestor_signal;
2016-06-08 07:25:08 +02:00
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/**
* Create a line objects
2016-10-07 11:15:46 +02:00
* @param par pointer to an object, it will be the parent of the new line
2016-06-08 07:25:08 +02:00
* @return pointer to the created line
*/
lv_obj_t * lv_line_create(lv_obj_t * par, const lv_obj_t * copy)
2016-06-08 07:25:08 +02:00
{
2018-10-05 17:22:49 +02:00
LV_LOG_TRACE("line create started");
2018-07-25 17:57:08 +02:00
2016-06-08 07:25:08 +02:00
/*Create a basic object*/
2016-10-07 11:15:46 +02:00
lv_obj_t * new_line = lv_obj_create(par, copy);
2019-09-24 23:14:17 +02:00
LV_ASSERT_MEM(new_line);
if(new_line == NULL) return NULL;
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_line);
2016-06-08 07:25:08 +02:00
2017-04-13 16:12:03 +02:00
/*Extend the basic object to line object*/
lv_line_ext_t * ext = lv_obj_allocate_ext_attr(new_line, sizeof(lv_line_ext_t));
2019-09-24 23:14:17 +02:00
LV_ASSERT_MEM(ext);
if(ext == NULL) {
lv_obj_del(new_line);
return NULL;
}
2019-04-04 07:15:40 +02:00
ext->point_num = 0;
ext->point_array = NULL;
2019-04-04 07:15:40 +02:00
ext->auto_size = 1;
ext->y_inv = 0;
lv_obj_set_design_cb(new_line, lv_line_design);
lv_obj_set_signal_cb(new_line, lv_line_signal);
2016-06-08 07:25:08 +02:00
2017-04-13 16:12:03 +02:00
/*Init the new line*/
2016-10-07 11:15:46 +02:00
if(copy == NULL) {
2019-06-06 06:05:40 +02:00
lv_obj_set_size(new_line, LV_DPI,
LV_DPI); /*Auto size is enables, but set default size until no points are added*/
2020-01-01 22:01:19 +01:00
2018-06-19 09:49:58 +02:00
lv_obj_set_click(new_line, false);
2016-06-08 07:25:08 +02:00
}
2016-10-07 11:15:46 +02:00
/*Copy an existing object*/
2016-06-08 07:25:08 +02:00
else {
lv_line_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
2018-06-19 09:49:58 +02:00
lv_line_set_auto_size(new_line, lv_line_get_auto_size(copy));
lv_line_set_y_invert(new_line, lv_line_get_y_invert(copy));
2018-06-19 09:49:58 +02:00
lv_line_set_auto_size(new_line, lv_line_get_auto_size(copy));
lv_line_set_points(new_line, copy_ext->point_array, copy_ext->point_num);
/*Refresh the style with new signal function*/
2020-01-01 22:01:19 +01:00
// lv_obj_refresh_style(new_line);
2016-06-08 07:25:08 +02:00
}
2018-10-05 17:22:49 +02:00
LV_LOG_INFO("line created");
2018-07-25 17:57:08 +02:00
2016-10-07 11:15:46 +02:00
return new_line;
2016-06-08 07:25:08 +02:00
}
/*=====================
* Setter functions
*====================*/
/**
* Set an array of points. The line object will connect these points.
2016-10-07 11:15:46 +02:00
* @param line pointer to a line object
2016-06-08 07:25:08 +02:00
* @param point_a an array of points. Only the address is saved,
2017-04-21 09:15:39 +02:00
* so the array can NOT be a local variable which will be destroyed
2016-06-08 07:25:08 +02:00
* @param point_num number of points in 'point_a'
*/
void lv_line_set_points(lv_obj_t * line, const lv_point_t point_a[], uint16_t point_num)
2016-06-08 07:25:08 +02:00
{
LV_ASSERT_OBJ(line, LV_OBJX_NAME);
2018-06-19 09:49:58 +02:00
lv_line_ext_t * ext = lv_obj_get_ext_attr(line);
2019-04-04 07:15:40 +02:00
ext->point_array = point_a;
ext->point_num = point_num;
2018-06-19 09:49:58 +02:00
if(point_num > 0 && ext->auto_size != 0) {
uint16_t i;
lv_coord_t xmax = LV_COORD_MIN;
lv_coord_t ymax = LV_COORD_MIN;
for(i = 0; i < point_num; i++) {
xmax = LV_MATH_MAX(point_a[i].x, xmax);
ymax = LV_MATH_MAX(point_a[i].y, ymax);
}
2020-01-06 22:14:04 +01:00
lv_style_int_t line_width = lv_obj_get_style_int(line, LV_LINE_PART_MAIN, LV_STYLE_LINE_WIDTH);
2020-01-01 22:01:19 +01:00
lv_obj_set_size(line, xmax + line_width, ymax + line_width);
2018-06-19 09:49:58 +02:00
}
lv_obj_invalidate(line);
2016-06-08 07:25:08 +02:00
}
/**
* Enable (or disable) the auto-size option. The size of the object will fit to its points.
* (set width to x max and height to y max)
2016-10-07 11:15:46 +02:00
* @param line pointer to a line object
* @param en true: auto size is enabled, false: auto size is disabled
2016-06-08 07:25:08 +02:00
*/
void lv_line_set_auto_size(lv_obj_t * line, bool en)
2016-06-08 07:25:08 +02:00
{
LV_ASSERT_OBJ(line, LV_OBJX_NAME);
2018-06-19 09:49:58 +02:00
lv_line_ext_t * ext = lv_obj_get_ext_attr(line);
if(ext->auto_size == en) return;
2016-06-08 07:25:08 +02:00
ext->auto_size = en == false ? 0 : 1;
2016-06-08 07:25:08 +02:00
2018-06-19 09:49:58 +02:00
/*Refresh the object*/
if(en) lv_line_set_points(line, ext->point_array, ext->point_num);
2016-06-08 07:25:08 +02:00
}
/**
* Enable (or disable) the y coordinate inversion.
* If enabled then y will be subtracted from the height of the object,
* therefore the y=0 coordinate will be on the bottom.
2016-10-07 11:15:46 +02:00
* @param line pointer to a line object
* @param en true: enable the y inversion, false:disable the y inversion
2016-06-08 07:25:08 +02:00
*/
void lv_line_set_y_invert(lv_obj_t * line, bool en)
2016-06-08 07:25:08 +02:00
{
LV_ASSERT_OBJ(line, LV_OBJX_NAME);
2018-06-19 09:49:58 +02:00
lv_line_ext_t * ext = lv_obj_get_ext_attr(line);
if(ext->y_inv == en) return;
2016-06-08 07:25:08 +02:00
ext->y_inv = en == false ? 0 : 1;
2016-06-08 07:25:08 +02:00
2018-06-19 09:49:58 +02:00
lv_obj_invalidate(line);
2016-06-08 07:25:08 +02:00
}
/*=====================
* Getter functions
*====================*/
/**
* Get the auto size attribute
2016-10-07 11:15:46 +02:00
* @param line pointer to a line object
2016-06-08 07:25:08 +02:00
* @return true: auto size is enabled, false: disabled
*/
bool lv_line_get_auto_size(const lv_obj_t * line)
2016-06-08 07:25:08 +02:00
{
LV_ASSERT_OBJ(line, LV_OBJX_NAME);
2018-06-19 09:49:58 +02:00
lv_line_ext_t * ext = lv_obj_get_ext_attr(line);
2016-06-08 07:25:08 +02:00
2018-06-19 09:49:58 +02:00
return ext->auto_size == 0 ? false : true;
2016-06-08 07:25:08 +02:00
}
/**
* Get the y inversion attribute
2016-10-07 11:15:46 +02:00
* @param line pointer to a line object
2016-06-08 07:25:08 +02:00
* @return true: y inversion is enabled, false: disabled
*/
bool lv_line_get_y_invert(const lv_obj_t * line)
2016-06-08 07:25:08 +02:00
{
LV_ASSERT_OBJ(line, LV_OBJX_NAME);
2018-06-19 09:49:58 +02:00
lv_line_ext_t * ext = lv_obj_get_ext_attr(line);
2016-06-08 07:25:08 +02:00
2018-06-19 09:49:58 +02:00
return ext->y_inv == 0 ? false : true;
2016-06-08 07:25:08 +02:00
}
/**********************
* STATIC FUNCTIONS
**********************/
/**
* Handle the drawing related tasks of the lines
2016-10-07 11:15:46 +02:00
* @param line pointer to an object
2019-09-06 19:53:39 +02:00
* @param clip_area the object will be drawn only in this area
2016-06-08 07:25:08 +02:00
* @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area
* (return 'true' if yes)
* LV_DESIGN_DRAW: draw the object (always return 'true')
* LV_DESIGN_DRAW_POST: drawing after every children are drawn
2019-09-06 19:53:39 +02:00
* @param return an element of `lv_design_res_t`
2016-06-08 07:25:08 +02:00
*/
2019-09-06 19:53:39 +02:00
static lv_design_res_t lv_line_design(lv_obj_t * line, const lv_area_t * clip_area, lv_design_mode_t mode)
2016-06-08 07:25:08 +02:00
{
/*A line never covers an area*/
2019-04-04 07:15:40 +02:00
if(mode == LV_DESIGN_COVER_CHK)
2019-09-06 19:53:39 +02:00
return LV_DESIGN_RES_NOT_COVER;
else if(mode == LV_DESIGN_DRAW_MAIN) {
2018-06-19 09:49:58 +02:00
lv_line_ext_t * ext = lv_obj_get_ext_attr(line);
if(ext->point_num == 0 || ext->point_array == NULL) return false;
lv_area_t area;
lv_obj_get_coords(line, &area);
2019-09-11 15:30:51 +02:00
lv_coord_t x_ofs = area.x1;
lv_coord_t y_ofs = area.y1;
2018-06-19 09:49:58 +02:00
lv_point_t p1;
lv_point_t p2;
lv_coord_t h = lv_obj_get_height(line);
uint16_t i;
2018-09-13 00:57:20 +02:00
2020-01-01 22:01:19 +01:00
lv_draw_line_dsc_t line_dsc;
lv_draw_line_dsc_init(&line_dsc);
lv_obj_init_draw_line_dsc(line, LV_LINE_PART_MAIN, &line_dsc);
line_dsc.round_end = 1;
line_dsc.round_start = 1;
2018-06-19 09:49:58 +02:00
2018-09-13 00:57:20 +02:00
/*Read all points and draw the lines*/
2018-06-19 09:49:58 +02:00
for(i = 0; i < ext->point_num - 1; i++) {
p1.x = ext->point_array[i].x + x_ofs;
p2.x = ext->point_array[i + 1].x + x_ofs;
if(ext->y_inv == 0) {
p1.y = ext->point_array[i].y + y_ofs;
p2.y = ext->point_array[i + 1].y + y_ofs;
} else {
2019-04-04 07:15:40 +02:00
p1.y = h - ext->point_array[i].y + y_ofs;
2018-06-19 09:49:58 +02:00
p2.y = h - ext->point_array[i + 1].y + y_ofs;
}
2020-01-01 22:01:19 +01:00
lv_draw_line(&p1, &p2, clip_area, &line_dsc);
line_dsc.round_start = 0;
2018-06-19 09:49:58 +02:00
}
}
2019-09-06 19:53:39 +02:00
return LV_DESIGN_RES_OK;
2016-06-08 07:25:08 +02:00
}
2017-11-13 16:11:05 +01:00
/**
* Signal function of the line
* @param line pointer to a line object
* @param sign a signal type from lv_signal_t enum
* @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
*/
static lv_res_t lv_line_signal(lv_obj_t * line, lv_signal_t sign, void * param)
{
lv_res_t res;
/* Include the ancient signal function */
2017-11-15 21:06:44 +01:00
res = ancestor_signal(line, sign, param);
2017-11-13 16:11:05 +01:00
if(res != LV_RES_OK) return res;
if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, LV_OBJX_NAME);
2017-11-13 16:11:05 +01:00
if(sign == LV_SIGNAL_REFR_EXT_DRAW_PAD) {
2020-01-01 22:01:19 +01:00
/*The corner of the skew lines is out of the intended area*/
lv_style_int_t line_width = lv_obj_get_style_int(line, LV_LINE_PART_MAIN, LV_STYLE_LINE_WIDTH);
2020-01-01 22:01:19 +01:00
if(line->ext_draw_pad < line_width) line->ext_draw_pad = line_width;
}
return res;
2017-11-13 16:11:05 +01:00
}
2016-06-08 07:25:08 +02:00
#endif