2019-09-06 19:53:51 +02:00
|
|
|
/**
|
|
|
|
* @file lv_objmask.c
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*********************
|
|
|
|
* INCLUDES
|
|
|
|
*********************/
|
|
|
|
#include "lv_objmask.h"
|
2019-09-26 15:24:47 +02:00
|
|
|
#include "../lv_core/lv_debug.h"
|
2019-11-14 07:16:24 +01:00
|
|
|
#include "../lv_draw/lv_draw.h"
|
2020-02-03 16:18:53 +01:00
|
|
|
#include "../lv_themes/lv_theme.h"
|
2019-09-06 19:53:51 +02:00
|
|
|
|
|
|
|
#if defined(LV_USE_OBJMASK) && LV_USE_OBJMASK != 0
|
|
|
|
|
|
|
|
/*********************
|
|
|
|
* DEFINES
|
|
|
|
*********************/
|
2019-09-26 15:24:47 +02:00
|
|
|
#define LV_OBJX_NAME "lv_objmask"
|
2019-09-06 19:53:51 +02:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* TYPEDEFS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC PROTOTYPES
|
|
|
|
**********************/
|
|
|
|
static lv_design_res_t lv_objmask_design(lv_obj_t * objmask, const lv_area_t * clip_area, lv_design_mode_t mode);
|
|
|
|
static lv_res_t lv_objmask_signal(lv_obj_t * objmask, lv_signal_t sign, void * param);
|
2019-11-14 14:04:53 +01:00
|
|
|
static uint16_t get_param_size(lv_draw_mask_type_t type);
|
2019-09-06 19:53:51 +02:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC VARIABLES
|
|
|
|
**********************/
|
|
|
|
static lv_signal_cb_t ancestor_signal;
|
|
|
|
static lv_design_cb_t ancestor_design;
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* MACROS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* GLOBAL FUNCTIONS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a object mask object
|
|
|
|
* @param par pointer to an object, it will be the parent of the new object mask
|
|
|
|
* @param copy pointer to a object mask object, if not NULL then the new object will be copied from it
|
|
|
|
* @return pointer to the created object mask
|
|
|
|
*/
|
|
|
|
lv_obj_t * lv_objmask_create(lv_obj_t * par, const lv_obj_t * copy)
|
|
|
|
{
|
|
|
|
LV_LOG_TRACE("object mask create started");
|
|
|
|
|
|
|
|
/*Create the ancestor of object mask*/
|
2020-02-03 16:18:53 +01:00
|
|
|
lv_obj_t * objmask = lv_cont_create(par, copy);
|
|
|
|
LV_ASSERT_MEM(objmask);
|
|
|
|
if(objmask == NULL) return NULL;
|
2019-09-06 19:53:51 +02:00
|
|
|
|
|
|
|
/*Allocate the object mask type specific extended data*/
|
2020-02-03 16:18:53 +01:00
|
|
|
lv_objmask_ext_t * ext = lv_obj_allocate_ext_attr(objmask, sizeof(lv_objmask_ext_t));
|
2019-09-26 15:24:47 +02:00
|
|
|
LV_ASSERT_MEM(ext);
|
2019-12-03 18:16:14 +01:00
|
|
|
if(ext == NULL) {
|
2020-02-03 16:18:53 +01:00
|
|
|
lv_obj_del(objmask);
|
2019-12-03 18:16:14 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-02-03 16:18:53 +01:00
|
|
|
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(objmask);
|
|
|
|
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(objmask);
|
2019-09-06 19:53:51 +02:00
|
|
|
|
|
|
|
/*Initialize the allocated 'ext' */
|
|
|
|
lv_ll_init(&ext->mask_ll, sizeof(lv_objmask_mask_t));
|
|
|
|
|
|
|
|
/*The signal and design functions are not copied so set them here*/
|
2020-02-03 16:18:53 +01:00
|
|
|
lv_obj_set_signal_cb(objmask, lv_objmask_signal);
|
|
|
|
lv_obj_set_design_cb(objmask, lv_objmask_design);
|
2019-09-06 19:53:51 +02:00
|
|
|
|
|
|
|
/*Init the new object mask object mask*/
|
|
|
|
if(copy == NULL) {
|
2020-02-03 16:18:53 +01:00
|
|
|
lv_theme_apply(objmask, LV_THEME_OBJMASK);
|
2019-09-06 19:53:51 +02:00
|
|
|
|
|
|
|
}
|
2019-11-14 21:08:23 -05:00
|
|
|
/*TODO: Copy an existing object mask*/
|
2019-09-06 19:53:51 +02:00
|
|
|
else {
|
2019-11-14 21:08:23 -05:00
|
|
|
/* lv_objmask_ext_t * copy_ext = lv_obj_get_ext_attr(copy); */
|
2019-09-06 19:53:51 +02:00
|
|
|
|
|
|
|
/*Refresh the style with new signal function*/
|
2020-02-03 16:18:53 +01:00
|
|
|
lv_obj_refresh_style(objmask);
|
2019-09-06 19:53:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LV_LOG_INFO("object mask created");
|
|
|
|
|
2020-02-03 16:18:53 +01:00
|
|
|
return objmask;
|
2019-09-06 19:53:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*======================
|
|
|
|
* Add/remove functions
|
|
|
|
*=====================*/
|
|
|
|
|
2019-11-14 14:04:53 +01:00
|
|
|
/**
|
|
|
|
* Add a mask
|
|
|
|
* @param objmask pointer to an Object mask object
|
|
|
|
* @param param an initialized mask parameter
|
|
|
|
* @return pointer to the added mask
|
|
|
|
*/
|
|
|
|
lv_objmask_mask_t * lv_objmask_add_mask(lv_obj_t * objmask, void * param)
|
2019-09-06 19:53:51 +02:00
|
|
|
{
|
2019-11-14 14:04:53 +01:00
|
|
|
LV_ASSERT_OBJ(objmask, LV_OBJX_NAME);
|
2019-12-26 03:37:28 +01:00
|
|
|
LV_ASSERT_NULL(param);
|
2019-11-14 14:04:53 +01:00
|
|
|
|
2019-09-06 19:53:51 +02:00
|
|
|
lv_objmask_ext_t * ext = lv_obj_get_ext_attr(objmask);
|
|
|
|
|
2019-11-14 07:16:24 +01:00
|
|
|
lv_draw_mask_common_dsc_t * dsc = param;
|
2019-11-14 14:04:53 +01:00
|
|
|
uint16_t param_size = get_param_size(dsc->type);
|
2019-11-14 07:16:24 +01:00
|
|
|
|
2019-09-06 19:53:51 +02:00
|
|
|
lv_objmask_mask_t * m = lv_ll_ins_head(&ext->mask_ll);
|
2019-11-14 07:16:24 +01:00
|
|
|
m->param = lv_mem_alloc(param_size);
|
|
|
|
LV_ASSERT_MEM(m->param);
|
2019-11-14 14:04:53 +01:00
|
|
|
if(m == NULL) return NULL;
|
2019-11-14 07:16:24 +01:00
|
|
|
|
|
|
|
memcpy(m->param, param, param_size);
|
2019-09-06 19:53:51 +02:00
|
|
|
|
2019-11-14 14:04:53 +01:00
|
|
|
lv_obj_invalidate(objmask);
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update an already created mask
|
|
|
|
* @param objmask pointer to an Object mask object
|
|
|
|
* @param mask pointer to created mask (returned by `lv_objmask_add_mask`)
|
|
|
|
* @param param an initialized mask parameter (initialized by `lv_draw_mask_line/angle/.../_init`)
|
|
|
|
*/
|
2019-11-15 11:05:11 +01:00
|
|
|
void lv_objmask_update_mask(lv_obj_t * objmask, lv_objmask_mask_t * mask, void * param)
|
2019-11-14 14:04:53 +01:00
|
|
|
{
|
|
|
|
LV_ASSERT_OBJ(objmask, LV_OBJX_NAME);
|
|
|
|
LV_ASSERT_NULL(mask);
|
|
|
|
LV_ASSERT_NULL(param);
|
|
|
|
lv_draw_mask_common_dsc_t * dsc = param;
|
|
|
|
|
|
|
|
memcpy(mask->param, param, get_param_size(dsc->type));
|
2019-11-14 07:16:24 +01:00
|
|
|
|
2019-11-14 14:04:53 +01:00
|
|
|
lv_obj_invalidate(objmask);
|
|
|
|
}
|
2019-11-14 07:16:24 +01:00
|
|
|
|
2019-11-14 14:04:53 +01:00
|
|
|
/**
|
|
|
|
* Remove a mask
|
|
|
|
* @param objmask pointer to an Object mask object
|
|
|
|
* @param mask pointer to created mask (returned by `lv_objmask_add_mask`)
|
2019-11-15 11:05:11 +01:00
|
|
|
* If `NULL` passed all masks will be deleted.
|
2019-11-14 14:04:53 +01:00
|
|
|
*/
|
|
|
|
void lv_objmask_remove_mask(lv_obj_t * objmask, lv_objmask_mask_t * mask)
|
|
|
|
{
|
|
|
|
LV_ASSERT_OBJ(objmask, LV_OBJX_NAME);
|
2019-11-14 07:16:24 +01:00
|
|
|
|
2019-11-14 14:04:53 +01:00
|
|
|
lv_objmask_ext_t * ext = lv_obj_get_ext_attr(objmask);
|
2019-11-15 11:05:11 +01:00
|
|
|
|
|
|
|
/*Remove all masks*/
|
|
|
|
if(mask == NULL) {
|
|
|
|
lv_objmask_mask_t * m;
|
|
|
|
LV_LL_READ(ext->mask_ll, m) {
|
|
|
|
lv_mem_free(m->param);
|
|
|
|
}
|
|
|
|
|
|
|
|
lv_ll_clear(&ext->mask_ll);
|
|
|
|
}
|
|
|
|
/*Remove only the specified mask*/
|
|
|
|
else {
|
|
|
|
lv_mem_free(mask->param);
|
|
|
|
lv_ll_remove(&ext->mask_ll, mask);
|
|
|
|
}
|
2019-11-14 07:16:24 +01:00
|
|
|
|
2019-11-14 14:04:53 +01:00
|
|
|
lv_obj_invalidate(objmask);
|
2019-09-06 19:53:51 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 11:05:11 +01:00
|
|
|
|
|
|
|
|
2019-09-06 19:53:51 +02:00
|
|
|
/*=====================
|
|
|
|
* Setter functions
|
|
|
|
*====================*/
|
|
|
|
|
|
|
|
|
|
|
|
/*=====================
|
|
|
|
* Getter functions
|
|
|
|
*====================*/
|
|
|
|
|
|
|
|
|
|
|
|
/*=====================
|
|
|
|
* Other functions
|
|
|
|
*====================*/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC FUNCTIONS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the drawing related tasks of the object masks
|
|
|
|
* @param objmask pointer to an object
|
|
|
|
* @param clip_area 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
|
|
|
|
* @param return an element of `lv_design_res_t`
|
|
|
|
*/
|
|
|
|
static lv_design_res_t lv_objmask_design(lv_obj_t * objmask, const lv_area_t * clip_area, lv_design_mode_t mode)
|
|
|
|
{
|
|
|
|
/*Return false if the object is not covers the mask_p area*/
|
|
|
|
if(mode == LV_DESIGN_COVER_CHK) {
|
|
|
|
lv_objmask_ext_t * ext = lv_obj_get_ext_attr(objmask);
|
|
|
|
if(lv_ll_get_len(&ext->mask_ll) > 0) return LV_DESIGN_RES_MASKED;
|
|
|
|
else return ancestor_design(objmask, clip_area, mode);
|
|
|
|
}
|
|
|
|
/*Draw the object*/
|
|
|
|
else if(mode == LV_DESIGN_DRAW_MAIN) {
|
|
|
|
ancestor_design(objmask, clip_area, mode);
|
|
|
|
|
|
|
|
lv_objmask_ext_t * ext = lv_obj_get_ext_attr(objmask);
|
2019-11-14 07:16:24 +01:00
|
|
|
|
|
|
|
lv_coord_t xofs = objmask->coords.x1;
|
|
|
|
lv_coord_t yofs = objmask->coords.y1;
|
|
|
|
|
2019-09-06 19:53:51 +02:00
|
|
|
lv_objmask_mask_t * m;
|
|
|
|
|
|
|
|
LV_LL_READ(ext->mask_ll, m) {
|
2019-11-14 07:16:24 +01:00
|
|
|
lv_draw_mask_common_dsc_t * dsc = m->param;
|
|
|
|
|
|
|
|
if(dsc->type == LV_DRAW_MASK_TYPE_LINE) {
|
|
|
|
lv_draw_mask_line_param_t * p_ori = m->param;
|
2019-11-19 06:25:14 +01:00
|
|
|
lv_draw_mask_line_param_t * p_new = lv_mem_buf_get(sizeof(lv_draw_mask_line_param_t));
|
2019-11-14 07:16:24 +01:00
|
|
|
|
|
|
|
lv_draw_mask_line_points_init(p_new, p_ori->cfg.p1.x + xofs, p_ori->cfg.p1.y + yofs,
|
2020-02-26 19:48:27 +01:00
|
|
|
p_ori->cfg.p2.x + xofs, p_ori->cfg.p2.y + yofs,
|
|
|
|
p_ori->cfg.side);
|
2019-11-14 07:16:24 +01:00
|
|
|
lv_draw_mask_add(p_new, m->param);
|
|
|
|
}
|
|
|
|
else if(dsc->type == LV_DRAW_MASK_TYPE_ANGLE) {
|
|
|
|
lv_draw_mask_angle_param_t * p_ori = m->param;
|
2019-11-19 06:25:14 +01:00
|
|
|
lv_draw_mask_angle_param_t * p_new = lv_mem_buf_get(sizeof(lv_draw_mask_angle_param_t));
|
2019-11-14 07:16:24 +01:00
|
|
|
|
|
|
|
lv_draw_mask_angle_init(p_new, p_ori->cfg.vertex_p.x + xofs, p_ori->cfg.vertex_p.y + yofs,
|
2020-02-26 19:48:27 +01:00
|
|
|
p_ori->cfg.start_angle, p_ori->cfg.end_angle);
|
2019-11-14 07:16:24 +01:00
|
|
|
lv_draw_mask_add(p_new, m->param);
|
|
|
|
}
|
|
|
|
else if(dsc->type == LV_DRAW_MASK_TYPE_RADIUS) {
|
|
|
|
lv_draw_mask_radius_param_t * p_ori = m->param;
|
2019-11-19 06:25:14 +01:00
|
|
|
lv_draw_mask_radius_param_t * p_new = lv_mem_buf_get(sizeof(lv_draw_mask_radius_param_t));
|
2019-11-14 07:16:24 +01:00
|
|
|
|
|
|
|
lv_area_t rect;
|
|
|
|
rect.x1 = p_ori->cfg.rect.x1 + xofs;
|
|
|
|
rect.y1 = p_ori->cfg.rect.y1 + yofs;
|
|
|
|
rect.x2 = p_ori->cfg.rect.x2 + xofs;
|
|
|
|
rect.y2 = p_ori->cfg.rect.y2 + yofs;
|
|
|
|
|
|
|
|
lv_draw_mask_radius_init(p_new, &rect, p_ori->cfg.radius, p_ori->cfg.outer);
|
|
|
|
lv_draw_mask_add(p_new, m->param);
|
|
|
|
}
|
|
|
|
else if(dsc->type == LV_DRAW_MASK_TYPE_FADE) {
|
|
|
|
lv_draw_mask_fade_param_t * p_ori = m->param;
|
2019-11-19 06:25:14 +01:00
|
|
|
lv_draw_mask_fade_param_t * p_new = lv_mem_buf_get(sizeof(lv_draw_mask_fade_param_t));
|
2019-11-14 07:16:24 +01:00
|
|
|
|
|
|
|
lv_area_t rect;
|
|
|
|
rect.x1 = p_ori->cfg.coords.x1 + xofs;
|
|
|
|
rect.y1 = p_ori->cfg.coords.y1 + yofs;
|
|
|
|
rect.x2 = p_ori->cfg.coords.x2 + xofs;
|
|
|
|
rect.y2 = p_ori->cfg.coords.y2 + yofs;
|
|
|
|
|
|
|
|
lv_draw_mask_fade_init(p_new, &rect, p_ori->cfg.opa_top, p_ori->cfg.y_top + yofs,
|
2020-02-26 19:48:27 +01:00
|
|
|
p_ori->cfg.opa_bottom, p_ori->cfg.y_bottom + yofs);
|
2019-11-14 07:16:24 +01:00
|
|
|
lv_draw_mask_add(p_new, m->param);
|
|
|
|
}
|
|
|
|
else if(dsc->type == LV_DRAW_MASK_TYPE_MAP) {
|
|
|
|
lv_draw_mask_map_param_t * p_ori = m->param;
|
2019-11-19 06:25:14 +01:00
|
|
|
lv_draw_mask_map_param_t * p_new = lv_mem_buf_get(sizeof(lv_draw_mask_map_param_t));
|
2019-11-14 07:16:24 +01:00
|
|
|
|
|
|
|
lv_area_t rect;
|
|
|
|
rect.x1 = p_ori->cfg.coords.x1 + xofs;
|
|
|
|
rect.y1 = p_ori->cfg.coords.y1 + yofs;
|
|
|
|
rect.x2 = p_ori->cfg.coords.x2 + xofs;
|
|
|
|
rect.y2 = p_ori->cfg.coords.y2 + yofs;
|
|
|
|
|
|
|
|
lv_draw_mask_map_init(p_new, &rect, p_ori->cfg.map);
|
|
|
|
lv_draw_mask_add(p_new, m->param);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-06 19:53:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*Post draw when the children are drawn*/
|
|
|
|
else if(mode == LV_DESIGN_DRAW_POST) {
|
2019-09-10 07:34:46 +02:00
|
|
|
lv_objmask_ext_t * ext = lv_obj_get_ext_attr(objmask);
|
2019-11-14 07:16:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
lv_objmask_mask_t * m;
|
|
|
|
|
|
|
|
LV_LL_READ(ext->mask_ll, m) {
|
|
|
|
void * param;
|
|
|
|
param = lv_draw_mask_remove_custom(m->param);
|
2019-11-19 06:25:14 +01:00
|
|
|
lv_mem_buf_release(param);
|
2019-11-14 07:16:24 +01:00
|
|
|
}
|
|
|
|
|
2019-09-06 19:53:51 +02:00
|
|
|
}
|
|
|
|
|
2019-09-07 01:23:55 +02:00
|
|
|
return LV_DESIGN_RES_OK;
|
2019-09-06 19:53:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Signal function of the object mask
|
|
|
|
* @param objmask pointer to a object mask 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_objmask_signal(lv_obj_t * objmask, lv_signal_t sign, void * param)
|
|
|
|
{
|
|
|
|
lv_res_t res;
|
|
|
|
|
|
|
|
/* Include the ancient signal function */
|
|
|
|
res = ancestor_signal(objmask, sign, param);
|
|
|
|
if(res != LV_RES_OK) return res;
|
2019-09-26 15:24:47 +02:00
|
|
|
if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, LV_OBJX_NAME);
|
2019-09-06 19:53:51 +02:00
|
|
|
|
|
|
|
if(sign == LV_SIGNAL_CLEANUP) {
|
2019-11-14 07:16:24 +01:00
|
|
|
lv_objmask_ext_t * ext = lv_obj_get_ext_attr(objmask);
|
|
|
|
lv_objmask_mask_t * i;
|
|
|
|
LV_LL_READ(ext->mask_ll, i) {
|
|
|
|
if(i->param) {
|
|
|
|
lv_mem_free(i->param);
|
|
|
|
i->param = NULL;
|
|
|
|
}
|
2019-09-06 19:53:51 +02:00
|
|
|
}
|
2019-11-14 14:04:53 +01:00
|
|
|
|
|
|
|
lv_ll_clear(&ext->mask_ll);
|
2019-09-06 19:53:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-11-14 14:04:53 +01:00
|
|
|
static uint16_t get_param_size(lv_draw_mask_type_t type)
|
|
|
|
{
|
|
|
|
uint16_t param_size;
|
|
|
|
switch(type) {
|
2020-02-26 19:48:27 +01:00
|
|
|
case LV_DRAW_MASK_TYPE_LINE:
|
|
|
|
param_size = sizeof(lv_draw_mask_line_param_t);
|
|
|
|
break;
|
|
|
|
case LV_DRAW_MASK_TYPE_ANGLE:
|
|
|
|
param_size = sizeof(lv_draw_mask_angle_param_t);
|
|
|
|
break;
|
|
|
|
case LV_DRAW_MASK_TYPE_RADIUS:
|
|
|
|
param_size = sizeof(lv_draw_mask_radius_param_t);
|
|
|
|
break;
|
|
|
|
case LV_DRAW_MASK_TYPE_FADE:
|
|
|
|
param_size = sizeof(lv_draw_mask_fade_param_t);
|
|
|
|
break;
|
|
|
|
case LV_DRAW_MASK_TYPE_MAP:
|
|
|
|
param_size = sizeof(lv_draw_mask_map_param_t);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
param_size = 0;
|
2019-11-14 14:04:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return param_size;
|
|
|
|
}
|
|
|
|
|
2019-09-06 19:53:51 +02:00
|
|
|
#else /* Enable this file at the top */
|
|
|
|
|
|
|
|
#endif
|