1
2 /**
3 * @file lv_slider.c
4 *
5 */
6
7 /*********************
8 * INCLUDES
9 *********************/
10 #include "lv_slider.h"
11 #if LV_USE_SLIDER != 0
12
13 #include "../lv_misc/lv_debug.h"
14 #include "../lv_core/lv_group.h"
15 #include "../lv_core/lv_indev.h"
16 #include "../lv_draw/lv_draw.h"
17 #include "../lv_themes/lv_theme.h"
18 #include "../lv_misc/lv_math.h"
19 #include "lv_img.h"
20
21 /*********************
22 * DEFINES
23 *********************/
24 #define LV_OBJX_NAME "lv_slider"
25
26 #define LV_SLIDER_KNOB_COORD(hor, is_rtl, area) (hor ? (is_rtl ? area.x1 : area.x2) : (is_rtl ? area.y1 : area.y2))
27
28 /**********************
29 * TYPEDEFS
30 **********************/
31
32 /**********************
33 * STATIC PROTOTYPES
34 **********************/
35 static lv_design_res_t lv_slider_design(lv_obj_t * slider, const lv_area_t * clip_area, lv_design_mode_t mode);
36 static lv_res_t lv_slider_signal(lv_obj_t * slider, lv_signal_t sign, void * param);
37 static lv_style_list_t * lv_slider_get_style(lv_obj_t * slider, uint8_t part);
38 static void lv_slider_position_knob(lv_obj_t * slider, lv_area_t * knob_area, lv_coord_t knob_size, bool hor);
39 static void lv_slider_draw_knob(lv_obj_t * slider, const lv_area_t * knob_area, const lv_area_t * clip_area);
40
41 /**********************
42 * STATIC VARIABLES
43 **********************/
44 static lv_design_cb_t ancestor_design_f;
45 static lv_signal_cb_t ancestor_signal;
46
47 /**********************
48 * MACROS
49 **********************/
50
51 /**********************
52 * GLOBAL FUNCTIONS
53 **********************/
54
55 /**
56 * Create a slider objects
57 * @param par pointer to an object, it will be the parent of the new slider
58 * @param copy pointer to a slider object, if not NULL then the new object will be copied from it
59 * @return pointer to the created slider
60 */
lv_slider_create(lv_obj_t * par,const lv_obj_t * copy)61 lv_obj_t * lv_slider_create(lv_obj_t * par, const lv_obj_t * copy)
62 {
63 LV_LOG_TRACE("slider create started");
64
65 /*Create the ancestor slider*/
66 lv_obj_t * slider = lv_bar_create(par, copy);
67 LV_ASSERT_MEM(slider);
68 if(slider == NULL) return NULL;
69
70 if(ancestor_design_f == NULL) ancestor_design_f = lv_obj_get_design_cb(slider);
71 if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(slider);
72
73 /*Allocate the slider type specific extended data*/
74 lv_slider_ext_t * ext = lv_obj_allocate_ext_attr(slider, sizeof(lv_slider_ext_t));
75 LV_ASSERT_MEM(ext);
76 if(ext == NULL) {
77 lv_obj_del(slider);
78 return NULL;
79 }
80
81 /*Initialize the allocated 'ext' */
82 ext->value_to_set = NULL;
83 ext->dragging = 0;
84 lv_style_list_init(&ext->style_knob);
85
86 /*The signal and design functions are not copied so set them here*/
87 lv_obj_set_signal_cb(slider, lv_slider_signal);
88 lv_obj_set_design_cb(slider, lv_slider_design);
89
90 /*Init the new slider slider*/
91 if(copy == NULL) {
92 lv_obj_set_click(slider, true);
93 lv_obj_add_protect(slider, LV_PROTECT_PRESS_LOST);
94 lv_obj_set_ext_click_area(slider, 0, 0, LV_DPI / 10, LV_DPI / 10);
95
96 lv_theme_apply(slider, LV_THEME_SLIDER);
97 lv_obj_set_height(slider, LV_DPI / 15);
98 }
99 /*Copy an existing slider*/
100 else {
101 lv_slider_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
102 lv_style_list_copy(&ext->style_knob, ©_ext->style_knob);
103 lv_area_copy(&ext->left_knob_area, ©_ext->left_knob_area);
104 lv_area_copy(&ext->right_knob_area, ©_ext->right_knob_area);
105
106 lv_obj_refresh_style(slider, LV_OBJ_PART_ALL, LV_STYLE_PROP_ALL);
107 }
108
109 LV_LOG_INFO("slider created");
110
111 return slider;
112 }
113
114 /*=====================
115 * Setter functions
116 *====================*/
117
118 /*=====================
119 * Getter functions
120 *====================*/
121
122 /**
123 * Get the value of a slider
124 * @param slider pointer to a slider object
125 * @return the value of the slider
126 */
lv_slider_get_value(const lv_obj_t * slider)127 int16_t lv_slider_get_value(const lv_obj_t * slider)
128 {
129 LV_ASSERT_OBJ(slider, LV_OBJX_NAME);
130 return lv_bar_get_value(slider);
131 }
132
133 /**
134 * Give the slider is being dragged or not
135 * @param slider pointer to a slider object
136 * @return true: drag in progress false: not dragged
137 */
lv_slider_is_dragged(const lv_obj_t * slider)138 bool lv_slider_is_dragged(const lv_obj_t * slider)
139 {
140 LV_ASSERT_OBJ(slider, LV_OBJX_NAME);
141
142 lv_slider_ext_t * ext = lv_obj_get_ext_attr(slider);
143 return ext->dragging ? true : false;
144 }
145
146 /**********************
147 * STATIC FUNCTIONS
148 **********************/
149
150 /**
151 * Handle the drawing related tasks of the sliders
152 * @param slider pointer to an object
153 * @param clip_area the object will be drawn only in this area
154 * @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area
155 * (return 'true' if yes)
156 * LV_DESIGN_DRAW: draw the object (always return 'true')
157 * LV_DESIGN_DRAW_POST: drawing after every children are drawn
158 * @param return an element of `lv_design_res_t`
159 */
lv_slider_design(lv_obj_t * slider,const lv_area_t * clip_area,lv_design_mode_t mode)160 static lv_design_res_t lv_slider_design(lv_obj_t * slider, const lv_area_t * clip_area, lv_design_mode_t mode)
161 {
162 /*Return false if the object is not covers the mask_p area*/
163 if(mode == LV_DESIGN_COVER_CHK) {
164 return LV_DESIGN_RES_NOT_COVER;
165 }
166 /*Draw the object*/
167 else if(mode == LV_DESIGN_DRAW_MAIN) {
168 /* The ancestor design function will draw the background and the indicator.
169 * It also sets ext->bar.indic_area*/
170 ancestor_design_f(slider, clip_area, mode);
171
172 lv_slider_ext_t * ext = lv_obj_get_ext_attr(slider);
173 lv_bidi_dir_t base_dir = lv_obj_get_base_dir(slider);
174
175 lv_coord_t objw = lv_obj_get_width(slider);
176 lv_coord_t objh = lv_obj_get_height(slider);
177 bool hor = objw >= objh ? true : false;
178 lv_coord_t knob_size = hor ? objh : objw;
179 bool sym = false;
180 if(ext->bar.type == LV_BAR_TYPE_SYMMETRICAL && ext->bar.min_value < 0 && ext->bar.max_value > 0) sym = true;
181
182 lv_area_t knob_area;
183
184 /*Horizontal*/
185 if(hor) {
186 if(!sym) {
187 knob_area.x1 = LV_SLIDER_KNOB_COORD(hor, base_dir == LV_BIDI_DIR_RTL, ext->bar.indic_area);
188 }
189 else {
190 if(ext->bar.cur_value >= 0) {
191 knob_area.x1 = LV_SLIDER_KNOB_COORD(hor, base_dir == LV_BIDI_DIR_RTL, ext->bar.indic_area);
192 }
193 else {
194 knob_area.x1 = LV_SLIDER_KNOB_COORD(hor, base_dir != LV_BIDI_DIR_RTL, ext->bar.indic_area);
195 }
196 }
197 }
198 /*Vertical*/
199 else {
200 if(!sym) {
201 knob_area.y1 = ext->bar.indic_area.y1;
202 }
203 else {
204 if(ext->bar.cur_value >= 0) {
205 knob_area.y1 = ext->bar.indic_area.y1;
206 }
207 else {
208 knob_area.y1 = ext->bar.indic_area.y2;
209 }
210 }
211 }
212 lv_slider_position_knob(slider, &knob_area, knob_size, hor);
213
214 lv_area_copy(&ext->right_knob_area, &knob_area);
215 lv_slider_draw_knob(slider, &knob_area, clip_area);
216
217 if(lv_slider_get_type(slider) == LV_SLIDER_TYPE_RANGE) {
218 /* Draw a second knob for the start_value side */
219 if(hor) {
220 knob_area.x1 = LV_SLIDER_KNOB_COORD(hor, base_dir != LV_BIDI_DIR_RTL, ext->bar.indic_area);
221 }
222 else {
223 knob_area.y1 = ext->bar.indic_area.y2;
224 }
225 lv_slider_position_knob(slider, &knob_area, knob_size, hor);
226
227 lv_area_copy(&ext->left_knob_area, &knob_area);
228 lv_slider_draw_knob(slider, &knob_area, clip_area);
229 }
230 }
231 /*Post draw when the children are drawn*/
232 else if(mode == LV_DESIGN_DRAW_POST) {
233 return ancestor_design_f(slider, clip_area, mode);
234 }
235
236 return LV_DESIGN_RES_OK;
237 }
238
239 /**
240 * Signal function of the slider
241 * @param slider pointer to a slider object
242 * @param sign a signal type from lv_signal_t enum
243 * @param param pointer to a signal specific variable
244 * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
245 */
lv_slider_signal(lv_obj_t * slider,lv_signal_t sign,void * param)246 static lv_res_t lv_slider_signal(lv_obj_t * slider, lv_signal_t sign, void * param)
247 {
248 lv_res_t res;
249
250 if(sign == LV_SIGNAL_GET_STYLE) {
251 lv_get_style_info_t * info = param;
252 info->result = lv_slider_get_style(slider, info->part);
253 if(info->result != NULL) return LV_RES_OK;
254 else return ancestor_signal(slider, sign, param);
255 }
256
257 /* Include the ancient signal function */
258 res = ancestor_signal(slider, sign, param);
259 if(res != LV_RES_OK) return res;
260 if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, LV_OBJX_NAME);
261
262 lv_slider_type_t type = lv_slider_get_type(slider);
263 lv_slider_ext_t * ext = lv_obj_get_ext_attr(slider);
264
265 /* Advanced hit testing: react only on dragging the knob(s) */
266 if(sign == LV_SIGNAL_HIT_TEST) {
267 lv_hit_test_info_t * info = param;
268
269 /* Ordinary slider: was the knob area hit? */
270 info->result = _lv_area_is_point_on(&ext->right_knob_area, info->point, 0);
271
272 /* There's still a change we have a hit, if we have another knob */
273 if((info->result == false) && (type == LV_SLIDER_TYPE_RANGE)) {
274 info->result = _lv_area_is_point_on(&ext->left_knob_area, info->point, 0);
275 }
276 }
277
278 lv_point_t p;
279
280 if(sign == LV_SIGNAL_PRESSED) {
281 ext->dragging = true;
282 if(type == LV_SLIDER_TYPE_NORMAL || type == LV_SLIDER_TYPE_SYMMETRICAL) {
283 ext->value_to_set = &ext->bar.cur_value;
284 }
285 else if(type == LV_SLIDER_TYPE_RANGE) {
286 lv_indev_get_point(param, &p);
287 bool hor = lv_obj_get_width(slider) >= lv_obj_get_height(slider);
288 lv_bidi_dir_t base_dir = lv_obj_get_base_dir(slider);
289
290 lv_coord_t dist_left, dist_right;
291 if(hor) {
292 if((base_dir != LV_BIDI_DIR_RTL && p.x > ext->right_knob_area.x2) || (base_dir == LV_BIDI_DIR_RTL &&
293 p.x < ext->right_knob_area.x1)) {
294 ext->value_to_set = &ext->bar.cur_value;
295 }
296 else if((base_dir != LV_BIDI_DIR_RTL && p.x < ext->left_knob_area.x1) || (base_dir == LV_BIDI_DIR_RTL &&
297 p.x > ext->left_knob_area.x2)) {
298 ext->value_to_set = &ext->bar.start_value;
299 }
300 else {
301 /* Calculate the distance from each knob */
302 dist_left = LV_MATH_ABS((ext->left_knob_area.x1 + (ext->left_knob_area.x2 - ext->left_knob_area.x1) / 2) - p.x);
303 dist_right = LV_MATH_ABS((ext->right_knob_area.x1 + (ext->right_knob_area.x2 - ext->right_knob_area.x1) / 2) - p.x);
304
305 /* Use whichever one is closer */
306 if(dist_right < dist_left)ext->value_to_set = &ext->bar.cur_value;
307 else ext->value_to_set = &ext->bar.start_value;
308 }
309 }
310 else {
311 if(p.y < ext->right_knob_area.y1) {
312 ext->value_to_set = &ext->bar.cur_value;
313 }
314 else if(p.y > ext->left_knob_area.y2) {
315 ext->value_to_set = &ext->bar.start_value;
316 }
317 else {
318 /* Calculate the distance from each knob */
319 dist_left = LV_MATH_ABS((ext->left_knob_area.y1 + (ext->left_knob_area.y2 - ext->left_knob_area.y1) / 2) - p.y);
320 dist_right = LV_MATH_ABS((ext->right_knob_area.y1 + (ext->right_knob_area.y2 - ext->right_knob_area.y1) / 2) - p.y);
321
322 /* Use whichever one is closer */
323 if(dist_right < dist_left)ext->value_to_set = &ext->bar.cur_value;
324 else ext->value_to_set = &ext->bar.start_value;
325 }
326 }
327 }
328 }
329 else if(sign == LV_SIGNAL_PRESSING && ext->value_to_set != NULL) {
330 lv_indev_get_point(param, &p);
331 lv_bidi_dir_t base_dir = lv_obj_get_base_dir(slider);
332
333 lv_coord_t w = lv_obj_get_width(slider);
334 lv_coord_t h = lv_obj_get_height(slider);
335
336 lv_style_int_t bg_left = lv_obj_get_style_pad_left(slider, LV_SLIDER_PART_BG);
337 lv_style_int_t bg_right = lv_obj_get_style_pad_right(slider, LV_SLIDER_PART_BG);
338 lv_style_int_t bg_top = lv_obj_get_style_pad_top(slider, LV_SLIDER_PART_BG);
339 lv_style_int_t bg_bottom = lv_obj_get_style_pad_bottom(slider, LV_SLIDER_PART_BG);
340
341 int32_t range = ext->bar.max_value - ext->bar.min_value;
342 int16_t new_value = 0;
343 int16_t real_max_value = ext->bar.max_value;
344 int16_t real_min_value = ext->bar.min_value;
345
346 if(w >= h) {
347 lv_coord_t indic_w = w - bg_left - bg_right;
348 if(base_dir == LV_BIDI_DIR_RTL) {
349 new_value = (slider->coords.x2 - bg_right) - p.x; /*Make the point relative to the indicator*/
350 }
351 else {
352 new_value = p.x - (slider->coords.x1 + bg_left); /*Make the point relative to the indicator*/
353 }
354 new_value = (new_value * range) / indic_w;
355 new_value += ext->bar.min_value;
356 }
357 else {
358 lv_coord_t indic_h = h - bg_bottom - bg_top;
359 new_value = p.y - (slider->coords.y2 + bg_bottom); /*Make the point relative to the indicator*/
360 new_value = (-new_value * range) / indic_h;
361 new_value += ext->bar.min_value;
362
363 }
364
365 /* Figure out the min. and max. for this mode */
366 if(ext->value_to_set == &ext->bar.start_value) {
367 real_max_value = ext->bar.cur_value;
368 }
369 else {
370 real_min_value = ext->bar.start_value;
371 }
372
373 if(new_value < real_min_value) new_value = real_min_value;
374 else if(new_value > real_max_value) new_value = real_max_value;
375 if(*ext->value_to_set != new_value) {
376 *ext->value_to_set = new_value;
377 lv_obj_invalidate(slider);
378 res = lv_event_send(slider, LV_EVENT_VALUE_CHANGED, NULL);
379 if(res != LV_RES_OK) return res;
380 }
381
382 }
383 else if(sign == LV_SIGNAL_RELEASED || sign == LV_SIGNAL_PRESS_LOST) {
384 ext->dragging = false;
385 ext->value_to_set = NULL;
386
387 #if LV_USE_GROUP
388 /*Leave edit mode if released. (No need to wait for LONG_PRESS) */
389 lv_group_t * g = lv_obj_get_group(slider);
390 bool editing = lv_group_get_editing(g);
391 lv_indev_type_t indev_type = lv_indev_get_type(lv_indev_get_act());
392 if(indev_type == LV_INDEV_TYPE_ENCODER) {
393 if(editing) lv_group_set_editing(g, false);
394 }
395 #endif
396
397 }
398 else if(sign == LV_SIGNAL_COORD_CHG) {
399 /* The knob size depends on slider size.
400 * During the drawing method the ext. size is used by the knob so refresh the ext. size.*/
401 if(lv_obj_get_width(slider) != lv_area_get_width(param) ||
402 lv_obj_get_height(slider) != lv_area_get_height(param)) {
403 slider->signal_cb(slider, LV_SIGNAL_REFR_EXT_DRAW_PAD, NULL);
404 }
405 }
406 else if(sign == LV_SIGNAL_REFR_EXT_DRAW_PAD) {
407 lv_style_int_t knob_left = lv_obj_get_style_pad_left(slider, LV_SLIDER_PART_KNOB);
408 lv_style_int_t knob_right = lv_obj_get_style_pad_right(slider, LV_SLIDER_PART_KNOB);
409 lv_style_int_t knob_top = lv_obj_get_style_pad_top(slider, LV_SLIDER_PART_KNOB);
410 lv_style_int_t knob_bottom = lv_obj_get_style_pad_bottom(slider, LV_SLIDER_PART_KNOB);
411
412 /* The smaller size is the knob diameter*/
413 lv_coord_t knob_size = LV_MATH_MIN(lv_obj_get_width(slider), lv_obj_get_height(slider)) >> 1;
414 knob_size += LV_MATH_MAX(LV_MATH_MAX(knob_left, knob_right), LV_MATH_MAX(knob_bottom, knob_top));
415 knob_size += 2; /*For rounding error*/
416
417 knob_size += lv_obj_get_draw_rect_ext_pad_size(slider, LV_SLIDER_PART_KNOB);
418
419 /*Indic. size is handled by bar*/
420 slider->ext_draw_pad = LV_MATH_MAX(slider->ext_draw_pad, knob_size);
421
422 }
423 else if(sign == LV_SIGNAL_CONTROL) {
424 #if LV_USE_GROUP
425 char c = *((char *)param);
426
427 if(c == LV_KEY_RIGHT || c == LV_KEY_UP) {
428 lv_slider_set_value(slider, lv_slider_get_value(slider) + 1, LV_ANIM_ON);
429 res = lv_event_send(slider, LV_EVENT_VALUE_CHANGED, NULL);
430 if(res != LV_RES_OK) return res;
431 }
432 else if(c == LV_KEY_LEFT || c == LV_KEY_DOWN) {
433 lv_slider_set_value(slider, lv_slider_get_value(slider) - 1, LV_ANIM_ON);
434 res = lv_event_send(slider, LV_EVENT_VALUE_CHANGED, NULL);
435 if(res != LV_RES_OK) return res;
436 }
437 #endif
438 }
439 else if(sign == LV_SIGNAL_CLEANUP) {
440 lv_obj_clean_style_list(slider, LV_SLIDER_PART_KNOB);
441 }
442 else if(sign == LV_SIGNAL_GET_EDITABLE) {
443 #if LV_USE_GROUP
444 bool * editable = (bool *)param;
445 *editable = true;
446 #endif
447 }
448
449 return res;
450 }
451
452
lv_slider_get_style(lv_obj_t * slider,uint8_t part)453 static lv_style_list_t * lv_slider_get_style(lv_obj_t * slider, uint8_t part)
454 {
455 LV_ASSERT_OBJ(slider, LV_OBJX_NAME);
456
457 lv_slider_ext_t * ext = lv_obj_get_ext_attr(slider);
458 lv_style_list_t * style_dsc_p;
459
460 switch(part) {
461 case LV_SLIDER_PART_BG:
462 style_dsc_p = &slider->style_list;
463 break;
464 case LV_SLIDER_PART_INDIC:
465 style_dsc_p = &ext->bar.style_indic;
466 break;
467 case LV_SLIDER_PART_KNOB:
468 style_dsc_p = &ext->style_knob;
469 break;
470 default:
471 style_dsc_p = NULL;
472 }
473
474 return style_dsc_p;
475 }
476
lv_slider_position_knob(lv_obj_t * slider,lv_area_t * knob_area,lv_coord_t knob_size,bool hor)477 static void lv_slider_position_knob(lv_obj_t * slider, lv_area_t * knob_area, lv_coord_t knob_size, bool hor)
478 {
479
480 if(hor) {
481 knob_area->x1 -= (knob_size >> 1);
482 knob_area->x2 = knob_area->x1 + knob_size - 1;
483 knob_area->y1 = slider->coords.y1;
484 knob_area->y2 = slider->coords.y2;
485 }
486 else {
487 knob_area->y1 -= (knob_size >> 1);
488 knob_area->y2 = knob_area->y1 + knob_size - 1;
489 knob_area->x1 = slider->coords.x1;
490 knob_area->x2 = slider->coords.x2;
491 }
492
493 lv_style_int_t knob_left = lv_obj_get_style_pad_left(slider, LV_SLIDER_PART_KNOB);
494 lv_style_int_t knob_right = lv_obj_get_style_pad_right(slider, LV_SLIDER_PART_KNOB);
495 lv_style_int_t knob_top = lv_obj_get_style_pad_top(slider, LV_SLIDER_PART_KNOB);
496 lv_style_int_t knob_bottom = lv_obj_get_style_pad_bottom(slider, LV_SLIDER_PART_KNOB);
497
498 /*Apply the paddings on the knob area*/
499 knob_area->x1 -= knob_left;
500 knob_area->x2 += knob_right;
501 knob_area->y1 -= knob_top;
502 knob_area->y2 += knob_bottom;
503 }
504
lv_slider_draw_knob(lv_obj_t * slider,const lv_area_t * knob_area,const lv_area_t * clip_area)505 static void lv_slider_draw_knob(lv_obj_t * slider, const lv_area_t * knob_area, const lv_area_t * clip_area)
506 {
507 lv_draw_rect_dsc_t knob_rect_dsc;
508 lv_draw_rect_dsc_init(&knob_rect_dsc);
509 lv_obj_init_draw_rect_dsc(slider, LV_SLIDER_PART_KNOB, &knob_rect_dsc);
510
511 lv_draw_rect(knob_area, clip_area, &knob_rect_dsc);
512 }
513 #endif
514