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

700 lines
22 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);
2019-12-31 22:13:32 +01:00
static lv_style_dsc_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*/
lv_obj_t * new_bar = lv_obj_create(par, copy);
2019-09-24 23:14:17 +02:00
LV_ASSERT_MEM(new_bar);
2018-10-05 17:22:49 +02:00
if(new_bar == NULL) return NULL;
2017-04-11 10:50:57 +02:00
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_bar);
if(ancestor_design_f == NULL) ancestor_design_f = lv_obj_get_design_cb(new_bar);
2017-11-08 11:07:29 +01:00
2017-04-11 10:50:57 +02:00
/*Allocate the object type specific extended data*/
lv_bar_ext_t * ext = lv_obj_allocate_ext_attr(new_bar, sizeof(lv_bar_ext_t));
2019-09-24 23:14:17 +02:00
LV_ASSERT_MEM(ext);
if(ext == NULL) {
lv_obj_del(new_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;
2019-12-02 07:37:33 -05:00
lv_bar_init_anim(new_bar, &ext->cur_value_anim);
lv_bar_init_anim(new_bar, &ext->start_value_anim);
#endif
2019-12-02 07:37:33 -05:00
ext->type = LV_BAR_TYPE_NORMAL;
2019-12-31 22:13:32 +01:00
lv_style_dsc_init(&ext->style_indic);
2017-04-11 10:50:57 +02:00
lv_obj_set_signal_cb(new_bar, lv_bar_signal);
lv_obj_set_design_cb(new_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) {
lv_obj_set_click(new_bar, false);
lv_obj_set_size(new_bar, LV_DPI * 2, LV_DPI / 4);
2019-03-01 23:50:30 +01:00
lv_bar_set_value(new_bar, ext->cur_value, false);
2019-12-31 22:13:32 +01:00
_ot(new_bar, LV_BAR_PART_BG, BAR);
_ot(new_bar, LV_BAR_PART_INDIC, BAR_INDIC);
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;
ext->style_indic = ext_copy->style_indic;
2019-12-02 07:37:33 -05:00
ext->type = ext_copy->type;
2017-04-11 10:50:57 +02:00
/*Refresh the style with new signal function*/
2019-12-31 22:13:32 +01:00
lv_obj_refresh_style(new_bar, LV_OBJ_PART_ALL);
2017-04-11 10:50:57 +02:00
2019-03-01 23:50:30 +01:00
lv_bar_set_value(new_bar, ext->cur_value, false);
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
2017-04-11 10:50:57 +02:00
return new_bar;
}
/*=====================
* 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
} 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*/
2019-12-31 22:13:32 +01:00
lv_style_value_t bg_left = lv_obj_get_style_value(bar, LV_BAR_PART_BG, LV_STYLE_PAD_LEFT);
lv_style_value_t bg_right = lv_obj_get_style_value(bar, LV_BAR_PART_BG, LV_STYLE_PAD_RIGHT);
lv_style_value_t bg_top = lv_obj_get_style_value(bar, LV_BAR_PART_BG, LV_STYLE_PAD_TOP);
lv_style_value_t bg_bottom = lv_obj_get_style_value(bar, LV_BAR_PART_BG, LV_STYLE_PAD_BOTTOM);
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;
anim_start_value_x = (((anim_start_value_end_x - anim_start_value_start_x) * ext->start_value_anim.anim_state) >> LV_BAR_ANIM_STATE_NORM);
if(anim_start_value_x < 0)
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;
anim_cur_value_x = (((anim_cur_value_end_x - anim_cur_value_start_x) * ext->cur_value_anim.anim_state) >> LV_BAR_ANIM_STATE_NORM);
if(anim_cur_value_x < 0)
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 */
*axis2 = *axis1 + anim_cur_value_x;
*axis1 += anim_start_value_x;
2018-10-05 17:22:49 +02:00
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*/
if(!sym && indic_length_calc(&ext->indic_area) == 0) return;
2019-09-16 10:58:28 +02:00
2019-12-31 22:13:32 +01:00
uint16_t bg_radius = lv_obj_get_style_value(bar, LV_BAR_PART_BG, LV_STYLE_RADIUS);
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;
draw_indic_dsc.bg_opa = LV_OPA_TRANSP;
draw_indic_dsc.border_opa = LV_OPA_TRANSP;
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;
}
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
/*Draw_only the background*/
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;
draw_indic_dsc.border_opa = LV_OPA_TRANSP;
draw_indic_dsc.shadow_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;
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;
/*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;
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);
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) {
uint8_t ** type_p = param;
lv_style_dsc_t ** style_dsc_p = param;
*style_dsc_p = lv_bar_get_style(bar, **type_p);
return LV_RES_OK;
}
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_style_value_t bg_sh_width = lv_obj_get_style_value(bar, LV_BAR_PART_BG, LV_STYLE_SHADOW_WIDTH);
lv_style_value_t bg_sh_spread = lv_obj_get_style_value(bar, LV_BAR_PART_BG, LV_STYLE_SHADOW_SPREAD);
lv_style_value_t bg_sh_ofs_x = lv_obj_get_style_value(bar, LV_BAR_PART_BG, LV_STYLE_SHADOW_OFFSET_X);
lv_style_value_t bg_sh_ofs_y = lv_obj_get_style_value(bar, LV_BAR_PART_BG, LV_STYLE_SHADOW_OFFSET_Y);
lv_coord_t bg_size = bg_sh_width + bg_sh_spread;
bg_size += LV_MATH_MAX(LV_MATH_ABS(bg_sh_ofs_x), LV_MATH_ABS(bg_sh_ofs_y));
2019-09-16 10:58:28 +02:00
2019-12-31 22:13:32 +01:00
lv_style_value_t indic_sh_width = lv_obj_get_style_value(bar, LV_BAR_PART_INDIC, LV_STYLE_SHADOW_WIDTH);
lv_style_value_t indic_sh_spread = lv_obj_get_style_value(bar, LV_BAR_PART_INDIC, LV_STYLE_SHADOW_SPREAD);
lv_style_value_t indic_sh_ofs_x = lv_obj_get_style_value(bar, LV_BAR_PART_INDIC, LV_STYLE_SHADOW_OFFSET_X);
lv_style_value_t indic_sh_ofs_y = lv_obj_get_style_value(bar, LV_BAR_PART_INDIC, LV_STYLE_SHADOW_OFFSET_Y);
2019-09-16 10:58:28 +02:00
2019-12-31 22:13:32 +01:00
lv_coord_t indic_size = indic_sh_width + indic_sh_spread;
indic_size += LV_MATH_MAX(LV_MATH_ABS(indic_sh_ofs_x), LV_MATH_ABS(indic_sh_ofs_y));
2019-09-16 10:58:28 +02:00
bar->ext_draw_pad = LV_MATH_MAX(bar->ext_draw_pad, bg_size);
bar->ext_draw_pad = LV_MATH_MAX(bar->ext_draw_pad, indic_size);
2018-02-28 15:37:41 +01:00
}
2017-11-08 11:07:29 +01:00
2019-12-02 07:37:33 -05:00
if(sign == LV_SIGNAL_CLEANUP) {
2019-12-26 03:37:28 +01:00
#if LV_USE_ANIMATION
2019-12-02 07:37:33 -05:00
lv_bar_ext_t * ext = lv_obj_get_ext_attr(bar);
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
}
2019-12-31 22:13:32 +01:00
static lv_style_dsc_t * lv_bar_get_style(lv_obj_t * bar, uint8_t part)
{
LV_ASSERT_OBJ(bar, LV_OBJX_NAME);
lv_bar_ext_t * ext = lv_obj_get_ext_attr(bar);
lv_style_dsc_t * style_dsc_p;
switch(part) {
case LV_BAR_PART_BG:
style_dsc_p = &bar->style_dsc;
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;
a.var = anim_info;
a.start = LV_BAR_ANIM_STATE_START;
a.end = LV_BAR_ANIM_STATE_END;
2019-12-02 07:37:33 -05:00
a.exec_cb = (lv_anim_exec_xcb_t)lv_bar_anim;
a.path_cb = lv_anim_path_linear;
a.ready_cb = lv_bar_anim_ready;
a.act_time = 0;
a.time = ext->anim_time;
a.playback = 0;
a.playback_pause = 0;
a.repeat = 0;
a.repeat_pause = 0;
lv_anim_create(&a);
}
}
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