1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-14 06:42:58 +08:00
lvgl/lv_objx/lv_line.c

364 lines
9.3 KiB
C
Raw Normal View History

2016-06-08 07:25:08 +02:00
/**
* @file lv_line.c
*
*/
/*********************
* INCLUDES
*********************/
#include "lv_conf.h"
#if USE_LV_LINE != 0
#include "lv_line.h"
#include "../lv_draw/lv_draw_vbasic.h"
#include "../lv_draw/lv_draw_rbasic.h"
#include "../lv_draw/lv_draw.h"
#include <lvgl/lv_misc/area.h>
2016-06-08 07:25:08 +02:00
#include <misc/math/math_base.h>
#include <misc/mem/dyn_mem.h>
#include <misc/others/color.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
2016-10-07 11:15:46 +02:00
static bool lv_line_design(lv_obj_t * line, const area_t * mask, lv_design_mode_t mode);
static void lv_lines_init(void);
2016-06-08 07:25:08 +02:00
/**********************
* STATIC VARIABLES
**********************/
static lv_lines_t lv_lines_def;
static lv_lines_t lv_lines_decor;
static lv_lines_t lv_lines_chart;
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
*/
2016-10-07 11:15:46 +02:00
lv_obj_t * lv_line_create(lv_obj_t * par, lv_obj_t * copy)
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);
dm_assert(new_line);
2016-06-08 07:25:08 +02:00
/*Extend the basic object to rectangle object*/
2016-10-07 11:15:46 +02:00
lv_line_ext_t * ext = lv_obj_alloc_ext(new_line, sizeof(lv_line_ext_t));
dm_assert(ext);
ext->point_num = 0;
ext->point_array = NULL;
ext->auto_size = 1;
ext->y_inv = 0;
ext->upscale = 0;
2016-10-07 11:15:46 +02:00
lv_obj_set_design_f(new_line, lv_line_design);
lv_obj_set_signal_f(new_line, lv_line_signal);
2016-06-08 07:25:08 +02:00
/*Init the new rectangle*/
2016-10-07 11:15:46 +02:00
if(copy == NULL) {
lv_obj_set_style(new_line, lv_lines_get(LV_LINES_DEF, NULL));
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 {
2016-10-07 11:15:46 +02:00
lv_line_set_auto_size(new_line,lv_line_get_auto_size(copy));
lv_line_set_y_inv(new_line,lv_line_get_y_inv(copy));
lv_line_set_auto_size(new_line,lv_line_get_auto_size(copy));
lv_line_set_upscale(new_line,lv_line_get_upscale(copy));
2016-10-07 17:33:35 +02:00
lv_line_set_points(new_line, LV_EA(copy, lv_line_ext_t)->point_array,
2016-10-07 11:15:46 +02:00
LV_EA(copy, lv_line_ext_t)->point_num);
/*Refresh the style with new signal function*/
lv_obj_refr_style(new_line);
2016-06-08 07:25:08 +02:00
}
2016-10-07 11:15:46 +02:00
return new_line;
2016-06-08 07:25:08 +02:00
}
/**
* Signal function of the line
2016-10-07 11:15:46 +02:00
* @param line pointer to a line object
2016-06-08 07:25:08 +02:00
* @param sign a signal type from lv_signal_t enum
* @param param pointer to a signal specific variable
*/
2016-10-07 11:15:46 +02:00
bool lv_line_signal(lv_obj_t * line, lv_signal_t sign, void * param)
2016-06-08 07:25:08 +02:00
{
bool valid;
/* Include the ancient signal function */
2016-10-07 11:15:46 +02:00
valid = lv_obj_signal(line, sign, param);
2016-06-08 07:25:08 +02:00
/* The object can be deleted so check its validity and then
* make the object specific signal handling */
if(valid != false) {
switch(sign) {
default:
break;
}
}
return valid;
}
/*=====================
* 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,
* so the array can be a local variable which will be destroyed
* @param point_num number of points in 'point_a'
*/
2016-10-07 11:15:46 +02:00
void lv_line_set_points(lv_obj_t * line, const point_t * point_a, uint16_t point_num)
2016-06-08 07:25:08 +02:00
{
2016-10-07 11:15:46 +02:00
lv_line_ext_t * ext = lv_obj_get_ext(line);
2016-10-07 17:33:35 +02:00
ext->point_array = point_a;
2016-10-07 11:15:46 +02:00
ext->point_num = point_num;
2016-06-08 07:25:08 +02:00
2016-06-15 11:08:18 +02:00
uint8_t us = 1;
2016-10-07 11:15:46 +02:00
if(ext->upscale != 0) {
2016-06-15 11:08:18 +02:00
us = LV_DOWNSCALE;
}
2016-10-07 11:15:46 +02:00
if(point_num > 0 && ext->auto_size != 0) {
2016-06-08 07:25:08 +02:00
uint16_t i;
cord_t xmax = LV_CORD_MIN;
cord_t ymax = LV_CORD_MIN;
for(i = 0; i < point_num; i++) {
2016-06-15 11:08:18 +02:00
xmax = max(point_a[i].x * us, xmax);
ymax = max(point_a[i].y * us, ymax);
2016-06-08 07:25:08 +02:00
}
2016-10-07 11:15:46 +02:00
lv_lines_t * lines = lv_obj_get_style(line);
lv_obj_set_size(line, xmax + lines->width, ymax + lines->width);
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 autosize true: auto size is enabled, false: auto size is disabled
2016-06-08 07:25:08 +02:00
*/
2016-10-07 11:15:46 +02:00
void lv_line_set_auto_size(lv_obj_t * line, bool autosize)
2016-06-08 07:25:08 +02:00
{
2016-10-07 11:15:46 +02:00
lv_line_ext_t * ext = lv_obj_get_ext(line);
2016-06-08 07:25:08 +02:00
2016-10-07 11:15:46 +02:00
ext->auto_size = autosize == false ? 0 : 1;
2016-06-08 07:25:08 +02:00
/*Refresh the object*/
2016-10-07 11:15:46 +02:00
if(autosize != false) {
2016-10-07 17:33:35 +02:00
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 yinv true: enable the y inversion, false:disable the y inversion
2016-06-08 07:25:08 +02:00
*/
2016-10-07 11:15:46 +02:00
void lv_line_set_y_inv(lv_obj_t * line, bool yinv)
2016-06-08 07:25:08 +02:00
{
2016-10-07 11:15:46 +02:00
lv_line_ext_t * ext = lv_obj_get_ext(line);
2016-06-08 07:25:08 +02:00
2016-10-07 11:15:46 +02:00
ext->y_inv = yinv == false ? 0 : 1;
2016-06-08 07:25:08 +02:00
2016-10-07 11:15:46 +02:00
lv_obj_inv(line);
2016-06-08 07:25:08 +02:00
}
2016-06-15 11:08:18 +02:00
/**
* Enable (or disable) the point coordinate upscaling (compensate LV_DOWNSCALE).
2016-10-07 11:15:46 +02:00
* @param line pointer to a line object
* @param unscale true: enable the point coordinate upscaling
2016-06-15 11:08:18 +02:00
*/
2016-10-07 11:15:46 +02:00
void lv_line_set_upscale(lv_obj_t * line, bool unscale)
2016-06-15 11:08:18 +02:00
{
2016-10-07 11:15:46 +02:00
lv_line_ext_t * ext = lv_obj_get_ext(line);
2016-06-15 11:08:18 +02:00
2016-10-07 11:15:46 +02:00
ext->upscale = unscale == false ? 0 : 1;
2016-06-15 11:08:18 +02:00
/*Refresh to point to handle auto size*/
2016-10-07 17:33:35 +02:00
lv_line_set_points(line, ext->point_array, ext->point_num);
2016-06-15 11:08:18 +02:00
}
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
*/
2016-10-07 11:15:46 +02:00
bool lv_line_get_auto_size(lv_obj_t * line)
2016-06-08 07:25:08 +02:00
{
2016-10-07 11:15:46 +02:00
lv_line_ext_t * ext = lv_obj_get_ext(line);
2016-06-08 07:25:08 +02:00
2016-10-07 11:15:46 +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
*/
2016-10-07 11:15:46 +02:00
bool lv_line_get_y_inv(lv_obj_t * line)
2016-06-08 07:25:08 +02:00
{
2016-10-07 11:15:46 +02:00
lv_line_ext_t * ext = lv_obj_get_ext(line);
2016-06-08 07:25:08 +02:00
2016-10-07 11:15:46 +02:00
return ext->y_inv == 0 ? false : true;
2016-06-08 07:25:08 +02:00
}
2016-06-15 11:08:18 +02:00
/**
* Get the point upscale enable attribute
2016-10-07 11:15:46 +02:00
* @param obj pointer to a line object
2016-06-15 11:08:18 +02:00
* @return true: point coordinate upscale is enabled, false: disabled
*/
2016-10-07 11:15:46 +02:00
bool lv_line_get_upscale(lv_obj_t * line)
2016-06-15 11:08:18 +02:00
{
2016-10-07 11:15:46 +02:00
lv_line_ext_t * ext = lv_obj_get_ext(line);
2016-06-15 11:08:18 +02:00
2016-10-07 11:15:46 +02:00
return ext->upscale == 0 ? false : true;
2016-06-15 11:08:18 +02:00
}
2016-06-08 07:25:08 +02:00
/**
* Return with a pointer to a built-in style and/or copy it to a variable
* @param style a style name from lv_lines_builtin_t enum
2016-10-07 11:15:46 +02:00
* @param copy copy the style to this variable. (NULL if unused)
2016-06-08 07:25:08 +02:00
* @return pointer to an lv_lines_t style
*/
2016-10-07 11:15:46 +02:00
lv_lines_t * lv_lines_get(lv_lines_builtin_t style, lv_lines_t * copy)
2016-06-08 07:25:08 +02:00
{
static bool style_inited = false;
/*Make the style initialization if it is not done yet*/
if(style_inited == false) {
lv_lines_init();
style_inited = true;
}
2016-06-08 07:25:08 +02:00
lv_lines_t *style_p;
switch(style) {
case LV_LINES_DEF:
style_p = &lv_lines_def;
break;
case LV_LINES_DECOR:
style_p = &lv_lines_decor;
break;
case LV_LINES_CHART:
style_p = &lv_lines_chart;
break;
default:
style_p = &lv_lines_def;
2016-06-08 07:25:08 +02:00
}
2016-10-07 11:15:46 +02:00
if(copy != NULL) {
if(style_p != NULL) memcpy(copy, style_p, sizeof(lv_lines_t));
else memcpy(copy, &lv_lines_def, sizeof(lv_lines_t));
2016-06-08 07:25:08 +02:00
}
return style_p;
}
/**********************
* STATIC FUNCTIONS
**********************/
/**
* Handle the drawing related tasks of the lines
2016-10-07 11:15:46 +02:00
* @param line pointer to an object
2016-06-08 07:25:08 +02:00
* @param mask the object will be drawn only in this area
* @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
2016-06-08 07:25:08 +02:00
* @param return true/false, depends on 'mode'
*/
2016-10-07 11:15:46 +02:00
static bool lv_line_design(lv_obj_t * line, const area_t * mask, lv_design_mode_t mode)
2016-06-08 07:25:08 +02:00
{
/*A line never covers an area*/
if(mode == LV_DESIGN_COVER_CHK) return false;
else if(mode == LV_DESIGN_DRAW_MAIN) {
2016-10-07 11:15:46 +02:00
lv_line_ext_t * ext = lv_obj_get_ext(line);
2016-06-08 07:25:08 +02:00
2016-10-07 17:33:35 +02:00
if(ext->point_num == 0 || ext->point_array == NULL) return false;
2016-06-08 07:25:08 +02:00
2016-10-07 11:15:46 +02:00
lv_lines_t * lines = lv_obj_get_style(line);
2016-10-07 11:15:46 +02:00
opa_t opa = lv_obj_get_opa(line);
area_t area;
2016-10-07 11:15:46 +02:00
lv_obj_get_cords(line, &area);
cord_t x_ofs = area.x1;
cord_t y_ofs = area.y1;
point_t p1;
point_t p2;
2016-10-07 11:15:46 +02:00
cord_t h = lv_obj_get_height(line);
uint16_t i;
uint8_t us = 1;
2016-10-07 11:15:46 +02:00
if(ext->upscale != 0) {
us = LV_DOWNSCALE;
}
2016-06-08 07:25:08 +02:00
/*Read all pints and draw the lines*/
2016-10-07 11:15:46 +02:00
for (i = 0; i < ext->point_num - 1; i++) {
2016-06-15 11:08:18 +02:00
2016-10-07 17:33:35 +02:00
p1.x = ext->point_array[i].x * us + x_ofs;
p2.x = ext->point_array[i + 1].x * us + x_ofs;
2016-06-08 07:25:08 +02:00
2016-10-07 11:15:46 +02:00
if(ext->y_inv == 0) {
2016-10-07 17:33:35 +02:00
p1.y = ext->point_array[i].y * us + y_ofs;
p2.y = ext->point_array[i + 1].y * us + y_ofs;
} else {
2016-10-07 17:33:35 +02:00
p1.y = h - ext->point_array[i].y * us + y_ofs;
p2.y = h - ext->point_array[i + 1].y * us + y_ofs;
}
2016-10-07 11:15:46 +02:00
lv_draw_line(&p1, &p2, mask, lines, opa);
2016-06-08 07:25:08 +02:00
}
}
2016-06-08 07:25:08 +02:00
return true;
}
/**
* Initialize the line styles
*/
static void lv_lines_init(void)
{
/*Default style*/
lv_lines_def.width = 2 * LV_DOWNSCALE;
lv_lines_def.objs.color = COLOR_RED;
lv_lines_def.objs.transp = 0;
/*Decoration line style*/
memcpy(&lv_lines_decor, &lv_lines_def, sizeof(lv_lines_t));
lv_lines_decor.width = 1 * LV_DOWNSCALE;
lv_lines_decor.objs.color = COLOR_GRAY;
/*Chart line style*/
memcpy(&lv_lines_chart, &lv_lines_def, sizeof(lv_lines_t));
lv_lines_chart.width = 3 * LV_DOWNSCALE;
lv_lines_chart.objs.color = COLOR_RED;
}
2016-06-08 07:25:08 +02:00
#endif