-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |