fix(examples) update the outdated bar and slider examples

This commit is contained in:
Gabor Kiss-Vamosi
2021-05-02 21:05:09 +02:00
parent 5662c8d52d
commit e76ccc7ed2
5 changed files with 172 additions and 143 deletions

View File

@@ -4,10 +4,6 @@
static void set_temp(void * bar, int32_t temp)
{
lv_bar_set_value(bar, temp, LV_ANIM_ON);
static char buf[10]; /*Only the pointer t saved so must be static*/
lv_snprintf(buf, sizeof(buf), "%d°C", temp);
// lv_obj_set_style_content_text(bar, LV_PART_INDICATOR, LV_STATE_DEFAULT, buf);
}
/**
@@ -22,9 +18,6 @@ void lv_example_bar_3(void)
lv_style_set_bg_color(&style_indic, lv_palette_main(LV_PALETTE_RED));
lv_style_set_bg_grad_color(&style_indic, lv_palette_main(LV_PALETTE_BLUE));
lv_style_set_bg_grad_dir(&style_indic, LV_GRAD_DIR_VER);
// lv_style_set_content_align(&style_indic, LV_ALIGN_OUT_LEFT_TOP);
// lv_style_set_content_ofs_x(&style_indic, -3);
// lv_style_set_content_color(&style_indic, lv_palette_main(LV_PALETTE_GREY));
lv_obj_t * bar = lv_bar_create(lv_scr_act());
lv_obj_add_style(bar, &style_indic, LV_PART_INDICATOR);

View File

@@ -6,20 +6,27 @@
*/
void lv_example_bar_5(void)
{
lv_obj_t * label;
lv_obj_t * bar_ltr = lv_bar_create(lv_scr_act());
lv_obj_set_size(bar_ltr, 200, 20);
lv_bar_set_value(bar_ltr, 70, LV_ANIM_OFF);
lv_obj_align(bar_ltr, LV_ALIGN_CENTER, 0, -30);
// lv_obj_add_style(bar_ltr, &style_bg);
// lv_obj_set_style_content_text(bar_ltr, "Left to Right base direction");
label = lv_label_create(lv_scr_act());
lv_label_set_text(label, "Left to Right base direction");
lv_obj_align_to(label, bar_ltr, LV_ALIGN_OUT_TOP_MID, 0, -5);
lv_obj_t * bar_rtl = lv_bar_create(lv_scr_act());
lv_obj_set_base_dir(bar_rtl, LV_BIDI_DIR_RTL);
lv_obj_set_size(bar_rtl, 200, 20);
lv_bar_set_value(bar_rtl, 70, LV_ANIM_OFF);
lv_obj_align(bar_rtl, LV_ALIGN_CENTER, 0, 30);
// lv_obj_add_style(bar_rtl, &style_bg);
// lv_obj_set_style_content_text(bar_rtl, "Right to Left base direction");
label = lv_label_create(lv_scr_act());
lv_label_set_text(label, "Right to Left base direction");
lv_obj_align_to(label, bar_rtl, LV_ALIGN_OUT_TOP_MID, 0, -5);
}
#endif

View File

