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

702 lines
23 KiB
C
Raw Normal View History

2016-06-08 07:25:08 +02:00
/**
* @file lv_btn.c
2018-06-19 09:49:58 +02:00
*
2016-06-08 07:25:08 +02:00
*/
/*********************
* INCLUDES
*********************/
2016-12-15 10:31:30 +01:00
#include "lv_btn.h"
#if LV_USE_BTN != 0
2016-06-08 07:25:08 +02:00
2017-10-20 15:37:50 +02:00
#include <string.h>
2017-11-30 11:35:33 +01:00
#include "../lv_core/lv_group.h"
2019-09-24 16:30:38 +02:00
#include "../lv_core/lv_debug.h"
#include "../lv_draw/lv_draw.h"
#include "../lv_themes/lv_theme.h"
2017-11-23 20:42:14 +01:00
#include "../lv_misc/lv_area.h"
#include "../lv_misc/lv_color.h"
2018-07-08 01:00:56 +02:00
#include "../lv_misc/lv_math.h"
2016-06-08 07:25:08 +02:00
/*********************
* DEFINES
*********************/
#define LV_OBJX_NAME "lv_btn"
2019-04-04 07:15:40 +02:00
#define LV_BTN_INK_VALUE_MAX 256
#define LV_BTN_INK_VALUE_MAX_SHIFT 8
2018-09-06 20:57:59 +02:00
2016-06-08 07:25:08 +02:00
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
2019-09-06 19:53:39 +02:00
static lv_design_res_t lv_btn_design(lv_obj_t * btn, const lv_area_t * clip_area, lv_design_mode_t mode);
2018-07-08 01:00:56 +02:00
static lv_res_t lv_btn_signal(lv_obj_t * btn, lv_signal_t sign, void * param);
2018-08-26 13:49:23 +02:00
#if LV_USE_ANIMATION && LV_BTN_INK_EFFECT
static void lv_btn_ink_effect_anim(lv_obj_t * btn, lv_anim_value_t val);
2019-04-22 08:45:07 +02:00
static void lv_btn_ink_effect_anim_ready(lv_anim_t * a);
2018-08-26 13:49:23 +02:00
#endif
2016-06-08 07:25:08 +02:00
/**********************
* STATIC VARIABLES
**********************/
static lv_signal_cb_t ancestor_signal;
static lv_design_cb_t ancestor_design;
2018-07-08 01:00:56 +02:00
#if LV_USE_ANIMATION && LV_BTN_INK_EFFECT
2018-08-26 13:49:23 +02:00
static lv_coord_t ink_act_value;
2018-07-08 01:00:56 +02:00
static lv_obj_t * ink_obj;
static lv_btn_state_t ink_bg_state;
2018-08-26 13:49:23 +02:00
static lv_btn_state_t ink_top_state;
2018-07-08 03:25:07 +02:00
static bool ink_ready;
static bool ink_playback;
static lv_point_t ink_point;
2018-07-08 01:00:56 +02:00
#endif
2016-06-08 07:25:08 +02:00
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/**
2019-07-10 11:56:36 +02:00
* Create a button object
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
*/
lv_obj_t * lv_btn_create(lv_obj_t * par, const lv_obj_t * copy)
2016-06-08 07:25:08 +02:00
{
2018-10-05 17:22:49 +02:00
LV_LOG_TRACE("button create started");
2018-07-25 17:57:08 +02:00
2016-10-07 11:15:46 +02:00
lv_obj_t * new_btn;
2018-06-19 09:49:58 +02:00
2017-04-13 16:12:03 +02:00
new_btn = lv_cont_create(par, copy);
2019-09-24 23:14:17 +02:00
LV_ASSERT_MEM(new_btn);
if(new_btn == NULL) return NULL;
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_btn);
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(new_btn);
2016-06-08 07:25:08 +02:00
/*Allocate the extended data*/
lv_btn_ext_t * ext = lv_obj_allocate_ext_attr(new_btn, sizeof(lv_btn_ext_t));
2019-09-24 23:14:17 +02:00
LV_ASSERT_MEM(ext);
if(ext == NULL) {
lv_obj_del(new_btn);
return NULL;
}
ext->toggle = 0;
2019-12-19 11:05:04 +01:00
#if LV_USE_ANIMATION && LV_BTN_INK_EFFECT
2019-04-04 07:15:40 +02:00
ext->ink_in_time = 0;
ext->ink_wait_time = 0;
2019-04-04 07:15:40 +02:00
ext->ink_out_time = 0;
2018-08-26 13:49:23 +02:00
#endif
lv_obj_set_signal_cb(new_btn, lv_btn_signal);
lv_obj_set_design_cb(new_btn, lv_btn_design);
2018-06-19 09:49:58 +02:00
2016-06-08 07:25:08 +02:00
/*If no copy do the basic initialization*/
if(copy == NULL) {
2017-12-13 15:12:04 +01:00
/*Set layout if the button is not a screen*/
if(par != NULL) {
lv_btn_set_layout(new_btn, LV_LAYOUT_CENTER);
}
2019-04-04 07:15:40 +02:00
lv_obj_set_click(new_btn, true); /*Be sure the button is clickable*/
2018-06-19 09:49:58 +02:00
/*Set the default styles*/
lv_theme_t * th = lv_theme_get_current();
if(th) {
2019-12-19 11:05:04 +01:00
// lv_btn_set_style(new_btn, LV_BTN_STYLE_REL, th->style.btn.rel);
// lv_btn_set_style(new_btn, LV_BTN_STYLE_PR, th->style.btn.pr);
// lv_btn_set_style(new_btn, LV_BTN_STYLE_TGL_REL, th->style.btn.tgl_rel);
// lv_btn_set_style(new_btn, LV_BTN_STYLE_TGL_PR, th->style.btn.tgl_pr);
// lv_btn_set_style(new_btn, LV_BTN_STYLE_INA, th->style.btn.ina);
} else {
2019-12-19 11:05:04 +01:00
// lv_obj_set_style(new_btn, ext->styles[LV_BTN_STATE_REL]);
}
2016-06-08 07:25:08 +02:00
}
2016-10-07 11:15:46 +02:00
/*Copy 'copy'*/
else {
2018-06-19 09:49:58 +02:00
lv_btn_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
2019-04-04 07:15:40 +02:00
ext->toggle = copy_ext->toggle;
#if LV_USE_ANIMATION && LV_BTN_INK_EFFECT
2019-04-04 07:15:40 +02:00
ext->ink_in_time = copy_ext->ink_in_time;
2018-08-20 10:56:17 +02:00
ext->ink_wait_time = copy_ext->ink_wait_time;
2019-04-04 07:15:40 +02:00
ext->ink_out_time = copy_ext->ink_out_time;
2018-08-26 13:49:23 +02:00
#endif
2019-12-19 11:05:04 +01:00
// memcpy((void*) ext->styles, copy_ext->styles, sizeof(ext->styles));
2018-06-19 09:49:58 +02:00
/*Refresh the style with new signal function*/
2019-12-19 11:05:04 +01:00
lv_obj_refresh_style(new_btn,LV_BTN_STYLE_MAIN);
2016-06-08 07:25:08 +02:00
}
2018-06-19 09:49:58 +02:00
2018-10-05 17:22:49 +02:00
LV_LOG_INFO("button created");
2018-07-25 17:57:08 +02:00
2016-10-07 11:15:46 +02:00
return new_btn;
2016-06-08 07:25:08 +02:00
}
/*=====================
2018-06-19 09:49:58 +02:00
* Setter functions
2016-06-08 07:25:08 +02:00
*====================*/
/**
* 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
*/
void lv_btn_set_toggle(lv_obj_t * btn, bool tgl)
2016-06-08 07:25:08 +02:00
{
LV_ASSERT_OBJ(btn, LV_OBJX_NAME);
lv_btn_ext_t * ext = lv_obj_get_ext_attr(btn);
2018-06-19 09:49:58 +02:00
ext->toggle = 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
{
LV_ASSERT_OBJ(btn, LV_OBJX_NAME);
2019-12-19 11:05:04 +01:00
switch(state) {
case LV_BTN_STATE_REL:
lv_obj_clear_state(btn, LV_OBJ_STATE_PRESSED | LV_OBJ_STATE_CHECKED);
break;
case LV_BTN_STATE_PR:
lv_obj_clear_state(btn, LV_OBJ_STATE_CHECKED);
lv_obj_set_state(btn, LV_OBJ_STATE_PRESSED);
break;
case LV_BTN_STATE_TGL_REL:
lv_obj_set_state(btn, LV_OBJ_STATE_CHECKED);
lv_obj_clear_state(btn, LV_OBJ_STATE_PRESSED);
break;
case LV_BTN_STATE_TGL_PR:
lv_obj_set_state(btn, LV_OBJ_STATE_PRESSED | LV_OBJ_STATE_CHECKED);
break;
}
2016-06-08 07:25:08 +02:00
}
2017-10-20 15:37:50 +02:00
/**
* Toggle the state of the button (ON->OFF, OFF->ON)
* @param btn pointer to a button object
*/
void lv_btn_toggle(lv_obj_t * btn)
{
LV_ASSERT_OBJ(btn, LV_OBJX_NAME);
2019-12-19 11:05:04 +01:00
if(lv_obj_get_state(btn) & LV_OBJ_STATE_CHECKED) {
lv_obj_clear_state(btn, LV_OBJ_STATE_CHECKED);
} else {
lv_obj_set_state(btn, LV_OBJ_STATE_CHECKED);
2017-10-20 15:37:50 +02:00
}
}
2018-07-08 01:11:59 +02:00
/**
* Set time of the ink effect (draw a circle on click to animate in the new state)
* @param btn pointer to a button object
2018-07-08 02:01:58 +02:00
* @param time the time of the ink animation
2018-07-08 01:11:59 +02:00
*/
2018-08-26 13:49:23 +02:00
void lv_btn_set_ink_in_time(lv_obj_t * btn, uint16_t time)
2018-07-08 01:11:59 +02:00
{
LV_ASSERT_OBJ(btn, LV_OBJX_NAME);
#if LV_USE_ANIMATION && LV_BTN_INK_EFFECT
2018-07-08 01:11:59 +02:00
lv_btn_ext_t * ext = lv_obj_get_ext_attr(btn);
2019-04-04 07:15:40 +02:00
ext->ink_in_time = time;
2018-08-26 13:49:23 +02:00
#else
2019-04-04 07:15:40 +02:00
(void)btn; /*Unused*/
2018-10-09 15:03:46 +02:00
(void)time; /*Unused*/
2019-04-04 07:15:40 +02:00
LV_LOG_WARN("`lv_btn_set_ink_ink_time` has no effect if LV_BTN_INK_EFEFCT or LV_USE_ANIMATION "
"is disabled")
2018-07-08 02:01:58 +02:00
#endif
}
/**
* Set the wait time before the ink disappears
* @param btn pointer to a button object
* @param time the time of the ink animation
*/
void lv_btn_set_ink_wait_time(lv_obj_t * btn, uint16_t time)
{
LV_ASSERT_OBJ(btn, LV_OBJX_NAME);
2018-07-08 02:01:58 +02:00
#if LV_USE_ANIMATION && LV_BTN_INK_EFFECT
2018-08-26 13:49:23 +02:00
lv_btn_ext_t * ext = lv_obj_get_ext_attr(btn);
ext->ink_wait_time = time;
#else
2019-04-04 07:15:40 +02:00
(void)btn; /*Unused*/
2018-10-09 15:03:46 +02:00
(void)time; /*Unused*/
2019-04-04 07:15:40 +02:00
LV_LOG_WARN("`lv_btn_set_ink_wait_time` has no effect if LV_BTN_INK_EFEFCT or LV_USE_ANIMATION "
"is disabled")
2018-07-08 01:11:59 +02:00
#endif
2018-08-26 13:49:23 +02:00
}
2018-07-08 01:11:59 +02:00
2018-08-26 13:49:23 +02:00
/**
* Set time of the ink out effect (animate to the released state)
* @param btn pointer to a button object
* @param time the time of the ink animation
*/
void lv_btn_set_ink_out_time(lv_obj_t * btn, uint16_t time)
{
LV_ASSERT_OBJ(btn, LV_OBJX_NAME);
#if LV_USE_ANIMATION && LV_BTN_INK_EFFECT
2018-08-26 13:49:23 +02:00
lv_btn_ext_t * ext = lv_obj_get_ext_attr(btn);
2019-04-04 07:15:40 +02:00
ext->ink_out_time = time;
2018-08-26 13:49:23 +02:00
#else
2019-04-04 07:15:40 +02:00
(void)btn; /*Unused*/
2018-10-09 15:03:46 +02:00
(void)time; /*Unused*/
2019-04-04 07:15:40 +02:00
LV_LOG_WARN("`lv_btn_set_ink_out_time` has no effect if LV_BTN_INK_EFEFCT or LV_USE_ANIMATION "
"is disabled")
2018-08-26 13:49:23 +02:00
#endif
2018-07-08 01:11:59 +02:00
}
2016-06-08 07:25:08 +02:00
/*=====================
2018-06-19 09:49:58 +02:00
* Getter functions
2016-06-08 07:25:08 +02:00
*====================*/
/**
* 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)
*/
lv_btn_state_t lv_btn_get_state(const lv_obj_t * btn)
2016-06-08 07:25:08 +02:00
{
LV_ASSERT_OBJ(btn, LV_OBJX_NAME);
2019-12-19 11:05:04 +01:00
lv_obj_state_t state = lv_obj_get_state(btn);
if(state & LV_OBJ_STATE_CHECKED) {
if(state & LV_OBJ_STATE_PRESSED) return LV_BTN_STATE_TGL_PR;
else return LV_BTN_STATE_TGL_REL;
} else {
if(state & LV_OBJ_STATE_PRESSED) return LV_BTN_STATE_PR;
else return LV_BTN_STATE_REL;
}
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
2019-07-10 11:56:36 +02:00
* @return true: toggle enabled, false: disabled
2016-06-08 07:25:08 +02:00
*/
bool lv_btn_get_toggle(const lv_obj_t * btn)
2016-06-08 07:25:08 +02:00
{
LV_ASSERT_OBJ(btn, LV_OBJX_NAME);
lv_btn_ext_t * ext = lv_obj_get_ext_attr(btn);
2018-06-19 09:49:58 +02:00
return ext->toggle != 0 ? true : false;
2016-06-08 07:25:08 +02:00
}
2018-07-08 01:11:59 +02:00
/**
2018-08-26 13:49:23 +02:00
* Get time of the ink in effect (draw a circle on click to animate in the new state)
2018-07-08 01:11:59 +02:00
* @param btn pointer to a button object
* @return the time of the ink animation
*/
2018-09-25 23:33:17 +02:00
uint16_t lv_btn_get_ink_in_time(const lv_obj_t * btn)
2018-07-08 01:11:59 +02:00
{
LV_ASSERT_OBJ(btn, LV_OBJX_NAME);
#if LV_USE_ANIMATION && LV_BTN_INK_EFFECT
2018-07-08 01:11:59 +02:00
lv_btn_ext_t * ext = lv_obj_get_ext_attr(btn);
2018-08-26 13:49:23 +02:00
return ext->ink_in_time;
#else
2018-10-09 15:03:46 +02:00
(void)btn; /*Unused*/
2018-08-26 13:49:23 +02:00
return 0;
#endif
2018-07-08 01:11:59 +02:00
}
2018-07-08 02:01:58 +02:00
/**
* Get the wait time before the ink disappears
* @param btn pointer to a button object
* @return the time of the ink animation
*/
2018-09-25 23:33:17 +02:00
uint16_t lv_btn_get_ink_wait_time(const lv_obj_t * btn)
2018-07-08 02:01:58 +02:00
{
LV_ASSERT_OBJ(btn, LV_OBJX_NAME);
#if LV_USE_ANIMATION && LV_BTN_INK_EFFECT
2018-07-08 02:01:58 +02:00
lv_btn_ext_t * ext = lv_obj_get_ext_attr(btn);
return ext->ink_wait_time;
2018-08-26 13:49:23 +02:00
#else
2018-10-09 15:03:46 +02:00
(void)btn; /*Unused*/
2018-08-26 13:49:23 +02:00
return 0;
#endif
}
/**
* Get time of the ink out effect (animate to the releases state)
* @param btn pointer to a button object
* @return the time of the ink animation
*/
2018-09-25 23:33:17 +02:00
uint16_t lv_btn_get_ink_out_time(const lv_obj_t * btn)
2018-08-26 13:49:23 +02:00
{
LV_ASSERT_OBJ(btn, LV_OBJX_NAME);
#if LV_USE_ANIMATION && LV_BTN_INK_EFFECT
2018-08-26 13:49:23 +02:00
lv_btn_ext_t * ext = lv_obj_get_ext_attr(btn);
2019-10-08 06:04:58 +02:00
return ext->ink_out_time;
2018-08-26 13:49:23 +02:00
#else
2018-10-09 15:03:46 +02:00
(void)btn; /*Unused*/
2018-08-26 13:49:23 +02:00
return 0;
#endif
2018-07-08 02:01:58 +02:00
}
2019-12-19 11:05:04 +01:00
lv_style_dsc_t * lv_btn_get_style(lv_obj_t * cont, uint8_t type)
2016-06-08 07:25:08 +02:00
{
2019-12-19 11:05:04 +01:00
lv_style_dsc_t * style_dsc_p;
switch(type) {
case LV_BTN_STYLE_MAIN:
style_dsc_p = &cont->style_dsc;
break;
default:
style_dsc_p = NULL;
}
2019-12-19 11:05:04 +01:00
return style_dsc_p;
2016-06-08 07:25:08 +02:00
}
/**********************
* STATIC FUNCTIONS
**********************/
2018-07-08 01:00:56 +02:00
/**
* Handle the drawing related tasks of the drop down lists
* @param btn pointer to an 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
2019-09-06 19:53:39 +02:00
* @param return an element of `lv_design_res_t`
2018-07-08 01:00:56 +02:00
*/
2019-09-06 19:53:39 +02:00
static lv_design_res_t lv_btn_design(lv_obj_t * btn, const lv_area_t * clip_area, lv_design_mode_t mode)
2018-07-08 01:00:56 +02:00
{
2018-10-05 17:22:49 +02:00
if(mode == LV_DESIGN_COVER_CHK) {
2019-09-06 19:53:39 +02:00
return ancestor_design(btn, clip_area, mode);
2018-10-05 17:22:49 +02:00
} else if(mode == LV_DESIGN_DRAW_MAIN) {
2018-07-08 01:00:56 +02:00
#if LV_USE_ANIMATION && LV_BTN_INK_EFFECT
2018-10-05 17:22:49 +02:00
if(btn != ink_obj) {
2019-09-06 19:53:39 +02:00
ancestor_design(btn, clip_area, mode);
2018-10-05 17:22:49 +02:00
} else {
lv_opa_t opa_scale = lv_obj_get_opa_scale(btn);
lv_btn_ext_t * ext = lv_obj_get_ext_attr(btn);
/*Draw the normal button*/
if(ink_playback == false) {
lv_style_t style_tmp;
lv_style_copy(&style_tmp, ext->styles[ink_bg_state]);
style_tmp.body.shadow.width = ext->styles[ink_top_state]->body.shadow.width;
2019-09-06 19:53:39 +02:00
lv_draw_rect(&btn->coords, clip_area, &style_tmp, opa_scale);
2018-10-05 17:22:49 +02:00
2019-04-04 07:15:40 +02:00
lv_coord_t w = lv_obj_get_width(btn);
lv_coord_t h = lv_obj_get_height(btn);
2018-10-05 17:22:49 +02:00
lv_coord_t r_max = LV_MATH_MIN(w, h) / 2;
/*In the first part of the animation increase the size of the circle (ink effect) */
lv_area_t cir_area;
2019-06-06 06:05:40 +02:00
lv_coord_t coord_state =
ink_act_value < LV_BTN_INK_VALUE_MAX / 2 ? ink_act_value : LV_BTN_INK_VALUE_MAX / 2;
2018-10-05 17:22:49 +02:00
lv_point_t p_act;
2019-04-04 07:15:40 +02:00
p_act.x = ink_point.x;
p_act.y = ink_point.y;
2018-10-05 17:22:49 +02:00
lv_coord_t x_err = (btn->coords.x1 + w / 2) - p_act.x;
lv_coord_t y_err = (btn->coords.y1 + h / 2) - p_act.y;
p_act.x += (x_err * coord_state) >> (LV_BTN_INK_VALUE_MAX_SHIFT - 1);
p_act.y += (y_err * coord_state) >> (LV_BTN_INK_VALUE_MAX_SHIFT - 1);
lv_coord_t half_side = LV_MATH_MAX(w, h) / 2;
2019-06-06 06:05:40 +02:00
cir_area.x1 = p_act.x - ((half_side * coord_state) >> (LV_BTN_INK_VALUE_MAX_SHIFT - 1));
cir_area.y1 = p_act.y - ((half_side * coord_state) >> (LV_BTN_INK_VALUE_MAX_SHIFT - 1));
cir_area.x2 = p_act.x + ((half_side * coord_state) >> (LV_BTN_INK_VALUE_MAX_SHIFT - 1));
cir_area.y2 = p_act.y + ((half_side * coord_state) >> (LV_BTN_INK_VALUE_MAX_SHIFT - 1));
lv_area_intersect(&cir_area, &btn->coords,
&cir_area); /*Limit the area. (It might be too big on the smaller side)*/
2018-10-05 17:22:49 +02:00
/*In the second part animate the radius. Circle -> body.radius*/
2019-06-06 06:05:40 +02:00
lv_coord_t r_state =
ink_act_value > LV_BTN_INK_VALUE_MAX / 2 ? ink_act_value - LV_BTN_INK_VALUE_MAX / 2 : 0;
2018-10-05 17:22:49 +02:00
lv_style_copy(&style_tmp, ext->styles[ink_top_state]);
2019-06-06 06:05:40 +02:00
style_tmp.body.radius = r_max + (((ext->styles[ink_bg_state]->body.radius - r_max) * r_state) >>
(LV_BTN_INK_VALUE_MAX_SHIFT - 1));
2018-10-05 17:22:49 +02:00
style_tmp.body.border.width = 0;
/*Draw the circle*/
2019-09-06 19:53:39 +02:00
lv_draw_rect(&cir_area, clip_area, &style_tmp, opa_scale);
2018-10-05 17:22:49 +02:00
} else {
lv_style_t res;
lv_style_copy(&res, ext->styles[ink_bg_state]);
2019-06-06 06:05:40 +02:00
lv_style_mix(ext->styles[ink_bg_state], ext->styles[ink_top_state], &res, ink_act_value);
2019-09-06 19:53:39 +02:00
lv_draw_rect(&btn->coords, clip_area, &res, opa_scale);
2018-10-05 17:22:49 +02:00
}
}
#else
2019-12-19 11:05:04 +01:00
lv_btn_ext_t * ext = lv_obj_get_ext_attr(btn);
2019-12-19 22:44:18 +01:00
lv_draw_rect_dsc_t draw_dsc;
lv_draw_rect_dsc_init(&draw_dsc);
2019-12-19 23:16:53 +01:00
lv_obj_init_draw_rect_dsc(btn, LV_OBJ_STYLE_MAIN, &draw_dsc);
2019-12-19 11:05:04 +01:00
lv_draw_rect(&btn->coords, clip_area, &draw_dsc, lv_obj_get_opa_scale(btn));
if(lv_obj_get_style_value(btn, LV_OBJ_STYLE_MAIN, LV_STYLE_BG_CLIP_CORNER)) {
lv_draw_mask_radius_param_t * mp = lv_mem_buf_get(sizeof(lv_draw_mask_radius_param_t));
lv_coord_t r = lv_obj_get_style_value(btn, LV_OBJ_STYLE_MAIN, LV_STYLE_RADIUS);
lv_draw_mask_radius_init(mp, &btn->coords, r, false);
/*Add the mask and use `obj+8` as custom id. Don't use `obj` directly because it might be used by the user*/
lv_draw_mask_add(mp, btn + 8);
}
2018-07-08 01:00:56 +02:00
#endif
2018-10-05 17:22:49 +02:00
} else if(mode == LV_DESIGN_DRAW_POST) {
2019-09-06 19:53:39 +02:00
ancestor_design(btn, clip_area, mode);
2018-10-05 17:22:49 +02:00
}
2018-07-08 01:00:56 +02:00
2019-09-06 19:53:39 +02:00
return LV_DESIGN_RES_OK;
2018-07-08 01:00:56 +02:00
}
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
* @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
2016-06-08 07:25:08 +02:00
*/
static lv_res_t lv_btn_signal(lv_obj_t * btn, lv_signal_t sign, void * param)
2016-06-08 07:25:08 +02:00
{
lv_res_t res;
/* Include the ancient signal function */
res = ancestor_signal(btn, sign, param);
if(res != LV_RES_OK) return res;
if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, LV_OBJX_NAME);
2016-06-08 07:25:08 +02:00
lv_btn_ext_t * ext = lv_obj_get_ext_attr(btn);
2019-04-04 07:15:40 +02:00
bool tgl = lv_btn_get_toggle(btn);
2019-12-19 11:05:04 +01:00
lv_btn_state_t state = lv_btn_get_state(btn);
if(sign == LV_SIGNAL_PRESSED) {
/*Refresh the state*/
2019-12-19 11:05:04 +01:00
if(state == LV_BTN_STATE_REL) {
lv_btn_set_state(btn, LV_BTN_STATE_PR);
#if LV_USE_ANIMATION && LV_BTN_INK_EFFECT
2019-04-04 07:15:40 +02:00
ink_bg_state = LV_BTN_STATE_REL;
2018-08-26 13:49:23 +02:00
ink_top_state = LV_BTN_STATE_PR;
2018-07-08 01:00:56 +02:00
#endif
2019-12-19 11:05:04 +01:00
} else if(state == LV_BTN_STATE_TGL_REL) {
lv_btn_set_state(btn, LV_BTN_STATE_TGL_PR);
#if LV_USE_ANIMATION && LV_BTN_INK_EFFECT
2019-04-04 07:15:40 +02:00
ink_bg_state = LV_BTN_STATE_TGL_REL;
2018-08-26 13:49:23 +02:00
ink_top_state = LV_BTN_STATE_TGL_PR;
2018-07-08 01:00:56 +02:00
#endif
}
#if LV_USE_ANIMATION && LV_BTN_INK_EFFECT
2018-07-08 01:00:56 +02:00
/*Forget the old inked button*/
if(ink_obj != NULL && ink_obj != btn) {
2019-06-12 23:10:54 +02:00
lv_anim_del(ink_obj, (lv_anim_exec_xcb_t)lv_btn_ink_effect_anim);
2018-10-05 17:22:49 +02:00
lv_obj_invalidate(ink_obj);
ink_obj = NULL;
2018-07-08 01:00:56 +02:00
}
/*Save the new data for inking and start it's animation if enabled*/
2018-08-26 13:49:23 +02:00
if(ext->ink_in_time > 0) {
2019-04-04 07:15:40 +02:00
ink_obj = btn;
2018-10-05 17:22:49 +02:00
ink_playback = false;
2019-04-04 07:15:40 +02:00
ink_ready = false;
2018-10-05 17:22:49 +02:00
lv_indev_get_point(lv_indev_get_act(), &ink_point);
lv_anim_t a;
2019-04-04 07:15:40 +02:00
a.var = btn;
a.start = 0;
a.end = LV_BTN_INK_VALUE_MAX;
2019-06-12 23:10:54 +02:00
a.exec_cb = (lv_anim_exec_xcb_t)lv_btn_ink_effect_anim;
a.path_cb = lv_anim_path_linear;
a.ready_cb = lv_btn_ink_effect_anim_ready;
2019-04-04 07:15:40 +02:00
a.act_time = 0;
a.time = ext->ink_in_time;
a.playback = 0;
2018-10-05 17:22:49 +02:00
a.playback_pause = 0;
2019-04-04 07:15:40 +02:00
a.repeat = 0;
a.repeat_pause = 0;
2018-10-05 17:22:49 +02:00
lv_anim_create(&a);
2018-07-08 01:00:56 +02:00
}
#endif
} else if(sign == LV_SIGNAL_PRESS_LOST) {
/*Refresh the state*/
2019-12-19 11:05:04 +01:00
if(state == LV_BTN_STATE_PR)
2019-04-04 07:15:40 +02:00
lv_btn_set_state(btn, LV_BTN_STATE_REL);
2019-12-19 11:05:04 +01:00
else if(state == LV_BTN_STATE_TGL_PR)
2019-04-04 07:15:40 +02:00
lv_btn_set_state(btn, LV_BTN_STATE_TGL_REL);
2018-06-19 09:49:58 +02:00
} else if(sign == LV_SIGNAL_PRESSING) {
/*When the button begins to drag revert pressed states to released*/
if(lv_indev_is_dragging(param) != false) {
2019-12-19 11:05:04 +01:00
if(state == LV_BTN_STATE_PR)
2019-04-04 07:15:40 +02:00
lv_btn_set_state(btn, LV_BTN_STATE_REL);
2019-12-19 11:05:04 +01:00
else if(state == LV_BTN_STATE_TGL_PR)
2019-04-04 07:15:40 +02:00
lv_btn_set_state(btn, LV_BTN_STATE_TGL_REL);
}
2018-06-19 09:49:58 +02:00
} else if(sign == LV_SIGNAL_RELEASED) {
/*If not dragged and it was not long press action then
*change state and run the action*/
2019-03-01 06:26:03 +01:00
if(lv_indev_is_dragging(param) == false) {
2019-06-20 14:28:25 +02:00
uint32_t toggled = 0;
2019-12-19 11:05:04 +01:00
if(state == LV_BTN_STATE_PR && tgl == false) {
lv_btn_set_state(btn, LV_BTN_STATE_REL);
2019-06-20 14:28:25 +02:00
toggled = 0;
2019-12-19 11:05:04 +01:00
} else if(state == LV_BTN_STATE_TGL_PR && tgl == false) {
lv_btn_set_state(btn, LV_BTN_STATE_TGL_REL);
2019-06-20 14:28:25 +02:00
toggled = 1;
2019-12-19 11:05:04 +01:00
} else if(state == LV_BTN_STATE_PR && tgl == true) {
lv_btn_set_state(btn, LV_BTN_STATE_TGL_REL);
2019-06-20 14:28:25 +02:00
toggled = 1;
2019-12-19 11:05:04 +01:00
} else if(state == LV_BTN_STATE_TGL_PR && tgl == true) {
lv_btn_set_state(btn, LV_BTN_STATE_REL);
2019-06-20 14:28:25 +02:00
toggled = 0;
}
2019-03-01 06:26:03 +01:00
if(tgl) {
2019-06-20 14:28:25 +02:00
res = lv_event_send(btn, LV_EVENT_VALUE_CHANGED, &toggled);
2019-03-07 00:42:08 +01:00
if(res != LV_RES_OK) return res;
2019-03-01 06:26:03 +01:00
}
} else { /*If dragged change back the state*/
2019-12-19 23:16:53 +01:00
if(state == LV_BTN_STATE_PR) {
lv_btn_set_state(btn, LV_BTN_STATE_REL);
2019-12-19 11:05:04 +01:00
} else if(state == LV_BTN_STATE_TGL_PR) {
lv_btn_set_state(btn, LV_BTN_STATE_TGL_REL);
}
}
2018-07-08 01:00:56 +02:00
#if LV_USE_ANIMATION && LV_BTN_INK_EFFECT
2018-07-08 01:00:56 +02:00
/*Draw the toggled state in the inking instead*/
if(ext->toggle) {
2018-10-05 17:22:49 +02:00
ink_top_state = ext->state;
2018-07-08 01:00:56 +02:00
}
2018-07-08 03:25:07 +02:00
/*If not a toggle button and the "IN" inking is ready then start an "OUT" inking*/
2018-08-26 13:49:23 +02:00
else if(ink_ready && ext->ink_out_time > 0) {
2019-04-04 07:15:40 +02:00
ink_obj = btn;
ink_playback = true; /*It is the playback. If not set `lv_btn_ink_effect_anim_ready`
will start its own playback*/
2018-10-05 17:22:49 +02:00
lv_indev_get_point(lv_indev_get_act(), &ink_point);
lv_anim_t a;
2019-04-04 07:15:40 +02:00
a.var = ink_obj;
a.start = LV_BTN_INK_VALUE_MAX;
a.end = 0;
2019-06-12 23:10:54 +02:00
a.exec_cb = (lv_anim_exec_xcb_t)lv_btn_ink_effect_anim;
a.path_cb = lv_anim_path_linear;
a.ready_cb = lv_btn_ink_effect_anim_ready;
2019-04-04 07:15:40 +02:00
a.act_time = 0;
a.time = ext->ink_out_time;
a.playback = 0;
2018-10-05 17:22:49 +02:00
a.playback_pause = 0;
2019-04-04 07:15:40 +02:00
a.repeat = 0;
a.repeat_pause = 0;
2018-10-05 17:22:49 +02:00
lv_anim_create(&a);
2018-07-08 03:25:07 +02:00
}
2018-07-08 01:00:56 +02:00
#endif
} else if(sign == LV_SIGNAL_CONTROL) {
2018-06-19 09:49:58 +02:00
char c = *((char *)param);
2019-04-08 14:36:20 +02:00
if(c == LV_KEY_RIGHT || c == LV_KEY_UP) {
2019-06-20 14:28:25 +02:00
if(lv_btn_get_toggle(btn)) {
lv_btn_set_state(btn, LV_BTN_STATE_TGL_REL);
uint32_t state = 1;
2019-06-27 07:16:15 +02:00
res = lv_event_send(btn, LV_EVENT_VALUE_CHANGED, &state);
2019-06-20 14:28:25 +02:00
if(res != LV_RES_OK) return res;
}
2019-04-08 14:36:20 +02:00
} else if(c == LV_KEY_LEFT || c == LV_KEY_DOWN) {
2019-06-20 14:28:25 +02:00
if(lv_btn_get_toggle(btn)) {
lv_btn_set_state(btn, LV_BTN_STATE_REL);
uint32_t state = 0;
2019-06-27 07:16:15 +02:00
res = lv_event_send(btn, LV_EVENT_VALUE_CHANGED, &state);
2019-06-20 14:28:25 +02:00
if(res != LV_RES_OK) return res;
}
}
2018-10-05 17:22:49 +02:00
} else if(sign == LV_SIGNAL_CLEANUP) {
#if LV_USE_ANIMATION && LV_BTN_INK_EFFECT
2018-10-05 17:22:49 +02:00
if(btn == ink_obj) {
2019-06-12 23:10:54 +02:00
lv_anim_del(ink_obj, (lv_anim_exec_xcb_t)lv_btn_ink_effect_anim);
2018-07-08 01:00:56 +02:00
ink_obj = NULL;
2018-10-05 17:22:49 +02:00
}
2018-07-08 01:00:56 +02:00
#endif
2018-02-28 15:37:41 +01:00
}
return res;
2016-06-08 07:25:08 +02:00
}
#if LV_USE_ANIMATION && LV_BTN_INK_EFFECT
2018-07-08 01:00:56 +02:00
/**
* The animator function of inking. CAlled to increase the radius of ink
* @param btn pointer to the animated button
* @param val the new radius
*/
static void lv_btn_ink_effect_anim(lv_obj_t * btn, lv_anim_value_t val)
2018-07-08 01:00:56 +02:00
{
2018-10-05 17:22:49 +02:00
if(btn) {
ink_act_value = val;
lv_obj_invalidate(btn);
}
2018-07-08 01:00:56 +02:00
}
/**
* Called to clean up when the ink animation is ready
2019-04-22 08:45:07 +02:00
* @param a unused
2018-07-08 01:00:56 +02:00
*/
2019-04-22 08:45:07 +02:00
static void lv_btn_ink_effect_anim_ready(lv_anim_t * a)
2018-07-08 01:00:56 +02:00
{
2019-06-06 06:05:40 +02:00
(void)a; /*Unused*/
2018-09-25 23:33:17 +02:00
2019-04-04 07:15:40 +02:00
lv_btn_ext_t * ext = lv_obj_get_ext_attr(ink_obj);
2018-07-08 03:25:07 +02:00
lv_btn_state_t state = lv_btn_get_state(ink_obj);
2018-10-05 17:22:49 +02:00
lv_obj_invalidate(ink_obj);
ink_ready = true;
2019-06-06 06:05:40 +02:00
if((state == LV_BTN_STATE_REL || state == LV_BTN_STATE_TGL_REL) && ext->toggle == 0 && ink_playback == false) {
2019-05-10 07:41:32 +02:00
lv_anim_t new_a;
new_a.var = ink_obj;
new_a.start = LV_BTN_INK_VALUE_MAX;
new_a.end = 0;
2019-06-12 23:10:54 +02:00
new_a.exec_cb = (lv_anim_exec_xcb_t)lv_btn_ink_effect_anim;
2019-05-10 07:41:32 +02:00
new_a.path_cb = lv_anim_path_linear;
new_a.ready_cb = lv_btn_ink_effect_anim_ready;
new_a.act_time = -ext->ink_wait_time;
new_a.time = ext->ink_out_time;
new_a.playback = 0;
new_a.playback_pause = 0;
new_a.repeat = 0;
new_a.repeat_pause = 0;
lv_anim_create(&new_a);
2018-10-05 17:22:49 +02:00
ink_playback = true;
2018-07-08 03:25:07 +02:00
} else {
2018-10-05 17:22:49 +02:00
ink_obj = NULL;
2018-07-08 03:25:07 +02:00
}
2018-07-08 01:00:56 +02:00
}
#endif /*LV_USE_ANIMATION*/
2016-06-08 07:25:08 +02:00
#endif