fix(example): fix the gradient text example (#6152)

This commit is contained in:
Gabor Kiss-Vamosi
2024-05-02 16:08:08 +02:00
committed by GitHub
parent 691296e2db
commit 0a03d9da1e
2 changed files with 30 additions and 47 deletions

View File

@@ -1,30 +1,33 @@
#include "../../lv_examples.h" #include "../../lv_examples.h"
//TODO
#if LV_USE_LABEL && LV_USE_CANVAS && LV_BUILD_EXAMPLES && LV_DRAW_SW_COMPLEX && 0
#define MASK_WIDTH 100 #if LV_USE_LABEL && LV_FONT_MONTSERRAT_24 && LV_USE_CANVAS && LV_BUILD_EXAMPLES && LV_DRAW_SW_COMPLEX
#define MASK_HEIGHT 45
static void add_mask_event_cb(lv_event_t * e) #define MASK_WIDTH 150
#define MASK_HEIGHT 60
static void generate_mask(uint8_t * mask, int32_t w, int32_t h, const char * txt)
{ {
static lv_draw_mask_map_param_t m; /*Create a "8 bit alpha" canvas and clear it*/
static int16_t mask_id; lv_obj_t * canvas = lv_canvas_create(lv_screen_active());
lv_canvas_set_buffer(canvas, mask, w, h, LV_COLOR_FORMAT_L8);
lv_canvas_fill_bg(canvas, lv_color_black(), LV_OPA_TRANSP);
lv_event_code_t code = lv_event_get_code(e); lv_layer_t layer;
lv_obj_t * obj = lv_event_get_target(e); lv_canvas_init_layer(canvas, &layer);
lv_opa_t * mask_map = lv_event_get_user_data(e);
if(code == LV_EVENT_COVER_CHECK) {
lv_event_set_cover_res(e, LV_COVER_RES_MASKED);
}
else if(code == LV_EVENT_DRAW_MAIN_BEGIN) {
lv_draw_mask_map_init(&m, &obj->coords, mask_map);
mask_id = lv_draw_mask_add(&m, NULL);
} /*Draw a label to the canvas. The result "image" will be used as mask*/
else if(code == LV_EVENT_DRAW_MAIN_END) { lv_draw_label_dsc_t label_dsc;
lv_draw_mask_free_param(&m); lv_draw_label_dsc_init(&label_dsc);
lv_draw_mask_remove_id(mask_id); label_dsc.color = lv_color_white();
} label_dsc.align = LV_TEXT_ALIGN_CENTER;
label_dsc.text = txt;
label_dsc.font = &lv_font_montserrat_24;
lv_area_t a = {0, 0, w - 1, h - 1};
lv_draw_label(&layer, &label_dsc, &a);
lv_canvas_finish_layer(canvas, &layer);
lv_obj_delete(canvas);
} }
/** /**
@@ -33,31 +36,11 @@ static void add_mask_event_cb(lv_event_t * e)
void lv_example_label_4(void) void lv_example_label_4(void)
{ {
/* Create the mask of a text by drawing it to a canvas*/ /* Create the mask of a text by drawing it to a canvas*/
static lv_color_t mask_map[MASK_WIDTH * MASK_HEIGHT]; static uint8_t mask[MASK_WIDTH * MASK_HEIGHT];
generate_mask(mask, MASK_WIDTH, MASK_HEIGHT, "Text with gradient");
/*Create a "8 bit alpha" canvas and clear it*/ static lv_draw_buf_t draw_buf;
lv_obj_t * canvas = lv_canvas_create(lv_screen_active()); lv_draw_buf_init(&draw_buf, MASK_WIDTH, MASK_HEIGHT, LV_COLOR_FORMAT_A8, LV_STRIDE_AUTO, mask, sizeof(mask));
lv_canvas_set_buffer(canvas, mask_map, MASK_WIDTH, MASK_HEIGHT, LV_COLOR_FORMAT_NATIVE);
lv_canvas_fill_bg(canvas, lv_color_black(), LV_OPA_TRANSP);
/*Draw a label to the canvas. The result "image" will be used as mask*/
lv_draw_label_dsc_t label_dsc;
lv_draw_label_dsc_init(&label_dsc);
label_dsc.color = lv_color_white();
label_dsc.align = LV_TEXT_ALIGN_CENTER;
label_dsc.text = "Text with gradient";
lv_canvas_draw_text(canvas, 5, 5, MASK_WIDTH, &label_dsc);
/*The mask is reads the canvas is not required anymore*/
lv_obj_delete(canvas);
/*Convert the mask to A8*/
uint32_t i;
uint8_t * mask8 = (uint8_t *) mask_map;
lv_color_t * mask_c = mask_map;
for(i = 0; i < MASK_WIDTH * MASK_HEIGHT; i++) {
mask8[i] = lv_color_brightness(mask_c[i]);
}
/* Create an object from where the text will be masked out. /* Create an object from where the text will be masked out.
* Now it's a rectangle with a gradient but it could be an image too*/ * Now it's a rectangle with a gradient but it could be an image too*/
@@ -67,7 +50,7 @@ void lv_example_label_4(void)
lv_obj_set_style_bg_color(grad, lv_color_hex(0xff0000), 0); lv_obj_set_style_bg_color(grad, lv_color_hex(0xff0000), 0);
lv_obj_set_style_bg_grad_color(grad, lv_color_hex(0x0000ff), 0); lv_obj_set_style_bg_grad_color(grad, lv_color_hex(0x0000ff), 0);
lv_obj_set_style_bg_grad_dir(grad, LV_GRAD_DIR_HOR, 0); lv_obj_set_style_bg_grad_dir(grad, LV_GRAD_DIR_HOR, 0);
lv_obj_add_event_cb(grad, add_mask_event_cb, LV_EVENT_ALL, mask_map); lv_obj_set_style_bitmap_mask_src(grad, &draw_buf, 0);
} }
#endif #endif

View File

@@ -87,7 +87,7 @@ void lv_example_keyboard_2(void);
void lv_example_label_1(void); void lv_example_label_1(void);
void lv_example_label_2(void); void lv_example_label_2(void);
void lv_example_label_3(void); void lv_example_label_3(void);
//void lv_example_label_4(void); void lv_example_label_4(void);
void lv_example_label_5(void); void lv_example_label_5(void);
void lv_example_led_1(void); void lv_example_led_1(void);