2017-09-03 16:01:51 +02:00
|
|
|
/**
|
|
|
|
* @file lv_roller.c
|
2018-06-19 09:49:58 +02:00
|
|
|
*
|
2017-09-03 16:01:51 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*********************
|
|
|
|
* INCLUDES
|
|
|
|
*********************/
|
2018-07-07 11:53:22 +02:00
|
|
|
#include "lv_roller.h"
|
2019-03-07 00:05:16 +01:00
|
|
|
#if LV_USE_ROLLER != 0
|
2017-09-03 16:01:51 +02:00
|
|
|
|
2019-09-24 16:30:38 +02:00
|
|
|
#include "../lv_core/lv_debug.h"
|
2017-09-03 16:01:51 +02:00
|
|
|
#include "../lv_draw/lv_draw.h"
|
2017-11-30 11:35:33 +01:00
|
|
|
#include "../lv_core/lv_group.h"
|
2017-11-16 15:32:33 +01:00
|
|
|
#include "../lv_themes/lv_theme.h"
|
2017-09-03 16:01:51 +02:00
|
|
|
|
|
|
|
/*********************
|
|
|
|
* DEFINES
|
|
|
|
*********************/
|
2019-09-26 10:51:54 +02:00
|
|
|
#define LV_OBJX_NAME "lv_roller"
|
|
|
|
|
2019-06-19 00:35:35 +02:00
|
|
|
#if LV_USE_ANIMATION == 0
|
2019-04-04 07:15:40 +02:00
|
|
|
#undef LV_ROLLER_DEF_ANIM_TIME
|
|
|
|
#define LV_ROLLER_DEF_ANIM_TIME 0 /*No animation*/
|
2017-12-21 00:19:59 +01:00
|
|
|
#endif
|
2017-09-03 16:01:51 +02:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* TYPEDEFS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC PROTOTYPES
|
|
|
|
**********************/
|
2019-09-06 19:53:39 +02:00
|
|
|
static lv_design_res_t lv_roller_design(lv_obj_t * roller, const lv_area_t * clip_area, lv_design_mode_t mode);
|
2017-11-05 22:37:03 +01:00
|
|
|
static lv_res_t lv_roller_scrl_signal(lv_obj_t * roller_scrl, lv_signal_t sign, void * param);
|
2020-01-16 14:26:36 +01:00
|
|
|
static lv_style_list_t * lv_roller_get_style(lv_obj_t * roller, uint8_t part);
|
2017-11-05 22:37:03 +01:00
|
|
|
static lv_res_t lv_roller_signal(lv_obj_t * roller, lv_signal_t sign, void * param);
|
2019-06-11 13:51:14 +02:00
|
|
|
static void refr_position(lv_obj_t * roller, lv_anim_enable_t animen);
|
2019-04-22 10:17:21 +02:00
|
|
|
static void refr_height(lv_obj_t * roller);
|
2020-01-21 22:09:50 +01:00
|
|
|
static void refr_width(lv_obj_t * roller);
|
|
|
|
static lv_res_t release_handler(lv_obj_t * roller);
|
2019-03-17 04:32:37 +01:00
|
|
|
static void inf_normalize(void * roller_scrl);
|
2020-01-21 22:09:50 +01:00
|
|
|
static lv_obj_t * get_label(const lv_obj_t * roller);
|
2019-05-20 18:31:47 -07:00
|
|
|
#if LV_USE_ANIMATION
|
2019-04-22 08:45:07 +02:00
|
|
|
static void scroll_anim_ready_cb(lv_anim_t * a);
|
2019-05-20 18:31:47 -07:00
|
|
|
#endif
|
2020-01-21 22:09:50 +01:00
|
|
|
static void draw_bg(lv_obj_t * roller, const lv_area_t * clip_area);
|
2017-09-03 16:01:51 +02:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC VARIABLES
|
|
|
|
**********************/
|
2019-02-26 09:25:46 +01:00
|
|
|
static lv_signal_cb_t ancestor_signal;
|
|
|
|
static lv_signal_cb_t ancestor_scrl_signal;
|
2017-09-03 16:01:51 +02:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* MACROS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* GLOBAL FUNCTIONS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a roller object
|
|
|
|
* @param par pointer to an object, it will be the parent of the new roller
|
|
|
|
* @param copy pointer to a roller object, if not NULL then the new object will be copied from it
|
|
|
|
* @return pointer to the created roller
|
|
|
|
*/
|
2018-07-30 06:52:29 +02:00
|
|
|
lv_obj_t * lv_roller_create(lv_obj_t * par, const lv_obj_t * copy)
|
2017-09-03 16:01:51 +02:00
|
|
|
{
|
2018-10-05 17:22:49 +02:00
|
|
|
LV_LOG_TRACE("roller create started");
|
2018-07-25 17:57:08 +02:00
|
|
|
|
2017-09-03 16:01:51 +02:00
|
|
|
/*Create the ancestor of roller*/
|
2020-01-21 22:09:50 +01:00
|
|
|
lv_obj_t * roller = lv_page_create(par, copy);
|
|
|
|
LV_ASSERT_MEM(roller);
|
|
|
|
if(roller == NULL) return NULL;
|
2018-07-25 13:33:53 +02:00
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
if(ancestor_scrl_signal == NULL) ancestor_scrl_signal = lv_obj_get_signal_cb(lv_page_get_scrl(roller));
|
|
|
|
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(roller);
|
2018-06-19 09:49:58 +02:00
|
|
|
|
2017-09-03 16:01:51 +02:00
|
|
|
/*Allocate the roller type specific extended data*/
|
2020-01-21 22:09:50 +01:00
|
|
|
lv_roller_ext_t * ext = lv_obj_allocate_ext_attr(roller, sizeof(lv_roller_ext_t));
|
2019-09-24 23:14:17 +02:00
|
|
|
LV_ASSERT_MEM(ext);
|
2019-12-03 18:16:14 +01:00
|
|
|
if(ext == NULL) {
|
2020-01-21 22:09:50 +01:00
|
|
|
lv_obj_del(roller);
|
2019-12-03 18:16:14 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-11-20 16:18:56 +01:00
|
|
|
|
2019-11-14 19:35:24 -08:00
|
|
|
ext->mode = LV_ROLLER_MODE_NORMAL;
|
2020-01-21 22:09:50 +01:00
|
|
|
ext->option_cnt = 0;
|
|
|
|
ext->sel_opt_id = 0;
|
|
|
|
ext->sel_opt_id_ori = 0;
|
|
|
|
lv_style_list_init(&ext->style_sel);
|
2018-12-05 11:46:37 +02:00
|
|
|
|
2017-09-03 16:01:51 +02:00
|
|
|
/*The signal and design functions are not copied so set them here*/
|
2020-01-21 22:09:50 +01:00
|
|
|
lv_obj_set_signal_cb(roller, lv_roller_signal);
|
|
|
|
lv_obj_set_design_cb(roller, lv_roller_design);
|
2017-09-03 16:01:51 +02:00
|
|
|
|
2020-02-03 16:18:53 +01:00
|
|
|
|
|
|
|
|
2017-09-03 16:01:51 +02:00
|
|
|
/*Init the new roller roller*/
|
|
|
|
if(copy == NULL) {
|
2020-01-21 22:09:50 +01:00
|
|
|
lv_obj_t * label = lv_label_create(roller, NULL);
|
|
|
|
lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
|
|
|
|
|
|
|
|
lv_obj_t * scrl = lv_page_get_scrl(roller);
|
2019-06-06 06:05:40 +02:00
|
|
|
lv_obj_set_drag(scrl, true); /*In ddlist it might be disabled*/
|
2020-02-14 13:38:48 +01:00
|
|
|
lv_page_set_scrl_fit2(roller, LV_FIT_PARENT, LV_FIT_NONE); /*Height is specified directly*/
|
2020-01-21 22:09:50 +01:00
|
|
|
lv_roller_set_anim_time(roller, LV_ROLLER_DEF_ANIM_TIME);
|
2020-02-19 11:52:15 +01:00
|
|
|
lv_roller_set_options(roller, "Option 1\nOption 2\nOption 3\nOption 4\nOption 5", LV_ROLLER_MODE_NORMAL);
|
2017-11-05 22:37:03 +01:00
|
|
|
|
2019-02-26 09:25:46 +01:00
|
|
|
lv_obj_set_signal_cb(scrl, lv_roller_scrl_signal);
|
2017-11-16 15:32:33 +01:00
|
|
|
|
2020-01-23 17:16:11 +01:00
|
|
|
lv_style_list_reset(lv_obj_get_style_list(roller, LV_PAGE_PART_SCRL));
|
2020-01-21 22:09:50 +01:00
|
|
|
lv_theme_apply(roller, LV_THEME_ROLLER);
|
|
|
|
refr_height(roller);
|
2020-01-01 15:25:16 +01:00
|
|
|
|
2020-02-19 11:52:15 +01:00
|
|
|
lv_roller_set_visible_row_count(roller, 4);
|
|
|
|
|
2017-09-03 16:01:51 +02:00
|
|
|
}
|
|
|
|
/*Copy an existing roller*/
|
|
|
|
else {
|
2019-03-17 04:32:37 +01:00
|
|
|
lv_roller_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
|
2020-02-13 14:39:15 +01:00
|
|
|
lv_roller_set_options(roller, lv_roller_get_options(copy), copy_ext->mode);
|
2019-03-17 04:32:37 +01:00
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
lv_obj_t * scrl = lv_page_get_scrl(roller);
|
2019-02-26 09:25:46 +01:00
|
|
|
lv_obj_set_signal_cb(scrl, lv_roller_scrl_signal);
|
2017-09-03 16:01:51 +02:00
|
|
|
}
|
2018-06-19 09:49:58 +02:00
|
|
|
|
2018-10-05 17:22:49 +02:00
|
|
|
LV_LOG_INFO("roller created");
|
2018-07-25 20:39:24 +02:00
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
return roller;
|
2017-09-03 16:01:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*=====================
|
|
|
|
* Setter functions
|
|
|
|
*====================*/
|
|
|
|
|
2019-03-17 04:32:37 +01:00
|
|
|
/**
|
|
|
|
* Set the options on a roller
|
|
|
|
* @param roller pointer to roller object
|
|
|
|
* @param options a string with '\n' separated options. E.g. "One\nTwo\nThree"
|
2019-06-25 15:14:47 +02:00
|
|
|
* @param mode `LV_ROLLER_MODE_NORMAL` or `LV_ROLLER_MODE_INFINITE`
|
2019-03-17 04:32:37 +01:00
|
|
|
*/
|
2019-06-25 15:14:47 +02:00
|
|
|
void lv_roller_set_options(lv_obj_t * roller, const char * options, lv_roller_mode_t mode)
|
2019-03-17 04:32:37 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(roller, LV_OBJX_NAME);
|
|
|
|
LV_ASSERT_STR(options);
|
|
|
|
|
2019-03-17 04:32:37 +01:00
|
|
|
lv_roller_ext_t * ext = lv_obj_get_ext_attr(roller);
|
2020-01-21 22:09:50 +01:00
|
|
|
lv_obj_t * label = get_label(roller);
|
|
|
|
|
|
|
|
ext->sel_opt_id = 0;
|
|
|
|
ext->sel_opt_id_ori = 0;
|
|
|
|
|
|
|
|
/*Count the '\n'-s to determine the number of options*/
|
|
|
|
ext->option_cnt = 0;
|
|
|
|
uint16_t i;
|
|
|
|
for(i = 0; options[i] != '\0'; i++) {
|
|
|
|
if(options[i] == '\n') ext->option_cnt++;
|
|
|
|
}
|
|
|
|
ext->option_cnt++; /*Last option has no `\n`*/
|
2019-03-17 04:32:37 +01:00
|
|
|
|
2019-06-25 15:14:47 +02:00
|
|
|
if(mode == LV_ROLLER_MODE_NORMAL) {
|
|
|
|
ext->mode = LV_ROLLER_MODE_NORMAL;
|
2020-01-21 22:09:50 +01:00
|
|
|
lv_label_set_text(label, options);
|
2019-03-17 04:32:37 +01:00
|
|
|
} else {
|
2019-06-25 15:14:47 +02:00
|
|
|
ext->mode = LV_ROLLER_MODE_INIFINITE;
|
2019-03-17 04:32:37 +01:00
|
|
|
|
2019-12-02 12:20:01 +01:00
|
|
|
size_t opt_len = strlen(options) + 1; /*+1 to add '\n' after option lists*/
|
2019-03-17 04:32:37 +01:00
|
|
|
char * opt_extra = lv_mem_alloc(opt_len * LV_ROLLER_INF_PAGES);
|
|
|
|
uint8_t i;
|
|
|
|
for(i = 0; i < LV_ROLLER_INF_PAGES; i++) {
|
|
|
|
strcpy(&opt_extra[opt_len * i], options);
|
|
|
|
opt_extra[opt_len * (i + 1) - 1] = '\n';
|
|
|
|
}
|
|
|
|
opt_extra[opt_len * LV_ROLLER_INF_PAGES - 1] = '\0';
|
2020-01-21 22:09:50 +01:00
|
|
|
lv_label_set_text(label, opt_extra);
|
2019-03-17 04:32:37 +01:00
|
|
|
lv_mem_free(opt_extra);
|
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
ext->sel_opt_id = ((LV_ROLLER_INF_PAGES / 2) + 1) * ext->option_cnt;
|
2019-04-22 10:17:21 +02:00
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
ext->option_cnt = ext->option_cnt * LV_ROLLER_INF_PAGES;
|
2019-03-17 04:32:37 +01:00
|
|
|
}
|
2020-01-21 22:09:50 +01:00
|
|
|
|
|
|
|
ext->sel_opt_id_ori = ext->sel_opt_id;
|
|
|
|
|
|
|
|
|
|
|
|
refr_width(roller);
|
|
|
|
refr_height(roller);
|
|
|
|
refr_position(roller, LV_ANIM_OFF);
|
2019-03-17 04:32:37 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 11:46:37 +02:00
|
|
|
/**
|
|
|
|
* Set the align of the roller's options (left or center)
|
|
|
|
* @param roller - pointer to a roller object
|
|
|
|
* @param align - one of lv_label_align_t values (left, right, center)
|
|
|
|
*/
|
|
|
|
void lv_roller_set_align(lv_obj_t * roller, lv_label_align_t align)
|
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(roller, LV_OBJX_NAME);
|
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
lv_obj_t * label = get_label(roller);
|
2019-08-16 16:43:05 +02:00
|
|
|
if(label == NULL) return; /*Probably the roller is being deleted if the label is NULL.*/
|
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
lv_label_set_align(label, align);
|
2018-12-05 11:46:37 +02:00
|
|
|
}
|
|
|
|
|
2017-11-06 16:27:00 +01:00
|
|
|
/**
|
|
|
|
* Set the selected option
|
|
|
|
* @param roller pointer to a roller object
|
|
|
|
* @param sel_opt id of the selected option (0 ... number of option - 1);
|
2019-06-11 13:51:14 +02:00
|
|
|
* @param anim_en LV_ANIM_ON: set with animation; LV_ANOM_OFF set immediately
|
2017-09-03 16:01:51 +02:00
|
|
|
*/
|
2019-06-11 13:51:14 +02:00
|
|
|
void lv_roller_set_selected(lv_obj_t * roller, uint16_t sel_opt, lv_anim_enable_t anim)
|
2017-11-06 16:27:00 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(roller, LV_OBJX_NAME);
|
|
|
|
|
2019-03-07 00:05:16 +01:00
|
|
|
#if LV_USE_ANIMATION == 0
|
2019-06-11 13:51:14 +02:00
|
|
|
anim = LV_ANIM_OFF;
|
2018-03-01 12:21:49 +01:00
|
|
|
#endif
|
2018-05-16 23:09:30 +02:00
|
|
|
|
2020-01-21 22:32:25 +01:00
|
|
|
/* Set the value even if it's the same as the current value because
|
|
|
|
* if moving to the next option with an animation which was just deleted in the PRESS signal
|
|
|
|
* nothing will continue the animation. */
|
2018-05-16 23:09:30 +02:00
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
lv_roller_ext_t * ext = lv_obj_get_ext_attr(roller);
|
|
|
|
ext->sel_opt_id = sel_opt < ext->option_cnt ? sel_opt : ext->option_cnt - 1;
|
|
|
|
ext->sel_opt_id_ori = ext->sel_opt_id;
|
|
|
|
|
2019-06-11 13:51:14 +02:00
|
|
|
refr_position(roller, anim);
|
2017-11-06 16:27:00 +01:00
|
|
|
}
|
|
|
|
|
2017-12-17 20:11:14 +01:00
|
|
|
/**
|
|
|
|
* Set the height to show the given number of rows (options)
|
|
|
|
* @param roller pointer to a roller object
|
|
|
|
* @param row_cnt number of desired visible rows
|
|
|
|
*/
|
2018-06-19 09:49:58 +02:00
|
|
|
void lv_roller_set_visible_row_count(lv_obj_t * roller, uint8_t row_cnt)
|
2017-12-17 20:11:14 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(roller, LV_OBJX_NAME);
|
|
|
|
|
2020-02-15 00:33:26 +01:00
|
|
|
const lv_font_t * font = lv_obj_get_style_text_font(roller, LV_ROLLER_PART_BG);
|
2020-02-12 08:54:03 +01:00
|
|
|
lv_style_int_t line_space = lv_obj_get_style_text_line_space(roller, LV_ROLLER_PART_BG);
|
2020-02-19 11:52:15 +01:00
|
|
|
lv_obj_set_height(roller, (lv_font_get_line_height(font) + line_space) * row_cnt);
|
2020-01-21 22:09:50 +01:00
|
|
|
|
|
|
|
refr_position(roller, LV_ANIM_OFF);
|
2017-11-15 15:50:33 +01:00
|
|
|
}
|
|
|
|
|
2017-09-03 16:01:51 +02:00
|
|
|
/*=====================
|
|
|
|
* Getter functions
|
|
|
|
*====================*/
|
|
|
|
|
2019-03-17 04:32:37 +01:00
|
|
|
/**
|
|
|
|
* Get the id of the selected option
|
|
|
|
* @param roller pointer to a roller object
|
|
|
|
* @return id of the selected option (0 ... number of option - 1);
|
|
|
|
*/
|
2019-04-04 07:15:40 +02:00
|
|
|
uint16_t lv_roller_get_selected(const lv_obj_t * roller)
|
2019-03-17 04:32:37 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(roller, LV_OBJX_NAME);
|
|
|
|
|
2019-03-17 04:32:37 +01:00
|
|
|
lv_roller_ext_t * ext = lv_obj_get_ext_attr(roller);
|
2019-06-25 15:14:47 +02:00
|
|
|
if(ext->mode == LV_ROLLER_MODE_INIFINITE) {
|
2020-01-21 22:09:50 +01:00
|
|
|
uint16_t real_id_cnt = ext->option_cnt / LV_ROLLER_INF_PAGES;
|
|
|
|
return ext->sel_opt_id % real_id_cnt;
|
2019-03-17 04:32:37 +01:00
|
|
|
} else {
|
2020-01-21 22:09:50 +01:00
|
|
|
return ext->sel_opt_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current selected option as a string
|
|
|
|
* @param ddlist pointer to ddlist object
|
|
|
|
* @param buf pointer to an array to store the string
|
|
|
|
* @param buf_size size of `buf` in bytes. 0: to ignore it.
|
|
|
|
*/
|
|
|
|
void lv_roller_get_selected_str(const lv_obj_t * roller, char * buf, uint16_t buf_size)
|
|
|
|
{
|
|
|
|
LV_ASSERT_OBJ(roller, LV_OBJX_NAME);
|
|
|
|
|
|
|
|
lv_roller_ext_t * ext = lv_obj_get_ext_attr(roller);
|
|
|
|
lv_obj_t * label = get_label(roller);
|
|
|
|
uint16_t i;
|
|
|
|
uint16_t line = 0;
|
|
|
|
const char * opt_txt = lv_label_get_text(label);
|
|
|
|
size_t txt_len = strlen(opt_txt);
|
|
|
|
|
|
|
|
for(i = 0; i < txt_len && line != ext->sel_opt_id; i++) {
|
|
|
|
if(opt_txt[i] == '\n') line++;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t c;
|
|
|
|
for(c = 0; opt_txt[i] != '\n' && i < txt_len; c++, i++) {
|
|
|
|
if(buf_size && c >= buf_size - 1) {
|
2020-02-14 12:36:44 +01:00
|
|
|
LV_LOG_WARN("lv_dropdown_get_selected_str: the buffer was too small")
|
2020-01-21 22:09:50 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf[c] = opt_txt[i];
|
2019-03-17 04:32:37 +01:00
|
|
|
}
|
2020-01-21 22:09:50 +01:00
|
|
|
|
|
|
|
buf[c] = '\0';
|
2019-03-17 04:32:37 +01:00
|
|
|
}
|
|
|
|
|
2019-12-19 16:34:10 -05:00
|
|
|
/**
|
|
|
|
* Get the total number of options
|
|
|
|
* @param roller pointer to a roller object
|
|
|
|
* @return the total number of options
|
|
|
|
*/
|
|
|
|
uint16_t lv_roller_get_option_cnt(const lv_obj_t * roller)
|
|
|
|
{
|
|
|
|
LV_ASSERT_OBJ(roller, LV_OBJX_NAME);
|
|
|
|
|
|
|
|
lv_roller_ext_t * ext = lv_obj_get_ext_attr(roller);
|
|
|
|
if(ext->mode == LV_ROLLER_MODE_INIFINITE) {
|
2020-01-21 22:09:50 +01:00
|
|
|
return ext->option_cnt / LV_ROLLER_INF_PAGES;
|
2019-12-19 16:34:10 -05:00
|
|
|
} else {
|
2020-01-21 22:09:50 +01:00
|
|
|
return ext->option_cnt;
|
2019-12-19 16:34:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 11:46:37 +02:00
|
|
|
/**
|
|
|
|
* Get the align attribute. Default alignment after _create is LV_LABEL_ALIGN_CENTER
|
|
|
|
* @param roller pointer to a roller object
|
|
|
|
* @return LV_LABEL_ALIGN_LEFT, LV_LABEL_ALIGN_RIGHT or LV_LABEL_ALIGN_CENTER
|
|
|
|
*/
|
|
|
|
lv_label_align_t lv_roller_get_align(const lv_obj_t * roller)
|
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(roller, LV_OBJX_NAME);
|
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
return lv_label_get_align(get_label(roller));
|
2018-12-05 11:46:37 +02:00
|
|
|
}
|
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
|
2017-11-06 16:27:00 +01:00
|
|
|
/**
|
2020-01-21 22:09:50 +01:00
|
|
|
* Get the options of a roller
|
|
|
|
* @param roller pointer to roller object
|
|
|
|
* @return the options separated by '\n'-s (E.g. "Option1\nOption2\nOption3")
|
2017-09-03 16:01:51 +02:00
|
|
|
*/
|
2020-01-21 22:09:50 +01:00
|
|
|
const char * lv_roller_get_options(const lv_obj_t * roller)
|
2017-11-06 16:27:00 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(roller, LV_OBJX_NAME);
|
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
return lv_label_get_text(get_label(roller));
|
2017-11-06 16:27:00 +01:00
|
|
|
}
|
2017-09-03 16:01:51 +02:00
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
|
2017-09-03 16:01:51 +02:00
|
|
|
/**********************
|
|
|
|
* STATIC FUNCTIONS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the drawing related tasks of the rollers
|
|
|
|
* @param roller pointer to an object
|
2019-09-06 19:53:39 +02:00
|
|
|
* @param clip_area the object will be drawn only in this area
|
2017-09-03 16:01:51 +02:00
|
|
|
* @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area
|
|
|
|
* (return 'true' if yes)
|
|
|
|
* LV_DESIGN_DRAW: draw the object (always return 'true')
|
|
|
|
* LV_DESIGN_DRAW_POST: drawing after every children are drawn
|
2019-09-06 19:53:39 +02:00
|
|
|
* @param return an element of `lv_design_res_t`
|
2017-09-03 16:01:51 +02:00
|
|
|
*/
|
2019-09-06 19:53:39 +02:00
|
|
|
static lv_design_res_t lv_roller_design(lv_obj_t * roller, const lv_area_t * clip_area, lv_design_mode_t mode)
|
2017-09-03 16:01:51 +02:00
|
|
|
{
|
|
|
|
/*Return false if the object is not covers the mask_p area*/
|
|
|
|
if(mode == LV_DESIGN_COVER_CHK) {
|
2019-09-06 19:53:39 +02:00
|
|
|
return LV_DESIGN_RES_NOT_COVER;
|
2017-09-03 16:01:51 +02:00
|
|
|
}
|
|
|
|
/*Draw the object*/
|
|
|
|
else if(mode == LV_DESIGN_DRAW_MAIN) {
|
2019-09-06 19:53:39 +02:00
|
|
|
draw_bg(roller, clip_area);
|
2017-11-05 22:37:03 +01:00
|
|
|
|
2020-02-15 00:33:26 +01:00
|
|
|
const lv_font_t * font = lv_obj_get_style_text_font(roller, LV_ROLLER_PART_BG);
|
2020-02-12 08:54:03 +01:00
|
|
|
lv_style_int_t line_space = lv_obj_get_style_text_line_space(roller, LV_ROLLER_PART_BG);
|
2019-04-23 15:56:59 +02:00
|
|
|
lv_coord_t font_h = lv_font_get_line_height(font);
|
2017-11-23 21:28:36 +01:00
|
|
|
lv_area_t rect_area;
|
2020-01-01 15:25:16 +01:00
|
|
|
rect_area.y1 = roller->coords.y1 + lv_obj_get_height(roller) / 2 - font_h / 2 - line_space / 2;
|
|
|
|
if((font_h & 0x1) && (line_space & 0x1)) rect_area.y1--; /*Compensate the two rounding error*/
|
|
|
|
rect_area.y2 = rect_area.y1 + font_h + line_space - 1;
|
2019-06-27 07:16:15 +02:00
|
|
|
lv_area_t roller_coords;
|
|
|
|
lv_obj_get_coords(roller, &roller_coords);
|
|
|
|
lv_obj_get_inner_coords(roller, &roller_coords);
|
2019-06-01 21:37:25 -04:00
|
|
|
|
|
|
|
rect_area.x1 = roller_coords.x1;
|
|
|
|
rect_area.x2 = roller_coords.x2;
|
2017-09-03 16:01:51 +02:00
|
|
|
|
2020-01-01 15:25:16 +01:00
|
|
|
lv_draw_rect_dsc_t sel_dsc;
|
|
|
|
lv_draw_rect_dsc_init(&sel_dsc);
|
|
|
|
lv_obj_init_draw_rect_dsc(roller, LV_ROLLER_PART_SEL, &sel_dsc);
|
|
|
|
lv_draw_rect(&rect_area, clip_area, &sel_dsc);
|
2017-09-03 16:01:51 +02:00
|
|
|
}
|
|
|
|
/*Post draw when the children are drawn*/
|
|
|
|
else if(mode == LV_DESIGN_DRAW_POST) {
|
2020-01-01 15:25:16 +01:00
|
|
|
lv_draw_label_dsc_t label_dsc;
|
|
|
|
lv_draw_label_dsc_init(&label_dsc);
|
|
|
|
lv_obj_init_draw_label_dsc(roller, LV_ROLLER_PART_SEL, &label_dsc);
|
|
|
|
|
|
|
|
lv_coord_t font_h = lv_font_get_line_height(label_dsc.font);
|
2017-12-17 20:11:14 +01:00
|
|
|
|
2017-11-15 22:23:17 +01:00
|
|
|
/*Redraw the text on the selected area with a different color*/
|
2017-11-23 21:28:36 +01:00
|
|
|
lv_area_t rect_area;
|
2020-01-01 15:25:16 +01:00
|
|
|
rect_area.y1 = roller->coords.y1 + lv_obj_get_height(roller) / 2 - font_h / 2 - label_dsc.line_space / 2;
|
|
|
|
if((font_h & 0x1) && (label_dsc.line_space & 0x1)) rect_area.y1--; /*Compensate the two rounding error*/
|
|
|
|
rect_area.y2 = rect_area.y1 + font_h + label_dsc.line_space - 1;
|
2017-11-15 22:23:17 +01:00
|
|
|
rect_area.x1 = roller->coords.x1;
|
|
|
|
rect_area.x2 = roller->coords.x2;
|
2017-11-23 21:28:36 +01:00
|
|
|
lv_area_t mask_sel;
|
2017-11-15 22:23:17 +01:00
|
|
|
bool area_ok;
|
2019-09-06 19:53:39 +02:00
|
|
|
area_ok = lv_area_intersect(&mask_sel, clip_area, &rect_area);
|
2017-11-15 22:23:17 +01:00
|
|
|
if(area_ok) {
|
2020-01-21 22:09:50 +01:00
|
|
|
lv_obj_t * label = get_label(roller);
|
|
|
|
lv_label_align_t label_align = lv_roller_get_align(roller);
|
2018-12-05 11:46:37 +02:00
|
|
|
|
2020-01-01 15:25:16 +01:00
|
|
|
if(LV_LABEL_ALIGN_CENTER == label_align) {
|
|
|
|
label_dsc.flag |= LV_TXT_FLAG_CENTER;
|
|
|
|
} else if(LV_LABEL_ALIGN_RIGHT == label_align) {
|
|
|
|
label_dsc.flag |= LV_TXT_FLAG_RIGHT;
|
2018-12-05 11:46:37 +02:00
|
|
|
}
|
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
lv_draw_label(&label->coords, &mask_sel, &label_dsc, lv_label_get_text(label), NULL);
|
2017-11-15 22:23:17 +01:00
|
|
|
}
|
2017-09-03 16:01:51 +02:00
|
|
|
}
|
|
|
|
|
2019-09-06 19:53:39 +02:00
|
|
|
return LV_DESIGN_RES_OK;
|
2017-09-03 16:01:51 +02:00
|
|
|
}
|
|
|
|
|
2017-11-05 22:37:03 +01:00
|
|
|
/**
|
|
|
|
* Signal function of the roller
|
|
|
|
* @param roller pointer to a roller object
|
|
|
|
* @param sign a signal type from lv_signal_t enum
|
|
|
|
* @param param pointer to a signal specific variable
|
|
|
|
* @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
|
|
|
|
*/
|
|
|
|
static lv_res_t lv_roller_signal(lv_obj_t * roller, lv_signal_t sign, void * param)
|
|
|
|
{
|
2020-02-15 15:22:24 -05:00
|
|
|
lv_res_t res = LV_RES_OK;
|
2020-01-01 15:25:16 +01:00
|
|
|
if(sign == LV_SIGNAL_GET_STYLE) {
|
2020-01-21 22:09:50 +01:00
|
|
|
lv_get_style_info_t * info = param;
|
|
|
|
info->result = lv_roller_get_style(roller, info->part);
|
|
|
|
if(info->result != NULL) return LV_RES_OK;
|
2020-01-01 15:25:16 +01:00
|
|
|
return LV_RES_OK;
|
|
|
|
}
|
2017-11-05 22:37:03 +01:00
|
|
|
|
2017-11-18 00:18:19 +01:00
|
|
|
/*Don't let the drop down list to handle the control signals. It works differently*/
|
2019-03-19 07:15:00 +01:00
|
|
|
if(sign != LV_SIGNAL_CONTROL && sign != LV_SIGNAL_FOCUS && sign != LV_SIGNAL_DEFOCUS) {
|
2018-06-19 09:49:58 +02:00
|
|
|
/* Include the ancient signal function */
|
2017-11-18 00:18:19 +01:00
|
|
|
res = ancestor_signal(roller, sign, param);
|
|
|
|
if(res != LV_RES_OK) return res;
|
|
|
|
}
|
2020-01-01 15:25:16 +01:00
|
|
|
|
2019-09-26 12:54:40 +02:00
|
|
|
if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, LV_OBJX_NAME);
|
2017-11-07 17:00:55 +01:00
|
|
|
|
|
|
|
lv_roller_ext_t * ext = lv_obj_get_ext_attr(roller);
|
2018-12-05 11:46:37 +02:00
|
|
|
|
2017-11-07 17:00:55 +01:00
|
|
|
if(sign == LV_SIGNAL_STYLE_CHG) {
|
2019-04-22 10:17:21 +02:00
|
|
|
refr_height(roller);
|
2020-01-21 22:09:50 +01:00
|
|
|
refr_width(roller);
|
2017-11-07 17:00:55 +01:00
|
|
|
refr_position(roller, false);
|
2020-01-05 01:16:13 +01:00
|
|
|
} else if(sign == LV_SIGNAL_COORD_CHG) {
|
2017-11-07 17:00:55 +01:00
|
|
|
|
2017-11-28 16:15:13 +01:00
|
|
|
if(lv_obj_get_width(roller) != lv_area_get_width(param) ||
|
2019-04-04 07:15:40 +02:00
|
|
|
lv_obj_get_height(roller) != lv_area_get_height(param)) {
|
2019-05-20 18:31:47 -07:00
|
|
|
#if LV_USE_ANIMATION
|
2019-06-12 23:10:54 +02:00
|
|
|
lv_anim_del(lv_page_get_scrl(roller), (lv_anim_exec_xcb_t)lv_obj_set_y);
|
2019-05-20 18:31:47 -07:00
|
|
|
#endif
|
2017-11-05 22:37:03 +01:00
|
|
|
refr_position(roller, false);
|
|
|
|
}
|
2020-01-21 22:09:50 +01:00
|
|
|
} else if(sign == LV_SIGNAL_RELEASED) {
|
|
|
|
release_handler(roller);
|
2018-06-19 09:49:58 +02:00
|
|
|
} else if(sign == LV_SIGNAL_FOCUS) {
|
2019-03-07 00:05:16 +01:00
|
|
|
#if LV_USE_GROUP
|
2019-04-04 07:15:40 +02:00
|
|
|
lv_group_t * g = lv_obj_get_group(roller);
|
|
|
|
bool editing = lv_group_get_editing(g);
|
2019-03-29 16:10:18 +01:00
|
|
|
lv_indev_type_t indev_type = lv_indev_get_type(lv_indev_get_act());
|
2018-09-24 22:59:48 +02:00
|
|
|
|
|
|
|
/*Encoders need special handling*/
|
|
|
|
if(indev_type == LV_INDEV_TYPE_ENCODER) {
|
|
|
|
/*In navigate mode revert the original value*/
|
|
|
|
if(!editing) {
|
2020-01-21 22:09:50 +01:00
|
|
|
if(ext->sel_opt_id != ext->sel_opt_id_ori) {
|
|
|
|
ext->sel_opt_id = ext->sel_opt_id_ori;
|
2018-09-24 22:59:48 +02:00
|
|
|
refr_position(roller, true);
|
|
|
|
}
|
2018-09-24 00:35:19 +02:00
|
|
|
}
|
2018-09-24 22:59:48 +02:00
|
|
|
/*Save the current state when entered to edit mode*/
|
|
|
|
else {
|
2020-01-21 22:09:50 +01:00
|
|
|
ext->sel_opt_id_ori = ext->sel_opt_id;
|
2018-09-24 22:59:48 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-01-21 22:09:50 +01:00
|
|
|
ext->sel_opt_id_ori = ext->sel_opt_id; /*Save the current value. Used to revert this state if
|
2019-06-06 06:05:40 +02:00
|
|
|
ENER wont't be pressed*/
|
2018-09-24 00:35:19 +02:00
|
|
|
}
|
2019-02-18 12:17:56 -05:00
|
|
|
#endif
|
2018-06-19 09:49:58 +02:00
|
|
|
} else if(sign == LV_SIGNAL_DEFOCUS) {
|
2019-03-07 00:05:16 +01:00
|
|
|
#if LV_USE_GROUP
|
2018-03-01 12:21:49 +01:00
|
|
|
/*Revert the original state*/
|
2020-01-21 22:09:50 +01:00
|
|
|
if(ext->sel_opt_id != ext->sel_opt_id_ori) {
|
|
|
|
ext->sel_opt_id = ext->sel_opt_id_ori;
|
2018-03-01 12:21:49 +01:00
|
|
|
refr_position(roller, true);
|
|
|
|
}
|
2019-02-18 12:17:56 -05:00
|
|
|
#endif
|
2019-03-19 07:15:00 +01:00
|
|
|
} else if(sign == LV_SIGNAL_CONTROL) {
|
2018-06-19 09:49:58 +02:00
|
|
|
char c = *((char *)param);
|
2019-04-08 14:36:20 +02:00
|
|
|
if(c == LV_KEY_RIGHT || c == LV_KEY_DOWN) {
|
2020-01-21 22:09:50 +01:00
|
|
|
if(ext->sel_opt_id + 1 < ext->option_cnt) {
|
|
|
|
uint16_t ori_id = ext->sel_opt_id_ori; /*lv_roller_set_selceted will overwrite this*/
|
|
|
|
lv_roller_set_selected(roller, ext->sel_opt_id + 1, true);
|
|
|
|
ext->sel_opt_id_ori = ori_id;
|
2017-11-18 00:18:19 +01:00
|
|
|
}
|
2019-04-08 14:36:20 +02:00
|
|
|
} else if(c == LV_KEY_LEFT || c == LV_KEY_UP) {
|
2020-01-21 22:09:50 +01:00
|
|
|
if(ext->sel_opt_id > 0) {
|
|
|
|
uint16_t ori_id = ext->sel_opt_id_ori; /*lv_roller_set_selceted will overwrite this*/
|
|
|
|
lv_roller_set_selected(roller, ext->sel_opt_id - 1, true);
|
|
|
|
ext->sel_opt_id_ori = ori_id;
|
2017-11-18 00:18:19 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-28 15:37:41 +01:00
|
|
|
}
|
2017-11-05 22:37:03 +01:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-01-01 15:25:16 +01:00
|
|
|
/**
|
|
|
|
* Get the style descriptor of a part of the object
|
|
|
|
* @param page pointer the object
|
|
|
|
* @param part the part from `lv_roller_part_t`. (LV_ROLLER_PART_...)
|
|
|
|
* @return pointer to the style descriptor of the specified part
|
|
|
|
*/
|
2020-01-16 14:26:36 +01:00
|
|
|
static lv_style_list_t * lv_roller_get_style(lv_obj_t * roller, uint8_t part)
|
2020-01-01 15:25:16 +01:00
|
|
|
{
|
|
|
|
LV_ASSERT_OBJ(roller, LV_OBJX_NAME);
|
|
|
|
|
|
|
|
lv_roller_ext_t * ext = lv_obj_get_ext_attr(roller);
|
2020-01-16 14:26:36 +01:00
|
|
|
lv_style_list_t * style_dsc_p;
|
2020-01-01 15:25:16 +01:00
|
|
|
|
|
|
|
switch(part) {
|
|
|
|
case LV_ROLLER_PART_BG:
|
2020-01-16 21:25:11 +01:00
|
|
|
style_dsc_p = &roller->style_list;
|
2020-01-01 15:25:16 +01:00
|
|
|
break;
|
|
|
|
case LV_ROLLER_PART_SEL:
|
2020-01-21 22:09:50 +01:00
|
|
|
style_dsc_p = &ext->style_sel;
|
2020-01-01 15:25:16 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
style_dsc_p = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return style_dsc_p;
|
|
|
|
}
|
|
|
|
|
2017-09-23 22:14:31 +02:00
|
|
|
/**
|
|
|
|
* Signal function of the scrollable part of the roller.
|
|
|
|
* @param roller_scrl ointer to the scrollable part of roller (page)
|
|
|
|
* @param sign a signal type from lv_signal_t enum
|
|
|
|
* @param param pointer to a signal specific variable
|
2017-11-05 22:37:03 +01:00
|
|
|
* @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
|
2017-09-23 22:14:31 +02:00
|
|
|
*/
|
2017-11-05 22:37:03 +01:00
|
|
|
static lv_res_t lv_roller_scrl_signal(lv_obj_t * roller_scrl, lv_signal_t sign, void * param)
|
2017-09-03 16:01:51 +02:00
|
|
|
{
|
2017-11-05 22:37:03 +01:00
|
|
|
lv_res_t res;
|
2017-09-03 16:01:51 +02:00
|
|
|
|
|
|
|
/* Include the ancient signal function */
|
2017-11-05 22:37:03 +01:00
|
|
|
res = ancestor_scrl_signal(roller_scrl, sign, param);
|
2017-11-07 17:00:55 +01:00
|
|
|
if(res != LV_RES_OK) return res;
|
2019-12-07 21:11:38 +01:00
|
|
|
if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, LV_OBJX_NAME);
|
2017-11-07 17:00:55 +01:00
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
lv_obj_t * roller = lv_obj_get_parent(roller_scrl);
|
|
|
|
|
|
|
|
/*On delete the ddlist signal deletes the label so nothing left to do here*/
|
|
|
|
lv_obj_t * label = get_label(roller);
|
|
|
|
if(label == NULL) return LV_RES_INV;
|
|
|
|
|
2019-04-04 07:15:40 +02:00
|
|
|
int32_t id = -1;
|
2017-11-07 17:00:55 +01:00
|
|
|
lv_roller_ext_t * ext = lv_obj_get_ext_attr(roller);
|
2017-12-03 00:35:39 +01:00
|
|
|
|
2020-02-15 00:33:26 +01:00
|
|
|
const lv_font_t * font = lv_obj_get_style_text_font(roller, LV_ROLLER_PART_BG);
|
2020-02-12 08:54:03 +01:00
|
|
|
lv_style_int_t line_space = lv_obj_get_style_text_line_space(roller, LV_ROLLER_PART_BG);
|
2019-04-23 15:56:59 +02:00
|
|
|
lv_coord_t font_h = lv_font_get_line_height(font);
|
2017-11-07 17:00:55 +01:00
|
|
|
|
|
|
|
if(sign == LV_SIGNAL_DRAG_END) {
|
2019-08-16 13:43:28 +02:00
|
|
|
/*If dragged then align the list to have an element in the middle*/
|
2020-01-21 22:09:50 +01:00
|
|
|
lv_coord_t label_y1 = label->coords.y1 - roller->coords.y1;
|
2020-01-01 15:25:16 +01:00
|
|
|
lv_coord_t label_unit = font_h + line_space;
|
2019-04-04 07:15:40 +02:00
|
|
|
lv_coord_t mid = (roller->coords.y2 - roller->coords.y1) / 2;
|
2019-05-04 07:53:31 +02:00
|
|
|
|
2020-01-01 15:25:16 +01:00
|
|
|
id = (mid - label_y1 + line_space / 2) / label_unit;
|
2019-03-17 04:32:37 +01:00
|
|
|
|
2017-11-07 17:00:55 +01:00
|
|
|
if(id < 0) id = 0;
|
2020-01-21 22:09:50 +01:00
|
|
|
if(id >= ext->option_cnt) id = ext->option_cnt - 1;
|
2019-03-17 04:32:37 +01:00
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
ext->sel_opt_id = id;
|
|
|
|
ext->sel_opt_id_ori = id;
|
2019-04-04 07:15:40 +02:00
|
|
|
res = lv_event_send(roller, LV_EVENT_VALUE_CHANGED, &id);
|
2019-03-07 00:42:08 +01:00
|
|
|
if(res != LV_RES_OK) return res;
|
2019-05-04 07:53:31 +02:00
|
|
|
}
|
|
|
|
/*If picked an option by clicking then set it*/
|
|
|
|
else if(sign == LV_SIGNAL_RELEASED) {
|
2020-01-21 22:09:50 +01:00
|
|
|
release_handler(roller);
|
2019-06-06 06:05:40 +02:00
|
|
|
} else if(sign == LV_SIGNAL_PRESSED) {
|
2019-05-20 18:31:47 -07:00
|
|
|
#if LV_USE_ANIMATION
|
2019-06-12 23:10:54 +02:00
|
|
|
lv_anim_del(roller_scrl, (lv_anim_exec_xcb_t)lv_obj_set_y);
|
2019-05-20 18:31:47 -07:00
|
|
|
#endif
|
2019-05-04 07:53:31 +02:00
|
|
|
}
|
2017-09-22 23:55:54 +02:00
|
|
|
|
2017-11-07 17:00:55 +01:00
|
|
|
/*Position the scrollable according to the new selected option*/
|
2018-11-08 22:39:08 +08:00
|
|
|
if(id != -1) {
|
2020-01-21 22:09:50 +01:00
|
|
|
refr_position(roller, LV_ANIM_ON);
|
2017-09-03 16:01:51 +02:00
|
|
|
}
|
|
|
|
|
2017-11-05 22:37:03 +01:00
|
|
|
return res;
|
2017-09-03 16:01:51 +02:00
|
|
|
}
|
|
|
|
|
2017-11-05 22:37:03 +01:00
|
|
|
/**
|
|
|
|
* Draw a rectangle which has gradient on its top and bottom
|
|
|
|
* @param roller pointer to a roller object
|
2020-01-21 22:09:50 +01:00
|
|
|
* @param clip_area pointer to the current mask (from the design function)
|
2017-11-05 22:37:03 +01:00
|
|
|
*/
|
2020-01-21 22:09:50 +01:00
|
|
|
static void draw_bg(lv_obj_t * roller, const lv_area_t * clip_area)
|
2017-11-05 22:37:03 +01:00
|
|
|
{
|
2020-01-01 15:25:16 +01:00
|
|
|
lv_draw_rect_dsc_t bg_dsc;
|
|
|
|
lv_draw_rect_dsc_init(&bg_dsc);
|
|
|
|
lv_obj_init_draw_rect_dsc(roller, LV_ROLLER_PART_BG, &bg_dsc);
|
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
/*With non-vertical gradient simply draw the background*/
|
|
|
|
if(bg_dsc.bg_grad_dir == LV_GRAD_DIR_NONE) {
|
|
|
|
lv_draw_rect(&roller->coords, clip_area, &bg_dsc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*With vertical gradient mirror it*/
|
|
|
|
|
2017-11-23 21:28:36 +01:00
|
|
|
lv_area_t half_mask;
|
|
|
|
lv_coord_t h = lv_obj_get_height(roller);
|
2017-11-05 22:37:03 +01:00
|
|
|
bool union_ok;
|
|
|
|
|
2020-01-01 15:25:16 +01:00
|
|
|
lv_area_copy(&half_mask, &roller->coords);
|
|
|
|
half_mask.x1 -= roller->ext_draw_pad; /*Add ext size too (e.g. because of shadow draw) */
|
|
|
|
half_mask.x2 += roller->ext_draw_pad;
|
|
|
|
half_mask.y1 -= roller->ext_draw_pad;
|
|
|
|
half_mask.y2 = roller->coords.y1 + h / 2;
|
2017-11-05 22:37:03 +01:00
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
union_ok = lv_area_intersect(&half_mask, &half_mask, clip_area);
|
2020-01-01 15:25:16 +01:00
|
|
|
bg_dsc.bg_main_color_stop = bg_dsc.bg_main_color_stop / 2;
|
|
|
|
bg_dsc.bg_grad_color_stop = 128 - (255 - bg_dsc.bg_grad_color_stop) / 2;
|
2017-11-05 22:37:03 +01:00
|
|
|
if(union_ok) {
|
2020-01-01 15:25:16 +01:00
|
|
|
lv_draw_rect(&roller->coords, &half_mask, &bg_dsc);
|
2017-11-05 22:37:03 +01:00
|
|
|
}
|
|
|
|
|
2020-01-01 15:25:16 +01:00
|
|
|
lv_area_copy(&half_mask, &roller->coords);
|
|
|
|
half_mask.x1 -= roller->ext_draw_pad; /*Revert ext. size adding*/
|
|
|
|
half_mask.x2 += roller->ext_draw_pad;
|
|
|
|
half_mask.y1 = roller->coords.y1 + h / 2;
|
|
|
|
half_mask.y2 += roller->ext_draw_pad;
|
2017-11-05 22:37:03 +01:00
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
union_ok = lv_area_intersect(&half_mask, &half_mask, clip_area);
|
2018-06-19 09:49:58 +02:00
|
|
|
if(union_ok) {
|
2020-01-01 15:25:16 +01:00
|
|
|
lv_color_t c = bg_dsc.bg_color;
|
|
|
|
bg_dsc.bg_color = bg_dsc.bg_grad_color;
|
|
|
|
bg_dsc.bg_grad_color = c;
|
|
|
|
|
|
|
|
bg_dsc.bg_main_color_stop += 127;
|
|
|
|
bg_dsc.bg_grad_color_stop += 127;
|
|
|
|
lv_draw_rect(&roller->coords, &half_mask, &bg_dsc);
|
2017-11-05 22:37:03 +01:00
|
|
|
}
|
|
|
|
}
|
2017-09-03 16:01:51 +02:00
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
|
2017-11-05 22:37:03 +01:00
|
|
|
/**
|
|
|
|
* Refresh the position of the roller. It uses the id stored in: ext->ddlist.selected_option_id
|
|
|
|
* @param roller pointer to a roller object
|
2019-06-19 00:35:35 +02:00
|
|
|
* @param anim_en LV_ANIM_ON: refresh with animation; LV_ANOM_OFF: without animation
|
2017-11-05 22:37:03 +01:00
|
|
|
*/
|
2019-06-19 00:35:35 +02:00
|
|
|
static void refr_position(lv_obj_t * roller, lv_anim_enable_t anim_en)
|
2017-11-05 22:37:03 +01:00
|
|
|
{
|
2019-03-07 00:05:16 +01:00
|
|
|
#if LV_USE_ANIMATION == 0
|
2019-06-19 00:35:35 +02:00
|
|
|
anim_en = LV_ANIM_OFF;
|
2018-03-01 12:21:49 +01:00
|
|
|
#endif
|
2019-06-19 00:35:35 +02:00
|
|
|
|
2019-04-11 19:59:55 +08:00
|
|
|
lv_obj_t * roller_scrl = lv_page_get_scrl(roller);
|
|
|
|
lv_roller_ext_t * ext = lv_obj_get_ext_attr(roller);
|
2020-02-15 00:33:26 +01:00
|
|
|
const lv_font_t * font = lv_obj_get_style_text_font(roller, LV_ROLLER_PART_BG);
|
2020-02-12 08:54:03 +01:00
|
|
|
lv_style_int_t line_space = lv_obj_get_style_text_line_space(roller, LV_ROLLER_PART_BG);
|
2019-04-23 15:56:59 +02:00
|
|
|
lv_coord_t font_h = lv_font_get_line_height(font);
|
2019-04-11 19:59:55 +08:00
|
|
|
lv_coord_t h = lv_obj_get_height(roller);
|
2019-06-27 07:16:15 +02:00
|
|
|
uint16_t anim_time = lv_roller_get_anim_time(roller);
|
2019-03-17 04:32:37 +01:00
|
|
|
|
|
|
|
/* Normally the animtaion's `end_cb` sets correct position of the roller is infinite.
|
|
|
|
* But without animations do it manually*/
|
2019-06-19 00:35:35 +02:00
|
|
|
if(anim_en == LV_ANIM_OFF || anim_time == 0) {
|
2019-03-17 04:32:37 +01:00
|
|
|
inf_normalize(roller_scrl);
|
|
|
|
}
|
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
lv_obj_t * label = get_label(roller);
|
|
|
|
|
|
|
|
int32_t id = ext->sel_opt_id;
|
|
|
|
lv_coord_t line_y1 = id * (font_h + line_space) + label->coords.y1 - roller_scrl->coords.y1;
|
2019-04-04 07:15:40 +02:00
|
|
|
lv_coord_t new_y = -line_y1 + (h - font_h) / 2;
|
2017-11-05 22:37:03 +01:00
|
|
|
|
2019-06-19 00:35:35 +02:00
|
|
|
if(anim_en == LV_ANIM_OFF || anim_time == 0) {
|
2020-02-19 12:46:31 +01:00
|
|
|
#if LV_USE_ANIMATION
|
2020-02-19 11:52:15 +01:00
|
|
|
lv_anim_del(roller_scrl, (lv_anim_exec_xcb_t)lv_obj_set_y);
|
2020-02-19 12:46:31 +01:00
|
|
|
#endif
|
2017-11-05 22:37:03 +01:00
|
|
|
lv_obj_set_y(roller_scrl, new_y);
|
|
|
|
} else {
|
2019-03-07 00:05:16 +01:00
|
|
|
#if LV_USE_ANIMATION
|
2017-11-23 21:28:36 +01:00
|
|
|
lv_anim_t a;
|
2020-02-19 06:18:24 +01:00
|
|
|
lv_anim_init(&a);
|
|
|
|
lv_anim_set_var(&a, roller_scrl);
|
|
|
|
lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_y);
|
|
|
|
lv_anim_set_values(&a, lv_obj_get_y(roller_scrl), new_y);
|
|
|
|
lv_anim_set_time(&a, anim_time);
|
2020-02-19 06:39:55 +01:00
|
|
|
lv_anim_set_ready_cb(&a, scroll_anim_ready_cb);
|
2020-02-19 06:18:24 +01:00
|
|
|
lv_anim_start(&a);
|
2017-11-27 17:48:54 +01:00
|
|
|
#endif
|
2017-11-05 22:37:03 +01:00
|
|
|
}
|
|
|
|
}
|
2017-09-03 16:01:51 +02:00
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a drop down list is released to open it or set new option
|
|
|
|
* @param ddlist pointer to a drop down list object
|
|
|
|
* @return LV_ACTION_RES_INV if the ddlist it deleted in the user callback else LV_ACTION_RES_OK
|
|
|
|
*/
|
|
|
|
static lv_res_t release_handler(lv_obj_t * roller)
|
|
|
|
{
|
2020-02-19 11:52:15 +01:00
|
|
|
|
|
|
|
/*If there was dragging `DRAG_END` signal will refresh the position and update the selected option*/
|
|
|
|
if(lv_indev_is_dragging(lv_indev_get_act())) return LV_RES_OK;
|
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
lv_roller_ext_t * ext = lv_obj_get_ext_attr(roller);
|
|
|
|
|
|
|
|
lv_indev_t * indev = lv_indev_get_act();
|
|
|
|
#if LV_USE_GROUP
|
|
|
|
/*Leave edit mode once a new option is selected*/
|
|
|
|
if(lv_indev_get_type(indev) == LV_INDEV_TYPE_ENCODER) {
|
|
|
|
ext->sel_opt_id_ori = ext->sel_opt_id;
|
|
|
|
lv_group_t * g = lv_obj_get_group(roller);
|
|
|
|
if(lv_group_get_editing(g)) {
|
|
|
|
lv_group_set_editing(g, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
lv_obj_t * label = get_label(roller);
|
|
|
|
|
|
|
|
/*Search the clicked option (For KEYPAD and ENCODER the new value should be already set)*/
|
|
|
|
uint16_t new_opt = 0;
|
|
|
|
if(lv_indev_get_type(indev) == LV_INDEV_TYPE_POINTER || lv_indev_get_type(indev) == LV_INDEV_TYPE_BUTTON) {
|
|
|
|
lv_point_t p;
|
|
|
|
lv_indev_get_point(indev, &p);
|
|
|
|
p.y -= label->coords.y1;
|
|
|
|
p.x -= label->coords.x1;
|
|
|
|
uint16_t letter_i;
|
|
|
|
letter_i = lv_label_get_letter_on(label, &p);
|
|
|
|
|
|
|
|
const char * txt = lv_label_get_text(label);
|
|
|
|
uint32_t i = 0;
|
|
|
|
uint32_t i_prev = 0;
|
|
|
|
|
|
|
|
uint32_t letter_cnt = 0;
|
|
|
|
uint32_t letter;
|
|
|
|
for(letter_cnt = 0; letter_cnt < letter_i; letter_cnt++) {
|
|
|
|
letter = lv_txt_encoded_next(txt, &i);
|
|
|
|
/*Count he lines to reach the clicked letter. But ignore the last '\n' because it
|
|
|
|
* still belongs to the clicked line*/
|
|
|
|
if(letter == '\n' && i_prev != letter_i) new_opt++;
|
|
|
|
i_prev = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lv_roller_set_selected(roller, new_opt, LV_ANIM_ON);
|
|
|
|
|
|
|
|
uint32_t id = ext->sel_opt_id; /*Just to use uint32_t in event data*/
|
|
|
|
lv_res_t res = lv_event_send(roller, LV_EVENT_VALUE_CHANGED, &id);
|
|
|
|
if(res != LV_RES_OK) return res;
|
|
|
|
return LV_RES_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void refr_width(lv_obj_t * roller)
|
|
|
|
{
|
|
|
|
lv_obj_t * label = get_label(roller);
|
|
|
|
lv_coord_t label_w = lv_obj_get_width(label);
|
|
|
|
|
2020-02-02 15:58:08 +01:00
|
|
|
lv_style_int_t left = lv_obj_get_style_pad_left(roller, LV_ROLLER_PART_BG);
|
|
|
|
lv_style_int_t right = lv_obj_get_style_pad_right(roller, LV_ROLLER_PART_BG);
|
2020-01-21 22:09:50 +01:00
|
|
|
|
|
|
|
lv_obj_set_width(roller, label_w + left + right);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-22 10:17:21 +02:00
|
|
|
/**
|
|
|
|
* Refresh the height of the roller and the scrolable
|
|
|
|
* @param roller pointer to roller
|
|
|
|
*/
|
|
|
|
static void refr_height(lv_obj_t * roller)
|
|
|
|
{
|
2020-01-21 22:09:50 +01:00
|
|
|
lv_obj_t * label = get_label(roller);
|
2019-04-22 10:17:21 +02:00
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
lv_obj_set_height(lv_page_get_scrl(roller), lv_obj_get_height(label) + lv_obj_get_height(roller));
|
|
|
|
lv_obj_align(label, NULL, LV_ALIGN_CENTER, 0, 0);
|
2019-05-20 18:31:47 -07:00
|
|
|
#if LV_USE_ANIMATION
|
2019-06-12 23:10:54 +02:00
|
|
|
lv_anim_del(lv_page_get_scrl(roller), (lv_anim_exec_xcb_t)lv_obj_set_y);
|
2019-05-20 18:31:47 -07:00
|
|
|
#endif
|
2020-01-21 22:09:50 +01:00
|
|
|
refr_position(roller, LV_ANIM_OFF);
|
2019-04-22 10:17:21 +02:00
|
|
|
}
|
|
|
|
|
2019-03-17 04:32:37 +01:00
|
|
|
/**
|
|
|
|
* Set the middle page for the roller if inifinte is enabled
|
2019-04-09 16:18:04 -04:00
|
|
|
* @param scrl pointer to the roller's scrollable (lv_obj_t *)
|
2019-03-17 04:32:37 +01:00
|
|
|
*/
|
2019-04-09 16:18:04 -04:00
|
|
|
static void inf_normalize(void * scrl)
|
2019-03-17 04:32:37 +01:00
|
|
|
{
|
2019-04-10 08:40:52 +02:00
|
|
|
lv_obj_t * roller_scrl = (lv_obj_t *)scrl;
|
|
|
|
lv_obj_t * roller = lv_obj_get_parent(roller_scrl);
|
|
|
|
lv_roller_ext_t * ext = lv_obj_get_ext_attr(roller);
|
2019-03-17 04:32:37 +01:00
|
|
|
|
2019-06-25 15:14:47 +02:00
|
|
|
if(ext->mode == LV_ROLLER_MODE_INIFINITE) {
|
2020-01-21 22:09:50 +01:00
|
|
|
uint16_t real_id_cnt = ext->option_cnt / LV_ROLLER_INF_PAGES;
|
2019-03-17 04:32:37 +01:00
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
ext->sel_opt_id = ext->sel_opt_id % real_id_cnt;
|
2019-03-17 04:32:37 +01:00
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
ext->sel_opt_id += (LV_ROLLER_INF_PAGES / 2) * real_id_cnt; /*Select the middle page*/
|
2019-03-17 04:32:37 +01:00
|
|
|
|
|
|
|
/*Move to the new id*/
|
2020-02-15 00:33:26 +01:00
|
|
|
const lv_font_t * font = lv_obj_get_style_text_font(roller, LV_ROLLER_PART_BG);
|
2020-02-12 08:54:03 +01:00
|
|
|
lv_style_int_t line_space = lv_obj_get_style_text_line_space(roller, LV_ROLLER_PART_BG);
|
2019-04-23 15:56:59 +02:00
|
|
|
lv_coord_t font_h = lv_font_get_line_height(font);
|
2019-04-11 19:59:55 +08:00
|
|
|
lv_coord_t h = lv_obj_get_height(roller);
|
2019-03-17 04:32:37 +01:00
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
lv_obj_t * label = get_label(roller);
|
|
|
|
|
|
|
|
lv_coord_t line_y1 = ext->sel_opt_id * (font_h + line_space) + label->coords.y1 - roller_scrl->coords.y1;
|
2019-04-04 07:15:40 +02:00
|
|
|
lv_coord_t new_y = -line_y1 + (h - font_h) / 2;
|
2019-03-17 04:32:37 +01:00
|
|
|
lv_obj_set_y(roller_scrl, new_y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-21 22:09:50 +01:00
|
|
|
static lv_obj_t * get_label(const lv_obj_t * roller)
|
|
|
|
{
|
|
|
|
return lv_obj_get_child(lv_page_get_scrl(roller), NULL);
|
|
|
|
}
|
|
|
|
|
2019-05-20 18:31:47 -07:00
|
|
|
#if LV_USE_ANIMATION
|
2019-04-22 08:45:07 +02:00
|
|
|
static void scroll_anim_ready_cb(lv_anim_t * a)
|
|
|
|
{
|
|
|
|
inf_normalize(a->var);
|
|
|
|
}
|
2019-05-20 18:31:47 -07:00
|
|
|
#endif
|
2019-04-22 08:45:07 +02:00
|
|
|
|
2017-09-03 16:01:51 +02:00
|
|
|
#endif
|