1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-21 06:53:01 +08:00
lvgl/src/lv_objx/lv_btnm.c

1035 lines
36 KiB
C
Raw Normal View History

2016-09-30 13:35:54 +02:00
/**
* @file lv_btnm.c
2018-06-19 09:49:58 +02:00
*
2016-09-30 13:35:54 +02:00
*/
/*********************
* INCLUDES
*********************/
#include "lv_btnm.h"
#if LV_USE_BTNM != 0
2016-09-30 13:35:54 +02:00
2017-11-30 11:35:33 +01:00
#include "../lv_core/lv_group.h"
2017-04-24 16:16:36 +02:00
#include "../lv_draw/lv_draw.h"
2017-11-30 11:35:33 +01:00
#include "../lv_core/lv_refr.h"
#include "../lv_themes/lv_theme.h"
2017-11-26 23:57:39 +01:00
#include "../lv_misc/lv_txt.h"
2016-09-30 13:35:54 +02:00
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
2017-11-10 15:01:40 +01:00
static lv_res_t lv_btnm_signal(lv_obj_t * btnm, lv_signal_t sign, void * param);
2017-11-23 21:28:36 +01:00
static bool lv_btnm_design(lv_obj_t * btnm, const lv_area_t * mask, lv_design_mode_t mode);
2019-02-27 06:17:37 +01:00
static uint8_t get_button_width(lv_btnm_ctrl_t ctrl_bits);
static bool button_is_hidden(lv_btnm_ctrl_t ctrl_bits);
static bool button_is_repeat_disabled(lv_btnm_ctrl_t ctrl_bits);
static bool button_is_inactive(lv_btnm_ctrl_t ctrl_bits);
static bool button_is_click_trig(lv_btnm_ctrl_t ctrl_bits);
static bool button_is_tgl_enabled(lv_btnm_ctrl_t ctrl_bits);
static bool button_get_tgl_state(lv_btnm_ctrl_t ctrl_bits);
2017-11-23 21:28:36 +01:00
static uint16_t get_button_from_point(lv_obj_t * btnm, lv_point_t * p);
static void allocate_btn_areas_and_controls(const lv_obj_t * btnm, const char ** map);
static void invalidate_button_area(const lv_obj_t * btnm, uint16_t btn_idx);
static bool maps_are_identical(const char ** map1, const char ** map2);
2016-09-30 13:35:54 +02:00
/**********************
* STATIC VARIABLES
**********************/
2018-06-19 09:49:58 +02:00
static const char * lv_btnm_def_map[] = {"Btn1", "Btn2", "Btn3", "\n",
2019-03-12 06:20:45 +01:00
"Btn4", "Btn5", ""
2018-06-19 09:49:58 +02:00
};
2016-09-30 13:35:54 +02:00
static lv_design_cb_t ancestor_design_f;
static lv_signal_cb_t ancestor_signal;
2016-09-30 13:35:54 +02:00
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/**
* Create a button matrix objects
2016-10-07 11:15:46 +02:00
* @param par pointer to an object, it will be the parent of the new button matrix
* @param copy pointer to a button matrix object, if not NULL then the new object will be copied from it
2016-09-30 13:35:54 +02:00
* @return pointer to the created button matrix
*/
lv_obj_t * lv_btnm_create(lv_obj_t * par, const lv_obj_t * copy)
2016-09-30 13:35:54 +02:00
{
LV_LOG_TRACE("button matrix create started");
2018-07-25 17:57:08 +02:00
2016-09-30 13:35:54 +02:00
/*Create the ancestor object*/
lv_obj_t * new_btnm = lv_obj_create(par, copy);
2017-11-26 11:38:28 +01:00
lv_mem_assert(new_btnm);
if(new_btnm == NULL) return NULL;
2017-11-10 15:01:40 +01:00
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_func(new_btnm);
2018-06-19 09:49:58 +02:00
2016-09-30 13:35:54 +02:00
/*Allocate the object type specific extended data*/
lv_btnm_ext_t * ext = lv_obj_allocate_ext_attr(new_btnm, sizeof(lv_btnm_ext_t));
2017-11-26 11:38:28 +01:00
lv_mem_assert(ext);
if(ext == NULL) return NULL;
2017-11-10 15:01:40 +01:00
ext->btn_cnt = 0;
2019-03-12 06:20:45 +01:00
ext->btn_id_pr = LV_BTNM_BTN_NONE;
2019-03-12 14:29:37 +01:00
ext->btn_id_act = LV_BTNM_BTN_NONE;
2017-10-20 15:37:50 +02:00
ext->button_areas = NULL;
ext->ctrl_bits = NULL;
ext->map_p = NULL;
2019-02-28 06:56:10 +01:00
ext->recolor = 0;
2017-11-20 14:26:18 +01:00
ext->styles_btn[LV_BTN_STATE_REL] = &lv_style_btn_rel;
ext->styles_btn[LV_BTN_STATE_PR] = &lv_style_btn_pr;
ext->styles_btn[LV_BTN_STATE_TGL_REL] = &lv_style_btn_tgl_rel;
ext->styles_btn[LV_BTN_STATE_TGL_PR] = &lv_style_btn_tgl_pr;
ext->styles_btn[LV_BTN_STATE_INA] = &lv_style_btn_ina;
2016-09-30 13:35:54 +02:00
if(ancestor_design_f == NULL) ancestor_design_f = lv_obj_get_design_func(new_btnm);
lv_obj_set_signal_cb(new_btnm, lv_btnm_signal);
lv_obj_set_design_cb(new_btnm, lv_btnm_design);
2016-09-30 13:35:54 +02:00
/*Init the new button matrix object*/
2016-10-07 11:15:46 +02:00
if(copy == NULL) {
2019-02-07 19:17:10 +01:00
lv_obj_set_size(new_btnm, LV_DPI * 3, LV_DPI * 2);
2018-06-19 09:49:58 +02:00
lv_btnm_set_map(new_btnm, lv_btnm_def_map);
/*Set the default styles*/
2018-06-19 09:49:58 +02:00
lv_theme_t * th = lv_theme_get_current();
if(th) {
lv_btnm_set_style(new_btnm, LV_BTNM_STYLE_BG, th->style.btnm.bg);
lv_btnm_set_style(new_btnm, LV_BTNM_STYLE_BTN_REL, th->style.btnm.btn.rel);
lv_btnm_set_style(new_btnm, LV_BTNM_STYLE_BTN_PR, th->style.btnm.btn.pr);
lv_btnm_set_style(new_btnm, LV_BTNM_STYLE_BTN_TGL_REL, th->style.btnm.btn.tgl_rel);
lv_btnm_set_style(new_btnm, LV_BTNM_STYLE_BTN_TGL_PR, th->style.btnm.btn.tgl_pr);
lv_btnm_set_style(new_btnm, LV_BTNM_STYLE_BTN_INA, th->style.btnm.btn.ina);
} else {
lv_obj_set_style(new_btnm, &lv_style_pretty);
}
2016-09-30 13:35:54 +02:00
}
/*Copy an existing object*/
else {
lv_btnm_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
memcpy(ext->styles_btn, copy_ext->styles_btn, sizeof(ext->styles_btn));
lv_btnm_set_map(new_btnm, lv_btnm_get_map(copy));
2016-09-30 13:35:54 +02:00
}
2018-07-25 20:39:24 +02:00
LV_LOG_INFO("button matrix created");
2018-06-19 09:49:58 +02:00
2016-10-07 11:15:46 +02:00
return new_btnm;
2016-09-30 13:35:54 +02:00
}
/*=====================
* Setter functions
*====================*/
/**
* Set a new map. Buttons will be created/deleted according to the map. The
* button matrix keeps a reference to the map and so the string array must not
* be deallocated during the life of the matrix.
2016-10-07 11:15:46 +02:00
* @param btnm pointer to a button matrix object
* @param map pointer a string array. The last string has to be: "". Use "\n" to make a line break.
2016-09-30 13:35:54 +02:00
*/
void lv_btnm_set_map(const lv_obj_t * btnm, const char ** map)
2016-09-30 13:35:54 +02:00
{
2018-06-19 09:49:58 +02:00
if(map == NULL) return;
/*
* lv_btnm_set_map is called on receipt of signals such as
* LV_SIGNAL_CORD_CHG regardless of whether the map has changed (e.g.
* calling lv_obj_align on the map will trigger this).
*
* We check if the map has changed here to avoid overwriting changes made
* to hidden/longpress/disabled states after the map was originally set.
*
* TODO: separate all map set/allocation from layout code below and skip
* set/allocation when map hasn't changed.
*/
2018-06-19 09:49:58 +02:00
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
if (!maps_are_identical(ext->map_p, map)) {
/*Analyze the map and create the required number of buttons*/
allocate_btn_areas_and_controls(btnm, map);
}
ext->map_p = map;
2018-06-19 09:49:58 +02:00
/*Set size and positions of the buttons*/
lv_style_t * style_bg = lv_btnm_get_style(btnm, LV_BTNM_STYLE_BG);
lv_coord_t max_w = lv_obj_get_width(btnm) - style_bg->body.padding.left - style_bg->body.padding.right;
lv_coord_t max_h = lv_obj_get_height(btnm) - style_bg->body.padding.top - style_bg->body.padding.bottom;
lv_coord_t act_y = style_bg->body.padding.top;
2018-06-19 09:49:58 +02:00
/*Count the lines to calculate button height*/
uint8_t line_cnt = 1;
uint8_t li;
for(li = 0; strlen(map[li]) != 0; li++) {
if(strcmp(map[li], "\n") == 0) line_cnt ++;
}
lv_coord_t btn_h = max_h - ((line_cnt - 1) * style_bg->body.padding.inner);
btn_h = btn_h / line_cnt;
btn_h --; /*-1 because e.g. height = 100 means 101 pixels (0..100)*/
/* Count the units and the buttons in a line
* (A button can be 1,2,3... unit wide)*/
uint16_t unit_cnt; /*Number of units in a row*/
uint16_t unit_act_cnt; /*Number of units currently put in a row*/
2018-06-19 09:49:58 +02:00
uint16_t btn_cnt; /*Number of buttons in a row*/
uint16_t i_tot = 0; /*Act. index in the str map*/
uint16_t btn_i = 0; /*Act. index of button areas*/
const char ** map_p_tmp = map;
/*Count the units and the buttons in a line*/
while(1) {
unit_cnt = 0;
btn_cnt = 0;
/*Count the buttons in a line*/
while(strcmp(map_p_tmp[btn_cnt], "\n") != 0 &&
strlen(map_p_tmp[btn_cnt]) != 0) { /*Check a line*/
2019-02-20 03:29:53 +08:00
unit_cnt += get_button_width(ext->ctrl_bits[btn_i + btn_cnt]);
2018-06-19 09:49:58 +02:00
btn_cnt ++;
}
2019-01-06 15:20:06 +01:00
/*Make sure the last row is at the bottom of 'btnm'*/
if(map_p_tmp[btn_cnt][0] == '\0') { /*Last row?*/
btn_h = max_h - act_y + style_bg->body.padding.bottom - 1;
2019-01-06 15:20:06 +01:00
}
2018-12-30 15:58:14 +01:00
2018-06-19 09:49:58 +02:00
/*Only deal with the non empty lines*/
if(btn_cnt != 0) {
/*Calculate the width of all units*/
lv_coord_t all_unit_w = max_w - ((btn_cnt - 1) * style_bg->body.padding.inner);
/*Set the button size and positions and set the texts*/
uint16_t i;
lv_coord_t act_x = style_bg->body.padding.left;
2018-06-19 09:49:58 +02:00
lv_coord_t act_unit_w;
unit_act_cnt = 0;
for(i = 0; i < btn_cnt; i++) {
/* one_unit_w = all_unit_w / unit_cnt
* act_unit_w = one_unit_w * button_width
* do this two operations but the multiply first to divide a greater number */
2019-02-20 03:29:53 +08:00
act_unit_w = (all_unit_w * get_button_width(ext->ctrl_bits[btn_i])) / unit_cnt;
2018-06-19 09:49:58 +02:00
act_unit_w --; /*-1 because e.g. width = 100 means 101 pixels (0..100)*/
/*Always recalculate act_x because of rounding errors */
act_x = (unit_act_cnt * all_unit_w) / unit_cnt + i * style_bg->body.padding.inner + style_bg->body.padding.left;
2018-06-19 09:49:58 +02:00
/* Set the button's area.
* If inner padding is zero then use the prev. button x2 as x1 to avoid rounding errors*/
if(style_bg->body.padding.inner == 0 && act_x != style_bg->body.padding.left) {
2017-12-19 22:00:32 +01:00
lv_area_set(&ext->button_areas[btn_i], ext->button_areas[btn_i - 1].x2, act_y,
2018-06-19 09:49:58 +02:00
act_x + act_unit_w, act_y + btn_h);
} else {
2017-12-19 22:00:32 +01:00
lv_area_set(&ext->button_areas[btn_i], act_x, act_y,
2018-06-19 09:49:58 +02:00
act_x + act_unit_w, act_y + btn_h);
}
2019-02-20 03:29:53 +08:00
unit_act_cnt += get_button_width(ext->ctrl_bits[btn_i]);
2018-06-19 09:49:58 +02:00
i_tot ++;
btn_i ++;
}
}
act_y += btn_h + style_bg->body.padding.inner;
2017-12-20 21:27:39 +01:00
2018-06-19 09:49:58 +02:00
if(strlen(map_p_tmp[btn_cnt]) == 0) break; /*Break on end of map*/
map_p_tmp = &map_p_tmp[btn_cnt + 1]; /*Set the map to the next line*/
i_tot ++; /*Skip the '\n'*/
}
2016-09-30 13:35:54 +02:00
2018-06-19 09:49:58 +02:00
lv_obj_invalidate(btnm);
2016-09-30 13:35:54 +02:00
}
/**
* Set the button control map (hidden, disabled etc.) for a button matrix. The
* control map array will be copied and so may be deallocated after this
* function returns.
* @param btnm pointer to a button matrix object
* @param ctrl_map pointer to an array of `lv_btn_ctrl_t` control bytes. The
* length of the array and position of the elements must match
2019-03-01 07:25:16 +01:00
* the number and order of the individual buttons (i.e. excludes
* newline entries).
* The control bits are:
* - bit 5 : 1 = inactive (disabled)
* - bit 4 : 1 = no repeat (on long press)
* - bit 3 : 1 = hidden
* - bit 2..0: Relative width compared to the buttons in the
* same row. [1..7]
*/
2019-02-27 06:17:37 +01:00
void lv_btnm_set_ctrl_map(const lv_obj_t * btnm, const lv_btnm_ctrl_t * ctrl_map)
{
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
2019-02-27 06:17:37 +01:00
memcpy(ext->ctrl_bits, ctrl_map, sizeof(lv_btnm_ctrl_t) * ext->btn_cnt);
lv_btnm_set_map(btnm, ext->map_p);
}
2016-09-30 13:35:54 +02:00
/**
* Set the pressed button i.e. visually highlight it.
* Mainly used a when the btnm is in a group to show the selected button
* @param btnm pointer to button matrix object
2019-03-12 06:20:45 +01:00
* @param id index of the currently pressed button (`LV_BTNM_BTN_NONE` to unpress)
*/
void lv_btnm_set_pressed(const lv_obj_t * btnm, uint16_t id)
{
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
2019-03-12 06:20:45 +01:00
if (id >= ext->btn_cnt && id != LV_BTNM_BTN_NONE)
return;
if (id == ext->btn_id_pr)
return;
ext->btn_id_pr = id;
lv_obj_invalidate(btnm);
}
2017-08-20 02:16:21 +02:00
/**
* Set a style of a button matrix
* @param btnm pointer to a button matrix object
* @param type which style should be set
* @param style pointer to a style
*/
2018-06-19 09:49:58 +02:00
void lv_btnm_set_style(lv_obj_t * btnm, lv_btnm_style_t type, lv_style_t * style)
{
2018-06-19 09:49:58 +02:00
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
2018-06-19 09:49:58 +02:00
switch(type) {
case LV_BTNM_STYLE_BG:
lv_obj_set_style(btnm, style);
break;
case LV_BTNM_STYLE_BTN_REL:
ext->styles_btn[LV_BTN_STATE_REL] = style;
lv_obj_invalidate(btnm);
break;
case LV_BTNM_STYLE_BTN_PR:
ext->styles_btn[LV_BTN_STATE_PR] = style;
lv_obj_invalidate(btnm);
break;
case LV_BTNM_STYLE_BTN_TGL_REL:
ext->styles_btn[LV_BTN_STATE_TGL_REL] = style;
lv_obj_invalidate(btnm);
break;
case LV_BTNM_STYLE_BTN_TGL_PR:
ext->styles_btn[LV_BTN_STATE_TGL_PR] = style;
lv_obj_invalidate(btnm);
break;
case LV_BTNM_STYLE_BTN_INA:
ext->styles_btn[LV_BTN_STATE_INA] = style;
lv_obj_invalidate(btnm);
break;
}
2016-09-30 13:35:54 +02:00
}
/**
* Enable recoloring of button's texts
* @param btnm pointer to button matrix object
* @param en true: enable recoloring; false: disable
*/
void lv_btnm_set_recolor(const lv_obj_t * btnm, bool en)
{
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
ext->recolor = en;
lv_obj_invalidate(btnm);
}
/**
* Set the attributes of a button of the button matrix
* @param btnm pointer to button matrix object
* @param btn_id 0 based index of the button to modify. (Not counting new lines)
* @param en true: set the attributes; false: clear the attributes
*/
void lv_btnm_set_btn_ctrl(const lv_obj_t * btnm, uint16_t btn_id, lv_btnm_ctrl_t ctrl, bool en)
{
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
if(btn_id >= ext->btn_cnt) return;
if(en) {
ext->ctrl_bits[btn_id] |= ctrl;
} else {
ext->ctrl_bits[btn_id] &= (~ctrl);
}
invalidate_button_area(btnm, btn_id);
2019-03-12 06:20:45 +01:00
}
2019-03-12 06:20:45 +01:00
/**
* Set the attributes of all buttons of a button matrix
* @param btnm pointer to a button matrix object
* @param ctrl attribute(s) to set from `lv_btnm_ctrl_t`. Values can be ORed.
* @param en true: set the attributes; false: clear the attributes
*/
void lv_btnm_set_btn_ctrl_all(lv_obj_t * btnm, lv_btnm_ctrl_t ctrl, bool en)
{
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
uint16_t i;
for(i= 0; i < ext->btn_cnt; i++) {
lv_btnm_set_btn_ctrl(btnm, i, ctrl, en);
}
}
/**
* Set a single buttons relative width.
* This method will cause the matrix be regenerated and is a relatively
* expensive operation. It is recommended that initial width be specified using
* `lv_btnm_set_ctrl_map` and this method only be used for dynamic changes.
* @param btnm pointer to button matrix object
* @param btn_id 0 based index of the button to modify.
* @param width Relative width compared to the buttons in the same row. [1..7]
*/
void lv_btnm_set_btn_width(const lv_obj_t * btnm, uint16_t btn_id, uint8_t width)
{
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
if (btn_id >= ext->btn_cnt) return;
ext->ctrl_bits[btn_id] &= (~LV_BTNM_WIDTH_MASK);
ext->ctrl_bits[btn_id] |= (LV_BTNM_WIDTH_MASK & width);
lv_btnm_set_map(btnm, ext->map_p);
}
2016-09-30 13:35:54 +02:00
/*=====================
* Getter functions
*====================*/
2016-10-04 15:29:52 +02:00
/**
* Get the current map of a button matrix
2016-10-07 11:15:46 +02:00
* @param btnm pointer to a button matrix object
2016-10-04 15:29:52 +02:00
* @return the current map
*/
const char ** lv_btnm_get_map(const lv_obj_t * btnm)
2016-10-04 15:29:52 +02:00
{
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
2018-06-19 09:49:58 +02:00
return ext->map_p;
2016-10-04 15:29:52 +02:00
}
/**
* Check whether the button's text can use recolor or not
* @param btnm pointer to button matrix object
* @return true: text recolor enable; false: disabled
*/
bool lv_btnm_get_recolor(const lv_obj_t * btnm)
{
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
return ext->recolor;
}
/**
* Get the index of the lastly "activated" button by the user (pressed, released etc)
* Useful in the the `event_cb` to get the text of the button, check if hidden etc.
* @param btnm pointer to button matrix object
2019-03-12 06:20:45 +01:00
* @return index of the last released button (LV_BTNM_BTN_NONE: if unset)
2016-10-04 15:29:52 +02:00
*/
uint16_t lv_btnm_get_active_btn(const lv_obj_t * btnm)
2016-10-04 15:29:52 +02:00
{
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
2019-03-12 06:20:45 +01:00
return ext->btn_id_act;
2016-10-04 15:29:52 +02:00
}
2019-03-12 06:20:45 +01:00
/**
* Get the text of the lastly "activated" button by the user (pressed, released etc)
* Useful in the the `event_cb`
* @param btnm pointer to button matrix object
* @return text of the last released button (NULL: if unset)
*/
const char * lv_btnm_get_active_btn_text(const lv_obj_t * btnm)
{
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
if(ext->btn_id_act != LV_BTNM_BTN_NONE) {
return lv_btnm_get_btn_text(btnm, ext->btn_id_act);
} else {
return NULL;
}
}
2018-11-19 07:00:17 +01:00
/**
* Get the pressed button's index.
* The button be really pressed by the user or manually set to pressed with `lv_btnm_set_pressed`
2018-11-19 07:00:17 +01:00
* @param btnm pointer to button matrix object
2019-03-12 06:20:45 +01:00
* @return index of the pressed button (LV_BTNM_BTN_NONE: if unset)
2018-11-19 07:00:17 +01:00
*/
uint16_t lv_btnm_get_pressed_btn(const lv_obj_t * btnm)
2018-11-19 07:00:17 +01:00
{
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
return ext->btn_id_pr;
}
2016-09-30 13:35:54 +02:00
/**
* Get the button's text
* @param btnm pointer to button matrix object
* @param btn_id the index a button not counting new line characters. (The return value of lv_btnm_get_pressed/released)
* @return text of btn_index` button
*/
2019-03-12 06:20:45 +01:00
const char * lv_btnm_get_btn_text(const lv_obj_t * btnm, uint16_t btn_id)
{
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
2019-03-12 06:20:45 +01:00
if(btn_id > ext->btn_cnt) return NULL;
uint16_t txt_i = 0;
uint16_t btn_i = 0;
/* Search the text of ext->btn_pr the buttons text in the map
* Skip "\n"-s*/
while(btn_i != btn_id) {
btn_i ++;
txt_i ++;
if(strcmp(ext->map_p[txt_i], "\n") == 0) txt_i ++;
}
2019-03-12 06:20:45 +01:00
if(btn_i == ext->btn_cnt) return NULL;
2019-03-12 06:20:45 +01:00
return ext->map_p[txt_i];
}
/**
* Get the whether a control value is enabled or disabled for button of a button matrix
* @param btnm pointer to a button matrix object
* @param btn_id the index a button not counting new line characters. (E.g. the return value of lv_btnm_get_pressed/released)
* @param ctrl control values to check (ORed value can be used)
* @return true: long press repeat is disabled; false: long press repeat enabled
*/
bool lv_btnm_get_btn_ctrl(lv_obj_t * btnm, uint16_t btn_id, lv_btnm_ctrl_t ctrl)
{
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
if(btn_id >= ext->btn_cnt) return false;
return ext->ctrl_bits[btn_id] & ctrl ? true : false;
2018-06-19 09:49:58 +02:00
}
/**
* Get a style of a button matrix
* @param btnm pointer to a button matrix object
* @param type which style should be get
* @return style pointer to a style
*/
lv_style_t * lv_btnm_get_style(const lv_obj_t * btnm, lv_btnm_style_t type)
{
lv_style_t * style = NULL;
2018-06-19 09:49:58 +02:00
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
switch(type) {
case LV_BTNM_STYLE_BG:
style = lv_obj_get_style(btnm);
break;
2018-06-19 09:49:58 +02:00
case LV_BTNM_STYLE_BTN_REL:
2018-11-07 17:48:50 +01:00
style = ext->styles_btn[LV_BTN_STATE_REL];
break;
2018-06-19 09:49:58 +02:00
case LV_BTNM_STYLE_BTN_PR:
style = ext->styles_btn[LV_BTN_STATE_PR];
break;
2018-06-19 09:49:58 +02:00
case LV_BTNM_STYLE_BTN_TGL_REL:
style = ext->styles_btn[LV_BTN_STATE_TGL_REL];
break;
2018-06-19 09:49:58 +02:00
case LV_BTNM_STYLE_BTN_TGL_PR:
style = ext->styles_btn[LV_BTN_STATE_TGL_PR];
break;
2018-06-19 09:49:58 +02:00
case LV_BTNM_STYLE_BTN_INA:
style = ext->styles_btn[LV_BTN_STATE_INA];
break;
2018-06-19 09:49:58 +02:00
default:
style = NULL;
break;
}
2016-09-30 13:35:54 +02:00
return style;
2016-09-30 13:35:54 +02:00
}
/**********************
* STATIC FUNCTIONS
**********************/
/**
* Handle the drawing related tasks of the button matrixs
* @param btnm pointer to a button matrix object
2016-09-30 13:35:54 +02:00
* @param mask 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
2016-09-30 13:35:54 +02:00
* @param return true/false, depends on 'mode'
*/
2017-11-23 21:28:36 +01:00
static bool lv_btnm_design(lv_obj_t * btnm, const lv_area_t * mask, lv_design_mode_t mode)
2016-09-30 13:35:54 +02:00
{
if(mode == LV_DESIGN_COVER_CHK) {
return ancestor_design_f(btnm, mask, mode);
2018-06-19 09:49:58 +02:00
/*Return false if the object is not covers the mask_p area*/
2016-09-30 13:35:54 +02:00
}
/*Draw the object*/
2018-06-19 09:49:58 +02:00
else if(mode == LV_DESIGN_DRAW_MAIN) {
2018-06-19 09:49:58 +02:00
ancestor_design_f(btnm, mask, mode);
2018-06-19 09:49:58 +02:00
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
2017-10-30 17:11:56 +01:00
lv_style_t * bg_style = lv_obj_get_style(btnm);
lv_style_t * btn_style;
2018-06-14 13:08:19 +02:00
lv_opa_t opa_scale = lv_obj_get_opa_scale(btnm);
2018-06-19 09:49:58 +02:00
lv_area_t area_btnm;
2017-11-21 10:35:57 +01:00
lv_obj_get_coords(btnm, &area_btnm);
2018-06-19 09:49:58 +02:00
lv_area_t area_tmp;
lv_coord_t btn_w;
lv_coord_t btn_h;
2018-06-19 09:49:58 +02:00
uint16_t btn_i = 0;
uint16_t txt_i = 0;
2018-12-26 08:24:18 +01:00
lv_style_t style_tmp;
lv_txt_flag_t txt_flag = LV_TXT_FLAG_NONE;
2018-12-26 08:24:18 +01:00
if(ext->recolor) txt_flag = LV_TXT_FLAG_RECOLOR;
2018-06-19 09:49:58 +02:00
for(btn_i = 0; btn_i < ext->btn_cnt; btn_i ++, txt_i ++) {
/*Search the next valid text in the map*/
2018-12-26 08:24:18 +01:00
while(strcmp(ext->map_p[txt_i], "\n") == 0) {
txt_i ++;
}
2017-10-12 16:59:51 +02:00
/*Skip hidden buttons*/
2019-02-20 03:29:53 +08:00
if(button_is_hidden(ext->ctrl_bits[btn_i])) continue;
2018-06-19 09:49:58 +02:00
lv_area_copy(&area_tmp, &ext->button_areas[btn_i]);
area_tmp.x1 += area_btnm.x1;
area_tmp.y1 += area_btnm.y1;
area_tmp.x2 += area_btnm.x1;
area_tmp.y2 += area_btnm.y1;
2018-06-19 09:49:58 +02:00
btn_w = lv_area_get_width(&area_tmp);
btn_h = lv_area_get_height(&area_tmp);
2018-06-19 09:49:58 +02:00
/*Load the style*/
bool tgl_state = button_get_tgl_state(ext->ctrl_bits[btn_i]);
2019-02-20 03:29:53 +08:00
if(button_is_inactive(ext->ctrl_bits[btn_i])) btn_style = lv_btnm_get_style(btnm, LV_BTNM_STYLE_BTN_INA);
else if(btn_i != ext->btn_id_pr && tgl_state == false) btn_style = lv_btnm_get_style(btnm, LV_BTNM_STYLE_BTN_REL);
else if(btn_i == ext->btn_id_pr && tgl_state == false) btn_style = lv_btnm_get_style(btnm, LV_BTNM_STYLE_BTN_PR);
else if(btn_i != ext->btn_id_pr && tgl_state == true) btn_style = lv_btnm_get_style(btnm, LV_BTNM_STYLE_BTN_TGL_REL);
else if(btn_i == ext->btn_id_pr && tgl_state == true) btn_style = lv_btnm_get_style(btnm, LV_BTNM_STYLE_BTN_TGL_PR);
2017-12-02 20:43:50 +01:00
else btn_style = lv_btnm_get_style(btnm, LV_BTNM_STYLE_BTN_REL); /*Not possible option, just to be sure*/
2017-11-21 10:35:57 +01:00
2018-12-26 08:24:18 +01:00
lv_style_copy(&style_tmp, btn_style);
/*Remove borders on the edges if `LV_BORDER_INTERNAL`*/
if(style_tmp.body.border.part & LV_BORDER_INTERNAL) {
if(area_tmp.y1 == btnm->coords.y1 + bg_style->body.padding.top) {
2018-12-26 08:24:18 +01:00
style_tmp.body.border.part &= ~LV_BORDER_TOP;
}
if(area_tmp.y2 == btnm->coords.y2 - bg_style->body.padding.bottom) {
2018-12-26 08:24:18 +01:00
style_tmp.body.border.part &= ~LV_BORDER_BOTTOM;
}
if(txt_i == 0) {
style_tmp.body.border.part &= ~LV_BORDER_LEFT;
}
else if(strcmp(ext->map_p[txt_i - 1],"\n") == 0) {
style_tmp.body.border.part &= ~LV_BORDER_LEFT;
}
if(ext->map_p[txt_i + 1][0] == '\0' || strcmp(ext->map_p[txt_i + 1], "\n") == 0) {
style_tmp.body.border.part &= ~LV_BORDER_RIGHT;
}
}
lv_draw_rect(&area_tmp, mask, &style_tmp, opa_scale);
2017-11-21 10:35:57 +01:00
2018-06-19 09:49:58 +02:00
/*Calculate the size of the text*/
if(btn_style->glass) btn_style = bg_style;
2018-06-19 09:49:58 +02:00
const lv_font_t * font = btn_style->text.font;
lv_point_t txt_size;
lv_txt_get_size(&txt_size, ext->map_p[txt_i], font,
btn_style->text.letter_space, btn_style->text.line_space,
lv_area_get_width(&area_btnm), txt_flag);
2018-06-19 09:49:58 +02:00
area_tmp.x1 += (btn_w - txt_size.x) / 2;
area_tmp.y1 += (btn_h - txt_size.y) / 2;
area_tmp.x2 = area_tmp.x1 + txt_size.x;
area_tmp.y2 = area_tmp.y1 + txt_size.y;
lv_draw_label(&area_tmp, mask, btn_style, opa_scale, ext->map_p[txt_i], txt_flag, NULL);
2018-06-19 09:49:58 +02:00
}
}
2016-09-30 13:35:54 +02:00
return true;
}
2017-11-10 15:01:40 +01:00
/**
* Signal function of the button matrix
* @param btnm pointer to a button matrix 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_btnm_signal(lv_obj_t * btnm, lv_signal_t sign, void * param)
{
lv_res_t res;
/* Include the ancient signal function */
res = ancestor_signal(btnm, sign, param);
if(res != LV_RES_OK) return res;
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
2017-11-23 21:28:36 +01:00
lv_point_t p;
2017-11-10 15:01:40 +01:00
if(sign == LV_SIGNAL_CLEANUP) {
2017-11-23 21:28:36 +01:00
lv_mem_free(ext->button_areas);
lv_mem_free(ext->ctrl_bits);
} else if(sign == LV_SIGNAL_STYLE_CHG || sign == LV_SIGNAL_CORD_CHG) {
2017-11-10 15:01:40 +01:00
lv_btnm_set_map(btnm, ext->map_p);
}
else if(sign == LV_SIGNAL_PRESSED) {
2019-03-12 14:29:37 +01:00
lv_indev_t * indev = lv_indev_get_act();
if(lv_indev_get_type(indev) == LV_INDEV_TYPE_POINTER || lv_indev_get_type(indev) == LV_INDEV_TYPE_BUTTON) {
2019-03-12 14:29:37 +01:00
uint16_t btn_pr;
/*Search the pressed area*/
lv_indev_get_point(param, &p);
btn_pr = get_button_from_point(btnm, &p);
invalidate_button_area(btnm, ext->btn_id_pr) /*Invalidate the old area*/;
ext->btn_id_pr = btn_pr;
ext->btn_id_act = btn_pr;
invalidate_button_area(btnm, ext->btn_id_pr); /*Invalidate the new area*/
}
if(ext->btn_id_act != LV_BTNM_BTN_NONE) {
if(button_is_click_trig(ext->ctrl_bits[ext->btn_id_act]) == false &&
button_is_inactive(ext->ctrl_bits[ext->btn_id_act]) == false &&
button_is_hidden(ext->ctrl_bits[ext->btn_id_act]) == false)
{
lv_event_send(btnm, LV_EVENT_SELECTED, lv_btnm_get_active_btn_text(btnm));
}
}
}
else if(sign == LV_SIGNAL_PRESSING) {
2017-11-10 15:01:40 +01:00
uint16_t btn_pr;
/*Search the pressed area*/
lv_indev_get_point(param, &p);
btn_pr = get_button_from_point(btnm, &p);
/*Invalidate to old and the new areas*/;
2017-11-16 10:20:30 +01:00
if(btn_pr != ext->btn_id_pr) {
lv_indev_reset_long_press(param);
2019-03-12 06:20:45 +01:00
if(ext->btn_id_pr != LV_BTNM_BTN_NONE) {
invalidate_button_area(btnm, ext->btn_id_pr);
2017-11-10 15:01:40 +01:00
}
2019-03-12 06:20:45 +01:00
if(btn_pr != LV_BTNM_BTN_NONE) {
if(btn_pr != LV_BTNM_BTN_NONE) lv_event_send(btnm, LV_EVENT_SELECTED, ext->map_p[btn_pr]);
invalidate_button_area(btnm, btn_pr);
2017-11-10 15:01:40 +01:00
}
}
2017-11-16 10:20:30 +01:00
ext->btn_id_pr = btn_pr;
2019-03-15 05:10:43 +01:00
ext->btn_id_act = btn_pr;
2017-11-10 15:01:40 +01:00
}
else if(sign == LV_SIGNAL_RELEASED) {
2019-03-12 06:20:45 +01:00
if(ext->btn_id_pr != LV_BTNM_BTN_NONE) {
/*Toggle the button if enabled*/
if(button_is_tgl_enabled(ext->ctrl_bits[ext->btn_id_pr])) {
if(button_get_tgl_state(ext->ctrl_bits[ext->btn_id_pr])) {
ext->ctrl_bits[ext->btn_id_pr] &= (~LV_BTNM_CTRL_TGL_STATE);
} else {
ext->ctrl_bits[ext->btn_id_pr] |= LV_BTNM_CTRL_TGL_STATE;
2017-11-10 15:01:40 +01:00
}
}
2018-12-26 08:24:18 +01:00
2019-03-15 05:10:43 +01:00
/*Invalidate to old pressed area*/;
invalidate_button_area(btnm, ext->btn_id_pr);
#if LV_USE_GROUP
/*Leave the clicked button when releases if this not the focused object in a group*/
lv_group_t * g = lv_obj_get_group(btnm);
if(lv_group_get_focused(g) != btnm) {
2019-03-12 06:20:45 +01:00
ext->btn_id_pr = LV_BTNM_BTN_NONE;
2018-07-12 23:38:27 +02:00
}
#else
2019-03-12 06:20:45 +01:00
ext->btn_id_pr = LV_BTNM_BTN_NONE;
#endif
if(button_is_click_trig(ext->ctrl_bits[ext->btn_id_act]) == true &&
button_is_inactive(ext->ctrl_bits[ext->btn_id_act]) == false &&
button_is_hidden(ext->ctrl_bits[ext->btn_id_act]) == false)
{
lv_event_send(btnm, LV_EVENT_SELECTED, lv_btnm_get_active_btn_text(btnm));
}
}
}
else if(sign == LV_SIGNAL_LONG_PRESS_REP) {
if(ext->btn_id_act != LV_BTNM_BTN_NONE) {
if(button_is_repeat_disabled(ext->ctrl_bits[ext->btn_id_act]) == false &&
button_is_inactive(ext->ctrl_bits[ext->btn_id_act]) == false &&
button_is_hidden(ext->ctrl_bits[ext->btn_id_act]) == false)
{
lv_event_send(btnm, LV_EVENT_SELECTED, lv_btnm_get_active_btn_text(btnm));
}
2017-11-10 15:01:40 +01:00
}
}
else if(sign == LV_SIGNAL_PRESS_LOST || sign == LV_SIGNAL_DEFOCUS) {
2019-03-12 06:20:45 +01:00
ext->btn_id_pr = LV_BTNM_BTN_NONE;
ext->btn_id_act = LV_BTNM_BTN_NONE;
2017-11-10 15:01:40 +01:00
lv_obj_invalidate(btnm);
}
else if(sign == LV_SIGNAL_FOCUS) {
#if LV_USE_GROUP
lv_indev_t * indev = lv_indev_get_act();
2018-09-26 14:21:39 +02:00
lv_hal_indev_type_t indev_type = lv_indev_get_type(indev);
if(indev_type == LV_INDEV_TYPE_POINTER) {
/*Select the clicked button*/
lv_point_t p1;
lv_indev_get_point(indev, &p1);
uint16_t btn_i = get_button_from_point(btnm, &p1);
ext->btn_id_pr = btn_i;
2019-03-12 14:29:37 +01:00
2018-10-05 17:22:49 +02:00
} else if(indev_type == LV_INDEV_TYPE_ENCODER) {
2018-09-26 14:21:39 +02:00
/*In navigation mode don't select any button but in edit mode select the fist*/
if(lv_group_get_editing(lv_obj_get_group(btnm))) ext->btn_id_pr = 0;
2019-03-12 06:20:45 +01:00
else ext->btn_id_pr = LV_BTNM_BTN_NONE;
} else {
ext->btn_id_pr = 0;
}
2018-07-13 00:37:28 +02:00
#else
2017-11-16 10:20:30 +01:00
ext->btn_id_pr = 0;
2018-07-13 00:37:28 +02:00
#endif
2019-03-12 14:29:37 +01:00
ext->btn_id_act = ext->btn_id_pr;
2017-11-10 15:01:40 +01:00
lv_obj_invalidate(btnm);
}
else if(sign == LV_SIGNAL_CONTROL) {
2018-06-19 09:49:58 +02:00
char c = *((char *)param);
if(c == LV_GROUP_KEY_RIGHT) {
2019-03-12 06:20:45 +01:00
if(ext->btn_id_pr == LV_BTNM_BTN_NONE) ext->btn_id_pr = 0;
2017-11-16 10:20:30 +01:00
else ext->btn_id_pr++;
if(ext->btn_id_pr >= ext->btn_cnt - 1) ext->btn_id_pr = ext->btn_cnt - 1;
2019-03-12 14:29:37 +01:00
ext->btn_id_act = ext->btn_id_pr;
2017-11-10 15:01:40 +01:00
lv_obj_invalidate(btnm);
2019-03-12 06:20:45 +01:00
}
else if(c == LV_GROUP_KEY_LEFT) {
if(ext->btn_id_pr == LV_BTNM_BTN_NONE) ext->btn_id_pr = 0;
2017-11-16 10:20:30 +01:00
if(ext->btn_id_pr > 0) ext->btn_id_pr--;
2019-03-12 14:29:37 +01:00
ext->btn_id_act = ext->btn_id_pr;
2017-11-10 15:01:40 +01:00
lv_obj_invalidate(btnm);
2019-03-12 06:20:45 +01:00
}
else if(c == LV_GROUP_KEY_DOWN) {
lv_style_t * style = lv_btnm_get_style(btnm, LV_BTNM_STYLE_BG);
/*Find the area below the the current*/
2019-03-12 06:20:45 +01:00
if(ext->btn_id_pr == LV_BTNM_BTN_NONE) {
ext->btn_id_pr = 0;
} else {
uint16_t area_below;
lv_coord_t pr_center = ext->button_areas[ext->btn_id_pr].x1 + (lv_area_get_width(&ext->button_areas[ext->btn_id_pr]) >> 1);
for(area_below = ext->btn_id_pr; area_below < ext->btn_cnt; area_below ++) {
if(ext->button_areas[area_below].y1 > ext->button_areas[ext->btn_id_pr].y1 &&
2018-06-19 09:49:58 +02:00
pr_center >= ext->button_areas[area_below].x1 &&
pr_center <= ext->button_areas[area_below].x2 + style->body.padding.left) {
break;
}
}
if(area_below < ext->btn_cnt) ext->btn_id_pr = area_below;
}
2019-03-12 14:29:37 +01:00
ext->btn_id_act = ext->btn_id_pr;
lv_obj_invalidate(btnm);
2019-03-12 06:20:45 +01:00
}
else if(c == LV_GROUP_KEY_UP) {
lv_style_t * style = lv_btnm_get_style(btnm, LV_BTNM_STYLE_BG);
/*Find the area below the the current*/
2019-03-12 06:20:45 +01:00
if(ext->btn_id_pr == LV_BTNM_BTN_NONE) {
ext->btn_id_pr = 0;
} else {
int16_t area_above;
lv_coord_t pr_center = ext->button_areas[ext->btn_id_pr].x1 + (lv_area_get_width(&ext->button_areas[ext->btn_id_pr]) >> 1);
for(area_above = ext->btn_id_pr; area_above >= 0; area_above --) {
if(ext->button_areas[area_above].y1 < ext->button_areas[ext->btn_id_pr].y1 &&
pr_center >= ext->button_areas[area_above].x1 - style->body.padding.left &&
2018-06-19 09:49:58 +02:00
pr_center <= ext->button_areas[area_above].x2) {
break;
}
}
if(area_above >= 0) ext->btn_id_pr = area_above;
}
2019-03-12 14:29:37 +01:00
ext->btn_id_act = ext->btn_id_pr;
lv_obj_invalidate(btnm);
2018-02-28 15:37:41 +01:00
}
}
else if(sign == LV_SIGNAL_GET_EDITABLE) {
bool * editable = (bool *)param;
*editable = true;
}
else if(sign == LV_SIGNAL_GET_TYPE) {
lv_obj_type_t * buf = param;
uint8_t i;
for(i = 0; i < LV_MAX_ANCESTOR_NUM - 1; i++) { /*Find the last set data*/
if(buf->type[i] == NULL) break;
}
buf->type[i] = "lv_btnm";
2017-11-10 15:01:40 +01:00
}
2017-11-10 15:01:40 +01:00
return res;
}
2016-09-30 13:35:54 +02:00
/**
* Create the required number of buttons and control bytes according to a map
2016-10-07 11:15:46 +02:00
* @param btnm pointer to button matrix object
2016-09-30 13:35:54 +02:00
* @param map_p pointer to a string array
*/
static void allocate_btn_areas_and_controls(const lv_obj_t * btnm, const char ** map)
2016-09-30 13:35:54 +02:00
{
2018-06-19 09:49:58 +02:00
/*Count the buttons in the map*/
uint16_t btn_cnt = 0;
uint16_t i = 0;
while(strlen(map[i]) != 0) {
if(strcmp(map[i], "\n") != 0) { /*Do not count line breaks*/
btn_cnt ++;
}
i++;
}
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
if(ext->button_areas != NULL) {
lv_mem_free(ext->button_areas);
ext->button_areas = NULL;
}
if(ext->ctrl_bits != NULL) {
lv_mem_free(ext->ctrl_bits);
ext->ctrl_bits = NULL;
}
2018-06-19 09:49:58 +02:00
ext->button_areas = lv_mem_alloc(sizeof(lv_area_t) * btn_cnt);
lv_mem_assert(ext->button_areas);
2019-02-27 06:17:37 +01:00
ext->ctrl_bits = lv_mem_alloc(sizeof(lv_btnm_ctrl_t) * btn_cnt);
lv_mem_assert(ext->ctrl_bits);
if(ext->button_areas == NULL || ext->ctrl_bits == NULL) btn_cnt = 0;
2019-03-12 06:20:45 +01:00
memset(ext->ctrl_bits, 0, sizeof(lv_btnm_ctrl_t) * btn_cnt);
2018-06-19 09:49:58 +02:00
ext->btn_cnt = btn_cnt;
2016-09-30 13:35:54 +02:00
}
/**
* Get the width of a button in units (default is 1).
* @param ctrl_bits least significant 3 bits used (1..7 valid values)
* @return the width of the button in units
*/
2019-02-27 06:17:37 +01:00
static uint8_t get_button_width(lv_btnm_ctrl_t ctrl_bits)
{
2019-03-12 06:20:45 +01:00
uint8_t w = ctrl_bits & LV_BTNM_WIDTH_MASK;
return w != 0 ? w: 1;
2017-10-20 15:37:50 +02:00
}
2019-02-27 06:17:37 +01:00
static bool button_is_hidden(lv_btnm_ctrl_t ctrl_bits)
2017-10-20 15:37:50 +02:00
{
return ctrl_bits & LV_BTNM_CTRL_HIDDEN ? true : false;
2017-10-20 15:37:50 +02:00
}
2019-02-27 06:17:37 +01:00
static bool button_is_repeat_disabled(lv_btnm_ctrl_t ctrl_bits)
{
return ctrl_bits & LV_BTNM_CTRL_NO_REPEAT ? true : false;
}
2019-02-27 06:17:37 +01:00
static bool button_is_inactive(lv_btnm_ctrl_t ctrl_bits)
{
return ctrl_bits & LV_BTNM_CTRL_INACTIVE ? true : false;
}
static bool button_is_click_trig(lv_btnm_ctrl_t ctrl_bits)
{
return ctrl_bits & LV_BTNM_CTRL_CLICK_TRIG ? true : false;
}
static bool button_is_tgl_enabled(lv_btnm_ctrl_t ctrl_bits)
{
return ctrl_bits & LV_BTNM_CTRL_TGL_ENABLE ? true : false;
}
static bool button_get_tgl_state(lv_btnm_ctrl_t ctrl_bits)
2017-10-20 15:37:50 +02:00
{
return ctrl_bits & LV_BTNM_CTRL_TGL_STATE ? true : false;
2017-10-20 15:37:50 +02:00
}
/**
* Gives the button id of a button under a given point
* @param btnm pointer to a button matrix object
* @param p a point with absolute coordinates
2019-03-12 06:20:45 +01:00
* @return the id of the button or LV_BTNM_BTN_NONE.
*/
2017-11-23 21:28:36 +01:00
static uint16_t get_button_from_point(lv_obj_t * btnm, lv_point_t * p)
{
2017-11-23 21:28:36 +01:00
lv_area_t btnm_cords;
lv_area_t btn_area;
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
uint16_t i;
lv_obj_get_coords(btnm, &btnm_cords);
2017-11-10 15:01:40 +01:00
for(i = 0; i < ext->btn_cnt; i++) {
2017-11-28 16:15:13 +01:00
lv_area_copy(&btn_area, &ext->button_areas[i]);
btn_area.x1 += btnm_cords.x1;
btn_area.y1 += btnm_cords.y1;
btn_area.x2 += btnm_cords.x1;
btn_area.y2 += btnm_cords.y1;
2017-11-23 21:28:36 +01:00
if(lv_area_is_point_on(&btn_area, p) != false) {
break;
}
}
2019-03-12 06:20:45 +01:00
if(i == ext->btn_cnt) i = LV_BTNM_BTN_NONE;
return i;
}
static void invalidate_button_area(const lv_obj_t * btnm, uint16_t btn_idx)
{
2019-03-17 04:55:18 +01:00
if(btn_idx == LV_BTNM_BTN_NONE) return;
lv_area_t btn_area;
lv_area_t btnm_area;
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
lv_area_copy(&btn_area, &ext->button_areas[btn_idx]);
lv_obj_get_coords(btnm, &btnm_area);
/* Convert relative coordinates to absolute */
btn_area.x1 += btnm_area.x1;
btn_area.y1 += btnm_area.y1;
btn_area.x2 += btnm_area.x1;
btn_area.y2 += btnm_area.y1;
2019-02-27 06:17:37 +01:00
lv_inv_area(lv_obj_get_disp(btnm), &btn_area);
}
/**
* Compares two button matrix maps for equality
* @param map1 map to compare
* @param map2 map to compare
* @return true if maps are identical in length and content
*/
static bool maps_are_identical(const char ** map1, const char ** map2)
{
if (map1 == map2)
return true;
if (map1 == NULL || map2 == NULL)
return map1 == map2;
uint16_t i = 0;
while (map1[i][0] != '\0' && map2[i][0] != '\0') {
if (strcmp(map1[i], map2[i]) !=0 )
return false;
2019-02-27 06:17:37 +01:00
i++;
}
return map1[i][0] == '\0' && map2[i][0] == '\0';
}
2016-09-30 13:35:54 +02:00
#endif