1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-28 07:03:00 +08:00

feat(scale): use the style_length property for ticks (#5141)

This commit is contained in:
Carlos Diaz 2024-01-03 04:34:54 -06:00 committed by GitHub
parent 28eced31a7
commit 91e2617410
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 40 additions and 72 deletions

View File

@ -737,7 +737,7 @@ static void analytics_create(lv_obj_t * parent)
lv_scale_set_angle_range(scale2, 360);
lv_scale_set_text_src(scale2, scale2_text);
lv_scale_set_total_tick_count(scale2, 11);
lv_scale_set_major_tick_length(scale2, 30);
lv_obj_set_style_length(scale2, 30, LV_PART_INDICATOR);
lv_scale_set_major_tick_every(scale2, 1);
arc = lv_arc_create(scale2);
lv_obj_set_size(arc, lv_pct(100), lv_pct(100));
@ -780,8 +780,8 @@ static void analytics_create(lv_obj_t * parent)
lv_scale_set_range(scale3, 10, 60);
lv_scale_set_total_tick_count(scale3, 21);
lv_scale_set_major_tick_every(scale3, 4);
lv_scale_set_minor_tick_length(scale3, 10);
lv_scale_set_major_tick_length(scale3, 20);
lv_obj_set_style_length(scale3, 10, LV_PART_ITEMS);
lv_obj_set_style_length(scale3, 20, LV_PART_INDICATOR);
lv_scale_set_angle_range(scale3, 225);
lv_scale_set_rotation(scale3, 135);

View File

@ -30,7 +30,9 @@ Configure ticks
Set the number of total ticks with :cpp:expr:`lv_scale_set_total_tick_count(scale, total_tick_count)` and then configure the major tick being every Nth ticks with :cpp:expr:`lv_scale_set_major_tick_every(scale, nth_tick)`.
Labels on major ticks can be configured with :cpp:expr:`lv_scale_set_label_show(scale, show_label)`, set `show_label` to true if labels should be drawn, :cpp:expr:`false` to hide them. If instead of a numerical value in the major ticks a text is required they can be set with :cpp:expr:`lv_scale_set_text_src(scale, custom_labels)` using NULL as the last element, i.e. :cpp:expr:`static char * custom_labels[3] = {"One", "Two", NULL};`
Labels on major ticks can be configured with :cpp:expr:`lv_scale_set_label_show(scale, show_label)`, set `show_label` to true if labels should be drawn, :cpp:expr:`false` to hide them. If instead of a numerical value in the major ticks a text is required they can be set with :cpp:expr:`lv_scale_set_text_src(scale, custom_labels)` using NULL as the last element, i.e. :cpp:expr:`static char * custom_labels[3] = {"One", "Two", NULL};`.
The length of the ticks can be configured with the length style property on the :cpp:enumerator: `LV_PART_INDICATOR` for major ticks and :cpp:enumerator: `LV_PART_ITEMS` for minor ticks, for example with local style: :cpp:expr:`lv_obj_set_style_length(scale, 5, LV_PART_INDICATOR);` for major ticks and :cpp:expr:`lv_obj_set_style_length(scale, 5, LV_PART_ITEMS);` for minor ticks.
Sections
--------

View File

@ -16,8 +16,8 @@ void lv_example_scale_1(void)
lv_scale_set_total_tick_count(scale, 31);
lv_scale_set_major_tick_every(scale, 5);
lv_scale_set_major_tick_length(scale, 10);
lv_scale_set_minor_tick_length(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);
}

View File

@ -15,8 +15,8 @@ void lv_example_scale_2(void)
lv_scale_set_total_tick_count(scale, 21);
lv_scale_set_major_tick_every(scale, 5);
lv_scale_set_major_tick_length(scale, 10);
lv_scale_set_minor_tick_length(scale, 5);
lv_obj_set_style_length(scale, 10, LV_PART_INDICATOR);
lv_obj_set_style_length(scale, 5, LV_PART_ITEMS);
lv_scale_set_range(scale, 0, 100);
static const char * custom_labels[] = {"0 °C", "25 °C", "50 °C", "75 °C", "100 °C", NULL};

View File

@ -36,8 +36,8 @@ void lv_example_scale_3(void)
lv_scale_set_total_tick_count(scale_line, 31);
lv_scale_set_major_tick_every(scale_line, 5);
lv_scale_set_major_tick_length(scale_line, 10);
lv_scale_set_minor_tick_length(scale_line, 5);
lv_obj_set_style_length(scale_line, 5, LV_PART_ITEMS);
lv_obj_set_style_length(scale_line, 10, LV_PART_INDICATOR);
lv_scale_set_range(scale_line, 10, 40);
lv_scale_set_angle_range(scale_line, 270);
@ -73,8 +73,8 @@ void lv_example_scale_3(void)
lv_scale_set_total_tick_count(scale_img, 31);
lv_scale_set_major_tick_every(scale_img, 5);
lv_scale_set_major_tick_length(scale_img, 10);
lv_scale_set_minor_tick_length(scale_img, 5);
lv_obj_set_style_length(scale_img, 5, LV_PART_ITEMS);
lv_obj_set_style_length(scale_img, 10, LV_PART_INDICATOR);
lv_scale_set_range(scale_img, 10, 40);
lv_scale_set_angle_range(scale_img, 270);

