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"
|
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);
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* 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*/
|
|
|
|
lv_obj_t * new_objmask = lv_cont_create(par, copy);
|
2019-09-26 15:24:47 +02:00
|
|
|
LV_ASSERT_MEM(new_objmask);
|
2019-09-06 19:53:51 +02:00
|
|
|
if(new_objmask == NULL) return NULL;
|
|
|
|
|
|
|
|
/*Allocate the object mask type specific extended data*/
|
|
|
|
lv_objmask_ext_t * ext = lv_obj_allocate_ext_attr(new_objmask, sizeof(lv_objmask_ext_t));
|
2019-09-26 15:24:47 +02:00
|
|
|
LV_ASSERT_MEM(ext);
|
2019-09-06 19:53:51 +02:00
|
|
|
if(ext == NULL) return NULL;
|
|
|
|
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_objmask);
|
2019-09-07 01:23:55 +02:00
|
|
|
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(new_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*/
|
|
|
|
lv_obj_set_signal_cb(new_objmask, lv_objmask_signal);
|
|
|
|
lv_obj_set_design_cb(new_objmask, lv_objmask_design);
|
|
|
|
|
|
|
|
/*Init the new object mask object mask*/
|
|
|
|
if(copy == NULL) {
|
2019-11-09 00:57:26 +01:00
|
|
|
lv_objmask_set_style(new_objmask, LV_OBJMASK_STYLE_BG, &lv_style_transp);
|
2019-09-06 19:53:51 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
/*Copy an existing object mask*/
|
|
|
|
else {
|
|
|
|
lv_objmask_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
|
|
|
|
|
|
|
|
/*Refresh the style with new signal function*/
|
|
|
|
lv_obj_refresh_style(new_objmask);
|
|
|
|
}
|
|
|
|
|
|
|
|
LV_LOG_INFO("object mask created");
|
|
|
|
|
|
|
|
return new_objmask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*======================
|
|
|
|
* Add/remove functions
|
|
|
|
*=====================*/
|
|
|
|
|
2019-11-14 07:16:24 +01:00
|
|
|
void lv_objmask_add_mask(lv_obj_t * objmask, void * param, uint8_t id)
|
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;
|
|
|
|
uint16_t param_size;
|
|
|
|
switch(dsc->type) {
|
|
|
|
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-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);
|
|
|
|
if(m == NULL) return;
|
|
|
|
|
|
|
|
memcpy(m->param, param, param_size);
|
2019-09-06 19:53:51 +02:00
|
|
|
|
|
|
|
m->id = id;
|
2019-11-14 07:16:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-09-06 19:53:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*=====================
|
|
|
|
* Setter functions
|
|
|
|
*====================*/
|
|
|
|
|
|
|
|
|
|
|
|
/*=====================
|
|
|
|
* Getter functions
|
|
|
|
*====================*/
|
|
|
|
|
|
|
|
|
|
|
|
/*=====================
|
|
|
|
* Other functions
|
|
|
|
*====================*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* New object specific "other" functions come here
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* 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;
|
|
|
|
lv_draw_mask_line_param_t * p_new = lv_draw_buf_get(sizeof(lv_draw_mask_line_param_t));
|
|
|
|
|
|
|
|
lv_draw_mask_line_points_init(p_new, p_ori->cfg.p1.x + xofs, p_ori->cfg.p1.y + yofs,
|
|
|
|
p_ori->cfg.p2.x + xofs, p_ori->cfg.p2.y + yofs,
|
|
|
|
p_ori->cfg.side);
|
|
|
|
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;
|
|
|
|
lv_draw_mask_angle_param_t * p_new = lv_draw_buf_get(sizeof(lv_draw_mask_angle_param_t));
|
|
|
|
|
|
|
|
lv_draw_mask_angle_init(p_new, p_ori->cfg.vertex_p.x + xofs, p_ori->cfg.vertex_p.y + yofs,
|
|
|
|
p_ori->cfg.start_angle, p_ori->cfg.end_angle);
|
|
|
|
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;
|
|
|
|
lv_draw_mask_radius_param_t * p_new = lv_draw_buf_get(sizeof(lv_draw_mask_radius_param_t));
|
|
|
|
|
|
|
|
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;
|
|
|
|
lv_draw_mask_fade_param_t * p_new = lv_draw_buf_get(sizeof(lv_draw_mask_fade_param_t));
|
|
|
|
|
|
|
|
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,
|
|
|
|
p_ori->cfg.opa_bottom, p_ori->cfg.y_bottom + yofs);
|
|
|
|
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;
|
|
|
|
lv_draw_mask_map_param_t * p_new = lv_draw_buf_get(sizeof(lv_draw_mask_map_param_t));
|
|
|
|
|
|
|
|
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);
|
|
|
|
lv_draw_buf_release(param);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* Enable this file at the top */
|
|
|
|
|
|
|
|
/* This dummy typedef exists purely to silence -Wpedantic. */
|
|
|
|
typedef int keep_pedantic_happy;
|
|
|
|
#endif
|