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

720 lines
23 KiB
C
Raw Normal View History

2017-04-11 10:50:57 +02:00
/**
* @file lv_bar.c
2018-06-19 09:49:58 +02:00
*
2017-04-11 10:50:57 +02:00
*/
/*********************
* INCLUDES
*********************/
#include "lv_bar.h"
#if LV_USE_BAR != 0
2017-04-11 10:50:57 +02:00
2019-09-24 16:30:38 +02:00
#include "../lv_core/lv_debug.h"
2017-04-11 10:50:57 +02:00
#include "../lv_draw/lv_draw.h"
#include "../lv_themes/lv_theme.h"
2017-11-23 20:42:14 +01:00
#include "../lv_misc/lv_anim.h"
2019-09-16 10:58:28 +02:00
#include "../lv_misc/lv_math.h"
2017-04-11 10:50:57 +02:00
#include <stdio.h>
/*********************
* DEFINES
*********************/
#define LV_OBJX_NAME "lv_bar"
2017-04-11 10:50:57 +02:00
2019-09-16 10:58:28 +02:00
#define LV_BAR_SIZE_MIN 4 /*hor. pad and ver. pad cannot make the indicator smaller then this [px]*/
2017-04-11 10:50:57 +02:00
#if LV_USE_ANIMATION
#define LV_BAR_IS_ANIMATING(anim_struct) (((anim_struct).anim_state) != LV_BAR_ANIM_STATE_INV)
#define LV_BAR_GET_ANIM_VALUE(orig_value, anim_struct) (LV_BAR_IS_ANIMATING(anim_struct) ? ((anim_struct).anim_end) : (orig_value))
#else
#define LV_BAR_GET_ANIM_VALUE(orig_value, anim_struct) (orig_value)
#endif
2017-04-11 10:50:57 +02:00
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
2019-09-06 19:53:39 +02:00
static lv_design_res_t lv_bar_design(lv_obj_t * bar, const lv_area_t * clip_area, lv_design_mode_t mode);
2017-11-08 11:07:29 +01:00
static lv_res_t lv_bar_signal(lv_obj_t * bar, lv_signal_t sign, void * param);
static lv_style_list_t * lv_bar_get_style(lv_obj_t * bar, uint8_t part);
2019-12-26 03:37:28 +01:00
2019-12-31 22:13:32 +01:00
static void draw_bg(lv_obj_t * bar, const lv_area_t * clip_area);
static void draw_indic(lv_obj_t * bar, const lv_area_t * clip_area);
2019-12-26 03:37:28 +01:00
#if LV_USE_ANIMATION
2019-12-26 03:37:28 +01:00
static void lv_bar_set_value_with_anim(lv_obj_t * bar, int16_t new_value, int16_t *value_ptr, lv_bar_anim_t *anim_info, lv_anim_enable_t en);
static void lv_bar_init_anim(lv_obj_t * bar, lv_bar_anim_t * bar_anim);
2019-12-02 07:37:33 -05:00
static void lv_bar_anim(lv_bar_anim_t * bar, lv_anim_value_t value);
2019-04-22 08:45:07 +02:00
static void lv_bar_anim_ready(lv_anim_t * a);
#endif
2017-04-11 10:50:57 +02:00
/**********************
* STATIC VARIABLES
**********************/
static lv_design_cb_t ancestor_design_f;
static lv_signal_cb_t ancestor_signal;
2017-04-11 10:50:57 +02:00
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/**
* Create a bar objects
* @param par pointer to an object, it will be the parent of the new bar
* @param copy pointer to a bar object, if not NULL then the new object will be copied from it
* @return pointer to the created bar
*/
lv_obj_t * lv_bar_create(lv_obj_t * par, const lv_obj_t * copy)
2017-04-11 10:50:57 +02:00
{
2018-10-05 17:22:49 +02:00
LV_LOG_TRACE("lv_bar create started");
2018-07-25 17:57:08 +02:00
2017-04-11 10:50:57 +02:00
/*Create the ancestor basic object*/
2020-01-18 23:34:34 +01:00
lv_obj_t * bar = lv_obj_create(par, copy);
LV_ASSERT_MEM(bar);
if(bar == NULL) return NULL;
2017-04-11 10:50:57 +02:00
2020-01-18 23:34:34 +01:00
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(bar);
if(ancestor_design_f == NULL) ancestor_design_f = lv_obj_get_design_cb(bar);
2017-11-08 11:07:29 +01:00
2017-04-11 10:50:57 +02:00
/*Allocate the object type specific extended data*/
2020-01-18 23:34:34 +01:00
lv_bar_ext_t * ext = lv_obj_allocate_ext_attr(bar, sizeof(lv_bar_ext_t));
2019-09-24 23:14:17 +02:00
LV_ASSERT_MEM(ext);
if(ext == NULL) {
2020-01-18 23:34:34 +01:00
lv_obj_del(bar);
return NULL;
}
2019-06-06 06:05:40 +02:00
ext->min_value = 0;
2019-12-02 07:37:33 -05:00
ext->start_value = 0;
2019-06-06 06:05:40 +02:00
ext->max_value = 100;
ext->cur_value = 0;
#if LV_USE_ANIMATION
2019-06-06 06:05:40 +02:00
ext->anim_time = 200;
2020-01-18 23:34:34 +01:00
lv_bar_init_anim(bar, &ext->cur_value_anim);
lv_bar_init_anim(bar, &ext->start_value_anim);
#endif
2019-12-02 07:37:33 -05:00
ext->type = LV_BAR_TYPE_NORMAL;
2017-04-11 10:50:57 +02:00
2020-01-20 16:11:38 +01:00
lv_style_list_init(&ext->style_indic);
2020-01-18 23:34:34 +01:00
lv_obj_set_signal_cb(bar, lv_bar_signal);
lv_obj_set_design_cb(bar, lv_bar_design);
2017-04-11 10:50:57 +02:00
2019-12-31 22:13:32 +01:00
2017-04-11 10:50:57 +02:00
/*Init the new bar object*/
if(copy == NULL) {
2020-01-18 23:34:34 +01:00
lv_obj_set_click(bar, false);
lv_obj_set_size(bar, LV_DPI * 2, LV_DPI / 10);
lv_bar_set_value(bar, ext->cur_value, false);
2019-12-31 22:13:32 +01:00
2020-01-18 23:34:34 +01:00
lv_theme_apply(bar, LV_THEME_BAR);
2017-04-11 10:50:57 +02:00
} else {
2018-06-19 09:49:58 +02:00
lv_bar_ext_t * ext_copy = lv_obj_get_ext_attr(copy);
2019-04-04 07:15:40 +02:00
ext->min_value = ext_copy->min_value;
2019-12-02 07:37:33 -05:00
ext->start_value = ext_copy->start_value;
2019-04-04 07:15:40 +02:00
ext->max_value = ext_copy->max_value;
ext->cur_value = ext_copy->cur_value;
2019-12-02 07:37:33 -05:00
ext->type = ext_copy->type;
2020-01-18 23:34:34 +01:00
lv_style_list_copy(&ext->style_indic, &ext_copy->style_indic);
2017-04-11 10:50:57 +02:00
/*Refresh the style with new signal function*/
2020-01-18 23:34:34 +01:00
lv_obj_refresh_style(bar);
2017-04-11 10:50:57 +02:00
2020-01-18 23:34:34 +01:00
lv_bar_set_value(bar, ext->cur_value, LV_ANIM_OFF);
2017-04-11 10:50:57 +02:00
}
2018-07-25 17:57:08 +02:00
2018-10-05 17:22:49 +02:00
LV_LOG_INFO("bar created");
2018-07-25 17:57:08 +02:00
2020-01-18 23:34:34 +01:00
return bar;
2017-04-11 10:50:57 +02:00
}
/*=====================
* Setter functions
*====================*/
/**
* Set a new value on the bar
* @param bar pointer to a bar object
* @param value new value
2019-06-11 13:51:14 +02:00
* @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediatelly
2017-04-11 10:50:57 +02:00
*/
2019-06-11 13:51:14 +02:00
void lv_bar_set_value(lv_obj_t * bar, int16_t value, lv_anim_enable_t anim)
{
LV_ASSERT_OBJ(bar, LV_OBJX_NAME);
lv_bar_ext_t * ext = lv_obj_get_ext_attr(bar);
2018-06-19 09:49:58 +02:00
if(ext->cur_value == value) return;
int16_t new_value;
new_value = value > ext->max_value ? ext->max_value : value;
new_value = new_value < ext->min_value ? ext->min_value : new_value;
if(ext->cur_value == new_value) return;
2019-12-26 03:37:28 +01:00
#if LV_USE_ANIMATION == 0
ext->cur_value = new_value;
lv_obj_invalidate(bar);
#else
2019-12-02 07:37:33 -05:00
lv_bar_set_value_with_anim(bar, new_value, &ext->cur_value, &ext->cur_value_anim, anim);
2019-12-26 03:37:28 +01:00
#endif
2019-12-02 07:37:33 -05:00
}
2019-03-01 23:50:30 +01:00
2019-12-02 07:37:33 -05:00
/**
* Set a new start value on the bar
* @param bar pointer to a bar object
* @param value new start value
* @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediatelly
*/
void lv_bar_set_start_value(lv_obj_t * bar, int16_t start_value, lv_anim_enable_t anim)
{
LV_ASSERT_OBJ(bar, LV_OBJX_NAME);
lv_bar_ext_t * ext = lv_obj_get_ext_attr(bar);
if(ext->start_value == start_value) return;
int16_t new_value;
new_value = start_value > ext->max_value ? ext->max_value : start_value;
new_value = new_value < ext->min_value ? ext->min_value : start_value;
if(ext->start_value == new_value) return;
2019-12-26 03:37:28 +01:00
#if LV_USE_ANIMATION == 0
ext->start_value = new_value;
#else
lv_bar_set_value_with_anim(bar, new_value, &ext->start_value, &ext->start_value_anim, anim);
#endif
2019-03-01 23:50:30 +01:00
}
2017-04-11 10:50:57 +02:00
/**
2017-04-21 09:15:39 +02:00
* Set minimum and the maximum values of a bar
* @param bar pointer to the bar object
2017-04-11 10:50:57 +02:00
* @param min minimum value
* @param max maximum value
*/
void lv_bar_set_range(lv_obj_t * bar, int16_t min, int16_t max)
{
LV_ASSERT_OBJ(bar, LV_OBJX_NAME);
2018-06-19 09:49:58 +02:00
lv_bar_ext_t * ext = lv_obj_get_ext_attr(bar);
if(ext->min_value == min && ext->max_value == max) return;
ext->max_value = max;
ext->min_value = min;
2019-12-02 07:37:33 -05:00
2019-12-19 16:36:29 -05:00
if(lv_bar_get_type(bar) != LV_BAR_TYPE_CUSTOM)
ext->start_value = min;
2019-12-02 07:37:33 -05:00
2018-06-19 09:49:58 +02:00
if(ext->cur_value > max) {
ext->cur_value = max;
2019-03-01 23:50:30 +01:00
lv_bar_set_value(bar, ext->cur_value, false);
2018-06-19 09:49:58 +02:00
}
if(ext->cur_value < min) {
ext->cur_value = min;
2019-03-01 23:50:30 +01:00
lv_bar_set_value(bar, ext->cur_value, false);
}
2018-06-19 09:49:58 +02:00
lv_obj_invalidate(bar);
2017-04-11 10:50:57 +02:00
}
/**
2019-12-02 07:37:33 -05:00
* Set the type of bar.
* @param bar pointer to bar object
* @param type bar type
*/
2019-12-02 07:37:33 -05:00
void lv_bar_set_type(lv_obj_t * bar, lv_bar_type_t type)
{
2019-12-02 07:37:33 -05:00
LV_ASSERT_OBJ(bar, LV_OBJX_NAME);
lv_bar_ext_t * ext = lv_obj_get_ext_attr(bar);
2019-12-02 07:37:33 -05:00
ext->type = type;
if(ext->type != LV_BAR_TYPE_CUSTOM)
ext->start_value = ext->min_value;
lv_obj_invalidate(bar);
}
2019-06-17 16:05:30 +02:00
/**
* Set the animation time of the bar
* @param bar pointer to a bar object
* @param anim_time the animation time in milliseconds.
*/
void lv_bar_set_anim_time(lv_obj_t * bar, uint16_t anim_time)
{
LV_ASSERT_OBJ(bar, LV_OBJX_NAME);
#if LV_USE_ANIMATION
2019-06-17 16:05:30 +02:00
lv_bar_ext_t * ext = lv_obj_get_ext_attr(bar);
2019-06-27 07:16:15 +02:00
ext->anim_time = anim_time;
2019-06-20 14:28:25 +02:00
#else
2019-06-27 07:16:15 +02:00
(void)bar; /*Unused*/
(void)anim_time; /*Unused*/
#endif
2019-06-17 16:05:30 +02:00
}
2017-04-11 10:50:57 +02:00
/*=====================
* Getter functions
*====================*/
/**
* Get the value of a bar
* @param bar pointer to a bar object
* @return the value of the bar
*/
int16_t lv_bar_get_value(const lv_obj_t * bar)
2017-04-11 10:50:57 +02:00
{
LV_ASSERT_OBJ(bar, LV_OBJX_NAME);
2018-06-19 09:49:58 +02:00
lv_bar_ext_t * ext = lv_obj_get_ext_attr(bar);
return LV_BAR_GET_ANIM_VALUE(ext->cur_value, ext->cur_value_anim);
2017-04-11 10:50:57 +02:00
}
2019-12-02 07:37:33 -05:00
/**
* Get the start value of a bar
* @param bar pointer to a bar object
* @return the start value of the bar
*/
int16_t lv_bar_get_start_value(const lv_obj_t * bar)
{
LV_ASSERT_OBJ(bar, LV_OBJX_NAME);
lv_bar_ext_t * ext = lv_obj_get_ext_attr(bar);
if(ext->type != LV_BAR_TYPE_CUSTOM) return ext->min_value;
return LV_BAR_GET_ANIM_VALUE(ext->start_value, ext->start_value_anim);
2019-12-02 07:37:33 -05:00
}
2017-04-21 09:15:39 +02:00
/**
* Get the minimum value of a bar
* @param bar pointer to a bar object
* @return the minimum value of the bar
*/
int16_t lv_bar_get_min_value(const lv_obj_t * bar)
2017-04-21 09:15:39 +02:00
{
LV_ASSERT_OBJ(bar, LV_OBJX_NAME);
lv_bar_ext_t * ext = lv_obj_get_ext_attr(bar);
2017-04-21 09:15:39 +02:00
return ext->min_value;
}
/**
* Get the maximum value of a bar
* @param bar pointer to a bar object
* @return the maximum value of the bar
*/
int16_t lv_bar_get_max_value(const lv_obj_t * bar)
2017-04-21 09:15:39 +02:00
{
LV_ASSERT_OBJ(bar, LV_OBJX_NAME);
lv_bar_ext_t * ext = lv_obj_get_ext_attr(bar);
2017-04-21 09:15:39 +02:00
return ext->max_value;
}
/**
2019-12-02 07:37:33 -05:00
* Get the type of bar.
* @param bar pointer to bar object
* @return bar type
*/
2019-12-02 07:37:33 -05:00
lv_bar_type_t lv_bar_get_type(lv_obj_t * bar) {
LV_ASSERT_OBJ(bar, LV_OBJX_NAME);
lv_bar_ext_t * ext = lv_obj_get_ext_attr(bar);
2019-12-02 07:37:33 -05:00
return ext->type;
}
2019-06-17 16:05:30 +02:00
/**
* Get the animation time of the bar
* @param bar pointer to a bar object
* @return the animation time in milliseconds.
*/
uint16_t lv_bar_get_anim_time(const lv_obj_t * bar)
2019-06-17 16:05:30 +02:00
{
LV_ASSERT_OBJ(bar, LV_OBJX_NAME);
2019-06-17 16:05:30 +02:00
#if LV_USE_ANIMATION
lv_bar_ext_t * ext = lv_obj_get_ext_attr(bar);
return ext->anim_time;
#else
2019-06-27 07:16:15 +02:00
(void)bar; /*Unused*/
2019-06-17 16:05:30 +02:00
return 0;
#endif
}
2017-04-11 10:50:57 +02:00
/**********************
* STATIC FUNCTIONS
**********************/
/**
* Handle the drawing related tasks of the bars
* @param bar pointer to an object
2019-09-06 19:53:39 +02:00
* @param clip_area the object will be drawn only in this area
2017-04-11 10:50:57 +02:00
* @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`
2017-04-11 10:50:57 +02:00
*/
2019-09-06 19:53:39 +02:00
static lv_design_res_t lv_bar_design(lv_obj_t * bar, const lv_area_t * clip_area, lv_design_mode_t mode)
2017-04-11 10:50:57 +02:00
{
if(mode == LV_DESIGN_COVER_CHK) {
2018-06-19 09:49:58 +02:00
/*Return false if the object is not covers the mask area*/
2019-09-06 19:53:39 +02:00
return ancestor_design_f(bar, clip_area, mode);
2017-04-11 10:50:57 +02:00
} else if(mode == LV_DESIGN_DRAW_MAIN) {
2019-12-31 22:13:32 +01:00
draw_bg(bar, clip_area);
draw_indic(bar, clip_area);
2019-09-16 10:58:28 +02:00
2020-02-15 00:33:26 +01:00
/*Get the value and draw it after the indicator*/
lv_draw_rect_dsc_t draw_dsc;
lv_draw_rect_dsc_init(&draw_dsc);
draw_dsc.bg_opa = LV_OPA_TRANSP;
draw_dsc.border_opa = LV_OPA_TRANSP;
draw_dsc.shadow_opa = LV_OPA_TRANSP;
draw_dsc.pattern_opa = LV_OPA_TRANSP;
draw_dsc.outline_opa = LV_OPA_TRANSP;
lv_obj_init_draw_rect_dsc(bar, LV_BAR_PART_BG, &draw_dsc);
lv_draw_rect(&bar->coords, clip_area, &draw_dsc);
2019-09-16 10:58:28 +02:00
} else if(mode == LV_DESIGN_DRAW_POST) {
}
return LV_DESIGN_RES_OK;
}
2019-12-31 22:13:32 +01:00
static void draw_bg(lv_obj_t * bar, const lv_area_t * clip_area)
2019-09-16 10:58:28 +02:00
{
2019-12-31 22:13:32 +01:00
/*Simply draw the background*/
lv_draw_rect_dsc_t draw_dsc;
lv_draw_rect_dsc_init(&draw_dsc);
lv_obj_init_draw_rect_dsc(bar, LV_BAR_PART_BG, &draw_dsc);
lv_draw_rect(&bar->coords, clip_area, &draw_dsc);
2019-09-16 10:58:28 +02:00
}
2017-04-11 10:50:57 +02:00
2019-12-31 22:13:32 +01:00
static void draw_indic(lv_obj_t * bar, const lv_area_t * clip_area)
2019-09-16 10:58:28 +02:00
{
lv_bar_ext_t * ext = lv_obj_get_ext_attr(bar);
lv_coord_t objw = lv_obj_get_width(bar);
lv_coord_t objh = lv_obj_get_height(bar);
int32_t range = ext->max_value - ext->min_value;
bool hor = objw >= objh ? true : false;
bool sym = false;
if(ext->type == LV_BAR_TYPE_SYM && ext->min_value < 0 && ext->max_value > 0 && ext->start_value == ext->min_value) sym = true;
2019-09-16 10:58:28 +02:00
/*Calculate the indicator area*/
lv_style_int_t bg_left = lv_obj_get_style_pad_left(bar, LV_BAR_PART_BG);
lv_style_int_t bg_right = lv_obj_get_style_pad_right(bar, LV_BAR_PART_BG);
lv_style_int_t bg_top = lv_obj_get_style_pad_top(bar, LV_BAR_PART_BG);
lv_style_int_t bg_bottom = lv_obj_get_style_pad_bottom(bar, LV_BAR_PART_BG);
2019-09-16 10:58:28 +02:00
/*Respect padding and minimum width/height too*/
2019-12-31 22:13:32 +01:00
lv_area_copy(&ext->indic_area, &bar->coords);
ext->indic_area.x1 += bg_left;
ext->indic_area.x2 -= bg_right;
ext->indic_area.y1 += bg_top;
ext->indic_area.y2 -= bg_bottom;
2019-09-16 10:58:28 +02:00
if(hor && lv_area_get_height(&ext->indic_area) < LV_BAR_SIZE_MIN) {
ext->indic_area.y1 = bar->coords.y1 + (objh / 2) - (LV_BAR_SIZE_MIN / 2);
ext->indic_area.y2 = ext->indic_area.y1 + LV_BAR_SIZE_MIN;
} else if(!hor && lv_area_get_width(&ext->indic_area) < LV_BAR_SIZE_MIN) {
ext->indic_area.x1 = bar->coords.x1 + (objw / 2) - (LV_BAR_SIZE_MIN / 2);
ext->indic_area.x2 = ext->indic_area.x1 + LV_BAR_SIZE_MIN;
}
lv_coord_t indicw = lv_area_get_width(&ext->indic_area);
lv_coord_t indich = lv_area_get_height(&ext->indic_area);
/*Calculate the indicator length*/
lv_coord_t anim_length = hor ? indicw : indich;
lv_coord_t anim_cur_value_x, anim_start_value_x;
lv_coord_t * axis1, * axis2;
lv_coord_t (*indic_length_calc)(const lv_area_t *area);
if(hor) {
axis1 = &ext->indic_area.x1;
axis2 = &ext->indic_area.x2;
indic_length_calc = lv_area_get_width;
} else {
axis1 = &ext->indic_area.y1;
axis2 = &ext->indic_area.y2;
indic_length_calc = lv_area_get_height;
}
2019-12-02 07:37:33 -05:00
#if LV_USE_ANIMATION
if(LV_BAR_IS_ANIMATING(ext->start_value_anim)) {
lv_coord_t anim_start_value_start_x =
(int32_t)((int32_t)anim_length * (ext->start_value_anim.anim_start - ext->min_value)) / range;
lv_coord_t anim_start_value_end_x =
(int32_t)((int32_t)anim_length * (ext->start_value_anim.anim_end - ext->min_value)) / range;
2020-01-09 19:11:14 -05:00
anim_start_value_x = (((anim_start_value_end_x - anim_start_value_start_x) * ext->start_value_anim.anim_state) / LV_BAR_ANIM_STATE_END);
2020-01-09 19:11:14 -05:00
if(anim_start_value_end_x < anim_start_value_start_x)
anim_start_value_x += anim_start_value_start_x;
} else
#endif
{
anim_start_value_x = (int32_t)((int32_t)anim_length * (ext->start_value - ext->min_value)) / range;
}
2019-12-02 07:37:33 -05:00
#if LV_USE_ANIMATION
if(LV_BAR_IS_ANIMATING(ext->cur_value_anim)) {
lv_coord_t anim_cur_value_start_x =
(int32_t)((int32_t)anim_length * (ext->cur_value_anim.anim_start - ext->min_value)) / range;
lv_coord_t anim_cur_value_end_x =
(int32_t)((int32_t)anim_length * (ext->cur_value_anim.anim_end - ext->min_value)) / range;
2020-01-09 19:11:14 -05:00
anim_cur_value_x = (((anim_cur_value_end_x - anim_cur_value_start_x) * ext->cur_value_anim.anim_state) / LV_BAR_ANIM_STATE_END);
2020-01-09 19:11:14 -05:00
if(anim_cur_value_end_x < anim_cur_value_start_x)
anim_cur_value_x += anim_cur_value_start_x;
} else
2019-12-02 07:37:33 -05:00
#endif
{
anim_cur_value_x = (int32_t)((int32_t)anim_length * (ext->cur_value - ext->min_value)) / range;
}
2019-12-02 07:37:33 -05:00
/* Set the indicator length */
2020-02-13 06:56:14 +01:00
if(hor) {
*axis2 = *axis1 + anim_cur_value_x;
*axis1 += anim_start_value_x;
} else {
*axis2 -= anim_start_value_x;
*axis1 = *axis2 - anim_cur_value_x;
}
if(sym) {
lv_coord_t zero;
zero = *axis1 + (-ext->min_value * anim_length) / range;
if(*axis2 > zero)
*axis1 = zero;
else {
*axis1 = *axis2;
*axis2 = zero;
}
2017-04-11 10:50:57 +02:00
}
2019-09-16 10:58:28 +02:00
/*Draw the indicator*/
/*Do not draw a zero length indicator*/
2020-01-18 23:34:34 +01:00
if(!sym && indic_length_calc(&ext->indic_area) <= 1) return;
2019-09-16 10:58:28 +02:00
uint16_t bg_radius = lv_obj_get_style_radius(bar, LV_BAR_PART_BG);
lv_coord_t short_side = LV_MATH_MIN(objw, objh);
if(bg_radius > short_side >> 1) bg_radius = short_side >> 1;
2019-12-31 22:13:32 +01:00
lv_draw_rect_dsc_t draw_indic_dsc;
lv_draw_rect_dsc_init(&draw_indic_dsc);
lv_obj_init_draw_rect_dsc(bar, LV_BAR_PART_INDIC, &draw_indic_dsc);
/* Draw only the shadow if the indicator is long enough.
* The radius of the bg and the indicator can make a strange shape where
* it'd be very difficult to draw shadow. */
if((hor && lv_area_get_width(&ext->indic_area) > bg_radius * 2) ||
(!hor && lv_area_get_height(&ext->indic_area) > bg_radius * 2)) {
2019-12-31 22:13:32 +01:00
lv_opa_t bg_opa = draw_indic_dsc.bg_opa;
lv_opa_t border_opa = draw_indic_dsc.border_opa;
2020-02-07 01:44:22 +01:00
lv_opa_t value_opa = draw_indic_dsc.border_opa;
2020-02-12 08:54:03 +01:00
const void * pattern_src = draw_indic_dsc.pattern_image;
2019-12-31 22:13:32 +01:00
draw_indic_dsc.bg_opa = LV_OPA_TRANSP;
draw_indic_dsc.border_opa = LV_OPA_TRANSP;
2020-02-07 01:44:22 +01:00
draw_indic_dsc.value_opa = LV_OPA_TRANSP;
draw_indic_dsc.pattern_image = NULL;
2019-12-31 22:13:32 +01:00
lv_draw_rect(&ext->indic_area, clip_area, &draw_indic_dsc);
draw_indic_dsc.bg_opa = bg_opa;
draw_indic_dsc.border_opa = border_opa;
2020-02-07 01:44:22 +01:00
draw_indic_dsc.value_opa = value_opa;
draw_indic_dsc.pattern_image = pattern_src;
2020-01-18 23:34:34 +01:00
}
lv_draw_mask_radius_param_t mask_bg_param;
2019-12-31 22:13:32 +01:00
lv_draw_mask_radius_init(&mask_bg_param, &bar->coords, bg_radius, false);
int16_t mask_bg_id = lv_draw_mask_add(&mask_bg_param, NULL);
2019-11-16 11:22:23 +01:00
2020-02-07 01:44:22 +01:00
/*Draw_only the background and the pattern*/
2019-12-31 22:13:32 +01:00
lv_opa_t shadow_opa = draw_indic_dsc.shadow_opa;
lv_opa_t border_opa = draw_indic_dsc.border_opa;
2020-02-07 01:44:22 +01:00
lv_opa_t value_opa = draw_indic_dsc.value_opa;
2019-12-31 22:13:32 +01:00
draw_indic_dsc.border_opa = LV_OPA_TRANSP;
draw_indic_dsc.shadow_opa = LV_OPA_TRANSP;
2020-02-07 01:44:22 +01:00
draw_indic_dsc.value_opa = LV_OPA_TRANSP;
2019-11-16 11:22:23 +01:00
/*Get the max possible indicator area. The gradient should be applied on this*/
lv_area_t mask_indic_max_area;
lv_area_copy(&mask_indic_max_area, &bar->coords);
2019-12-31 22:13:32 +01:00
mask_indic_max_area.x1 += bg_left;
mask_indic_max_area.y1 += bg_top;
mask_indic_max_area.x2 -= bg_right;
mask_indic_max_area.y2 -= bg_bottom;
2020-01-18 23:34:34 +01:00
if(hor && lv_area_get_height(&mask_indic_max_area) < LV_BAR_SIZE_MIN) {
mask_indic_max_area.y1 = bar->coords.y1 + (objh / 2) - (LV_BAR_SIZE_MIN / 2);
mask_indic_max_area.y2 = mask_indic_max_area.y1 + LV_BAR_SIZE_MIN;
} else if(!hor && lv_area_get_width(&mask_indic_max_area) < LV_BAR_SIZE_MIN) {
mask_indic_max_area.x1 = bar->coords.x1 + (objw / 2) - (LV_BAR_SIZE_MIN / 2);
mask_indic_max_area.x2 = mask_indic_max_area.x1 + LV_BAR_SIZE_MIN;
}
2019-11-16 11:22:23 +01:00
/*Create a mask to the current indicator area to see only this part from the whole gradient.*/
2019-11-16 11:22:23 +01:00
lv_draw_mask_radius_param_t mask_indic_param;
2019-12-31 22:13:32 +01:00
lv_draw_mask_radius_init(&mask_indic_param, &ext->indic_area, draw_indic_dsc.radius, false);
2019-11-16 11:22:23 +01:00
int16_t mask_indic_id = lv_draw_mask_add(&mask_indic_param, NULL);
2019-12-31 22:13:32 +01:00
lv_draw_rect(&mask_indic_max_area, clip_area, &draw_indic_dsc);
draw_indic_dsc.border_opa = border_opa;
draw_indic_dsc.shadow_opa = shadow_opa;
2020-02-07 01:44:22 +01:00
draw_indic_dsc.value_opa = value_opa;
2020-02-15 00:33:26 +01:00
/*Draw the border*/
2019-12-31 22:13:32 +01:00
draw_indic_dsc.bg_opa = LV_OPA_TRANSP;
draw_indic_dsc.shadow_opa = LV_OPA_TRANSP;
2020-02-15 00:33:26 +01:00
draw_indic_dsc.value_opa = LV_OPA_TRANSP;
draw_indic_dsc.pattern_image = NULL;
2019-12-31 22:13:32 +01:00
lv_draw_rect(&ext->indic_area, clip_area, &draw_indic_dsc);
2019-11-16 11:22:23 +01:00
lv_draw_mask_remove_id(mask_indic_id);
lv_draw_mask_remove_id(mask_bg_id);
2020-02-15 00:33:26 +01:00
/*When not masks draw the value*/
draw_indic_dsc.value_opa = value_opa;
lv_draw_rect(&ext->indic_area, clip_area, &draw_indic_dsc);
2017-04-11 10:50:57 +02:00
}
2017-11-08 11:07:29 +01:00
/**
* Signal function of the bar
* @param bar pointer to a bar 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_bar_signal(lv_obj_t * bar, lv_signal_t sign, void * param)
{
lv_res_t res;
2019-12-31 22:13:32 +01:00
if(sign == LV_SIGNAL_GET_STYLE) {
lv_get_style_info_t * info = param;
info->result = lv_bar_get_style(bar, info->part);
if(info->result != NULL) return LV_RES_OK;
else return ancestor_signal(bar, sign, param);
2019-12-31 22:13:32 +01:00
}
2017-11-08 11:07:29 +01:00
/* Include the ancient signal function */
res = ancestor_signal(bar, 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);
2017-11-08 11:07:29 +01:00
if(sign == LV_SIGNAL_REFR_EXT_DRAW_PAD) {
2019-12-31 22:13:32 +01:00
lv_coord_t indic_size;
2020-02-12 08:54:03 +01:00
indic_size = lv_obj_get_draw_rect_ext_pad_size(bar, LV_BAR_PART_INDIC);
2019-12-31 22:13:32 +01:00
/*Bg size is handled by lv_obj*/
2019-09-16 10:58:28 +02:00
bar->ext_draw_pad = LV_MATH_MAX(bar->ext_draw_pad, indic_size);
2018-02-28 15:37:41 +01:00
}
2019-12-02 07:37:33 -05:00
if(sign == LV_SIGNAL_CLEANUP) {
lv_obj_clean_style_list(bar, LV_BAR_PART_INDIC);
2019-12-26 03:37:28 +01:00
#if LV_USE_ANIMATION
2020-02-22 07:06:31 +01:00
lv_bar_ext_t * ext = lv_obj_get_ext_attr(bar);
2019-12-02 07:37:33 -05:00
lv_anim_del(&ext->cur_value_anim, NULL);
lv_anim_del(&ext->start_value_anim, NULL);
2019-12-26 03:37:28 +01:00
#endif
2019-12-02 07:37:33 -05:00
}
return res;
2017-11-08 11:07:29 +01:00
}
static lv_style_list_t * lv_bar_get_style(lv_obj_t * bar, uint8_t part)
2019-12-31 22:13:32 +01:00
{
LV_ASSERT_OBJ(bar, LV_OBJX_NAME);
lv_bar_ext_t * ext = lv_obj_get_ext_attr(bar);
lv_style_list_t * style_dsc_p;
2019-12-31 22:13:32 +01:00
switch(part) {
case LV_BAR_PART_BG:
2020-01-16 21:25:11 +01:00
style_dsc_p = &bar->style_list;
2019-12-31 22:13:32 +01:00
break;
case LV_BAR_PART_INDIC:
style_dsc_p = &ext->style_indic;
break;
default:
style_dsc_p = NULL;
}
return style_dsc_p;
}
#if LV_USE_ANIMATION
2019-12-02 07:37:33 -05:00
static void lv_bar_anim(lv_bar_anim_t * var, lv_anim_value_t value)
{
var->anim_state = value;
2019-12-02 07:37:33 -05:00
lv_obj_invalidate(var->bar);
}
2019-04-22 08:45:07 +02:00
static void lv_bar_anim_ready(lv_anim_t * a)
{
2019-12-02 07:37:33 -05:00
lv_bar_anim_t * var = a->var;
lv_bar_ext_t * ext = lv_obj_get_ext_attr(var->bar);
var->anim_state = LV_BAR_ANIM_STATE_INV;
2019-12-02 07:37:33 -05:00
if(var == &ext->cur_value_anim)
ext->cur_value = var->anim_end;
else if(var == &ext->start_value_anim)
ext->start_value = var->anim_end;
lv_obj_invalidate(var->bar);
}
2019-12-02 07:37:33 -05:00
static void lv_bar_set_value_with_anim(lv_obj_t * bar, int16_t new_value, int16_t *value_ptr, lv_bar_anim_t *anim_info, lv_anim_enable_t en) {
if(en == LV_ANIM_OFF) {
*value_ptr = new_value;
lv_obj_invalidate(bar);
} else {
lv_bar_ext_t *ext = lv_obj_get_ext_attr(bar);
/*No animation in progress -> simply set the values*/
if(anim_info->anim_state == LV_BAR_ANIM_STATE_INV) {
2019-12-02 07:37:33 -05:00
anim_info->anim_start = *value_ptr;
anim_info->anim_end = new_value;
}
/*Animation in progress. Start from the animation end value*/
else {
anim_info->anim_start = anim_info->anim_end;
anim_info->anim_end = new_value;
}
/* Stop the previous animation if it exists */
lv_anim_del(anim_info, NULL);
lv_anim_t a;
2020-02-19 06:18:24 +01:00
lv_anim_init(&a);
lv_anim_set_var(&a, anim_info);
lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_bar_anim);
lv_anim_set_values(&a, LV_BAR_ANIM_STATE_START, LV_BAR_ANIM_STATE_END);
lv_anim_set_ready_cb(&a, lv_bar_anim_ready);
lv_anim_set_time(&a, ext->anim_time);
lv_anim_start(&a);
2020-01-10 07:58:39 +01:00
ext->cur_value = new_value;
2019-12-02 07:37:33 -05:00
}
}
static void lv_bar_init_anim(lv_obj_t * bar, lv_bar_anim_t * bar_anim)
{
bar_anim->bar = bar;
bar_anim->anim_start = 0;
bar_anim->anim_end = 0;
bar_anim->anim_state = LV_BAR_ANIM_STATE_INV;
2019-12-02 07:37:33 -05:00
}
2019-12-26 03:37:28 +01:00
#endif
2019-12-02 07:37:33 -05:00
2017-04-11 10:50:57 +02:00
#endif