diff --git a/docs/widgets/extra/spinbox.md b/docs/widgets/extra/spinbox.md index f39936e73..1b4444e21 100644 --- a/docs/widgets/extra/spinbox.md +++ b/docs/widgets/extra/spinbox.md @@ -22,6 +22,8 @@ The parts of the Spinbox are identical to the [Text area](/widgets/core/textarea `lv_spinbox_set_pos(spinbox, 1)` sets the cursor to a specific digit to change on increment/decrement. For example position '0' sets the cursor to the least significant digit. +If an encoder is used as input device, the selected digit is shifted to the right by default whenever the encoder button is clicked. To change this behaviour to shifting to the left, the `lv_spinbox_set_digit_step_direction(spinbox, LV_DIR_LEFT)` can be used + ### Format `lv_spinbox_set_digit_format(spinbox, digit_count, separator_position)` sets the number format. `digit_count` is the number of digits excluding the decimal separator and the sign. diff --git a/src/extra/widgets/spinbox/lv_spinbox.c b/src/extra/widgets/spinbox/lv_spinbox.c index 83fd2f458..97d00404e 100644 --- a/src/extra/widgets/spinbox/lv_spinbox.c +++ b/src/extra/widgets/spinbox/lv_spinbox.c @@ -169,6 +169,20 @@ void lv_spinbox_set_pos(lv_obj_t * obj, uint8_t pos) lv_spinbox_updatevalue(obj); } + +/** + * Set direction of digit step when clicking an encoder button while in editing mode + * @param spinbox pointer to spinbox + * @param direction the direction (LV_DIR_RIGHT or LV_DIR_LEFT) + */ +void lv_spinbox_set_digit_step_direction(lv_obj_t *obj, lv_dir_t direction) +{ + LV_ASSERT_OBJ(obj, MY_CLASS); + lv_spinbox_t * spinbox = (lv_spinbox_t *)obj; + spinbox->digit_step_dir = direction; + + lv_spinbox_updatevalue(obj); +} /*===================== * Getter functions *====================*/ @@ -318,6 +332,7 @@ static void lv_spinbox_constructor(const lv_obj_class_t * class_p, lv_obj_t * ob spinbox->range_max = 99999; spinbox->range_min = -99999; spinbox->rollover = false; + spinbox->digit_step_dir = LV_DIR_RIGHT; lv_textarea_set_one_line(obj, true); lv_textarea_set_cursor_click_pos(obj, true); @@ -345,19 +360,27 @@ static void lv_spinbox_event(const lv_obj_class_t * class_p, lv_event_t * e) lv_indev_t * indev = lv_indev_get_act(); if(lv_indev_get_type(indev) == LV_INDEV_TYPE_ENCODER) { if(lv_group_get_editing(lv_obj_get_group(obj))) { - if(spinbox->step > 1) { - lv_spinbox_step_next(obj); - } - else { - /*Restart from the MSB*/ - spinbox->step = 1; - uint32_t i; - for(i = 0; i < spinbox->digit_count; i++) { - int32_t new_step = spinbox->step * 10; - if(new_step >= spinbox->range_max) break; - spinbox->step = new_step; + if (spinbox->digit_count > 1) { + if (spinbox->digit_step_dir == LV_DIR_RIGHT) { + if(spinbox->step > 1) { + lv_spinbox_step_next(obj); + } + else { + /*Restart from the MSB*/ + spinbox->step = lv_pow(10, spinbox->digit_count - 2); + lv_spinbox_step_prev(obj); + } + } + else { + if(spinbox->step < lv_pow(10, spinbox->digit_count - 1)) { + lv_spinbox_step_prev(obj); + } + else { + /*Restart from the LSB*/ + spinbox->step = 10; + lv_spinbox_step_next(obj); + } } - lv_spinbox_step_prev(obj); } } } diff --git a/src/extra/widgets/spinbox/lv_spinbox.h b/src/extra/widgets/spinbox/lv_spinbox.h index ce3bce27c..71856c917 100644 --- a/src/extra/widgets/spinbox/lv_spinbox.h +++ b/src/extra/widgets/spinbox/lv_spinbox.h @@ -42,6 +42,7 @@ typedef struct { uint16_t digit_count : 4; uint16_t dec_point_pos : 4; /*if 0, there is no separator and the number is an integer*/ uint16_t rollover : 1; // Set to true for rollover functionality + uint16_t digit_step_dir : 2; // the direction the digit will step on encoder button press when editing } lv_spinbox_t; extern const lv_obj_class_t lv_spinbox_class; @@ -105,6 +106,14 @@ void lv_spinbox_set_range(lv_obj_t * obj, int32_t range_min, int32_t range_max); * @param pos selected position in spinbox */ void lv_spinbox_set_pos(lv_obj_t * obj, uint8_t pos); + +/** + * Set direction of digit step when clicking an encoder button while in editing mode + * @param spinbox pointer to spinbox + * @param direction the direction (LV_DIR_RIGHT or LV_DIR_LEFT) + */ +void lv_spinbox_set_digit_step_direction(lv_obj_t * obj, lv_dir_t direction); + /*===================== * Getter functions *====================*/ @@ -113,7 +122,7 @@ void lv_spinbox_set_pos(lv_obj_t * obj, uint8_t pos); * Get spinbox rollover function status * @param spinbox pointer to spinbox */ -bool lv_spinbox_get_rollover(lv_obj_t * obj); +bool lv_spinbox_get_rollover(lv_obj_t *obj); /** * Get the spinbox numeral value (user has to convert to float according to its digit format)