1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-28 07:03:00 +08:00

feat(btnmatrix) make 'recolor' a button flag insted of a function

This commit is contained in:
Gabor Kiss-Vamosi 2021-04-30 20:35:16 +02:00
parent 9c7ba22f66
commit e9aa30ff7d
2 changed files with 20 additions and 48 deletions

View File

@ -42,8 +42,10 @@ static bool button_is_hidden(lv_btnmatrix_ctrl_t ctrl_bits);
static bool button_is_repeat_disabled(lv_btnmatrix_ctrl_t ctrl_bits);
static bool button_is_inactive(lv_btnmatrix_ctrl_t ctrl_bits);
static bool button_is_click_trig(lv_btnmatrix_ctrl_t ctrl_bits);
static bool button_is_tgl_enabled(lv_btnmatrix_ctrl_t ctrl_bits);
static bool button_is_checkable(lv_btnmatrix_ctrl_t ctrl_bits);
static bool button_is_recolor(lv_btnmatrix_ctrl_t ctrl_bits);
static bool button_get_checked(lv_btnmatrix_ctrl_t ctrl_bits);
static bool button_is_recolor(lv_btnmatrix_ctrl_t ctrl_bits);
static uint16_t get_button_from_point(lv_obj_t * obj, lv_point_t * p);
static void allocate_btn_areas_and_controls(const lv_obj_t * obj, const char ** map);
static void invalidate_button_area(const lv_obj_t * obj, uint16_t btn_idx);
@ -197,16 +199,6 @@ void lv_btnmatrix_set_selected_btn(lv_obj_t * obj, uint16_t btn_id)
invalidate_button_area(obj, btn_id);
}
void lv_btnmatrix_set_recolor(const lv_obj_t * obj, bool en)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_btnmatrix_t * btnm = (lv_btnmatrix_t *)obj;;
btnm->recolor = en;
lv_obj_invalidate(obj);
}
void lv_btnmatrix_set_btn_ctrl(lv_obj_t * obj, uint16_t btn_id, lv_btnmatrix_ctrl_t ctrl)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
@ -292,15 +284,6 @@ const char ** lv_btnmatrix_get_map(const lv_obj_t * obj)
return btnm->map_p;
}
bool lv_btnmatrix_get_recolor(const lv_obj_t * obj)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_btnmatrix_t * btnm = (lv_btnmatrix_t *)obj;;
return btnm->recolor;
}
uint16_t lv_btnmatrix_get_selected_btn(const lv_obj_t * obj)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
@ -367,7 +350,6 @@ static void lv_btnmatrix_constructor(const lv_obj_class_t * class_p, lv_obj_t *
btnm->button_areas = NULL;
btnm->ctrl_bits = NULL;
btnm->map_p = NULL;
btnm->recolor = 0;
btnm->one_check = 0;
lv_btnmatrix_set_map(obj, lv_btnmatrix_def_map);
@ -473,7 +455,7 @@ static void lv_btnmatrix_event(const lv_obj_class_t * class_p, lv_event_t * e)
else if(code == LV_EVENT_RELEASED) {
if(btnm->btn_id_sel != LV_BTNMATRIX_BTN_NONE) {
/*Toggle the button if enabled*/
if(button_is_tgl_enabled(btnm->ctrl_bits[btnm->btn_id_sel]) &&
if(button_is_checkable(btnm->ctrl_bits[btnm->btn_id_sel]) &&
!button_is_inactive(btnm->ctrl_bits[btnm->btn_id_sel])) {
if(button_get_checked(btnm->ctrl_bits[btnm->btn_id_sel]) && !btnm->one_check) {
btnm->ctrl_bits[btnm->btn_id_sel] &= (~LV_BTNMATRIX_CTRL_CHECKED);
@ -660,7 +642,6 @@ static void draw_main(lv_event_t * e)
lv_draw_rect_dsc_t draw_rect_dsc_def;
lv_draw_label_dsc_t draw_label_dsc_def;
lv_text_flag_t recolor_flag = btnm->recolor ? LV_TEXT_FLAG_RECOLOR : 0;
lv_state_t state_ori = obj->state;
obj->state = LV_STATE_DEFAULT;
@ -669,7 +650,6 @@ static void draw_main(lv_event_t * e)
lv_draw_label_dsc_init(&draw_label_dsc_def);
lv_obj_init_draw_rect_dsc(obj, LV_PART_ITEMS, &draw_rect_dsc_def);
lv_obj_init_draw_label_dsc(obj, LV_PART_ITEMS, &draw_label_dsc_def);
draw_label_dsc_act.flag |= recolor_flag;
obj->skip_trans = 0;
obj->state = state_ori;
@ -729,11 +709,15 @@ static void draw_main(lv_event_t * e)
lv_draw_label_dsc_init(&draw_label_dsc_act);
lv_obj_init_draw_rect_dsc(obj, LV_PART_ITEMS, &draw_rect_dsc_act);
lv_obj_init_draw_label_dsc(obj, LV_PART_ITEMS, &draw_label_dsc_act);
draw_label_dsc_act.flag = recolor_flag;
obj->state = state_ori;
obj->skip_trans = 0;
}
bool recolor = button_is_hidden(btnm->ctrl_bits[btnm->ctrl_bits[btn_i]]);
if(recolor) draw_label_dsc_act.flag |= LV_TEXT_FLAG_RECOLOR;
else draw_label_dsc_act.flag &= ~LV_TEXT_FLAG_RECOLOR;
dsc.draw_area = &btn_area;
dsc.id = btn_i;
lv_event_send(obj,LV_EVENT_DRAW_PART_BEGIN, &dsc);
@ -764,10 +748,9 @@ static void draw_main(lv_event_t * e)
txt = txt_ap;
}
#endif
lv_point_t txt_size;
lv_txt_get_size(&txt_size, txt, font, letter_space,
line_space, lv_area_get_width(&area_obj), recolor_flag);
line_space, lv_area_get_width(&area_obj), draw_label_dsc_act.flag);
btn_area.x1 += (lv_area_get_width(&btn_area) - txt_size.x) / 2;
btn_area.y1 += (lv_area_get_height(&btn_area) - txt_size.y) / 2;
@ -777,7 +760,6 @@ static void draw_main(lv_event_t * e)
/*Draw the text*/
lv_draw_label(&btn_area, clip_area, &draw_label_dsc_act, txt, NULL);
lv_event_send(obj,LV_EVENT_DRAW_PART_END, &dsc);
}
@ -859,7 +841,7 @@ static bool button_is_click_trig(lv_btnmatrix_ctrl_t ctrl_bits)
return (ctrl_bits & LV_BTNMATRIX_CTRL_CLICK_TRIG) ? true : false;
}
static bool button_is_tgl_enabled(lv_btnmatrix_ctrl_t ctrl_bits)
static bool button_is_checkable(lv_btnmatrix_ctrl_t ctrl_bits)
{
return (ctrl_bits & LV_BTNMATRIX_CTRL_CHECKABLE) ? true : false;
}
@ -869,6 +851,10 @@ static bool button_get_checked(lv_btnmatrix_ctrl_t ctrl_bits)
return (ctrl_bits & LV_BTNMATRIX_CTRL_CHECKED) ? true : false;
}
static bool button_is_recolor(lv_btnmatrix_ctrl_t ctrl_bits)
{
return (ctrl_bits & LV_BTNMATRIX_CTRL_RECOLOR) ? true : false;
}
/**
* Gives the button id of a button under a given point
* @param obj pointer to a button matrix object

View File

@ -36,13 +36,13 @@ enum {
LV_BTNMATRIX_CTRL_HIDDEN = 0x0008, /**< Button hidden*/
LV_BTNMATRIX_CTRL_NO_REPEAT = 0x0010, /**< Do not repeat press this button.*/
LV_BTNMATRIX_CTRL_DISABLED = 0x0020, /**< Disable this button.*/
LV_BTNMATRIX_CTRL_CHECKABLE = 0x0040, /**< Button *can* be toggled.*/
LV_BTNMATRIX_CTRL_CHECKABLE = 0x0040, /**< The button can be toggled.*/
LV_BTNMATRIX_CTRL_CHECKED = 0x0080, /**< Button is currently toggled (e.g. checked).*/
LV_BTNMATRIX_CTRL_CLICK_TRIG = 0x0100, /**< 1: Send LV_EVENT_VALUE_CHANGE on CLICK, 0: Send LV_EVENT_VALUE_CHANGE on PRESS*/
LV_BTNMATRIX_CTRL_CUSTOM_1 = 0x1000, /**< Custom free to use flag*/
LV_BTNMATRIX_CTRL_CUSTOM_2 = 0x2000, /**< Custom free to use flag*/
LV_BTNMATRIX_CTRL_CUSTOM_3 = 0x4000, /**< Custom free to use flag*/
LV_BTNMATRIX_CTRL_CUSTOM_4 = 0x8000, /**< Custom free to use flag*/
LV_BTNMATRIX_CTRL_RECOLOR = 0x1000, /**< Enable text recoloring with `#color`*/
_LV_BTNMATRIX_CTRL_RESERVED = 0x2000, /**< Reserved for later use*/
LV_BTNMATRIX_CTRL_CUSTOM_1 = 0x4000, /**< Custom free to use flag*/
LV_BTNMATRIX_CTRL_CUSTOM_2 = 0x8000, /**< Custom free to use flag*/
};
typedef uint16_t lv_btnmatrix_ctrl_t;
@ -57,7 +57,6 @@ typedef struct {
lv_btnmatrix_ctrl_t * ctrl_bits; /*Array of control bytes*/
uint16_t btn_cnt; /*Number of button in 'map_p'(Handled by the library)*/
uint16_t btn_id_sel; /*Index of the active button (being pressed/released etc) or LV_BTNMATRIX_BTN_NONE*/
uint8_t recolor : 1; /*Enable button recoloring*/
uint8_t one_check : 1; /*Single button toggled at once*/
} lv_btnmatrix_t;
@ -109,12 +108,6 @@ void lv_btnmatrix_set_ctrl_map(lv_obj_t * obj, const lv_btnmatrix_ctrl_t ctrl_ma
*/
void lv_btnmatrix_set_selected_btn(lv_obj_t * obj, uint16_t btn_id);
/**
* Enable recoloring of button's texts. E.g. "a #ff0000 red# word"
* @param obj pointer to button matrix object
* @param en true: enable recoloring; false: disable
*/
void lv_btnmatrix_set_recolor(const lv_obj_t * obj, bool en);
/**
* Set the attributes of a button of the button matrix
* @param obj pointer to button matrix object
@ -177,13 +170,6 @@ void lv_btnmatrix_set_one_checked(lv_obj_t * obj, bool en);
*/
const char ** lv_btnmatrix_get_map(const lv_obj_t * obj);
/**
* Check whether the button's text can use recolor or not
* @param obj pointer to button matrix object
* @return true: text recolor enable; false: disabled
*/
bool lv_btnmatrix_get_recolor(const lv_obj_t * obj);
/**
* Get the index of the lastly "activated" button by the user (pressed, released, focused etc)
* Useful in the the `event_cb` to get the text of the button, check if hidden etc.