2018-02-24 11:26:06 +01:00
|
|
|
|
2017-04-11 10:51:31 +02:00
|
|
|
/**
|
|
|
|
* @file lv_slider.c
|
2018-06-19 09:49:58 +02:00
|
|
|
*
|
2017-04-11 10:51:31 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*********************
|
|
|
|
* INCLUDES
|
|
|
|
*********************/
|
2018-07-07 11:53:22 +02:00
|
|
|
#include "lv_slider.h"
|
2019-03-07 00:05:16 +01:00
|
|
|
#if LV_USE_SLIDER != 0
|
2017-04-11 10:51:31 +02:00
|
|
|
|
2019-09-24 16:30:38 +02:00
|
|
|
#include "../lv_core/lv_debug.h"
|
2017-11-30 11:35:33 +01:00
|
|
|
#include "../lv_core/lv_group.h"
|
2019-11-03 14:43:26 +01:00
|
|
|
#include "../lv_core/lv_indev.h"
|
2017-04-11 10:51:31 +02:00
|
|
|
#include "../lv_draw/lv_draw.h"
|
2017-11-16 15:32:33 +01:00
|
|
|
#include "../lv_themes/lv_theme.h"
|
2017-11-23 20:42:14 +01:00
|
|
|
#include "../lv_misc/lv_math.h"
|
2019-11-28 15:18:06 +01:00
|
|
|
#include "lv_img.h"
|
2017-04-11 10:51:31 +02:00
|
|
|
|
|
|
|
/*********************
|
|
|
|
* DEFINES
|
|
|
|
*********************/
|
2019-09-26 10:51:54 +02:00
|
|
|
#define LV_OBJX_NAME "lv_slider"
|
2017-11-18 00:18:19 +01:00
|
|
|
|
2017-04-11 10:51:31 +02:00
|
|
|
/**********************
|
|
|
|
* TYPEDEFS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC PROTOTYPES
|
|
|
|
**********************/
|
2019-09-06 19:53:39 +02:00
|
|
|
static lv_design_res_t lv_slider_design(lv_obj_t * slider, const lv_area_t * clip_area, lv_design_mode_t mode);
|
2017-11-08 11:37:01 +01:00
|
|
|
static lv_res_t lv_slider_signal(lv_obj_t * slider, lv_signal_t sign, void * param);
|
2017-04-11 10:51:31 +02:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC VARIABLES
|
|
|
|
**********************/
|
2019-02-26 09:25:46 +01:00
|
|
|
static lv_design_cb_t ancestor_design_f;
|
|
|
|
static lv_signal_cb_t ancestor_signal;
|
2017-04-11 10:51:31 +02:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* MACROS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* GLOBAL FUNCTIONS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a slider objects
|
|
|
|
* @param par pointer to an object, it will be the parent of the new slider
|
|
|
|
* @param copy pointer to a slider object, if not NULL then the new object will be copied from it
|
|
|
|
* @return pointer to the created slider
|
|
|
|
*/
|
2018-07-30 06:52:29 +02:00
|
|
|
lv_obj_t * lv_slider_create(lv_obj_t * par, const lv_obj_t * copy)
|
2017-04-11 10:51:31 +02:00
|
|
|
{
|
2018-10-05 17:22:49 +02:00
|
|
|
LV_LOG_TRACE("slider create started");
|
2018-07-25 17:57:08 +02:00
|
|
|
|
2017-04-11 10:51:31 +02:00
|
|
|
/*Create the ancestor slider*/
|
|
|
|
lv_obj_t * new_slider = lv_bar_create(par, copy);
|
2019-09-24 23:14:17 +02:00
|
|
|
LV_ASSERT_MEM(new_slider);
|
2018-07-25 13:33:53 +02:00
|
|
|
if(new_slider == NULL) return NULL;
|
2018-06-19 09:49:58 +02:00
|
|
|
|
2019-04-10 06:40:49 +02:00
|
|
|
if(ancestor_design_f == NULL) ancestor_design_f = lv_obj_get_design_cb(new_slider);
|
|
|
|
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_slider);
|
2017-11-08 11:07:29 +01:00
|
|
|
|
2017-04-11 10:51:31 +02:00
|
|
|
/*Allocate the slider type specific extended data*/
|
2017-10-20 10:17:02 +02:00
|
|
|
lv_slider_ext_t * ext = lv_obj_allocate_ext_attr(new_slider, sizeof(lv_slider_ext_t));
|
2019-09-24 23:14:17 +02:00
|
|
|
LV_ASSERT_MEM(ext);
|
2018-07-25 13:33:53 +02:00
|
|
|
if(ext == NULL) return NULL;
|
2017-04-11 10:51:31 +02:00
|
|
|
|
|
|
|
/*Initialize the allocated 'ext' */
|
2017-10-30 17:31:48 +01:00
|
|
|
ext->style_knob = &lv_style_pretty;
|
2019-11-28 15:18:06 +01:00
|
|
|
ext->knob_img = NULL;
|
2017-04-11 10:51:31 +02:00
|
|
|
|
|
|
|
/*The signal and design functions are not copied so set them here*/
|
2019-02-26 09:25:46 +01:00
|
|
|
lv_obj_set_signal_cb(new_slider, lv_slider_signal);
|
|
|
|
lv_obj_set_design_cb(new_slider, lv_slider_design);
|
2017-04-11 10:51:31 +02:00
|
|
|
|
|
|
|
/*Init the new slider slider*/
|
|
|
|
if(copy == NULL) {
|
|
|
|
lv_obj_set_click(new_slider, true);
|
2018-02-24 11:26:06 +01:00
|
|
|
lv_obj_set_protect(new_slider, LV_PROTECT_PRESS_LOST);
|
2017-11-16 15:32:33 +01:00
|
|
|
|
|
|
|
/*Set the default styles*/
|
2018-06-19 09:49:58 +02:00
|
|
|
lv_theme_t * th = lv_theme_get_current();
|
2017-11-16 15:32:33 +01:00
|
|
|
if(th) {
|
2019-02-11 09:35:06 +01:00
|
|
|
lv_slider_set_style(new_slider, LV_SLIDER_STYLE_BG, th->style.slider.bg);
|
|
|
|
lv_slider_set_style(new_slider, LV_SLIDER_STYLE_INDIC, th->style.slider.indic);
|
|
|
|
lv_slider_set_style(new_slider, LV_SLIDER_STYLE_KNOB, th->style.slider.knob);
|
2017-11-16 15:32:33 +01:00
|
|
|
} else {
|
|
|
|
lv_slider_set_style(new_slider, LV_SLIDER_STYLE_KNOB, ext->style_knob);
|
|
|
|
}
|
2017-04-11 10:51:31 +02:00
|
|
|
}
|
|
|
|
/*Copy an existing slider*/
|
|
|
|
else {
|
2018-06-19 09:49:58 +02:00
|
|
|
lv_slider_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
|
2019-04-04 07:15:40 +02:00
|
|
|
ext->style_knob = copy_ext->style_knob;
|
2017-04-11 10:51:31 +02:00
|
|
|
/*Refresh the style with new signal function*/
|
2017-10-20 10:17:02 +02:00
|
|
|
lv_obj_refresh_style(new_slider);
|
2017-04-11 10:51:31 +02:00
|
|
|
}
|
2018-06-19 09:49:58 +02:00
|
|
|
|
2018-10-05 17:22:49 +02:00
|
|
|
LV_LOG_INFO("slider created");
|
2018-07-25 20:39:24 +02:00
|
|
|
|
2017-04-11 10:51:31 +02:00
|
|
|
return new_slider;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*=====================
|
|
|
|
* Setter functions
|
|
|
|
*====================*/
|
|
|
|
|
2019-11-28 15:18:06 +01:00
|
|
|
/**
|
|
|
|
* Set an image to display on the knob of the slider
|
|
|
|
* @param slider pointer to a slider object
|
|
|
|
* @param img_src pointer to an `lv_img_dsc_t` variable or a path to an image (not an `lv_img` object)
|
|
|
|
*/
|
|
|
|
void lv_slider_set_knob_img(lv_obj_t * slider, const void * img_src)
|
|
|
|
{
|
|
|
|
LV_ASSERT_OBJ(slider, LV_OBJX_NAME);
|
|
|
|
|
|
|
|
lv_slider_ext_t * ext = lv_obj_get_ext_attr(slider);
|
|
|
|
|
|
|
|
ext->knob_img = img_src;
|
|
|
|
lv_obj_refresh_ext_draw_pad(slider);
|
|
|
|
lv_obj_invalidate(slider);
|
|
|
|
}
|
|
|
|
|
2017-04-13 10:20:35 +02:00
|
|
|
/**
|
2017-11-15 15:50:33 +01:00
|
|
|
* Set a style of a slider
|
2017-11-05 00:48:57 +01:00
|
|
|
* @param slider pointer to a slider object
|
2017-11-15 15:50:33 +01:00
|
|
|
* @param type which style should be set
|
|
|
|
* @param style pointer to a style
|
2017-04-13 10:20:35 +02:00
|
|
|
*/
|
2019-04-11 19:59:55 +08:00
|
|
|
void lv_slider_set_style(lv_obj_t * slider, lv_slider_style_t type, const lv_style_t * style)
|
2017-04-13 10:20:35 +02:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(slider, LV_OBJX_NAME);
|
|
|
|
|
2017-11-15 15:50:33 +01:00
|
|
|
lv_slider_ext_t * ext = lv_obj_get_ext_attr(slider);
|
2017-04-21 09:15:39 +02:00
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
switch(type) {
|
2019-04-04 07:15:40 +02:00
|
|
|
case LV_SLIDER_STYLE_BG: lv_bar_set_style(slider, LV_BAR_STYLE_BG, style); break;
|
|
|
|
case LV_SLIDER_STYLE_INDIC: lv_bar_set_style(slider, LV_BAR_STYLE_INDIC, style); break;
|
2017-11-15 15:50:33 +01:00
|
|
|
case LV_SLIDER_STYLE_KNOB:
|
|
|
|
ext->style_knob = style;
|
2019-04-11 06:26:41 +02:00
|
|
|
lv_obj_refresh_ext_draw_pad(slider);
|
2017-11-15 15:50:33 +01:00
|
|
|
break;
|
|
|
|
}
|
2017-04-13 10:20:35 +02:00
|
|
|
}
|
|
|
|
|
2017-04-11 10:51:31 +02:00
|
|
|
/*=====================
|
|
|
|
* Getter functions
|
|
|
|
*====================*/
|
|
|
|
|
2017-12-11 11:42:12 +01:00
|
|
|
/**
|
|
|
|
* Get the value of a slider
|
|
|
|
* @param slider pointer to a slider object
|
|
|
|
* @return the value of the slider
|
|
|
|
*/
|
2018-07-30 06:52:29 +02:00
|
|
|
int16_t lv_slider_get_value(const lv_obj_t * slider)
|
2017-12-11 11:42:12 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(slider, LV_OBJX_NAME);
|
2019-09-16 10:58:28 +02:00
|
|
|
return lv_bar_get_value(slider);
|
2017-12-11 11:42:12 +01:00
|
|
|
}
|
|
|
|
|
2017-04-11 10:51:31 +02:00
|
|
|
/**
|
2017-12-11 11:42:12 +01:00
|
|
|
* Give the slider is being dragged or not
|
2017-11-05 00:48:57 +01:00
|
|
|
* @param slider pointer to a slider object
|
2017-12-11 11:42:12 +01:00
|
|
|
* @return true: drag in progress false: not dragged
|
2017-04-11 10:51:31 +02:00
|
|
|
*/
|
2018-07-30 06:52:29 +02:00
|
|
|
bool lv_slider_is_dragged(const lv_obj_t * slider)
|
2017-04-11 10:51:31 +02:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(slider, LV_OBJX_NAME);
|
|
|
|
|
2017-10-20 10:17:02 +02:00
|
|
|
lv_slider_ext_t * ext = lv_obj_get_ext_attr(slider);
|
2019-09-16 10:58:28 +02:00
|
|
|
return ext->dragging ? true : false;
|
2017-08-18 21:11:22 +02:00
|
|
|
}
|
|
|
|
|
2019-11-28 15:18:06 +01:00
|
|
|
/**
|
|
|
|
* Get an image to display on the knob of the slider
|
|
|
|
* @param slider pointer to a slider object
|
|
|
|
* @return the image source: pointer to an `lv_img_dsc_t` variable or a path to an image (not an `lv_img` object)
|
|
|
|
*/
|
|
|
|
const void * lv_slider_get_knob_img(lv_obj_t * slider, const void * img_src)
|
|
|
|
{
|
|
|
|
LV_ASSERT_OBJ(slider, LV_OBJX_NAME);
|
|
|
|
|
|
|
|
lv_slider_ext_t * ext = lv_obj_get_ext_attr(slider);
|
|
|
|
|
|
|
|
return ext->knob_img;
|
|
|
|
}
|
|
|
|
|
2017-11-15 15:50:33 +01:00
|
|
|
/**
|
|
|
|
* Get a style of a slider
|
|
|
|
* @param slider pointer to a slider object
|
|
|
|
* @param type which style should be get
|
|
|
|
* @return style pointer to a style
|
|
|
|
*/
|
2019-04-11 19:59:55 +08:00
|
|
|
const lv_style_t * lv_slider_get_style(const lv_obj_t * slider, lv_slider_style_t type)
|
2017-11-15 15:50:33 +01:00
|
|
|
{
|
2019-09-26 12:54:40 +02:00
|
|
|
LV_ASSERT_OBJ(slider, LV_OBJX_NAME);
|
|
|
|
|
2019-06-06 06:05:40 +02:00
|
|
|
const lv_style_t * style = NULL;
|
|
|
|
lv_slider_ext_t * ext = lv_obj_get_ext_attr(slider);
|
2017-11-15 15:50:33 +01:00
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
switch(type) {
|
2019-04-04 07:15:40 +02:00
|
|
|
case LV_SLIDER_STYLE_BG: style = lv_bar_get_style(slider, LV_BAR_STYLE_BG); break;
|
|
|
|
case LV_SLIDER_STYLE_INDIC: style = lv_bar_get_style(slider, LV_BAR_STYLE_INDIC); break;
|
|
|
|
case LV_SLIDER_STYLE_KNOB: style = ext->style_knob; break;
|
|
|
|
default: style = NULL; break;
|
2017-11-15 15:50:33 +01:00
|
|
|
}
|
|
|
|
|
2018-11-05 22:21:20 -06:00
|
|
|
return style;
|
2017-11-15 15:50:33 +01:00
|
|
|
}
|
|
|
|
|
2017-04-11 10:51:31 +02:00
|
|
|
/**********************
|
|
|
|
* STATIC FUNCTIONS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the drawing related tasks of the sliders
|
|
|
|
* @param slider 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-04-11 10:51:31 +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-04-11 10:51:31 +02:00
|
|
|
*/
|
2019-09-06 19:53:39 +02:00
|
|
|
static lv_design_res_t lv_slider_design(lv_obj_t * slider, const lv_area_t * clip_area, lv_design_mode_t mode)
|
2017-04-11 10:51:31 +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-04-11 10:51:31 +02:00
|
|
|
}
|
|
|
|
/*Draw the object*/
|
|
|
|
else if(mode == LV_DESIGN_DRAW_MAIN) {
|
2017-08-18 21:11:22 +02:00
|
|
|
|
2019-09-16 10:58:28 +02:00
|
|
|
/*The ancestor design function will draw the background and the indicator.
|
|
|
|
* It also sets ext->bar.indic_area*/
|
|
|
|
ancestor_design_f(slider, clip_area, mode);
|
2017-04-21 09:15:39 +02:00
|
|
|
|
2019-09-16 10:58:28 +02:00
|
|
|
lv_slider_ext_t * ext = lv_obj_get_ext_attr(slider);
|
|
|
|
const lv_style_t * style_knob = lv_slider_get_style(slider, LV_SLIDER_STYLE_KNOB);
|
2018-09-23 23:27:28 +02:00
|
|
|
lv_opa_t opa_scale = lv_obj_get_opa_scale(slider);
|
|
|
|
|
2019-09-16 10:58:28 +02:00
|
|
|
lv_coord_t objw = lv_obj_get_width(slider);
|
|
|
|
lv_coord_t objh = lv_obj_get_height(slider);
|
|
|
|
bool hor = objw >= objh ? true : false;
|
|
|
|
lv_coord_t knob_size = hor ? objh : objw;
|
2019-09-12 15:25:42 +02:00
|
|
|
bool sym = false;
|
|
|
|
if(ext->bar.sym && ext->bar.min_value < 0 && ext->bar.max_value > 0) sym = true;
|
2017-04-11 10:51:31 +02:00
|
|
|
|
2017-11-23 21:28:36 +01:00
|
|
|
lv_area_t knob_area;
|
2017-04-11 10:51:31 +02:00
|
|
|
|
2019-09-16 10:58:28 +02:00
|
|
|
/*Horizontal*/
|
|
|
|
if(hor) {
|
|
|
|
if(!sym) {
|
|
|
|
knob_area.x1 = ext->bar.indic_area.x2;
|
2017-08-18 21:11:22 +02:00
|
|
|
} else {
|
2019-09-16 10:58:28 +02:00
|
|
|
if(ext->bar.cur_value >= 0) {
|
|
|
|
knob_area.x1 = ext->bar.indic_area.x2;
|
|
|
|
} else {
|
|
|
|
knob_area.x1 = ext->bar.indic_area.x1;
|
2019-03-01 23:50:30 +01:00
|
|
|
}
|
2017-08-18 21:11:22 +02:00
|
|
|
}
|
2019-09-16 10:58:28 +02:00
|
|
|
knob_area.x1 -= (knob_size >> 1);
|
|
|
|
knob_area.x2 = knob_area.x1 + knob_size;
|
2017-10-20 10:17:02 +02:00
|
|
|
knob_area.y1 = slider->coords.y1;
|
|
|
|
knob_area.y2 = slider->coords.y2;
|
2017-04-11 10:51:31 +02:00
|
|
|
}
|
2019-09-16 10:58:28 +02:00
|
|
|
/*Vertical*/
|
|
|
|
else {
|
|
|
|
if(!sym) {
|
|
|
|
knob_area.y1 = ext->bar.indic_area.y1;
|
|
|
|
} else {
|
|
|
|
if(ext->bar.cur_value >= 0) {
|
|
|
|
knob_area.y1 = ext->bar.indic_area.y1;
|
|
|
|
} else {
|
|
|
|
knob_area.y1 = ext->bar.indic_area.y2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
knob_area.y1 -= (knob_size >> 1);
|
|
|
|
knob_area.y2 = knob_area.y1 + knob_size;
|
|
|
|
knob_area.x1 = slider->coords.x1;
|
|
|
|
knob_area.x2 = slider->coords.x2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*Apply the paddings on the knob area*/
|
|
|
|
knob_area.x1 -= style_knob->body.padding.left;
|
|
|
|
knob_area.x2 += style_knob->body.padding.right;
|
|
|
|
knob_area.y1 -= style_knob->body.padding.top;
|
|
|
|
knob_area.y2 += style_knob->body.padding.bottom;
|
|
|
|
|
2019-09-06 19:53:39 +02:00
|
|
|
lv_draw_rect(&knob_area, clip_area, style_knob, opa_scale);
|
2019-11-28 15:18:06 +01:00
|
|
|
|
|
|
|
if(ext->knob_img) {
|
|
|
|
lv_res_t res;
|
|
|
|
lv_img_header_t info;
|
|
|
|
res = lv_img_decoder_get_info(ext->knob_img, &info);
|
|
|
|
if(res == LV_RES_OK) {
|
|
|
|
lv_coord_t x_ofs = knob_area.x1 + (lv_area_get_width(&knob_area) - info.w) / 2;
|
|
|
|
lv_coord_t y_ofs = knob_area.y1 + (lv_area_get_height(&knob_area) - info.h) / 2;
|
|
|
|
lv_area_t a;
|
|
|
|
a.x1 = x_ofs;
|
|
|
|
a.y1 = y_ofs;
|
|
|
|
a.x2 = info.w - 1 + x_ofs;
|
|
|
|
a.y2 = info.h - 1 + y_ofs;
|
|
|
|
|
|
|
|
lv_draw_img(&a, clip_area, ext->knob_img, style_knob, 0, LV_IMG_ZOOM_NONE, false, opa_scale);
|
|
|
|
} else {
|
|
|
|
LV_LOG_WARN("lv_slider_design: can't get knob image info")
|
|
|
|
}
|
|
|
|
}
|
2017-04-11 10:51:31 +02:00
|
|
|
}
|
|
|
|
/*Post draw when the children are drawn*/
|
|
|
|
else if(mode == LV_DESIGN_DRAW_POST) {
|
2019-11-16 11:22:23 +01:00
|
|
|
return ancestor_design_f(slider, clip_area, mode);
|
2017-04-11 10:51:31 +02:00
|
|
|
}
|
|
|
|
|
2019-09-06 19:53:39 +02:00
|
|
|
return LV_DESIGN_RES_OK;
|
2017-04-11 10:51:31 +02:00
|
|
|
}
|
|
|
|
|
2017-11-08 11:37:01 +01:00
|
|
|
/**
|
|
|
|
* Signal function of the slider
|
|
|
|
* @param slider pointer to a slider 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_slider_signal(lv_obj_t * slider, lv_signal_t sign, void * param)
|
|
|
|
{
|
|
|
|
lv_res_t res;
|
|
|
|
|
|
|
|
/* Include the ancient signal function */
|
|
|
|
res = ancestor_signal(slider, sign, param);
|
|
|
|
if(res != LV_RES_OK) return res;
|
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-08 11:37:01 +01:00
|
|
|
|
|
|
|
lv_slider_ext_t * ext = lv_obj_get_ext_attr(slider);
|
2017-11-23 21:28:36 +01:00
|
|
|
lv_point_t p;
|
2017-11-08 11:37:01 +01:00
|
|
|
|
|
|
|
if(sign == LV_SIGNAL_PRESSED) {
|
2019-09-16 10:58:28 +02:00
|
|
|
ext->dragging = true;
|
2018-06-19 09:49:58 +02:00
|
|
|
} else if(sign == LV_SIGNAL_PRESSING) {
|
2017-11-08 11:37:01 +01:00
|
|
|
lv_indev_get_point(param, &p);
|
2019-09-16 10:58:28 +02:00
|
|
|
|
|
|
|
lv_coord_t w = lv_obj_get_width(slider);
|
|
|
|
lv_coord_t h = lv_obj_get_height(slider);
|
|
|
|
const lv_style_t * indic_style = lv_slider_get_style(slider, LV_SLIDER_STYLE_INDIC);
|
|
|
|
int32_t range = ext->bar.max_value - ext->bar.min_value;
|
|
|
|
int16_t new_value = 0;
|
|
|
|
if(w >= h) {
|
|
|
|
lv_coord_t indic_w = w - indic_style->body.padding.left - indic_style->body.padding.right;
|
|
|
|
new_value = p.x - (slider->coords.x1 + indic_style->body.padding.left); /*Make the point relative to the indicator*/
|
|
|
|
new_value = (new_value * range) / indic_w;
|
|
|
|
new_value += ext->bar.min_value;
|
2017-11-08 11:37:01 +01:00
|
|
|
} else {
|
2019-09-16 10:58:28 +02:00
|
|
|
lv_coord_t indic_h = h - indic_style->body.padding.bottom - indic_style->body.padding.top;
|
|
|
|
new_value = p.y - (slider->coords.y2 + indic_style->body.padding.bottom); /*Make the point relative to the indicator*/
|
|
|
|
new_value = (-new_value * range) / indic_h;
|
|
|
|
new_value += ext->bar.min_value;
|
|
|
|
|
2017-11-08 11:37:01 +01:00
|
|
|
}
|
2017-11-09 08:37:43 +01:00
|
|
|
|
2019-09-16 10:58:28 +02:00
|
|
|
if(new_value < ext->bar.min_value) new_value = ext->bar.min_value;
|
|
|
|
else if(new_value > ext->bar.max_value) new_value = ext->bar.max_value;
|
2017-11-08 11:37:01 +01:00
|
|
|
|
2019-09-16 10:58:28 +02:00
|
|
|
if(new_value != ext->bar.cur_value) {
|
|
|
|
ext->bar.cur_value = new_value;
|
2017-12-11 11:42:12 +01:00
|
|
|
lv_obj_invalidate(slider);
|
2019-03-19 06:30:05 +01:00
|
|
|
res = lv_event_send(slider, LV_EVENT_VALUE_CHANGED, NULL);
|
2019-03-07 00:42:08 +01:00
|
|
|
if(res != LV_RES_OK) return res;
|
2017-12-11 11:42:12 +01:00
|
|
|
}
|
2018-06-19 09:49:58 +02:00
|
|
|
} else if(sign == LV_SIGNAL_RELEASED || sign == LV_SIGNAL_PRESS_LOST) {
|
2019-09-16 10:58:28 +02:00
|
|
|
ext->dragging = false;
|
2019-03-12 19:20:23 +01:00
|
|
|
|
|
|
|
#if LV_USE_GROUP
|
|
|
|
/*Leave edit mode if released. (No need to wait for LONG_PRESS) */
|
2019-04-04 07:15:40 +02:00
|
|
|
lv_group_t * g = lv_obj_get_group(slider);
|
|
|
|
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());
|
2019-03-12 19:20:23 +01:00
|
|
|
if(indev_type == LV_INDEV_TYPE_ENCODER) {
|
|
|
|
if(editing) lv_group_set_editing(g, false);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-06-19 09:49:58 +02:00
|
|
|
} else if(sign == LV_SIGNAL_CORD_CHG) {
|
2017-11-08 11:37:01 +01:00
|
|
|
/* The knob size depends on slider size.
|
|
|
|
* During the drawing method the ext. size is used by the knob so refresh the ext. size.*/
|
2017-11-28 16:15:13 +01:00
|
|
|
if(lv_obj_get_width(slider) != lv_area_get_width(param) ||
|
2019-04-04 07:15:40 +02:00
|
|
|
lv_obj_get_height(slider) != lv_area_get_height(param)) {
|
2019-04-11 06:26:41 +02:00
|
|
|
slider->signal_cb(slider, LV_SIGNAL_REFR_EXT_DRAW_PAD, NULL);
|
2017-11-08 11:37:01 +01:00
|
|
|
}
|
2019-04-11 06:26:41 +02:00
|
|
|
} else if(sign == LV_SIGNAL_REFR_EXT_DRAW_PAD) {
|
2019-09-16 10:58:28 +02:00
|
|
|
const lv_style_t * bg_style = lv_slider_get_style(slider, LV_SLIDER_STYLE_BG);
|
|
|
|
const lv_style_t * indic_style = lv_slider_get_style(slider, LV_SLIDER_STYLE_INDIC);
|
2019-04-11 19:59:55 +08:00
|
|
|
const lv_style_t * knob_style = lv_slider_get_style(slider, LV_SLIDER_STYLE_KNOB);
|
2019-04-14 16:01:48 +02:00
|
|
|
|
2019-09-16 10:58:28 +02:00
|
|
|
/* The smaller size is the knob diameter*/
|
|
|
|
lv_coord_t knob_size = LV_MATH_MIN(lv_obj_get_width(slider), lv_obj_get_height(slider)) >> 1;
|
|
|
|
knob_size += LV_MATH_MAX(
|
|
|
|
LV_MATH_MAX(knob_style->body.padding.left, knob_style->body.padding.right),
|
|
|
|
LV_MATH_MAX(knob_style->body.padding.bottom,knob_style->body.padding.top));
|
|
|
|
|
|
|
|
knob_size += knob_style->body.shadow.width + knob_style->body.shadow.spread;
|
|
|
|
knob_size += LV_MATH_MAX(LV_MATH_ABS(knob_style->body.shadow.offset.x), LV_MATH_ABS(knob_style->body.shadow.offset.y));
|
|
|
|
|
2019-11-28 15:18:06 +01:00
|
|
|
if(ext->knob_img) {
|
|
|
|
lv_img_header_t info;
|
|
|
|
lv_res_t res;
|
|
|
|
res = lv_img_decoder_get_info(ext->knob_img, &info);
|
|
|
|
if(res == LV_RES_OK) {
|
|
|
|
knob_size = LV_MATH_MAX(knob_size, info.w);
|
|
|
|
knob_size = LV_MATH_MAX(knob_size, info.h);
|
|
|
|
} else {
|
|
|
|
LV_LOG_WARN("slider signal (LV_SIGNAL_REFR_EXT_DRAW_PAD): can't get knob image info")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-16 10:58:28 +02:00
|
|
|
lv_coord_t bg_size = bg_style->body.shadow.width + bg_style->body.shadow.spread;
|
|
|
|
bg_size += LV_MATH_MAX(LV_MATH_ABS(bg_style->body.shadow.offset.x), LV_MATH_ABS(bg_style->body.shadow.offset.y));
|
|
|
|
|
|
|
|
lv_coord_t indic_size = indic_style->body.shadow.width + indic_style->body.shadow.spread;
|
|
|
|
indic_size += LV_MATH_MAX(LV_MATH_ABS(indic_style->body.shadow.offset.x), LV_MATH_ABS(indic_style->body.shadow.offset.y));
|
|
|
|
|
|
|
|
slider->ext_draw_pad = LV_MATH_MAX(slider->ext_draw_pad, knob_size);
|
|
|
|
slider->ext_draw_pad = LV_MATH_MAX(slider->ext_draw_pad, indic_size);
|
|
|
|
slider->ext_draw_pad = LV_MATH_MAX(slider->ext_draw_pad, bg_size);
|
|
|
|
|
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);
|
2018-12-14 10:33:18 +01:00
|
|
|
|
2019-04-08 14:36:20 +02:00
|
|
|
if(c == LV_KEY_RIGHT || c == LV_KEY_UP) {
|
2019-03-01 23:50:30 +01:00
|
|
|
lv_slider_set_value(slider, lv_slider_get_value(slider) + 1, true);
|
2019-03-19 06:30:05 +01:00
|
|
|
res = lv_event_send(slider, LV_EVENT_VALUE_CHANGED, NULL);
|
2019-03-07 00:42:08 +01:00
|
|
|
if(res != LV_RES_OK) return res;
|
2019-04-08 14:36:20 +02:00
|
|
|
} else if(c == LV_KEY_LEFT || c == LV_KEY_DOWN) {
|
2019-03-01 23:50:30 +01:00
|
|
|
lv_slider_set_value(slider, lv_slider_get_value(slider) - 1, true);
|
2019-03-19 06:30:05 +01:00
|
|
|
res = lv_event_send(slider, LV_EVENT_VALUE_CHANGED, NULL);
|
2019-03-07 00:42:08 +01:00
|
|
|
if(res != LV_RES_OK) return res;
|
2017-11-08 11:37:01 +01:00
|
|
|
}
|
2018-07-25 21:52:50 +02:00
|
|
|
} else if(sign == LV_SIGNAL_GET_EDITABLE) {
|
2018-10-05 17:22:49 +02:00
|
|
|
bool * editable = (bool *)param;
|
2019-04-04 07:15:40 +02:00
|
|
|
*editable = true;
|
2018-02-28 15:37:41 +01:00
|
|
|
}
|
2017-11-08 11:37:01 +01:00
|
|
|
|
2017-11-15 15:50:33 +01:00
|
|
|
return res;
|
2017-11-08 11:37:01 +01:00
|
|
|
}
|
2017-04-11 10:51:31 +02:00
|
|
|
#endif
|