2018-06-08 10:26:10 +02:00
|
|
|
/**
|
|
|
|
* @file lv_arc.c
|
2018-06-19 09:49:58 +02:00
|
|
|
*
|
2018-06-08 10:26:10 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*********************
|
|
|
|
* INCLUDES
|
|
|
|
*********************/
|
|
|
|
#include "lv_arc.h"
|
2019-03-07 00:05:16 +01:00
|
|
|
#if LV_USE_ARC != 0
|
2018-06-08 10:26:10 +02:00
|
|
|
|
2019-09-24 16:30:38 +02:00
|
|
|
#include "../lv_core/lv_debug.h"
|
2018-06-08 10:26:10 +02:00
|
|
|
#include "../lv_misc/lv_math.h"
|
|
|
|
#include "../lv_draw/lv_draw_arc.h"
|
2018-09-20 22:14:48 +02:00
|
|
|
#include "../lv_themes/lv_theme.h"
|
2018-06-08 10:26:10 +02:00
|
|
|
|
|
|
|
/*********************
|
|
|
|
* DEFINES
|
|
|
|
*********************/
|
2019-09-26 10:51:54 +02:00
|
|
|
#define LV_OBJX_NAME "lv_arc"
|
2018-06-08 10:26:10 +02:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* TYPEDEFS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC PROTOTYPES
|
|
|
|
**********************/
|
2019-09-06 19:53:39 +02:00
|
|
|
static lv_design_res_t lv_arc_design(lv_obj_t * arc, const lv_area_t * clip_area, lv_design_mode_t mode);
|
2018-06-08 10:26:10 +02:00
|
|
|
static lv_res_t lv_arc_signal(lv_obj_t * arc, lv_signal_t sign, void * param);
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC VARIABLES
|
|
|
|
**********************/
|
2019-02-26 09:25:46 +01:00
|
|
|
static lv_signal_cb_t ancestor_signal;
|
|
|
|
static lv_design_cb_t ancestor_design;
|
2018-06-08 10:26:10 +02:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* MACROS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* GLOBAL FUNCTIONS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a arc object
|
|
|
|
* @param par pointer to an object, it will be the parent of the new arc
|
|
|
|
* @param copy pointer to a arc object, if not NULL then the new object will be copied from it
|
|
|
|
* @return pointer to the created arc
|
|
|
|
*/
|
2018-07-30 06:52:29 +02:00
|
|
|
lv_obj_t * lv_arc_create(lv_obj_t * par, const lv_obj_t * copy)
|
2018-06-08 10:26:10 +02:00
|
|
|
{
|
2018-07-25 17:57:08 +02:00
|
|
|
|
2018-10-05 17:22:49 +02:00
|
|
|
LV_LOG_TRACE("arc create started");
|
2018-07-25 17:57:08 +02:00
|
|
|
|
2018-06-08 10:26:10 +02:00
|
|
|
/*Create the ancestor of arc*/
|
|
|
|
lv_obj_t * new_arc = lv_obj_create(par, copy);
|
2019-09-24 23:14:17 +02:00
|
|
|
LV_ASSERT_MEM(new_arc);
|
2018-07-25 13:33:53 +02:00
|
|
|
if(new_arc == NULL) return NULL;
|
2018-06-19 09:49:58 +02:00
|
|
|
|
2018-06-08 10:26:10 +02:00
|
|
|
/*Allocate the arc type specific extended data*/
|
|
|
|
lv_arc_ext_t * ext = lv_obj_allocate_ext_attr(new_arc, sizeof(lv_arc_ext_t));
|
2019-09-24 23:14:17 +02:00
|
|
|
LV_ASSERT_MEM(ext);
|
2019-12-03 18:16:14 +01:00
|
|
|
if(ext == NULL) {
|
|
|
|
lv_obj_del(new_arc);
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-07-25 13:33:53 +02:00
|
|
|
|
2019-04-10 06:40:49 +02:00
|
|
|
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_arc);
|
|
|
|
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(new_arc);
|
2018-06-08 10:26:10 +02:00
|
|
|
|
|
|
|
/*Initialize the allocated 'ext' */
|
|
|
|
ext->angle_start = 45;
|
2019-04-04 07:15:40 +02:00
|
|
|
ext->angle_end = 315;
|
2018-06-08 10:26:10 +02:00
|
|
|
|
|
|
|
/*The signal and design functions are not copied so set them here*/
|
2019-02-26 09:25:46 +01:00
|
|
|
lv_obj_set_signal_cb(new_arc, lv_arc_signal);
|
|
|
|
lv_obj_set_design_cb(new_arc, lv_arc_design);
|
2018-06-08 10:26:10 +02:00
|
|
|
|
|
|
|
/*Init the new arc arc*/
|
|
|
|
if(copy == NULL) {
|
2020-01-07 23:43:57 +01:00
|
|
|
lv_style_dsc_reset(&new_arc->style_dsc);
|
|
|
|
_ot(new_arc, LV_ARC_PART_MAIN, ARC);
|
2018-09-20 22:14:48 +02:00
|
|
|
|
2018-06-08 10:26:10 +02:00
|
|
|
}
|
|
|
|
/*Copy an existing arc*/
|
|
|
|
else {
|
2018-06-19 09:49:58 +02:00
|
|
|
lv_arc_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
|
2019-04-04 07:15:40 +02:00
|
|
|
ext->angle_start = copy_ext->angle_start;
|
|
|
|
ext->angle_end = copy_ext->angle_end;
|
2018-06-08 10:26:10 +02:00
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
/*Refresh the style with new signal function*/
|
2018-06-08 10:26:10 +02:00
|
|
|
lv_obj_refresh_style(new_arc);
|
|
|
|
}
|
2018-06-19 09:49:58 +02:00
|
|
|
|
2018-10-05 17:22:49 +02:00
|
|
|
LV_LOG_INFO("arc created");
|
2018-07-25 17:57:08 +02:00
|
|
|
|
2018-06-08 10:26:10 +02:00
|
|
|
return new_arc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*======================
|
|
|
|
* Add/remove functions
|
|
|
|
*=====================*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* New object specific "add" or "remove" functions come here
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*=====================
|
|
|
|
* Setter functions
|
|
|
|
*====================*/
|
|
|
|
|
|
|
|
/**
|
2019-11-26 13:22:03 +01:00
|
|
|
* Set the start angle of an arc. 0 deg: right, 90 bottom: right etc.
|
2018-06-08 10:26:10 +02:00
|
|
|
* @param arc pointer to an arc object
|
|
|
|
* @param start the start angle [0..360]
|
|
|
|
*/
|
2019-11-26 13:22:03 +01:00
|
|
|
void lv_arc_set_start_angle(lv_obj_t * arc, int16_t start)
|
2018-06-08 10:26:10 +02:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(arc, LV_OBJX_NAME);
|
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
lv_arc_ext_t * ext = lv_obj_get_ext_attr(arc);
|
2018-06-08 10:26:10 +02:00
|
|
|
|
2019-11-26 13:22:03 +01:00
|
|
|
if(start > 360) start -= 360;
|
|
|
|
if(start < 0) start += 360;
|
2018-06-08 10:26:10 +02:00
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
ext->angle_start = start;
|
2019-11-26 13:22:03 +01:00
|
|
|
|
|
|
|
lv_obj_invalidate(arc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the start angle of an arc. 0 deg: right, 90 bottom: right etc.
|
|
|
|
* @param arc pointer to an arc object
|
|
|
|
* @param start the start angle [0..360]
|
|
|
|
*/
|
|
|
|
void lv_arc_set_end_angle(lv_obj_t * arc, int16_t end)
|
|
|
|
{
|
|
|
|
LV_ASSERT_OBJ(arc, LV_OBJX_NAME);
|
|
|
|
|
|
|
|
lv_arc_ext_t * ext = lv_obj_get_ext_attr(arc);
|
|
|
|
|
|
|
|
if(end > 360) end -= 360;
|
|
|
|
if(end < 0) end += 360;
|
|
|
|
|
|
|
|
ext->angle_end= end;
|
2018-06-08 10:26:10 +02:00
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
lv_obj_invalidate(arc);
|
2018-06-08 10:26:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*=====================
|
|
|
|
* Getter functions
|
|
|
|
*====================*/
|
|
|
|
|
|
|
|
/**
|
2018-12-23 06:38:54 -05:00
|
|
|
* Get the start angle of an arc.
|
2018-06-08 10:26:10 +02:00
|
|
|
* @param arc pointer to an arc object
|
|
|
|
* @return the start angle [0..360]
|
|
|
|
*/
|
2018-06-09 08:45:38 +02:00
|
|
|
uint16_t lv_arc_get_angle_start(lv_obj_t * arc)
|
2018-06-08 10:26:10 +02:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(arc, LV_OBJX_NAME);
|
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
lv_arc_ext_t * ext = lv_obj_get_ext_attr(arc);
|
2018-06-08 10:26:10 +02:00
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
return ext->angle_start;
|
2018-06-08 10:26:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-12-23 06:38:54 -05:00
|
|
|
* Get the end angle of an arc.
|
2018-06-08 10:26:10 +02:00
|
|
|
* @param arc pointer to an arc object
|
|
|
|
* @return the end angle [0..360]
|
|
|
|
*/
|
2018-06-09 08:45:38 +02:00
|
|
|
uint16_t lv_arc_get_angle_end(lv_obj_t * arc)
|
2018-06-08 10:26:10 +02:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(arc, LV_OBJX_NAME);
|
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
lv_arc_ext_t * ext = lv_obj_get_ext_attr(arc);
|
2018-06-08 10:26:10 +02:00
|
|
|
|
2018-12-23 06:38:54 -05:00
|
|
|
return ext->angle_end;
|
2018-06-08 10:26:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*=====================
|
|
|
|
* Other functions
|
|
|
|
*====================*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* New object specific "other" functions come here
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC FUNCTIONS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the drawing related tasks of the arcs
|
|
|
|
* @param arc pointer to an object
|
2019-09-06 19:53:39 +02:00
|
|
|
* @param clip_area the object will be drawn only in this area
|
2018-06-08 10:26:10 +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`
|
2018-06-08 10:26:10 +02:00
|
|
|
*/
|
2019-09-06 19:53:39 +02:00
|
|
|
static lv_design_res_t lv_arc_design(lv_obj_t * arc, const lv_area_t * clip_area, lv_design_mode_t mode)
|
2018-06-08 10:26:10 +02:00
|
|
|
{
|
|
|
|
/*Return false if the object is not covers the mask_p area*/
|
|
|
|
if(mode == LV_DESIGN_COVER_CHK) {
|
2019-09-06 19:53:39 +02:00
|
|
|
return LV_DESIGN_RES_NOT_COVER;
|
2018-06-08 10:26:10 +02:00
|
|
|
}
|
|
|
|
/*Draw the object*/
|
|
|
|
else if(mode == LV_DESIGN_DRAW_MAIN) {
|
2019-04-11 19:59:55 +08:00
|
|
|
lv_arc_ext_t * ext = lv_obj_get_ext_attr(arc);
|
2020-01-07 23:43:57 +01:00
|
|
|
lv_draw_line_dsc_t arc_dsc;
|
|
|
|
lv_draw_line_dsc_init(&arc_dsc);
|
|
|
|
lv_obj_init_draw_line_dsc(arc, LV_ARC_PART_MAIN, &arc_dsc);
|
2018-06-19 09:49:58 +02:00
|
|
|
|
2019-04-04 07:15:40 +02:00
|
|
|
lv_coord_t r = (LV_MATH_MIN(lv_obj_get_width(arc), lv_obj_get_height(arc))) / 2;
|
|
|
|
lv_coord_t x = arc->coords.x1 + lv_obj_get_width(arc) / 2;
|
|
|
|
lv_coord_t y = arc->coords.y1 + lv_obj_get_height(arc) / 2;
|
2020-01-07 23:43:57 +01:00
|
|
|
lv_draw_arc(x, y, r, ext->angle_start, ext->angle_end, clip_area, &arc_dsc);
|
2018-06-08 10:26:10 +02:00
|
|
|
}
|
|
|
|
/*Post draw when the children are drawn*/
|
|
|
|
else if(mode == LV_DESIGN_DRAW_POST) {
|
|
|
|
}
|
|
|
|
|
2019-09-06 19:53:39 +02:00
|
|
|
return LV_DESIGN_RES_OK;
|
2018-06-08 10:26:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Signal function of the arc
|
|
|
|
* @param arc pointer to a arc object
|
|
|
|
* @param sign a signal type from lv_signal_t enum
|
|
|
|
* @param param pointer to a signal specific variable
|
|
|
|
* @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
|
|
|
|
*/
|
|
|
|
static lv_res_t lv_arc_signal(lv_obj_t * arc, lv_signal_t sign, void * param)
|
|
|
|
{
|
|
|
|
lv_res_t res;
|
|
|
|
|
|
|
|
/* Include the ancient signal function */
|
|
|
|
res = ancestor_signal(arc, sign, param);
|
|
|
|
if(res != LV_RES_OK) return res;
|
|
|
|
|
2019-09-26 12:54:40 +02:00
|
|
|
if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, LV_OBJX_NAME);
|
2019-09-26 10:51:54 +02:00
|
|
|
|
2018-06-08 10:26:10 +02:00
|
|
|
if(sign == LV_SIGNAL_CLEANUP) {
|
|
|
|
/*Nothing to cleanup. (No dynamically allocated memory in 'ext')*/
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|