fix(color): Button matrix styling (#4318)
This commit is contained in:
+24
-23
@@ -25,17 +25,18 @@ static const char _filler[] = "0";
|
||||
static const char _newline[] = "\n";
|
||||
static const char _map_end[] = "";
|
||||
|
||||
static void btn_matrix_event(lv_event_t *e)
|
||||
static void btn_matrix_event(lv_event_t* e)
|
||||
{
|
||||
lv_event_code_t code = lv_event_get_code(e);
|
||||
|
||||
if (code == LV_EVENT_VALUE_CHANGED) {
|
||||
lv_obj_t* obj = lv_event_get_target(e);
|
||||
auto btn_id = *((uint8_t *)lv_event_get_param(e));
|
||||
auto btn_id = *((uint8_t*)lv_event_get_param(e));
|
||||
auto btnm = (ButtonMatrix*)lv_event_get_user_data(e);
|
||||
|
||||
bool edited = lv_obj_has_state(obj, LV_STATE_EDITED);
|
||||
bool is_pointer = lv_indev_get_type(lv_indev_get_act()) == LV_INDEV_TYPE_POINTER;
|
||||
bool is_pointer =
|
||||
lv_indev_get_type(lv_indev_get_act()) == LV_INDEV_TYPE_POINTER;
|
||||
if (edited || is_pointer) {
|
||||
btnm->onPress(btn_id);
|
||||
}
|
||||
@@ -43,7 +44,7 @@ static void btn_matrix_event(lv_event_t *e)
|
||||
}
|
||||
|
||||
ButtonMatrix::ButtonMatrix(Window* parent, const rect_t& r) :
|
||||
FormField(parent, r, 0, 0, etx_btnmatrix_create)
|
||||
FormField(parent, r, 0, 0, etx_btnmatrix_create)
|
||||
{
|
||||
lv_obj_add_flag(lvobj, LV_OBJ_FLAG_SCROLL_ON_FOCUS);
|
||||
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_CLICK_FOCUSABLE);
|
||||
@@ -51,10 +52,7 @@ ButtonMatrix::ButtonMatrix(Window* parent, const rect_t& r) :
|
||||
lv_obj_add_event_cb(lvobj, btn_matrix_event, LV_EVENT_ALL, this);
|
||||
}
|
||||
|
||||
ButtonMatrix::~ButtonMatrix()
|
||||
{
|
||||
deallocate();
|
||||
}
|
||||
ButtonMatrix::~ButtonMatrix() { deallocate(); }
|
||||
|
||||
void ButtonMatrix::deallocate()
|
||||
{
|
||||
@@ -77,47 +75,50 @@ void ButtonMatrix::initBtnMap(uint8_t cols, uint8_t btns)
|
||||
deallocate();
|
||||
|
||||
uint8_t rows = ((btns - 1) / cols) + 1;
|
||||
txt_cnt = btns + rows;
|
||||
if (rows == 1) cols = btns;
|
||||
txt_cnt = (cols + 1) * rows;
|
||||
btn_cnt = btns;
|
||||
|
||||
lv_btnm_map = (char**)malloc(sizeof(char*) * txt_cnt);
|
||||
txt_index = (uint8_t*)malloc(sizeof(uint8_t) * btns);
|
||||
txt_index = (uint8_t*)malloc(sizeof(uint8_t) * cols * rows);
|
||||
|
||||
uint8_t col = 0;
|
||||
uint8_t btn = 0;
|
||||
uint8_t txt_i = 0;
|
||||
|
||||
while (btn < btns) {
|
||||
|
||||
while (btn < cols * rows) {
|
||||
if (col == cols) {
|
||||
lv_btnm_map[txt_i++] = (char*)_newline;
|
||||
col = 0;
|
||||
}
|
||||
|
||||
|
||||
txt_index[btn] = txt_i;
|
||||
lv_btnm_map[txt_i++] = (char*)_filler;
|
||||
btn++;
|
||||
col++;
|
||||
}
|
||||
lv_btnm_map[txt_i++] = (char*)_map_end;
|
||||
lv_btnm_map[txt_i] = (char*)_map_end;
|
||||
update();
|
||||
}
|
||||
|
||||
void ButtonMatrix::setText(uint8_t btn_id, const char* txt)
|
||||
{
|
||||
if (btn_id >= btn_cnt) return;
|
||||
|
||||
uint8_t txt_i = txt_index[btn_id];
|
||||
lv_btnm_map[txt_i] = strdup(txt);
|
||||
if (btn_id < btn_cnt) lv_btnm_map[txt_index[btn_id]] = strdup(txt);
|
||||
}
|
||||
|
||||
void ButtonMatrix::update()
|
||||
{
|
||||
lv_btnmatrix_set_map(lvobj, (const char**)lv_btnm_map);
|
||||
|
||||
lv_btnmatrix_ctrl_t ctrl = LV_BTNMATRIX_CTRL_CLICK_TRIG |
|
||||
LV_BTNMATRIX_CTRL_NO_REPEAT;
|
||||
|
||||
lv_btnmatrix_set_btn_ctrl_all(lvobj, ctrl);
|
||||
lv_btnmatrix_set_btn_ctrl_all(
|
||||
lvobj, LV_BTNMATRIX_CTRL_CLICK_TRIG | LV_BTNMATRIX_CTRL_NO_REPEAT);
|
||||
int btn = 0;
|
||||
for (int i = 0; lv_btnm_map[i] != _map_end; i += 1) {
|
||||
if (lv_btnm_map[i] == _filler)
|
||||
lv_btnmatrix_set_btn_ctrl(lvobj, btn, LV_BTNMATRIX_CTRL_HIDDEN);
|
||||
else
|
||||
lv_btnmatrix_clear_btn_ctrl(lvobj, btn, LV_BTNMATRIX_CTRL_HIDDEN);
|
||||
if (lv_btnm_map[i] != _newline) btn += 1;
|
||||
}
|
||||
}
|
||||
|
||||
void ButtonMatrix::onClicked()
|
||||
|
||||
Reference in New Issue
Block a user