mirror of
https://github.com/lvgl/lvgl.git
synced 2024-11-24 10:22:53 +08:00
669 lines
22 KiB
C
669 lines
22 KiB
C
/**
|
|
* @file lv_btnm.c
|
|
*
|
|
*/
|
|
|
|
/*********************
|
|
* INCLUDES
|
|
*********************/
|
|
#include "lv_conf.h"
|
|
#if USE_LV_BTNM != 0
|
|
|
|
#include "lv_btnm.h"
|
|
#include "../lv_obj/lv_group.h"
|
|
#include "../lv_draw/lv_draw.h"
|
|
#include "../lv_obj/lv_refr.h"
|
|
#include "misc/gfx/text.h"
|
|
|
|
/*********************
|
|
* DEFINES
|
|
*********************/
|
|
#define LV_BTNM_PR_NONE 0xFFFF
|
|
|
|
/**********************
|
|
* TYPEDEFS
|
|
**********************/
|
|
|
|
/**********************
|
|
* STATIC PROTOTYPES
|
|
**********************/
|
|
static bool lv_btnm_design(lv_obj_t * btnm, const area_t * mask, lv_design_mode_t mode);
|
|
static uint8_t get_button_width(const char * btn_str);
|
|
static bool button_is_hidden(const char * btn_str);
|
|
static bool button_is_repeat_disabled(const char * btn_str);
|
|
static bool button_is_inactive(const char * btn_str);
|
|
const char * cut_ctrl_byte(const char * btn_str);
|
|
static uint16_t get_button_from_point(lv_obj_t * btnm, point_t * p);
|
|
static uint16_t get_button_text(lv_obj_t * btnm, uint16_t btn_id);
|
|
static void create_buttons(lv_obj_t * btnm, const char ** map);
|
|
|
|
/**********************
|
|
* STATIC VARIABLES
|
|
**********************/
|
|
static const char * lv_btnm_def_map[] = {"Btn1","Btn2", "Btn3","\n",
|
|
"\002Btn4","Btn5", ""};
|
|
|
|
static lv_design_func_t ancestor_design_f;
|
|
|
|
/**********************
|
|
* MACROS
|
|
**********************/
|
|
|
|
/**********************
|
|
* GLOBAL FUNCTIONS
|
|
**********************/
|
|
|
|
/*-----------------
|
|
* Create function
|
|
*-----------------*/
|
|
|
|
/**
|
|
* Create a button matrix objects
|
|
* @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
|
|
* @return pointer to the created button matrix
|
|
*/
|
|
lv_obj_t * lv_btnm_create(lv_obj_t * par, lv_obj_t * copy)
|
|
{
|
|
/*Create the ancestor object*/
|
|
lv_obj_t * new_btnm = lv_obj_create(par, copy);
|
|
dm_assert(new_btnm);
|
|
|
|
/*Allocate the object type specific extended data*/
|
|
lv_btnm_ext_t * ext = lv_obj_allocate_ext_attr(new_btnm, sizeof(lv_btnm_ext_t));
|
|
dm_assert(ext);
|
|
ext->button_cnt = 0;
|
|
ext->button_id_pressed = LV_BTNM_PR_NONE;
|
|
ext->button_id_toggled = LV_BTNM_PR_NONE;
|
|
ext->button_areas = NULL;
|
|
ext->action = NULL;
|
|
ext->map_p = NULL;
|
|
ext->toggle = 0;
|
|
ext->styles_btn[LV_BTN_STATE_RELEASED] = &lv_style_btn_off_released;
|
|
ext->styles_btn[LV_BTN_STATE_PRESSED] = &lv_style_btn_off_pressed;
|
|
ext->styles_btn[LV_BTN_STATE_TGL_RELEASED] = &lv_style_btn_on_released;
|
|
ext->styles_btn[LV_BTN_STATE_TGL_PRESSED] = &lv_style_btn_on_pressed;
|
|
ext->styles_btn[LV_BTN_STATE_INACTIVE] = &lv_style_btn_inactive;
|
|
|
|
if(ancestor_design_f == NULL) ancestor_design_f = lv_obj_get_design_func(new_btnm);
|
|
|
|
lv_obj_set_signal_func(new_btnm, lv_btnm_signal);
|
|
lv_obj_set_design_func(new_btnm, lv_btnm_design);
|
|
|
|
/*Init the new button matrix object*/
|
|
if(copy == NULL) {
|
|
lv_obj_set_size(new_btnm, LV_HOR_RES, LV_VER_RES / 2);
|
|
lv_obj_set_style(new_btnm, &lv_style_pretty);
|
|
lv_btnm_set_map(new_btnm, lv_btnm_def_map);
|
|
}
|
|
/*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));
|
|
ext->action = copy_ext->action;
|
|
lv_btnm_set_map(new_btnm, lv_btnm_get_map(copy));
|
|
}
|
|
|
|
return new_btnm;
|
|
}
|
|
|
|
/**
|
|
* 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 true: the object is still valid (not deleted), false: the object become invalid
|
|
*/
|
|
bool lv_btnm_signal(lv_obj_t * btnm, lv_signal_t sign, void * param)
|
|
{
|
|
bool valid;
|
|
|
|
/* Include the ancient signal function */
|
|
valid = lv_obj_signal(btnm, sign, param);
|
|
|
|
/* The object can be deleted so check its validity and then
|
|
* make the object specific signal handling */
|
|
if(valid != false) {
|
|
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
|
area_t btnm_area;
|
|
area_t btn_area;
|
|
point_t p;
|
|
if(sign == LV_SIGNAL_CLEANUP) {
|
|
dm_free(ext->button_areas);
|
|
}
|
|
else if(sign == LV_SIGNAL_STYLE_CHG || sign == LV_SIGNAL_CORD_CHG) {
|
|
lv_btnm_set_map(btnm, ext->map_p);
|
|
}
|
|
else if(sign == LV_SIGNAL_PRESSING) {
|
|
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*/;
|
|
lv_obj_get_coords(btnm, &btnm_area);
|
|
if(btn_pr != ext->button_id_pressed) {
|
|
lv_indev_reset_lpr(param);
|
|
if(ext->button_id_pressed != LV_BTNM_PR_NONE) {
|
|
area_cpy(&btn_area, &ext->button_areas[ext->button_id_pressed]);
|
|
btn_area.x1 += btnm_area.x1;
|
|
btn_area.y1 += btnm_area.y1;
|
|
btn_area.x2 += btnm_area.x1;
|
|
btn_area.y2 += btnm_area.y1;
|
|
lv_inv_area(&btn_area);
|
|
}
|
|
if(btn_pr != LV_BTNM_PR_NONE) {
|
|
area_cpy(&btn_area, &ext->button_areas[btn_pr]);
|
|
btn_area.x1 += btnm_area.x1;
|
|
btn_area.y1 += btnm_area.y1;
|
|
btn_area.x2 += btnm_area.x1;
|
|
btn_area.y2 += btnm_area.y1;
|
|
lv_inv_area(&btn_area);
|
|
}
|
|
}
|
|
|
|
ext->button_id_pressed = btn_pr;
|
|
}
|
|
|
|
else if(sign == LV_SIGNAL_LONG_PRESS_REP) {
|
|
if(ext->action && ext->button_id_pressed != LV_BTNM_PR_NONE) {
|
|
uint16_t txt_i = get_button_text(btnm, ext->button_id_pressed);
|
|
if(txt_i != LV_BTNM_PR_NONE) {
|
|
if(button_is_repeat_disabled(ext->map_p[txt_i]) == false &&
|
|
button_is_inactive(ext->map_p[txt_i]) == false) {
|
|
ext->action(btnm, cut_ctrl_byte(ext->map_p[txt_i]));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else if(sign == LV_SIGNAL_RELEASED) {
|
|
if(ext->button_id_pressed != LV_BTNM_PR_NONE) {
|
|
if(ext->action) {
|
|
uint16_t txt_i = get_button_text(btnm, ext->button_id_pressed);
|
|
if(txt_i != LV_BTNM_PR_NONE && button_is_inactive(ext->map_p[txt_i]) == false) {
|
|
ext->action(btnm, cut_ctrl_byte(ext->map_p[txt_i]));
|
|
}
|
|
}
|
|
|
|
/*Invalidate to old pressed area*/;
|
|
lv_obj_get_coords(btnm, &btnm_area);
|
|
area_cpy(&btn_area, &ext->button_areas[ext->button_id_pressed]);
|
|
btn_area.x1 += btnm_area.x1;
|
|
btn_area.y1 += btnm_area.y1;
|
|
btn_area.x2 += btnm_area.x1;
|
|
btn_area.y2 += btnm_area.y1;
|
|
lv_inv_area(&btn_area);
|
|
|
|
if(ext->toggle != 0) {
|
|
/*Invalidate to old toggled area*/;
|
|
area_cpy(&btn_area, &ext->button_areas[ext->button_id_toggled]);
|
|
btn_area.x1 += btnm_area.x1;
|
|
btn_area.y1 += btnm_area.y1;
|
|
btn_area.x2 += btnm_area.x1;
|
|
btn_area.y2 += btnm_area.y1;
|
|
lv_inv_area(&btn_area);
|
|
|
|
ext->button_id_toggled = ext->button_id_pressed;
|
|
}
|
|
|
|
ext->button_id_pressed = LV_BTNM_PR_NONE;
|
|
}
|
|
} else if(sign == LV_SIGNAL_PRESS_LOST || sign == LV_SIGNAL_DEFOCUS) {
|
|
ext->button_id_pressed = LV_BTNM_PR_NONE;
|
|
lv_obj_invalidate(btnm);
|
|
} else if(sign == LV_SIGNAL_FOCUS) {
|
|
ext->button_id_pressed = 0;
|
|
lv_obj_invalidate(btnm);
|
|
} else if(sign == LV_SIGNAL_CONTROLL) {
|
|
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
|
char c = *((char*)param);
|
|
if(c == LV_GROUP_KEY_RIGHT || c == LV_GROUP_KEY_UP) {
|
|
if(ext->button_id_pressed == LV_BTNM_PR_NONE) ext->button_id_pressed = 0;
|
|
else ext->button_id_pressed++;
|
|
if(ext->button_id_pressed >= ext->button_cnt - 1) ext->button_id_pressed = ext->button_cnt - 1;
|
|
lv_obj_invalidate(btnm);
|
|
} else if(c == LV_GROUP_KEY_LEFT || c == LV_GROUP_KEY_DOWN) {
|
|
if(ext->button_id_pressed == LV_BTNM_PR_NONE) ext->button_id_pressed = 0;
|
|
if(ext->button_id_pressed > 0) ext->button_id_pressed--;
|
|
lv_obj_invalidate(btnm);
|
|
} else if(c == LV_GROUP_KEY_ENTER) {
|
|
if(ext->action != NULL) {
|
|
uint16_t txt_i = get_button_text(btnm, ext->button_id_pressed);
|
|
if(txt_i != LV_BTNM_PR_NONE) {
|
|
ext->action(btnm, cut_ctrl_byte(ext->map_p[txt_i]));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return valid;
|
|
}
|
|
|
|
/*=====================
|
|
* Setter functions
|
|
*====================*/
|
|
|
|
/**
|
|
* Set a new map. Buttons will be created/deleted according to the map.
|
|
* @param btnm pointer to a button matrix object
|
|
* @param map pointer a string array. The last string has to be: "".
|
|
* Use "\n" to begin a new line.
|
|
* The first byte can be a control data:
|
|
* - bit 7: always 1
|
|
* - bit 6: always 0
|
|
* - bit 5: inactive (disabled)
|
|
* - bit 4: no repeat (on long press)
|
|
* - bit 3: hidden
|
|
* - bit 2..0: button relative width
|
|
* Example (practically use octal numbers): "\224abc": "abc" text with 4 width and no long press
|
|
*/
|
|
void lv_btnm_set_map(lv_obj_t * btnm, const char ** map)
|
|
{
|
|
if(map == NULL) return;
|
|
|
|
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
|
ext->map_p = map;
|
|
|
|
/*Analyze the map and create the required number of buttons*/
|
|
create_buttons(btnm, map);
|
|
|
|
/*Set size and positions of the buttons*/
|
|
lv_style_t * btnms = lv_obj_get_style(btnm);
|
|
cord_t max_w = lv_obj_get_width(btnm) - 2 * btnms->body.padding.hor;
|
|
cord_t max_h = lv_obj_get_height(btnm) - 2 * btnms->body.padding.ver;
|
|
cord_t act_y = btnms->body.padding.ver;
|
|
|
|
/*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 ++;
|
|
}
|
|
|
|
cord_t btn_h = max_h - ((line_cnt - 1) * btnms->body.padding.inner);
|
|
btn_h = btn_h / line_cnt;
|
|
|
|
/* 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*/
|
|
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*/
|
|
unit_cnt += get_button_width(map_p_tmp[btn_cnt]);
|
|
btn_cnt ++;
|
|
}
|
|
|
|
/*Only deal with the non empty lines*/
|
|
if(btn_cnt != 0) {
|
|
/*Calculate the width of all units*/
|
|
cord_t all_unit_w = max_w - ((btn_cnt-1) * btnms->body.padding.inner);
|
|
|
|
/*Set the button size and positions and set the texts*/
|
|
uint16_t i;
|
|
cord_t act_x = btnms->body.padding.hor;
|
|
cord_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 operation but the multiply first to divide a greater number */
|
|
act_unit_w = (all_unit_w * get_button_width(map_p_tmp[i])) / unit_cnt;
|
|
|
|
/*Always recalculate act_x because of rounding errors */
|
|
act_x = (unit_act_cnt * all_unit_w) / unit_cnt + i * btnms->body.padding.inner + btnms->body.padding.hor;
|
|
|
|
area_set(&ext->button_areas[btn_i], act_x,
|
|
act_y,
|
|
act_x + act_unit_w,
|
|
act_y + btn_h);
|
|
|
|
unit_act_cnt += get_button_width(map_p_tmp[i]);
|
|
|
|
i_tot ++;
|
|
btn_i ++;
|
|
}
|
|
}
|
|
act_y += btn_h + btnms->body.padding.inner;
|
|
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'*/
|
|
}
|
|
|
|
lv_obj_invalidate(btnm);
|
|
}
|
|
|
|
/**
|
|
* Set a new callback function for the buttons (It will be called when a button is released)
|
|
* @param btnm: pointer to button matrix object
|
|
* @param cb pointer to a callback function
|
|
*/
|
|
void lv_btnm_set_action(lv_obj_t * btnm, lv_btnm_action_t cb)
|
|
{
|
|
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
|
ext->action = cb;
|
|
}
|
|
|
|
/**
|
|
* Enable or disable button toggling
|
|
* @param btnm pointer to button matrix object
|
|
* @param en true: enable toggling; false: disable toggling
|
|
* @param id index of the currently toggled button (ignored if 'en' == false)
|
|
*/
|
|
void lv_btnm_set_toggle(lv_obj_t * btnm, bool en, uint16_t id)
|
|
{
|
|
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
|
|
|
ext->toggle = en == false ? 0 : 1;
|
|
if(ext->toggle != 0) {
|
|
if(id > ext->button_cnt) id = LV_BTNM_PR_NONE;
|
|
ext->button_id_toggled = id;
|
|
} else {
|
|
ext->button_id_toggled = LV_BTNM_PR_NONE;
|
|
}
|
|
|
|
lv_obj_invalidate(btnm);
|
|
}
|
|
|
|
/**
|
|
* Set styles of the button is each state. Use NULL for any style to leave it unchanged.
|
|
* @param btnm pointer to button matrix object
|
|
* @param rel pointer to a style for releases state
|
|
* @param pr pointer to a style for pressed state
|
|
* @param tgl_rel pointer to a style for toggled releases state
|
|
* @param tgl_pr pointer to a style for toggled pressed state
|
|
* @param ina pointer to a style for inactive state
|
|
*/
|
|
void lv_btnm_set_style_btn(lv_obj_t *btnm, lv_style_t *rel, lv_style_t *pr,
|
|
lv_style_t *tgl_rel, lv_style_t *tgl_pr,
|
|
lv_style_t *ina)
|
|
|
|
{
|
|
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
|
|
|
if(rel != NULL) ext->styles_btn[LV_BTN_STATE_RELEASED] = rel;
|
|
if(pr != NULL) ext->styles_btn[LV_BTN_STATE_PRESSED] = pr;
|
|
if(tgl_rel != NULL) ext->styles_btn[LV_BTN_STATE_TGL_RELEASED] = tgl_rel;
|
|
if(tgl_pr != NULL) ext->styles_btn[LV_BTN_STATE_TGL_PRESSED] = tgl_pr;
|
|
if(ina != NULL) ext->styles_btn[LV_BTN_STATE_INACTIVE] = ina;
|
|
|
|
lv_obj_invalidate(btnm);
|
|
}
|
|
|
|
/*=====================
|
|
* Getter functions
|
|
*====================*/
|
|
|
|
/**
|
|
* Get the current map of a button matrix
|
|
* @param btnm pointer to a button matrix object
|
|
* @return the current map
|
|
*/
|
|
const char ** lv_btnm_get_map(lv_obj_t * btnm)
|
|
{
|
|
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
|
return ext->map_p;
|
|
}
|
|
|
|
/**
|
|
* Get a the callback function of the buttons on a button matrix
|
|
* @param btnm: pointer to button matrix object
|
|
* @return pointer to the callback function
|
|
*/
|
|
lv_btnm_action_t lv_btnm_get_action(lv_obj_t * btnm)
|
|
{
|
|
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
|
return ext->action;
|
|
}
|
|
|
|
/**
|
|
* Get the style of buttons in button matrix
|
|
* @param btnm pointer to a button matrix object
|
|
* @param state style in this state (LV_BTN_STATE_PR or LV_BTN_STATE_REL)
|
|
* @return pointer the button style in the given state
|
|
*/
|
|
lv_style_t * lv_btnm_get_style_btn(lv_obj_t * btnm, lv_btn_state_t state)
|
|
{
|
|
if(state >= LV_BTN_STATE_NUM) return lv_obj_get_style(btnm);
|
|
|
|
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
|
lv_style_t * style = ext->styles_btn[state];
|
|
|
|
if(style == NULL) style = lv_obj_get_style(btnm);
|
|
|
|
return style;
|
|
}
|
|
|
|
/**********************
|
|
* STATIC FUNCTIONS
|
|
**********************/
|
|
|
|
/**
|
|
* Handle the drawing related tasks of the button matrixs
|
|
* @param btnm pointer to a button matrix object
|
|
* @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
|
|
* @param return true/false, depends on 'mode'
|
|
*/
|
|
static bool lv_btnm_design(lv_obj_t * btnm, const area_t * mask, lv_design_mode_t mode)
|
|
{
|
|
if(mode == LV_DESIGN_COVER_CHK) {
|
|
return ancestor_design_f(btnm, mask, mode);
|
|
/*Return false if the object is not covers the mask_p area*/
|
|
}
|
|
/*Draw the object*/
|
|
else if (mode == LV_DESIGN_DRAW_MAIN) {
|
|
ancestor_design_f(btnm, mask, mode);
|
|
|
|
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
|
lv_style_t * bg_style = lv_obj_get_style(btnm);
|
|
lv_style_t * btn_style;
|
|
|
|
area_t area_btnm;
|
|
area_t area_tmp;
|
|
cord_t btn_w;
|
|
cord_t btn_h;
|
|
|
|
uint16_t btn_i = 0;
|
|
uint16_t txt_i = 0;
|
|
for(btn_i = 0; btn_i < ext->button_cnt; btn_i ++, txt_i ++) {
|
|
/*Search the next valid text in the map*/
|
|
while(strcmp(ext->map_p[txt_i], "\n") == 0) txt_i ++;
|
|
|
|
/*Skip hidden buttons*/
|
|
if(button_is_hidden(ext->map_p[txt_i])) continue;
|
|
|
|
lv_obj_get_coords(btnm, &area_btnm);
|
|
|
|
area_cpy(&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;
|
|
|
|
btn_w = area_get_width(&area_tmp);
|
|
btn_h = area_get_height(&area_tmp);
|
|
|
|
/*Load the style*/
|
|
if(button_is_inactive(ext->map_p[txt_i])) btn_style = lv_btnm_get_style_btn(btnm, LV_BTN_STATE_INACTIVE);
|
|
else if(btn_i != ext->button_id_pressed && btn_i != ext->button_id_toggled) btn_style = lv_btnm_get_style_btn(btnm, LV_BTN_STATE_RELEASED);
|
|
else if(btn_i == ext->button_id_pressed && btn_i != ext->button_id_toggled) btn_style = lv_btnm_get_style_btn(btnm, LV_BTN_STATE_PRESSED);
|
|
else if(btn_i != ext->button_id_pressed && btn_i == ext->button_id_toggled) btn_style = lv_btnm_get_style_btn(btnm, LV_BTN_STATE_TGL_RELEASED);
|
|
else if(btn_i == ext->button_id_pressed && btn_i == ext->button_id_toggled) btn_style = lv_btnm_get_style_btn(btnm, LV_BTN_STATE_TGL_PRESSED);
|
|
lv_draw_rect(&area_tmp, mask, btn_style);
|
|
|
|
/*Calculate the size of the text*/
|
|
const font_t * font = btn_style->text.font;
|
|
point_t txt_size;
|
|
txt_get_size(&txt_size, ext->map_p[txt_i], font,
|
|
btn_style->text.letter_space, btn_style->text.line_space,
|
|
area_get_width(&area_btnm), TXT_FLAG_NONE);
|
|
|
|
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;
|
|
|
|
|
|
if(btn_style->glass) btn_style = bg_style;
|
|
lv_draw_label(&area_tmp, mask, btn_style, ext->map_p[txt_i], TXT_FLAG_NONE, NULL);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Create the required number of buttons according to a map
|
|
* @param btnm pointer to button matrix object
|
|
* @param map_p pointer to a string array
|
|
*/
|
|
static void create_buttons(lv_obj_t * btnm, const char ** map)
|
|
{
|
|
/*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) {
|
|
dm_free(ext->button_areas);
|
|
ext->button_areas = NULL;
|
|
}
|
|
|
|
ext->button_areas = dm_alloc(sizeof(area_t) * btn_cnt);
|
|
ext->button_cnt = btn_cnt;
|
|
}
|
|
|
|
/**
|
|
* Get the width of a button in units. It comes from the first "letter".
|
|
* @param btn_str The descriptor string of a button. E.g. "apple" or "\004banana"
|
|
* @return the width of the button in units
|
|
*/
|
|
static uint8_t get_button_width(const char * btn_str)
|
|
{
|
|
if((btn_str[0] & LV_BTNM_CTRL_MASK) == LV_BTNM_CTRL_CODE){
|
|
return btn_str[0] & LV_BTNM_WIDTH_MASK;
|
|
}
|
|
|
|
return 1; /*Default width is 1*/
|
|
}
|
|
|
|
static bool button_is_hidden(const char * btn_str)
|
|
{
|
|
/*If control byte presents and hidden bit is '1' then the button is hidden*/
|
|
if(((btn_str[0] & LV_BTNM_CTRL_MASK) == LV_BTNM_CTRL_CODE) &&
|
|
(btn_str[0] & LV_BTNM_HIDE_MASK)) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static bool button_is_repeat_disabled(const char * btn_str)
|
|
{
|
|
/*If control byte presents and hidden bit is '1' then the button is hidden*/
|
|
if(((btn_str[0] & LV_BTNM_CTRL_MASK) == LV_BTNM_CTRL_CODE) &&
|
|
(btn_str[0] & LV_BTNM_REPEAT_DISABLE_MASK)) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static bool button_is_inactive(const char * btn_str)
|
|
{
|
|
/*If control byte presents and hidden bit is '1' then the button is hidden*/
|
|
if(((btn_str[0] & LV_BTNM_CTRL_MASK) == LV_BTNM_CTRL_CODE) &&
|
|
(btn_str[0] & LV_BTNM_INACTIVE_MASK)) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
const char * cut_ctrl_byte(const char * btn_str)
|
|
{
|
|
/*Cut the control byte if present*/
|
|
if((btn_str[0] & LV_BTNM_CTRL_MASK) == LV_BTNM_CTRL_CODE) return &btn_str[1];
|
|
else return btn_str;
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
* @return the id of the button or LV_BTNM_PR_NONE.
|
|
*/
|
|
static uint16_t get_button_from_point(lv_obj_t * btnm, point_t * p)
|
|
{
|
|
area_t btnm_cords;
|
|
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);
|
|
|
|
for(i = 0; i < ext->button_cnt; i++) {
|
|
area_cpy(&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;
|
|
if(area_is_point_on(&btn_area, p) != false) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
if(i == ext->button_cnt) i = LV_BTNM_PR_NONE;
|
|
|
|
return i;
|
|
}
|
|
|
|
/**
|
|
* Get the text of a button
|
|
* @param btnm pointer to a button matrix object
|
|
* @param btn_id button id
|
|
* @return text id in ext->map_p or LV_BTNM_PR_NONE if 'btn_id' was invalid
|
|
*/
|
|
static uint16_t get_button_text(lv_obj_t * btnm, uint16_t btn_id)
|
|
{
|
|
lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
|
|
if(btn_id > ext->button_cnt) return LV_BTNM_PR_NONE;
|
|
|
|
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 ++;
|
|
}
|
|
|
|
if(btn_i == ext->button_cnt) return LV_BTNM_PR_NONE;
|
|
|
|
return txt_i;
|
|
}
|
|
|
|
|
|
#endif
|