lv_bar: add symmetric feature to start the indicator from zero

This commit is contained in:
Gabor Kiss-Vamosi
2019-01-01 14:14:17 +01:00
parent 1927a4cf47
commit d3101f8e96
2 changed files with 62 additions and 1 deletions

View File

@@ -70,6 +70,7 @@ lv_obj_t * lv_bar_create(lv_obj_t * par, const lv_obj_t * copy)
ext->min_value = 0;
ext->max_value = 100;
ext->cur_value = 0;
ext->sym = 0;
ext->style_indic = &lv_style_pretty_color;
lv_obj_set_signal_func(new_bar, lv_bar_signal);
@@ -94,6 +95,7 @@ lv_obj_t * lv_bar_create(lv_obj_t * par, const lv_obj_t * copy)
ext->max_value = ext_copy->max_value;
ext->cur_value = ext_copy->cur_value;
ext->style_indic = ext_copy->style_indic;
ext->sym = ext_copy->sym;
/*Refresh the style with new signal function*/
lv_obj_refresh_style(new_bar);
@@ -183,6 +185,16 @@ void lv_bar_set_range(lv_obj_t * bar, int16_t min, int16_t max)
lv_obj_invalidate(bar);
}
/**
* Make the bar symmetric to zero. The indicator will grow from zero instead of the minimum position.
* @param bar pointer to a bar object
* @param en true: enable disable symmetric behavior; false: disable
*/
void lv_bar_set_sym(lv_obj_t * bar, bool en)
{
lv_bar_ext_t * ext = lv_obj_get_ext_attr(bar);
ext->sym = en ? 1 : 0;
}
/**
* Set a style of a bar
@@ -242,6 +254,17 @@ int16_t lv_bar_get_max_value(const lv_obj_t * bar)
return ext->max_value;
}
/**
* Get whether the bar is symmetric or not.
* @param bar pointer to a bar object
* @return true: symmetric is enabled; false: disable
*/
bool lv_bar_get_sym(lv_obj_t * bar)
{
lv_bar_ext_t * ext = lv_obj_get_ext_attr(bar);
return ext->sym ? true : false;
}
/**
* Get a style of a bar
* @param bar pointer to a bar object
@@ -307,7 +330,7 @@ static bool lv_bar_design(lv_obj_t * bar, const lv_area_t * mask, lv_design_mode
#endif
lv_bar_ext_t * ext = lv_obj_get_ext_attr(bar);
if(ext->cur_value != ext->min_value) {
if(ext->cur_value != ext->min_value || ext->sym) {
lv_style_t * style_indic = lv_bar_get_style(bar, LV_BAR_STYLE_INDIC);
lv_area_t indic_area;
lv_area_copy(&indic_area, &bar->coords);
@@ -320,13 +343,37 @@ static bool lv_bar_design(lv_obj_t * bar, const lv_area_t * mask, lv_design_mode
lv_coord_t h = lv_area_get_height(&indic_area);
if(w >= h) {
/*Horizontal*/
indic_area.x2 = (int32_t)((int32_t)w * (ext->cur_value - ext->min_value)) / (ext->max_value - ext->min_value);
indic_area.x2 = indic_area.x1 + indic_area.x2 - 1;
if(ext->sym && ext->min_value < 0 && ext->max_value > 0) {
/*Calculate the coordinate of the zero point*/
lv_coord_t zero;
zero = indic_area.x1 + (-ext->min_value * w) / (ext->max_value - ext->min_value);
if(indic_area.x2 > zero) indic_area.x1 = zero;
else {
indic_area.x1 = indic_area.x2;
indic_area.x2 = zero;
}
}
} else {
indic_area.y1 = (int32_t)((int32_t)h * (ext->cur_value - ext->min_value)) / (ext->max_value - ext->min_value);
indic_area.y1 = indic_area.y2 - indic_area.y1 + 1;
if(ext->sym && ext->min_value < 0 && ext->max_value > 0) {
/*Calculate the coordinate of the zero point*/
lv_coord_t zero;
zero = indic_area.y2 - (-ext->min_value * h) / (ext->max_value - ext->min_value);
if(indic_area.y1 < zero) indic_area.y2 = zero;
else {
indic_area.y2 = indic_area.y1;
indic_area.y1 = zero;
}
}
}
/*Draw the indicator*/
lv_draw_rect(&indic_area, mask, style_indic, opa_scale);
}

View File

@@ -42,6 +42,7 @@ typedef struct
int16_t cur_value; /*Current value of the bar*/
int16_t min_value; /*Minimum value of the bar*/
int16_t max_value; /*Maximum value of the bar*/
uint8_t sym :1; /*Symmetric: means the center is around zero value*/
lv_style_t *style_indic; /*Style of the indicator*/
} lv_bar_ext_t;
@@ -91,6 +92,13 @@ void lv_bar_set_value_anim(lv_obj_t * bar, int16_t value, uint16_t anim_time);
*/
void lv_bar_set_range(lv_obj_t * bar, int16_t min, int16_t max);
/**
* Make the bar symmetric to zero. The indicator will grow from zero instead of the minimum position.
* @param bar pointer to a bar object
* @param en true: enable disable symmetric behavior; false: disable
*/
void lv_bar_set_sym(lv_obj_t * bar, bool en);
/**
* Set a style of a bar
* @param bar pointer to a bar object
@@ -124,6 +132,12 @@ int16_t lv_bar_get_min_value(const lv_obj_t * bar);
*/
int16_t lv_bar_get_max_value(const lv_obj_t * bar);
/**
* Get whether the bar is symmetric or not.
* @param bar pointer to a bar object
* @return true: symmetric is enabled; false: disable
*/
bool lv_bar_get_sym(lv_obj_t * bar);
/**
* Get a style of a bar