View File

@ -15,8 +15,8 @@ void lv_example_scale_4(void)
lv_scale_set_total_tick_count(scale, 21);
lv_scale_set_major_tick_every(scale, 5);
lv_scale_set_major_tick_length(scale, 10);
lv_scale_set_minor_tick_length(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, 0, 100);
static const char * custom_labels[] = {"0 °C", "25 °C", "50 °C", "75 °C", "100 °C", NULL};

View File

@ -13,8 +13,8 @@ void lv_example_scale_5(void)
lv_scale_set_total_tick_count(scale, 10);
lv_scale_set_major_tick_every(scale, 5);
lv_scale_set_major_tick_length(scale, 10);
lv_scale_set_minor_tick_length(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, 25, 35);
static const char * custom_labels[3] = {"One", "Two", NULL};

View File

@ -22,6 +22,8 @@
#define LV_SCALE_LABEL_TXT_LEN (20U)
#define LV_SCALE_DEFAULT_ANGLE_RANGE ((uint32_t) 270U)
#define LV_SCALE_DEFAULT_ROTATION ((int32_t) 135U)
#define LV_SCALE_TICK_IDX_DEFAULT_ID ((uint32_t) 255U)
#define LV_SCALE_DEFAULT_LABEL_GAP ((uint32_t) 15U)
/**********************
* TYPEDEFS
@ -130,26 +132,6 @@ void lv_scale_set_major_tick_every(lv_obj_t * obj, uint32_t major_tick_every)
lv_obj_invalidate(obj);
}
void lv_scale_set_major_tick_length(lv_obj_t * obj, uint32_t major_len)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_scale_t * scale = (lv_scale_t *)obj;
scale->major_len = major_len;
lv_obj_invalidate(obj);
}
void lv_scale_set_minor_tick_length(lv_obj_t * obj, uint32_t minor_len)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_scale_t * scale = (lv_scale_t *)obj;
scale->minor_len = minor_len;
lv_obj_invalidate(obj);
}
void lv_scale_set_label_show(lv_obj_t * obj, bool show_label)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
@ -315,8 +297,8 @@ lv_scale_section_t * lv_scale_add_section(lv_obj_t * obj)
section->items_style = NULL;
section->minor_range = 0U;
section->major_range = 0U;
section->first_tick_idx_in_section = 255U;
section->last_tick_idx_in_section = 255U;
section->first_tick_idx_in_section = LV_SCALE_TICK_IDX_DEFAULT_ID;
section->last_tick_idx_in_section = LV_SCALE_TICK_IDX_DEFAULT_ID;
section->first_tick_idx_is_major = 0U;
section->last_tick_idx_is_major = 0U;
section->first_tick_in_section_width = 0U;
@ -424,8 +406,6 @@ static void lv_scale_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
scale->rotation = LV_SCALE_DEFAULT_ROTATION;
scale->range_min = 0U;
scale->range_max = 100U;
scale->major_len = 10U;
scale->minor_len = 5u;
scale->last_tick_width = 0U;
scale->first_tick_width = 0U;
scale->post_draw = false;
@ -515,6 +495,8 @@ static void scale_draw_indicator(lv_obj_t * obj, lv_event_t * event)
lv_draw_line_dsc_init(&main_line_dsc);
lv_obj_init_draw_line_dsc(obj, LV_PART_MAIN, &main_line_dsc);
const int32_t major_len = lv_obj_get_style_length(obj, LV_PART_INDICATOR);
if((LV_SCALE_MODE_VERTICAL_LEFT == scale->mode || LV_SCALE_MODE_VERTICAL_RIGHT == scale->mode)
|| (LV_SCALE_MODE_HORIZONTAL_BOTTOM == scale->mode || LV_SCALE_MODE_HORIZONTAL_TOP == scale->mode)) {
@ -607,7 +589,7 @@ static void scale_draw_indicator(lv_obj_t * obj, lv_event_t * event)
/* Major tick */
major_tick_dsc.raw_end = 0;
uint32_t label_gap = 15U; /* TODO: Add to style properties */
uint32_t label_gap = LV_SCALE_DEFAULT_LABEL_GAP; /* TODO: Add to style properties */
uint32_t tick_idx = 0;
uint32_t major_tick_idx = 0;
for(tick_idx = 0; tick_idx < scale->total_tick_count; tick_idx++) {
@ -666,10 +648,10 @@ static void scale_draw_indicator(lv_obj_t * obj, lv_event_t * event)
uint32_t radius_text = 0;
if(LV_SCALE_MODE_ROUND_INNER == scale->mode) {
radius_text = (radius_edge - scale->major_len) - (label_gap + label_dsc.letter_space);
radius_text = (radius_edge - major_len) - (label_gap + label_dsc.letter_space);
}
else if(LV_SCALE_MODE_ROUND_OUTER == scale->mode) {
radius_text = (radius_edge + scale->major_len) + (label_gap + label_dsc.letter_space);
radius_text = (radius_edge + major_len) + (label_gap + label_dsc.letter_space);
}
else { /* Nothing to do */ }
@ -914,10 +896,10 @@ static void scale_get_tick_points(lv_obj_t * obj, const uint32_t tick_idx, bool
int32_t major_len = 0;
if(is_major_tick) {
major_len = scale->major_len;
major_len = lv_obj_get_style_length(obj, LV_PART_INDICATOR);
}
else {
minor_len = scale->minor_len;
minor_len = lv_obj_get_style_length(obj, LV_PART_ITEMS);
}
if((LV_SCALE_MODE_VERTICAL_LEFT == scale->mode || LV_SCALE_MODE_VERTICAL_RIGHT == scale->mode)
@ -1290,7 +1272,7 @@ static void scale_find_section_tick_idx(lv_obj_t * obj)
lv_scale_section_t * section;
_LV_LL_READ_BACK(&scale->section_ll, section) {
if(section->minor_range <= tick_value && section->major_range >= tick_value) {
if(section->first_tick_idx_in_section == 255) {
if(LV_SCALE_TICK_IDX_DEFAULT_ID == section->first_tick_idx_in_section) {
section->first_tick_idx_in_section = tick_idx;
section->first_tick_idx_is_major = is_major_tick;
}

View File

@ -76,8 +76,6 @@ typedef struct {
lv_ll_t section_ll; /**< Linked list for the sections (stores lv_scale_section_t)*/
const char ** txt_src;
lv_scale_mode_t mode;
uint32_t major_len;
uint32_t minor_len;
int32_t range_min;
int32_t range_max;
uint32_t total_tick_count : 15;
@ -142,20 +140,6 @@ void lv_scale_set_major_tick_every(lv_obj_t * obj, uint32_t major_tick_every);
*/
void lv_scale_set_label_show(lv_obj_t * obj, bool show_label);
/**
* Sets major tick length
* @param obj pointer the scale object
* @param major_len major tick length
*/
void lv_scale_set_major_tick_length(lv_obj_t * obj, uint32_t major_len);
/**
* Sets major tick length
* @param obj pointer the scale object
* @param minor_len minor tick length
*/
void lv_scale_set_minor_tick_length(lv_obj_t * obj, uint32_t minor_len);
/**
* Set the minimal and maximal values on a scale
* @param obj pointer to a scale object
@ -222,7 +206,7 @@ lv_scale_section_t * lv_scale_add_section(lv_obj_t * obj);
/**
* Set the range for the given scale section
* @param obj pointer to a scale section object
* @param section pointer to a scale section object
* @param minor_range section new minor range
* @param major_range section new major range
*/
@ -230,7 +214,7 @@ void lv_scale_section_set_range(lv_scale_section_t * section, int32_t minor_rang
/**
* Set the style of the part for the given scale section
* @param obj pointer to a scale section object
* @param section pointer to a scale section object
* @param part the part for the section, e.g. LV_PART_INDICATOR
* @param section_part_style Pointer to the section part style
*/

View File

@ -27,8 +27,8 @@ void test_scale_render_example_1(void)
lv_scale_set_total_tick_count(scale, 31);
lv_scale_set_major_tick_every(scale, 5);
lv_scale_set_major_tick_length(scale, 10);
lv_scale_set_minor_tick_length(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);
TEST_ASSERT_EQUAL_SCREENSHOT("scale_1.png");
@ -46,8 +46,8 @@ void test_scale_render_example_2(void)
lv_scale_set_total_tick_count(scale, 21);
lv_scale_set_major_tick_every(scale, 5);
lv_scale_set_major_tick_length(scale, 10);
lv_scale_set_minor_tick_length(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, 0, 100);
static const char * custom_labels[] = {"0 °C", "25 °C", "50 °C", "75 °C", "100 °C", NULL};
@ -135,8 +135,8 @@ void test_scale_render_example_3(void)
lv_scale_set_total_tick_count(scale, 11);
lv_scale_set_major_tick_every(scale, 5);
lv_scale_set_major_tick_length(scale, 10);
lv_scale_set_minor_tick_length(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);
TEST_ASSERT_EQUAL_SCREENSHOT("scale_3.png");
@ -154,8 +154,8 @@ void test_scale_render_example_4(void)
lv_scale_set_total_tick_count(scale, 21);
lv_scale_set_major_tick_every(scale, 5);
lv_scale_set_major_tick_length(scale, 10);
lv_scale_set_minor_tick_length(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, 0, 100);
static const char * custom_labels[] = {"0 °C", "25 °C", "50 °C", "75 °C", "100 °C", NULL};