I try to color a single button
I found the Post in this forum whre you can Color alls Buttons at once.
But I did not get a Solution for a single Buitton.
static void event_handler_1(lv_event_t* e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t* obj = (lv_obj_t*)lv_event_get_target(e);
if (code == LV_EVENT_VALUE_CHANGED) {
uint32_t id = lv_buttonmatrix_get_selected_button(obj);
const char* txt = lv_buttonmatrix_get_button_text(obj, id);
// Check if the selected button is "Bad" and if it's pressed
if (strcmp(txt, "Bad") == 0 && lv_obj_has_state(obj, LV_STATE_PRESSED)) {
lv_obj_set_style_bg_color(obj, lv_color_hex(0x0000FF), LV_PART_ITEMS);
}
else {
// Reset the background color of the button to default
lv_obj_set_style_bg_color(obj, lv_color_hex(0xFFFFFF), LV_PART_ITEMS);
}
LV_LOG_USER("%s was pressed\n", txt);
}
}
static const char* btnm_map[] = {"Main", "Wohnzi.", "Bad", "\n", "Garage", "Hütte", "Terasse", ""};
void lv_button_matrix_1(void)
{
lv_obj_t* btnm = lv_buttonmatrix_create(lv_screen_active());
lv_buttonmatrix_set_map(btnm, btnm_map);
static lv_style_t style_bkg, style_red, style_green, style_blue, style_yellow, style_magenta, style_cyan;
//lv_style_init(&style_red);
lv_style_set_bg_color(&style_bkg, lv_color_hex(0xb1d049));
lv_style_set_bg_color(&style_red, lv_color_hex(0xff0000));
lv_style_set_bg_color(&style_green, lv_color_hex(0x00ff00));
lv_style_set_bg_color(&style_blue, lv_color_hex(0x0000ff));
lv_style_set_bg_color(&style_blue, lv_color_hex(0xffff00));
lv_style_set_bg_color(&style_magenta, lv_color_hex(0x0ffff0));
lv_style_set_bg_color(&style_magenta, lv_color_hex(0xffff00));
// Weise den Buttons die Styles zu
lv_obj_add_style(btnm, &style_bkg, LV_STATE_PRESSED);
lv_obj_add_style(btnm, &style_red, LV_STATE_HOVERED);
lv_buttonmatrix_set_button_width(btnm, 10, 2); /*Make "Action1" twice as wide as "Action2"*/
lv_buttonmatrix_set_button_ctrl(btnm, 10, LV_BUTTONMATRIX_CTRL_CHECKABLE);
lv_buttonmatrix_set_button_ctrl(btnm, 11, LV_BUTTONMATRIX_CTRL_CHECKED);
lv_obj_align(btnm, LV_ALIGN_CENTER, 0, 0);
lv_obj_add_event_cb(btnm, event_handler_1, LV_EVENT_ALL, NULL);
}
///----------------------------------------------------------------------------------------------------
void lv_button_matrix_5(void)
{
lv_obj_t* btnm = lv_buttonmatrix_create(lv_screen_active());
// lv_obj_add_event_cb(btnm, event_cb, LV_EVENT_DRAW_TASK_ADDED, NULL);
lv_obj_add_flag(btnm, LV_OBJ_FLAG_SEND_DRAW_TASK_EVENTS);
lv_obj_center(btnm);
}
///++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
static void event_cb_2(lv_event_t* e)
{
lv_obj_t* obj = (lv_obj_t*)lv_event_get_target(e);
lv_draw_task_t* draw_task = (lv_draw_task_t*)lv_event_get_draw_task(e);
lv_draw_dsc_base_t* base_dsc = (lv_draw_dsc_base_t * )lv_draw_task_get_draw_dsc(draw_task);
/*When the button matrix draws the buttons...*/
if (base_dsc->part == LV_PART_ITEMS) {
bool pressed = false;
if (lv_buttonmatrix_get_selected_button(obj) == base_dsc->id1 && lv_obj_has_state(obj, LV_STATE_PRESSED)) {
pressed = true;
}
/*Change the draw descriptor of the 2nd button*/
if (base_dsc->id1 == 1) {
lv_draw_fill_dsc_t* fill_draw_dsc = lv_draw_task_get_fill_dsc(draw_task);
if (fill_draw_dsc) {
fill_draw_dsc->radius = 0;
if (pressed) fill_draw_dsc->color = lv_palette_darken(LV_PALETTE_BLUE, 3);
else fill_draw_dsc->color = lv_palette_main(LV_PALETTE_BLUE);
}
lv_draw_box_shadow_dsc_t* box_shadow_draw_dsc = lv_draw_task_get_box_shadow_dsc(draw_task);
if (box_shadow_draw_dsc) {
box_shadow_draw_dsc->width = 6;
box_shadow_draw_dsc->ofs_x = 3;
box_shadow_draw_dsc->ofs_y = 3;
}
lv_draw_label_dsc_t* label_draw_dsc = lv_draw_task_get_label_dsc(draw_task);
if (label_draw_dsc) {
label_draw_dsc->color = lv_color_white();
}
}
/*Change the draw descriptor of the 3rd button*/
else if (base_dsc->id1 == 2) {
lv_draw_fill_dsc_t* fill_draw_dsc = lv_draw_task_get_fill_dsc(draw_task);
if (fill_draw_dsc) {
fill_draw_dsc->radius = LV_RADIUS_CIRCLE;
if (pressed) fill_draw_dsc->color = lv_palette_darken(LV_PALETTE_RED, 3);
else fill_draw_dsc->color = lv_palette_main(LV_PALETTE_RED);
}
}
else if (base_dsc->id1 == 3) {
lv_draw_label_dsc_t* label_draw_dsc = lv_draw_task_get_label_dsc(draw_task);
if (label_draw_dsc) {
label_draw_dsc->opa = 0;
}
/*
if (lv_draw_task_get_type(draw_task) == LV_DRAW_TASK_TYPE_FILL) {
LV_IMAGE_DECLARE(img_star);
lv_image_header_t header;
lv_result_t res = lv_image_decoder_get_info(&img_star, &header);
if (res != LV_RESULT_OK) return;
lv_area_t a;
a.x1 = 0;
a.x2 = header.w - 1;
a.y1 = 0;
a.y2 = header.h - 1;
lv_area_t draw_task_area;
lv_draw_task_get_area(draw_task, &draw_task_area);
lv_area_align(&draw_task_area, &a, LV_ALIGN_CENTER, 0, 0);
lv_draw_image_dsc_t img_draw_dsc;
lv_draw_image_dsc_init(&img_draw_dsc);
img_draw_dsc.src = &img_star;
img_draw_dsc.recolor = lv_color_black();
if (pressed) img_draw_dsc.recolor_opa = LV_OPA_30;
lv_draw_image(base_dsc->layer, &img_draw_dsc, &a);
}
*/
}
}
}
/**
* Add custom drawer to the button matrix to customize buttons one by one
*/
void lv_button_matrix_2(void)
{
lv_obj_t* btnm = lv_buttonmatrix_create(lv_screen_active());
lv_obj_add_event_cb(btnm, event_cb_2, LV_EVENT_DRAW_TASK_ADDED, NULL);
lv_obj_add_flag(btnm, LV_OBJ_FLAG_SEND_DRAW_TASK_EVENTS);
lv_obj_center(btnm);
}