@@ -1,72 +1,67 @@
#include "../../lv_examples.h"
#if LV_USE_BAR && LV_BUILD_EXAMPLES
//static void set_value(void *bar, int32_t v)
//{
// lv_bar_set_value(bar, v, LV_ANIM_OFF);
//}
static void set_value(void *bar, int32_t v)
{
lv_bar_set_value(bar, v, LV_ANIM_OFF);
}
//static void event_cb(lv_obj_t * obj, lv_event_t e)
//{
// if(code == LV_EVENT_DRAW_POST_END) {
// lv_bar_t * bar = (lv_bar_t *)obj;
//
// lv_draw_label_dsc_t dsc;
// lv_draw_label_dsc_init(&dsc);
// dsc.font = LV_FONT_DEFAULT;
//
// char buf[8];
// lv_snprintf(buf, sizeof(buf), "%d", lv_bar_get_value(obj));
//
// lv_point_t txt_size;
// lv_txt_get_size(&txt_size, buf, dsc.font, dsc.letter_space, dsc.line_space, LV_COORD_MAX, dsc.flag);
//
// lv_area_t txt_area;
// /*If the indicator is long enough put the text inside on the right*/
// if(lv_area_get_width(&bar->indic_area) > txt_size.x + 20) {
// txt_area.x2 = bar->indic_area.x2 - 5;
// txt_area.x1 = txt_area.x2 - txt_size.x + 1;
// dsc.color = lv_color_white();
// }
// /*If the indicator is still short put the text out of it on the right*/
// else {
// txt_area.x1 = bar->indic_area.x2 + 5;
// txt_area.x2 = txt_area.x1 + txt_size.x - 1;
// dsc.color = lv_color_black();
// }
//
// txt_area.y1 = bar->indic_area.y1 + (lv_area_get_height(&bar->indic_area) - txt_size.y) / 2;
// txt_area.y2 = txt_area.y1 + txt_size.y - 1;
//
// const lv_area_t * clip_area = lv_event_get_param(e);
// lv_draw_label(&txt_area, clip_area, &dsc, buf, NULL);
// }
//}
static void event_cb(lv_event_t * e)
{
lv_obj_draw_dsc_t * dsc = lv_event_get_param(e);
if(dsc->part != LV_PART_INDICATOR) return;
lv_obj_t * obj= lv_event_get_target(e);
lv_draw_label_dsc_t label_dsc;
lv_draw_label_dsc_init(&label_dsc);
label_dsc.font = LV_FONT_DEFAULT;
char buf[8];
lv_snprintf(buf, sizeof(buf), "%d", lv_bar_get_value(obj));
lv_point_t txt_size;
lv_txt_get_size(&txt_size, buf, label_dsc.font, label_dsc.letter_space, label_dsc.line_space, LV_COORD_MAX, label_dsc.flag);
lv_area_t txt_area;
/*If the indicator is long enough put the text inside on the right*/
if(lv_area_get_width(dsc->draw_area) > txt_size.x + 20) {
txt_area.x2 = dsc->draw_area->x2 - 5;
txt_area.x1 = txt_area.x2 - txt_size.x + 1;
label_dsc.color = lv_color_white();
}
/*If the indicator is still short put the text out of it on the right*/
else {
txt_area.x1 = dsc->draw_area->x2 + 5;
txt_area.x2 = txt_area.x1 + txt_size.x - 1;
label_dsc.color = lv_color_black();
}
txt_area.y1 = dsc->draw_area->y1 + (lv_area_get_height(dsc->draw_area) - txt_size.y) / 2;
txt_area.y2 = txt_area.y1 + txt_size.y - 1;
lv_draw_label(&txt_area, dsc->clip_area, &label_dsc, buf, NULL);
}
/**
* Custom drawer on bar to display the current value
* Custom drawer on the bar to display the current value
*/
void lv_example_bar_6(void)
{
// static lv_style_t style_bg;
// lv_style_init(&style_bg);
// lv_style_set_content_ofs_y(&style_bg, -3);
// lv_style_set_content_align(&style_bg, LV_ALIGN_OUT_TOP_MID);
//
// lv_obj_t * bar = lv_bar_create(lv_scr_act());
// lv_obj_add_event_cb(bar, event_cb, NULL);
// lv_obj_set_size(bar, 200, 20);
// lv_obj_align(bar, NULL, LV_ALIGN_CENTER, 0, 0);
//
// lv_anim_t a;
// lv_anim_init(&a);
// lv_anim_set_var(&a, bar);
// lv_anim_set_values(&a, 0, 100);
// lv_anim_set_exec_cb(&a, set_value);
// lv_anim_set_time(&a, 2000);
// lv_anim_set_playback_time(&a, 2000);
// lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
// lv_anim_start(&a);
lv_obj_t * bar = lv_bar_create(lv_scr_act());
lv_obj_add_event_cb(bar, event_cb, LV_EVENT_DRAW_PART_END, NULL);
lv_obj_set_size(bar, 200, 20);
lv_obj_center(bar);
lv_anim_t a;
lv_anim_init(&a);
lv_anim_set_var(&a, bar);
lv_anim_set_values(&a, 0, 100);
lv_anim_set_exec_cb(&a, set_value);
lv_anim_set_time(&a, 2000);
lv_anim_set_playback_time(&a, 2000);
lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
lv_anim_start(&a);
}

View File

