diff --git a/examples/widgets/scale/index.rst b/examples/widgets/scale/index.rst index e9bfa5a5a428..3f2c7cfc8d2a 100644 --- a/examples/widgets/scale/index.rst +++ b/examples/widgets/scale/index.rst @@ -34,3 +34,9 @@ A round scale with multiple needles, resembling a clock .. lv_example:: widgets/scale/lv_example_scale_6 :language: c +Customizing scale major tick labels with `LV_EVENT_DRAW_TASK_ADDED` event +""""""""""""""""""""""""""""""""""""""""""""""""""""""" + +.. lv_example:: widgets/scale/lv_example_scale_7 + :language: c + diff --git a/examples/widgets/scale/lv_example_scale_7.c b/examples/widgets/scale/lv_example_scale_7.c new file mode 100644 index 000000000000..e1cd7aa3b73c --- /dev/null +++ b/examples/widgets/scale/lv_example_scale_7.c @@ -0,0 +1,65 @@ +#include "../../lv_examples.h" +#if LV_USE_SCALE && LV_BUILD_EXAMPLES + +#include "lvgl/src/lvgl_private.h" //To expose the fields of lv_draw_task_t + +static void draw_event_cb(lv_event_t * e) +{ + lv_obj_t * obj = lv_event_get_target(e); + lv_draw_task_t * draw_task = lv_event_get_draw_task(e); + lv_draw_dsc_base_t * base_dsc = lv_draw_task_get_draw_dsc(draw_task); + lv_draw_label_dsc_t * label_draw_dsc = lv_draw_task_get_label_dsc(draw_task); + if(base_dsc->part == LV_PART_INDICATOR) { + if(label_draw_dsc) { + const lv_color_t color_idx[7]={ + lv_palette_main(LV_PALETTE_RED), + lv_palette_main(LV_PALETTE_ORANGE), + lv_palette_main(LV_PALETTE_YELLOW), + lv_palette_main(LV_PALETTE_GREEN), + lv_palette_main(LV_PALETTE_CYAN), + lv_palette_main(LV_PALETTE_BLUE), + lv_palette_main(LV_PALETTE_PURPLE), + }; + uint8_t major_tick=lv_scale_get_major_tick_every(obj); + label_draw_dsc->color=color_idx[base_dsc->id1/major_tick]; + lv_snprintf((char*)label_draw_dsc->text, + sizeof(label_draw_dsc->text), + "%.1f", + base_dsc->id2*1.0f); + lv_point_t size; + lv_text_get_size(&size, label_draw_dsc->text, label_draw_dsc->font, 0, 0, 1000, LV_TEXT_FLAG_NONE); + int32_t new_w = size.x; + int32_t old_w = lv_area_get_width(&draw_task->area); + + // Distribute the new size equally on both sides + draw_task->area.x1 -= (new_w - old_w) / 2; + draw_task->area.x2 += ((new_w - old_w) + 1) / 2; // +1 for rounding + + } + } +} + +/** + * Customizing scale major tick labels with `LV_EVENT_DRAW_TASK_ADDED` event + */ +void lv_example_scale_7(void) +{ + lv_obj_t * scale = lv_scale_create(lv_screen_active()); + lv_obj_set_size(scale, lv_pct(80), 100); + lv_scale_set_mode(scale, LV_SCALE_MODE_HORIZONTAL_BOTTOM); + lv_obj_center(scale); + + lv_scale_set_label_show(scale, true); + + lv_scale_set_total_tick_count(scale, 31); + lv_scale_set_major_tick_every(scale, 5); + + lv_obj_set_style_length(scale, 5, LV_PART_ITEMS); + lv_obj_set_style_length(scale, 10, LV_PART_INDICATOR); + lv_scale_set_range(scale, 10, 40); + + lv_obj_add_event_cb(scale, draw_event_cb, LV_EVENT_DRAW_TASK_ADDED, NULL); + lv_obj_add_flag(scale, LV_OBJ_FLAG_SEND_DRAW_TASK_EVENTS); +} + +#endif