mirror of
https://github.com/lvgl/lvgl.git
synced 2025-01-28 07:03:00 +08:00
lv_sw: animation fixes
This commit is contained in:
parent
938819a8b8
commit
07f2e18dfc
@ -20,7 +20,6 @@
|
||||
/*********************
|
||||
* DEFINES
|
||||
*********************/
|
||||
#define LV_SWITCH_SLIDER_ANIM_MAX 1000
|
||||
|
||||
/**********************
|
||||
* TYPEDEFS
|
||||
@ -68,7 +67,7 @@ lv_obj_t * lv_sw_create(lv_obj_t * par, const lv_obj_t * copy)
|
||||
if(ext == NULL) return NULL;
|
||||
|
||||
/*Initialize the allocated 'ext' */
|
||||
ext->tmp_state = 0;
|
||||
ext->changed = 0;
|
||||
ext->anim_time = 0;
|
||||
ext->style_knob_off = ext->slider.style_knob;
|
||||
ext->style_knob_on = ext->slider.style_knob;
|
||||
@ -260,23 +259,42 @@ static lv_res_t lv_sw_signal(lv_obj_t * sw, lv_signal_t sign, void * param)
|
||||
if(sign == LV_SIGNAL_CLEANUP) {
|
||||
/*Nothing to cleanup. (No dynamically allocated memory in 'ext')*/
|
||||
}
|
||||
else if(sign == LV_SIGNAL_PRESSED) {
|
||||
ext->tmp_state = lv_sw_get_state(sw);
|
||||
else if(sign == LV_SIGNAL_PRESSING) {
|
||||
int16_t threshold = LV_SWITCH_SLIDER_ANIM_MAX / 2;
|
||||
if((old_val < threshold && ext->slider.drag_value > threshold) ||
|
||||
(old_val > threshold && ext->slider.drag_value < threshold))
|
||||
{
|
||||
ext->changed = 1;
|
||||
}
|
||||
printf("tmp: %d\n", ext->changed);
|
||||
}
|
||||
else if(sign == LV_SIGNAL_PRESS_LOST) {
|
||||
if(lv_sw_get_state(sw)) lv_slider_set_style(sw, LV_SLIDER_STYLE_KNOB, ext->style_knob_on);
|
||||
else lv_slider_set_style(sw, LV_SLIDER_STYLE_KNOB, ext->style_knob_off);
|
||||
}
|
||||
else if(sign == LV_SIGNAL_RELEASED) {
|
||||
int16_t v = lv_slider_get_value(sw);
|
||||
if(v > LV_SWITCH_SLIDER_ANIM_MAX / 2) {
|
||||
lv_sw_on(sw);
|
||||
if(ext->tmp_state == 0 && slider_action != NULL) res = slider_action(sw);
|
||||
if(lv_sw_get_state(sw)) {
|
||||
lv_slider_set_style(sw, LV_SLIDER_STYLE_KNOB, ext->style_knob_on);
|
||||
lv_sw_anim_to_value(sw, LV_SWITCH_SLIDER_ANIM_MAX);
|
||||
}
|
||||
else {
|
||||
lv_sw_off(sw);
|
||||
if(ext->tmp_state && slider_action != NULL) res = slider_action(sw);
|
||||
lv_slider_set_style(sw, LV_SLIDER_STYLE_KNOB, ext->style_knob_off);
|
||||
lv_sw_anim_to_value(sw, 0);
|
||||
}
|
||||
ext->changed = 0;
|
||||
}
|
||||
else if(sign == LV_SIGNAL_RELEASED) {
|
||||
/*If not dragged then toggle the switch*/
|
||||
if(ext->changed == 0) {
|
||||
if(lv_sw_get_state(sw)) lv_sw_off(sw);
|
||||
else lv_sw_on(sw);
|
||||
}
|
||||
/*If the switch was dragged then calculate the new state based on the current position*/
|
||||
else {
|
||||
int16_t v = lv_slider_get_value(sw);
|
||||
if(v > LV_SWITCH_SLIDER_ANIM_MAX / 2) lv_sw_on(sw);
|
||||
else lv_sw_off(sw);
|
||||
|
||||
if(slider_action != NULL) res = slider_action(sw);
|
||||
}
|
||||
|
||||
ext->changed = 0;
|
||||
|
||||
} else if(sign == LV_SIGNAL_CONTROLL) {
|
||||
|
||||
|
@ -32,6 +32,7 @@ extern "C" {
|
||||
/*********************
|
||||
* DEFINES
|
||||
*********************/
|
||||
#define LV_SWITCH_SLIDER_ANIM_MAX 1000
|
||||
|
||||
/**********************
|
||||
* TYPEDEFS
|
||||
@ -43,7 +44,7 @@ typedef struct
|
||||
/*New data for this type */
|
||||
lv_style_t *style_knob_off; /*Style of the knob when the switch is OFF*/
|
||||
lv_style_t *style_knob_on; /*Style of the knob when the switch is ON (NULL to use the same as OFF)*/
|
||||
uint8_t tmp_state :1; /*Saves the initial state of the switch when it begins to drag*/
|
||||
uint8_t changed :1; /*Indicates the switch explicitly changed by drag*/
|
||||
#if USE_LV_ANIMATION
|
||||
uint16_t anim_time; /*switch animation time */
|
||||
#endif
|
||||
@ -124,7 +125,7 @@ void lv_sw_set_anim_time(lv_obj_t *sw, uint16_t anim_time);
|
||||
*/
|
||||
static inline bool lv_sw_get_state(const lv_obj_t *sw)
|
||||
{
|
||||
return lv_bar_get_value(sw) == 0 ? false : true;
|
||||
return lv_bar_get_value(sw) < LV_SWITCH_SLIDER_ANIM_MAX / 2 ? false : true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user