1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-14 06:42:58 +08:00
lvgl/lv_objx/lv_btn.c

454 lines
14 KiB
C
Raw Normal View History

2016-06-08 07:25:08 +02:00
/**
* @file lv_btn.c
*
*/
/*********************
* INCLUDES
*********************/
2016-12-15 10:31:30 +01:00
#include <lvgl/lv_misc/area.h>
#include <lvgl/lv_obj/lv_obj.h>
#include <misc/others/color.h>
#include <stdbool.h>
2016-06-08 07:25:08 +02:00
#if USE_LV_BTN != 0
#include <string.h>
#include "lv_btn.h"
#include "../lv_draw/lv_draw.h"
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
2016-10-07 11:15:46 +02:00
static bool lv_btn_design(lv_obj_t * btn, const area_t * mask, lv_design_mode_t mode);
static void lv_btn_style_load(lv_obj_t * btn, lv_rects_t * new_rects);
2016-10-04 15:29:52 +02:00
static void lv_btns_init(void);
2016-06-08 07:25:08 +02:00
/**********************
* STATIC VARIABLES
**********************/
static lv_btns_t lv_btns_def;
static lv_btns_t lv_btns_transp;
static lv_btns_t lv_btns_border;
2016-06-08 07:25:08 +02:00
static lv_design_f_t ancestor_design_f;
2016-06-08 07:25:08 +02:00
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/**
* Create a button objects
2016-10-07 11:15:46 +02:00
* @param par pointer to an object, it will be the parent of the new button
* @param copy pointer to a button object, if not NULL then the new object will be copied from it
2016-06-08 07:25:08 +02:00
* @return pointer to the created button
*/
2016-10-07 11:15:46 +02:00
lv_obj_t * lv_btn_create(lv_obj_t * par, lv_obj_t * copy)
2016-06-08 07:25:08 +02:00
{
2016-10-07 11:15:46 +02:00
lv_obj_t * new_btn;
2016-06-08 07:25:08 +02:00
2016-10-07 11:15:46 +02:00
new_btn = lv_rect_create(par, copy);
2016-06-08 07:25:08 +02:00
/*Allocate the extended data*/
2016-10-07 11:15:46 +02:00
lv_obj_alloc_ext(new_btn, sizeof(lv_btn_ext_t));
if(ancestor_design_f == NULL) {
ancestor_design_f = lv_obj_get_design_f(new_btn);
}
2016-10-07 11:15:46 +02:00
lv_obj_set_signal_f(new_btn, lv_btn_signal);
lv_obj_set_design_f(new_btn, lv_btn_design);
2016-06-08 07:25:08 +02:00
2016-10-07 11:15:46 +02:00
lv_btn_ext_t * ext = lv_obj_get_ext(new_btn);
ext->lpr_exec = 0;
2016-06-08 07:25:08 +02:00
2016-06-08 07:25:08 +02:00
/*If no copy do the basic initialization*/
2016-10-07 11:15:46 +02:00
if(copy == NULL)
2016-06-08 07:25:08 +02:00
{
2016-10-07 11:15:46 +02:00
ext->state = LV_BTN_STATE_REL;
ext->pr_action = NULL;
ext->rel_action = NULL;
ext->lpr_action = NULL;
ext->tgl = 0;
lv_rect_set_layout(new_btn, LV_RECT_LAYOUT_CENTER);
2016-06-08 07:25:08 +02:00
}
2016-10-07 11:15:46 +02:00
/*Copy 'copy'*/
2016-06-08 07:25:08 +02:00
else{
2016-10-07 11:15:46 +02:00
lv_btn_ext_t * ori_btn_ext = lv_obj_get_ext(copy);
ext->state = ori_btn_ext->state;
ext->pr_action = ori_btn_ext->pr_action;
ext->rel_action = ori_btn_ext->rel_action;
ext->lpr_action = ori_btn_ext->lpr_action;
ext->tgl = ori_btn_ext->tgl;
2016-06-08 07:25:08 +02:00
}
lv_obj_set_style(new_btn, lv_btns_get(LV_BTNS_DEF, NULL));
2016-10-07 11:15:46 +02:00
return new_btn;
2016-06-08 07:25:08 +02:00
}
/**
* Signal function of the button
2016-10-07 11:15:46 +02:00
* @param btn pointer to a button object
2016-06-08 07:25:08 +02:00
* @param sign a signal type from lv_signal_t enum
* @param param pointer to a signal specific variable
*/
2016-10-07 11:15:46 +02:00
bool lv_btn_signal(lv_obj_t * btn, lv_signal_t sign, void* param)
2016-06-08 07:25:08 +02:00
{
bool valid;
/* Include the ancient signal function */
2016-10-07 11:15:46 +02:00
valid = lv_rect_signal(btn, sign, param);
2016-06-08 07:25:08 +02:00
/* The object can be deleted so check its validity and then
* make the object specific signal handling */
if(valid != false) {
2016-10-07 11:15:46 +02:00
lv_btn_state_t state = lv_btn_get_state(btn);
lv_btn_ext_t * ext = lv_obj_get_ext(btn);
bool tgl = lv_btn_get_tgl(btn);
2016-06-08 07:25:08 +02:00
switch (sign){
case LV_SIGNAL_PRESSED:
/*Refresh the state*/
2016-10-07 11:15:46 +02:00
if(ext->state == LV_BTN_STATE_REL) {
lv_btn_set_state(btn, LV_BTN_STATE_PR);
} else if(ext->state == LV_BTN_STATE_TGL_REL) {
lv_btn_set_state(btn, LV_BTN_STATE_TGL_PR);
2016-06-08 07:25:08 +02:00
}
2016-10-07 11:15:46 +02:00
lv_obj_inv(btn);
2016-06-08 07:25:08 +02:00
2016-10-07 11:15:46 +02:00
ext->lpr_exec = 0;
2016-06-08 07:25:08 +02:00
/*Call the press action, here 'param' is the caller dispi*/
2016-10-07 11:15:46 +02:00
if(ext->pr_action != NULL && state != LV_BTN_STATE_INA) {
valid = ext->pr_action(btn, param);
2016-06-08 07:25:08 +02:00
}
break;
case LV_SIGNAL_PRESS_LOST:
/*Refresh the state*/
2016-10-07 11:15:46 +02:00
if(ext->state == LV_BTN_STATE_PR) {
lv_btn_set_state(btn, LV_BTN_STATE_REL);
} else if(ext->state == LV_BTN_STATE_TGL_PR) {
lv_btn_set_state(btn, LV_BTN_STATE_TGL_REL);
2016-06-08 07:25:08 +02:00
}
2016-10-07 11:15:46 +02:00
lv_obj_inv(btn);
2016-06-08 07:25:08 +02:00
break;
case LV_SIGNAL_RELEASED:
/*If not dragged and it was not long press action then
*change state and run the action*/
2016-10-07 11:15:46 +02:00
if(lv_dispi_is_dragging(param) == false && ext->lpr_exec == 0) {
if(ext->state == LV_BTN_STATE_PR && tgl == false) {
lv_btn_set_state(btn, LV_BTN_STATE_REL);
} else if(ext->state == LV_BTN_STATE_TGL_PR && tgl == false) {
lv_btn_set_state(btn, LV_BTN_STATE_TGL_REL);
} else if(ext->state == LV_BTN_STATE_PR && tgl == true) {
lv_btn_set_state(btn, LV_BTN_STATE_TGL_REL);
} else if(ext->state == LV_BTN_STATE_TGL_PR && tgl == true) {
lv_btn_set_state(btn, LV_BTN_STATE_REL);
2016-06-08 07:25:08 +02:00
}
2016-10-07 11:15:46 +02:00
if(ext->rel_action != NULL && state != LV_BTN_STATE_INA) {
valid = ext->rel_action(btn, param);
2016-06-08 07:25:08 +02:00
}
} else { /*If dragged change back the state*/
2016-10-07 11:15:46 +02:00
if(ext->state == LV_BTN_STATE_PR) {
lv_btn_set_state(btn, LV_BTN_STATE_REL);
} else if(ext->state == LV_BTN_STATE_TGL_PR) {
lv_btn_set_state(btn, LV_BTN_STATE_TGL_REL);
2016-06-08 07:25:08 +02:00
}
}
2016-10-07 11:15:46 +02:00
lv_obj_inv(btn);
2016-06-08 07:25:08 +02:00
break;
case LV_SIGNAL_LONG_PRESS:
/*Call the long press action, here 'param' is the caller dispi*/
2016-10-07 11:15:46 +02:00
if(ext->lpr_action != NULL && state != LV_BTN_STATE_INA) {
ext->lpr_exec = 1;
valid = ext->lpr_action(btn, param);
2016-06-08 07:25:08 +02:00
}
2016-06-15 09:38:20 +02:00
break;
2016-06-08 07:25:08 +02:00
default:
/*Do nothing*/
break;
}
}
return valid;
}
/*=====================
* Setter functions
*====================*/
/**
* Enable the toggled states
2016-10-07 11:15:46 +02:00
* @param btn pointer to a button object
2016-06-08 07:25:08 +02:00
* @param tgl true: enable toggled states, false: disable
*/
2016-10-07 11:15:46 +02:00
void lv_btn_set_tgl(lv_obj_t * btn, bool tgl)
2016-06-08 07:25:08 +02:00
{
2016-10-07 11:15:46 +02:00
lv_btn_ext_t * ext = lv_obj_get_ext(btn);
2016-06-08 07:25:08 +02:00
2016-10-07 11:15:46 +02:00
ext->tgl = tgl != false ? 1 : 0;
2016-06-08 07:25:08 +02:00
}
/**
* Set the state of the button
2016-10-07 11:15:46 +02:00
* @param btn pointer to a button object
2016-06-08 07:25:08 +02:00
* @param state the new state of the button (from lv_btn_state_t enum)
*/
2016-10-07 11:15:46 +02:00
void lv_btn_set_state(lv_obj_t * btn, lv_btn_state_t state)
2016-06-08 07:25:08 +02:00
{
2016-10-07 11:15:46 +02:00
lv_btn_ext_t * ext = lv_obj_get_ext(btn);
2016-06-08 07:25:08 +02:00
2016-10-07 11:15:46 +02:00
ext->state = state;
lv_obj_inv(btn);
2016-06-08 07:25:08 +02:00
}
/**
* Set a function to call when the button is pressed
2016-10-07 11:15:46 +02:00
* @param btn pointer to a button object
2016-06-08 07:25:08 +02:00
* @param pr_action pointer to function
*/
2016-10-07 11:15:46 +02:00
void lv_btn_set_pr_action(lv_obj_t * btn, bool (*pr_action)(lv_obj_t *, lv_dispi_t *))
2016-06-08 07:25:08 +02:00
{
2016-10-07 11:15:46 +02:00
lv_btn_ext_t * ext = lv_obj_get_ext(btn);
2016-06-08 07:25:08 +02:00
2016-10-07 11:15:46 +02:00
ext->pr_action = pr_action;
2016-06-08 07:25:08 +02:00
}
/**
* Set a function to call when the button is released
2016-10-07 11:15:46 +02:00
* @param btn pointer to a button object
2016-06-08 07:25:08 +02:00
* @param pr_action pointer to function
*/
2016-10-07 11:15:46 +02:00
void lv_btn_set_rel_action(lv_obj_t * btn, bool (*rel_action)(lv_obj_t *, lv_dispi_t *))
2016-06-08 07:25:08 +02:00
{
2016-10-07 11:15:46 +02:00
lv_btn_ext_t * btn_p = lv_obj_get_ext(btn);
2016-06-08 07:25:08 +02:00
btn_p->rel_action = rel_action;
}
/**
* Set a function to call when the button is long pressed
2016-10-07 11:15:46 +02:00
* @param btn pointer to a button object
2016-06-08 07:25:08 +02:00
* @param pr_action pointer to function
*/
2016-10-07 11:15:46 +02:00
void lv_btn_set_lpr_action(lv_obj_t * btn, bool (*lpr_action)(lv_obj_t *, lv_dispi_t *))
2016-06-08 07:25:08 +02:00
{
2016-10-07 11:15:46 +02:00
lv_btn_ext_t * ext = lv_obj_get_ext(btn);
2016-06-08 07:25:08 +02:00
2016-10-07 11:15:46 +02:00
ext->lpr_action = lpr_action;
2016-06-08 07:25:08 +02:00
}
/*=====================
* Getter functions
*====================*/
/**
* Get the current state of the button
2016-10-07 11:15:46 +02:00
* @param btn pointer to a button object
2016-06-08 07:25:08 +02:00
* @return the state of the button (from lv_btn_state_t enum)
*/
2016-10-07 11:15:46 +02:00
lv_btn_state_t lv_btn_get_state(lv_obj_t * btn)
2016-06-08 07:25:08 +02:00
{
2016-10-07 11:15:46 +02:00
lv_btn_ext_t * ext = lv_obj_get_ext(btn);
2016-06-08 07:25:08 +02:00
2016-10-07 11:15:46 +02:00
return ext->state;
2016-06-08 07:25:08 +02:00
}
/**
* Get the toggle enable attribute of the button
2016-10-07 11:15:46 +02:00
* @param btn pointer to a button object
2016-06-08 07:25:08 +02:00
* @return ture: toggle enabled, false: disabled
*/
2016-10-07 11:15:46 +02:00
bool lv_btn_get_tgl(lv_obj_t * btn)
2016-06-08 07:25:08 +02:00
{
2016-10-07 11:15:46 +02:00
lv_btn_ext_t * ext = lv_obj_get_ext(btn);
2016-06-08 07:25:08 +02:00
2016-10-07 11:15:46 +02:00
return ext->tgl != 0 ? true : false;
2016-06-08 07:25:08 +02:00
}
/**
* Return with a pointer to a built-in style and/or copy it to a variable
* @param style a style name from lv_btns_builtin_t enum
2016-10-07 11:15:46 +02:00
* @param copy copy the style to this variable. (NULL if unused)
2016-06-08 07:25:08 +02:00
* @return pointer to an lv_btns_t style
*/
2016-10-07 11:15:46 +02:00
lv_btns_t * lv_btns_get(lv_btns_builtin_t style, lv_btns_t * copy)
2016-06-08 07:25:08 +02:00
{
static bool style_inited = false;
/*Make the style initialization if it is not done yet*/
if(style_inited == false) {
lv_btns_init();
style_inited = true;
}
2016-10-07 11:15:46 +02:00
lv_btns_t * style_p;
2016-06-08 07:25:08 +02:00
switch(style) {
case LV_BTNS_DEF:
style_p = &lv_btns_def;
break;
case LV_BTNS_TRANSP:
style_p = &lv_btns_transp;
break;
case LV_BTNS_BORDER:
style_p = &lv_btns_border;
break;
default:
style_p = &lv_btns_def;
2016-06-08 07:25:08 +02:00
}
2016-10-07 11:15:46 +02:00
if(copy != NULL) {
if(style_p != NULL) memcpy(copy, style_p, sizeof(lv_btns_t));
else memcpy(copy, &lv_btns_def, sizeof(lv_btns_t));
2016-06-08 07:25:08 +02:00
}
return style_p;
}
/**********************
* STATIC FUNCTIONS
**********************/
/**
* Handle the drawing related tasks of the buttons
2016-10-07 11:15:46 +02:00
* @param btn pointer to a button object
2016-06-08 07:25:08 +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-06-08 07:25:08 +02:00
* @param return true/false, depends on 'mode'
*/
2016-10-07 11:15:46 +02:00
static bool lv_btn_design(lv_obj_t * btn, const area_t * mask, lv_design_mode_t mode)
2016-06-08 07:25:08 +02:00
{
/* Because of the radius it is not sure the area is covered*/
if(mode == LV_DESIGN_COVER_CHK) {
return ancestor_design_f(btn, mask, mode);
} else if(mode == LV_DESIGN_DRAW_MAIN) {
area_t area;
2016-10-07 11:15:46 +02:00
lv_obj_get_cords(btn, &area);
2016-06-08 07:25:08 +02:00
/*Temporally set a rectangle style for the button to draw it as rectangle*/
lv_rects_t rects_tmp;
lv_btns_t * btns_tmp = lv_obj_get_style(btn);
2016-10-07 11:15:46 +02:00
lv_btn_style_load(btn, &rects_tmp);
btn->style_p = &rects_tmp;
ancestor_design_f(btn, mask, mode); /*Draw the rectangle*/
btn->style_p = btns_tmp; /*Reload the original button style*/
}
2016-06-08 07:25:08 +02:00
return true;
}
2016-10-04 15:29:52 +02:00
/**
* Crate a rectangle style according to the state of the button
* @param btn pointer to a button object
* @param new_rects load the style here (pointer to a rectangle style)
2016-10-04 15:29:52 +02:00
*/
static void lv_btn_style_load(lv_obj_t * btn, lv_rects_t * new_rects)
2016-10-04 15:29:52 +02:00
{
2016-10-07 11:15:46 +02:00
lv_btn_state_t state = lv_btn_get_state(btn);
lv_btns_t * style = lv_obj_get_style(btn);
2016-10-04 15:29:52 +02:00
/*Load the style*/
memcpy(new_rects, &style->rects, sizeof(lv_rects_t));
new_rects->objs.color = style->mcolor[state];
new_rects->gcolor = style->gcolor[state];
new_rects->bcolor = style->bcolor[state];
new_rects->lcolor = style->lcolor[state];
if(style->light_en[state] != 0) {
new_rects->light = style->rects.light;
} else {
new_rects->light = 0;
}
2016-10-04 15:29:52 +02:00
}
/**
* Initialize the button styles
*/
static void lv_btns_init(void)
{
/*Default style*/
lv_btns_def.mcolor[LV_BTN_STATE_REL] = COLOR_MAKE(0x40, 0x60, 0x80);
lv_btns_def.gcolor[LV_BTN_STATE_REL] = COLOR_BLACK;
lv_btns_def.bcolor[LV_BTN_STATE_REL] = COLOR_WHITE;
lv_btns_def.lcolor[LV_BTN_STATE_REL] = COLOR_MAKE(0x30, 0x40, 0x50);
lv_btns_def.light_en[LV_BTN_STATE_REL] = 0;
lv_btns_def.mcolor[LV_BTN_STATE_PR] = COLOR_MAKE(0x60, 0x80, 0xa0);
lv_btns_def.gcolor[LV_BTN_STATE_PR] = COLOR_MAKE(0x20, 0x30, 0x40);
lv_btns_def.bcolor[LV_BTN_STATE_PR] = COLOR_WHITE;
lv_btns_def.lcolor[LV_BTN_STATE_PR] = COLOR_MAKE(0x30, 0x40, 0x50);
lv_btns_def.light_en[LV_BTN_STATE_PR] = 1;
lv_btns_def.mcolor[LV_BTN_STATE_TGL_REL] = COLOR_MAKE(0x80, 0x00, 0x00);
lv_btns_def.gcolor[LV_BTN_STATE_TGL_REL] = COLOR_MAKE(0x20, 0x20, 0x20);
lv_btns_def.bcolor[LV_BTN_STATE_TGL_REL] = COLOR_WHITE;
lv_btns_def.lcolor[LV_BTN_STATE_TGL_REL] = COLOR_MAKE(0x30, 0x40, 0x50);
lv_btns_def.light_en[LV_BTN_STATE_TGL_REL] = 0;
lv_btns_def.mcolor[LV_BTN_STATE_TGL_PR] = COLOR_MAKE(0xf0, 0x26, 0x26);
lv_btns_def.gcolor[LV_BTN_STATE_TGL_PR] = COLOR_MAKE(0x40, 0x40, 0x40);
lv_btns_def.bcolor[LV_BTN_STATE_TGL_PR] = COLOR_WHITE;
lv_btns_def.lcolor[LV_BTN_STATE_TGL_PR] = COLOR_MAKE(0x30, 0x40, 0x50);
lv_btns_def.light_en[LV_BTN_STATE_TGL_PR] = 1;
lv_btns_def.mcolor[LV_BTN_STATE_INA] = COLOR_SILVER;
lv_btns_def.gcolor[LV_BTN_STATE_INA] = COLOR_GRAY;
lv_btns_def.bcolor[LV_BTN_STATE_INA] = COLOR_WHITE;
lv_btns_def.lcolor[LV_BTN_STATE_INA] = COLOR_MAKE(0x30, 0x40, 0x50);
lv_btns_def.light_en[LV_BTN_STATE_INA] = 0;
lv_btns_def.rects.bwidth = 2 * LV_STYLE_MULT;
lv_btns_def.rects.bopa = 50;
lv_btns_def.rects.light = 8 * LV_STYLE_MULT;
lv_btns_def.rects.empty = 0;
lv_btns_def.rects.round = 4 * LV_STYLE_MULT;
lv_btns_def.rects.hpad = 10 * LV_STYLE_MULT;
lv_btns_def.rects.vpad = 15 * LV_STYLE_MULT;
lv_btns_def.rects.opad = 5 * LV_STYLE_MULT;
/*Transparent style*/
memcpy(&lv_btns_transp, &lv_btns_def, sizeof(lv_btns_t));
lv_btns_transp.rects.objs.transp = 1;
lv_btns_transp.rects.bwidth = 0;
lv_btns_transp.rects.empty = 1;
/*Border style*/
memcpy(&lv_btns_border, &lv_btns_def, sizeof(lv_btns_t));
lv_btns_border.bcolor[LV_BTN_STATE_REL] = COLOR_BLACK;
lv_btns_border.bcolor[LV_BTN_STATE_PR] = COLOR_BLACK;
lv_btns_border.bcolor[LV_BTN_STATE_TGL_REL] = COLOR_BLACK;
lv_btns_border.bcolor[LV_BTN_STATE_TGL_PR] = COLOR_BLACK;
lv_btns_border.bcolor[LV_BTN_STATE_INA] = COLOR_GRAY;
lv_btns_border.rects.bwidth = 2 * LV_STYLE_MULT;
lv_btns_border.rects.empty = 1;
lv_btns_border.rects.bopa = 50;
lv_btns_border.rects.round = 4 * LV_STYLE_MULT;
lv_btns_border.rects.hpad = 10 * LV_STYLE_MULT;
lv_btns_border.rects.vpad = 10 * LV_STYLE_MULT;
lv_btns_border.rects.vpad = 5 * LV_STYLE_MULT;
}
2016-06-08 07:25:08 +02:00
#endif