@@ -1,41 +1,57 @@
#include "../../lv_examples.h"
//#if LV_USE_SLIDER && LV_BUILD_EXAMPLES
//
//static void slider_event_cb(lv_obj_t * slider, lv_event_t event);
//
//static lv_style_t style_pr;
//static lv_style_t style_def;
//
///**
// * Show the current value when the slider if pressed using a fancy style transition.
// */
#if LV_USE_SLIDER && LV_BUILD_EXAMPLES
/**
* Show how to style a slider.
*/
void lv_example_slider_2(void)
{
// lv_style_init(&style_def);
// lv_style_set_content_opa(&style_def, LV_OPA_TRANSP);
// lv_style_set_content_align(&style_def, LV_ALIGN_OUT_TOP_MID);
//
// lv_style_init(&style_pr);
// lv_style_set_content_opa(&style_pr, LV_OPA_COVER);
// lv_style_set_content_ofs_y(&style_pr, -15);
//
// /*Create a slider in the center of the display*/
// lv_obj_t * slider;
// slider = lv_slider_create(lv_scr_act());
// lv_obj_align(slider, NULL, LV_ALIGN_CENTER, 0, 0);
// lv_obj_add_event_cb(slider, slider_event_cb, NULL);
//
// lv_obj_add_style(slider, LV_PART_KNOB, LV_STATE_DEFAULT, &style_def);
// lv_obj_add_style(slider, LV_PART_KNOB, LV_STATE_PRESSED, &style_pr);
/*Create a transition*/
static const lv_style_prop_t props[] = {LV_STYLE_BG_COLOR, 0};
static lv_style_transition_dsc_t transition_dsc;
lv_style_transition_dsc_init(&transition_dsc, props, lv_anim_path_linear, 300, 0);
static lv_style_t style_main;
static lv_style_t style_indicator;
static lv_style_t style_knob;
static lv_style_t style_pressed_color;
lv_style_init(&style_main);
lv_style_set_bg_opa(&style_main, LV_OPA_COVER);
lv_style_set_bg_color(&style_main, lv_color_hex3(0xbbb));
lv_style_set_radius(&style_main, LV_RADIUS_CIRCLE);
lv_style_set_pad_ver(&style_main, -2); /*Makes the indicator larger*/
lv_style_init(&style_indicator);
lv_style_set_bg_opa(&style_indicator, LV_OPA_COVER);
lv_style_set_bg_color(&style_indicator, lv_palette_main(LV_PALETTE_CYAN));
lv_style_set_radius(&style_indicator, LV_RADIUS_CIRCLE);
lv_style_set_transition(&style_indicator, &transition_dsc);
lv_style_init(&style_knob);
lv_style_set_bg_opa(&style_knob, LV_OPA_COVER);
lv_style_set_bg_color(&style_knob, lv_palette_main(LV_PALETTE_CYAN));
lv_style_set_border_color(&style_knob, lv_palette_darken(LV_PALETTE_CYAN, 3));
lv_style_set_border_width(&style_knob, 2);
lv_style_set_radius(&style_knob, LV_RADIUS_CIRCLE);
lv_style_set_pad_all(&style_knob, 6); /*Makes the knob larger*/
lv_style_set_transition(&style_knob, &transition_dsc);
lv_style_init(&style_pressed_color);
lv_style_set_bg_color(&style_pressed_color, lv_palette_darken(LV_PALETTE_CYAN, 2));
/*Create a slider and add the style*/
lv_obj_t * slider = lv_slider_create(lv_scr_act());
lv_obj_remove_style_all(slider); /*Remove the styles coming from the theme*/
lv_obj_add_style(slider, &style_main, LV_PART_MAIN);
lv_obj_add_style(slider, &style_indicator, LV_PART_INDICATOR);
lv_obj_add_style(slider, &style_pressed_color, LV_PART_INDICATOR | LV_STATE_PRESSED);
lv_obj_add_style(slider, &style_knob, LV_PART_KNOB);
lv_obj_add_style(slider, &style_pressed_color, LV_PART_KNOB | LV_STATE_PRESSED);
lv_obj_center(slider);
}
//
//static void slider_event_cb(lv_obj_t * slider, lv_event_t event)
//{
// if(code == LV_EVENT_VALUE_CHANGED) {
// static char buf[8];
// lv_snprintf(buf, sizeof(buf), "%u", lv_slider_get_value(slider));
// lv_obj_set_style_content_text(slider, LV_PART_KNOB, LV_STATE_DEFAULT, buf);
// }
//}
//
//#endif
#endif

View File

@@ -1,39 +1,57 @@
#include "../../lv_examples.h"
//#if LV_USE_SLIDER && LV_BUILD_EXAMPLES
//
//static void slider_event_cb(lv_obj_t * slider, lv_event_t event);
//
///**
// * Show the current value when the slider if pressed (using only styles).
// *
// */
#if LV_USE_SLIDER && LV_BUILD_EXAMPLES
static void slider_event_cb(lv_event_t * e);
/**
* Show the current value when the slider if pressed by extending the drawer
*
*/
void lv_example_slider_3(void)
{
// /*Create a slider in the center of the display*/
// lv_obj_t * slider;
// slider = lv_slider_create(lv_scr_act());
// lv_obj_align(slider, NULL, LV_ALIGN_CENTER, 0, 0);
// lv_obj_add_event_cb(slider, slider_event_cb, NULL);
// lv_slider_set_mode(slider, LV_SLIDER_MODE_RANGE);
//
// lv_slider_set_value(slider, 70, LV_ANIM_OFF);
// lv_slider_set_left_value(slider, 20, LV_ANIM_OFF);
//
// /*Now use only a local style.*/
// lv_obj_set_style_content_ofs_y(slider, LV_PART_INDICATOR, LV_STATE_DEFAULT, -20);
// lv_obj_set_style_content_color(slider, LV_PART_INDICATOR, LV_STATE_DEFAULT, lv_color_grey_darken_3());
//
// /*To update the value text*/
// lv_event_send(slider, LV_EVENT_VALUE_CHANGED, NULL);
/*Create a slider in the center of the display*/
lv_obj_t * slider;
slider = lv_slider_create(lv_scr_act());
lv_obj_center(slider);
lv_slider_set_mode(slider, LV_SLIDER_MODE_RANGE);
lv_slider_set_value(slider, 70, LV_ANIM_OFF);
lv_slider_set_left_value(slider, 20, LV_ANIM_OFF);
lv_obj_add_event_cb(slider, slider_event_cb, LV_EVENT_ALL, NULL);
lv_obj_refresh_ext_draw_size(slider);
}
//
//static void slider_event_cb(lv_obj_t * slider, lv_event_t event)
//{
// if(code == LV_EVENT_VALUE_CHANGED) {
// static char buf[8];
// lv_snprintf(buf, sizeof(buf), "%d - %d", lv_slider_get_value(slider), lv_slider_get_left_value(slider));
// lv_obj_set_style_content_text(slider, LV_PART_INDICATOR, LV_STATE_DEFAULT, buf);
// }
//}
//
//#endif
static void slider_event_cb(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * obj = lv_event_get_target(e);
/*Provide some extra space for the value*/
if(code == LV_EVENT_REFR_EXT_DRAW_SIZE) {
lv_coord_t * size = lv_event_get_param(e);
*size = LV_MAX(*size, 50);
}
else if(code == LV_EVENT_DRAW_PART_END) {
lv_obj_draw_dsc_t * dsc = lv_event_get_param(e);
if(dsc->part == LV_PART_INDICATOR) {
char buf[16];
lv_snprintf(buf, sizeof(buf), "%d - %d", lv_slider_get_left_value(obj), lv_slider_get_value(obj));
lv_point_t label_size;
lv_txt_get_size(&label_size, buf, LV_FONT_DEFAULT, 0, 0, LV_COORD_MAX, 0);
lv_area_t label_area;
label_area.x1 = dsc->draw_area->x1 + lv_area_get_width(dsc->draw_area) / 2 - label_size.x / 2;
label_area.x2 = label_area.x1 + label_size.x;
label_area.y2 = dsc->draw_area->y1 - 10;
label_area.y1 = label_area.y2 - label_size.y;
lv_draw_label_dsc_t label_draw_dsc;
lv_draw_label_dsc_init(&label_draw_dsc);
lv_draw_label(&label_area, dsc->clip_area, &label_draw_dsc, buf, NULL);
}
}
}
#endif