1 /**
2 * @file lv_tileview.c
3 *
4 */
5
6 /*********************
7 * INCLUDES
8 *********************/
9 #include "lv_tileview.h"
10 #if LV_USE_TILEVIEW != 0
11
12 #include <stdbool.h>
13 #include "lv_cont.h"
14 #include "../lv_themes/lv_theme.h"
15
16 /*********************
17 * DEFINES
18 *********************/
19 #if LV_USE_ANIMATION
20 #ifndef LV_TILEVIEW_DEF_ANIM_TIME
21 #define LV_TILEVIEW_DEF_ANIM_TIME 300 /*Animation time loading a tile [ms] (0: no animation) */
22 #endif
23 #else
24 #undef LV_TILEVIEW_DEF_ANIM_TIME
25 #define LV_TILEVIEW_DEF_ANIM_TIME 0 /*No animations*/
26 #endif
27
28 /**********************
29 * TYPEDEFS
30 **********************/
31
32 /**********************
33 * STATIC PROTOTYPES
34 **********************/
35 static lv_res_t lv_tileview_signal(lv_obj_t * tileview, lv_signal_t sign, void * param);
36 static lv_res_t lv_tileview_scrl_signal(lv_obj_t * scrl, lv_signal_t sign, void * param);
37 static void tileview_scrl_event_cb(lv_obj_t * scrl, lv_event_t event);
38 static void drag_end_handler(lv_obj_t * tileview);
39 static bool set_valid_drag_dirs(lv_obj_t * tileview);
40
41 /**********************
42 * STATIC VARIABLES
43 **********************/
44 static lv_signal_cb_t ancestor_signal;
45 static lv_signal_cb_t ancestor_scrl_signal;
46 static lv_design_cb_t ancestor_design;
47
48 /**********************
49 * MACROS
50 **********************/
51
52 /**********************
53 * GLOBAL FUNCTIONS
54 **********************/
55
56 /**
57 * Create a tileview object
58 * @param par pointer to an object, it will be the parent of the new tileview
59 * @param copy pointer to a tileview object, if not NULL then the new object will be copied from it
60 * @return pointer to the created tileview
61 */
lv_tileview_create(lv_obj_t * par,const lv_obj_t * copy)62 lv_obj_t * lv_tileview_create(lv_obj_t * par, const lv_obj_t * copy)
63 {
64 LV_LOG_TRACE("tileview create started");
65
66 /*Create the ancestor of tileview*/
67 lv_obj_t * new_tileview = lv_page_create(par, copy);
68 lv_mem_assert(new_tileview);
69 if(new_tileview == NULL) return NULL;
70
71 /*Allocate the tileview type specific extended data*/
72 lv_tileview_ext_t * ext = lv_obj_allocate_ext_attr(new_tileview, sizeof(lv_tileview_ext_t));
73 lv_mem_assert(ext);
74 if(ext == NULL) return NULL;
75 if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_tileview);
76 if(ancestor_scrl_signal == NULL) ancestor_scrl_signal = lv_obj_get_signal_cb(lv_page_get_scrl(new_tileview));
77 if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(new_tileview);
78
79 /*Initialize the allocated 'ext' */
80 #if LV_USE_ANIMATION
81 ext->anim_time = LV_TILEVIEW_DEF_ANIM_TIME;
82 #endif
83 ext->act_id.x = 0;
84 ext->act_id.y = 0;
85 ext->valid_pos = NULL;
86 ext->valid_pos_cnt = 0;
87
88 /*The signal and design functions are not copied so set them here*/
89 lv_obj_set_signal_cb(new_tileview, lv_tileview_signal);
90 lv_obj_set_signal_cb(lv_page_get_scrl(new_tileview), lv_tileview_scrl_signal);
91
92 /*Init the new tileview*/
93 if(copy == NULL) {
94 /* Set a size which fits into the parent.
95 * Don't use `par` directly because if the tileview is created on a page it is moved to the
96 * scrollable so the parent has changed */
97 lv_obj_set_size(new_tileview, lv_obj_get_width_fit(lv_obj_get_parent(new_tileview)),
98 lv_obj_get_height_fit(lv_obj_get_parent(new_tileview)));
99
100 lv_obj_set_drag_throw(lv_page_get_scrl(new_tileview), false);
101 lv_page_set_scrl_fit(new_tileview, LV_FIT_TIGHT);
102 lv_obj_set_event_cb(ext->page.scrl, tileview_scrl_event_cb);
103 /*Set the default styles*/
104 lv_theme_t * th = lv_theme_get_current();
105 if(th) {
106 lv_page_set_style(new_tileview, LV_PAGE_STYLE_BG, th->style.tileview.bg);
107 lv_page_set_style(new_tileview, LV_PAGE_STYLE_SCRL, th->style.tileview.scrl);
108 lv_page_set_style(new_tileview, LV_PAGE_STYLE_SB, th->style.tileview.sb);
109 } else {
110 lv_page_set_style(new_tileview, LV_PAGE_STYLE_BG, &lv_style_transp_tight);
111 lv_page_set_style(new_tileview, LV_PAGE_STYLE_SCRL, &lv_style_transp_tight);
112 }
113 }
114 /*Copy an existing tileview*/
115 else {
116 lv_tileview_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
117 ext->act_id.x = copy_ext->act_id.x;
118 ext->act_id.y = copy_ext->act_id.y;
119 ext->valid_pos = copy_ext->valid_pos;
120 ext->valid_pos_cnt = copy_ext->valid_pos_cnt;
121 #if LV_USE_ANIMATION
122 ext->anim_time = copy_ext->anim_time;
123 #endif
124
125 /*Refresh the style with new signal function*/
126 lv_obj_refresh_style(new_tileview);
127 }
128
129 LV_LOG_INFO("tileview created");
130
131 return new_tileview;
132 }
133
134 /*======================
135 * Add/remove functions
136 *=====================*/
137
138 /**
139 * Register an object on the tileview. The register object will able to slide the tileview
140 * @param tileview pointer to a Tileview object
141 * @param element pointer to an object
142 */
lv_tileview_add_element(lv_obj_t * tileview,lv_obj_t * element)143 void lv_tileview_add_element(lv_obj_t * tileview, lv_obj_t * element)
144 {
145 /* Let the objects event to propagate to the scrollable part of the tileview.
146 * It is required the handle dargging of the tileview with the element.*/
147 element->parent_event = 1;
148 lv_obj_set_drag_parent(element, true);
149
150 /* When adding a new element the coordinates may shift.
151 * For example y=1 can become y=1 if an element is added to the top.
152 * So be sure the current tile is correctly shown*/
153 lv_tileview_ext_t * ext = lv_obj_get_ext_attr(tileview);
154 lv_tileview_set_tile_act(tileview, ext->act_id.x, ext->act_id.y, false);
155 }
156
157 /*=====================
158 * Setter functions
159 *====================*/
160
161 /**
162 * Set the valid position's indices. The scrolling will be possible only to these positions.
163 * @param tileview pointer to a Tileview object
164 * @param valid_pos array width the indices. E.g. `lv_point_t p[] = {{0,0}, {1,0}, {1,1}`. Only the
165 * pointer is saved so can't be a local variable.
166 * @param valid_pos_cnt numner of elements in `valid_pos` array
167 */
lv_tileview_set_valid_positions(lv_obj_t * tileview,const lv_point_t * valid_pos,uint16_t valid_pos_cnt)168 void lv_tileview_set_valid_positions(lv_obj_t * tileview, const lv_point_t * valid_pos, uint16_t valid_pos_cnt)
169 {
170 lv_tileview_ext_t * ext = lv_obj_get_ext_attr(tileview);
171 ext->valid_pos = valid_pos;
172 ext->valid_pos_cnt = valid_pos_cnt;
173
174 /*If valid pos. is selected do nothing*/
175 uint16_t i;
176 for(i = 0; i < valid_pos_cnt; i++) {
177 if(valid_pos->x == ext->act_id.x && valid_pos->y == ext->act_id.y) {
178 return;
179 }
180 }
181
182 /*Set a valid position if now an invalid is selected*/
183 if(valid_pos_cnt > 0) {
184 lv_tileview_set_tile_act(tileview, valid_pos[0].x, valid_pos[0].y, LV_ANIM_OFF);
185 }
186 }
187
188 /**
189 * Set the tile to be shown
190 * @param tileview pointer to a tileview object
191 * @param x column id (0, 1, 2...)
192 * @param y line id (0, 1, 2...)
193 * @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately
194 */
lv_tileview_set_tile_act(lv_obj_t * tileview,lv_coord_t x,lv_coord_t y,lv_anim_enable_t anim)195 void lv_tileview_set_tile_act(lv_obj_t * tileview, lv_coord_t x, lv_coord_t y, lv_anim_enable_t anim)
196 {
197 #if LV_USE_ANIMATION == 0
198 anim = LV_ANIM_OFF;
199 #endif
200
201 lv_tileview_ext_t * ext = lv_obj_get_ext_attr(tileview);
202
203 uint32_t tile_id;
204 bool valid = false;
205 for(tile_id = 0; tile_id < ext->valid_pos_cnt; tile_id++) {
206 if(ext->valid_pos[tile_id].x == x && ext->valid_pos[tile_id].y == y) {
207 valid = true;
208 }
209 }
210
211 if(valid == false) return; /*Don't load not valid tiles*/
212
213 ext->act_id.x = x;
214 ext->act_id.y = y;
215
216 lv_coord_t x_coord = -x * lv_obj_get_width(tileview);
217 lv_coord_t y_coord = -y * lv_obj_get_height(tileview);
218 lv_obj_t * scrl = lv_page_get_scrl(tileview);
219 if(anim) {
220 #if LV_USE_ANIMATION
221 lv_coord_t x_act = lv_obj_get_x(scrl);
222 lv_coord_t y_act = lv_obj_get_y(scrl);
223
224 lv_anim_t a;
225 a.var = scrl;
226 a.exec_cb = (lv_anim_exec_xcb_t)lv_obj_set_x;
227 a.path_cb = lv_anim_path_linear;
228 a.ready_cb = NULL;
229 a.act_time = 0;
230 a.time = ext->anim_time;
231 a.playback = 0;
232 a.playback_pause = 0;
233 a.repeat = 0;
234 a.repeat_pause = 0;
235
236 if(x_coord != x_act) {
237 a.start = x_act;
238 a.end = x_coord;
239 lv_anim_create(&a);
240 }
241
242 if(y_coord != y_act) {
243 a.start = y_act;
244 a.end = y_coord;
245 a.exec_cb = (lv_anim_exec_xcb_t)lv_obj_set_y;
246 lv_anim_create(&a);
247 }
248 #endif
249 } else {
250 lv_obj_set_pos(scrl, x_coord, y_coord);
251 }
252
253 lv_res_t res = LV_RES_OK;
254 res = lv_event_send(tileview, LV_EVENT_VALUE_CHANGED, &tile_id);
255 if(res != LV_RES_OK) return; /*Prevent the tile loading*/
256 }
257
258 /**
259 * Set a style of a tileview.
260 * @param tileview pointer to tileview object
261 * @param type which style should be set
262 * @param style pointer to a style
263 */
lv_tileview_set_style(lv_obj_t * tileview,lv_tileview_style_t type,const lv_style_t * style)264 void lv_tileview_set_style(lv_obj_t * tileview, lv_tileview_style_t type, const lv_style_t * style)
265 {
266
267 switch(type) {
268 case LV_TILEVIEW_STYLE_MAIN: lv_obj_set_style(tileview, style); break;
269 }
270 }
271
272 /*=====================
273 * Getter functions
274 *====================*/
275
276 /*
277 * New object specific "get" functions come here
278 */
279
280 /**
281 * Get style of a tileview.
282 * @param tileview pointer to tileview object
283 * @param type which style should be get
284 * @return style pointer to the style
285 */
lv_tileview_get_style(const lv_obj_t * tileview,lv_tileview_style_t type)286 const lv_style_t * lv_tileview_get_style(const lv_obj_t * tileview, lv_tileview_style_t type)
287 {
288 const lv_style_t * style = NULL;
289 switch(type) {
290 case LV_TILEVIEW_STYLE_MAIN: style = lv_obj_get_style(tileview); break;
291 default: style = NULL;
292 }
293
294 return style;
295 }
296
297 /*=====================
298 * Other functions
299 *====================*/
300
301 /*
302 * New object specific "other" functions come here
303 */
304
305 /**********************
306 * STATIC FUNCTIONS
307 **********************/
308
309 /**
310 * Signal function of the tileview
311 * @param tileview pointer to a tileview object
312 * @param sign a signal type from lv_signal_t enum
313 * @param param pointer to a signal specific variable
314 * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
315 */
lv_tileview_signal(lv_obj_t * tileview,lv_signal_t sign,void * param)316 static lv_res_t lv_tileview_signal(lv_obj_t * tileview, lv_signal_t sign, void * param)
317 {
318 lv_res_t res;
319
320 /* Include the ancient signal function */
321 res = ancestor_signal(tileview, sign, param);
322 if(res != LV_RES_OK) return res;
323
324 if(sign == LV_SIGNAL_CLEANUP) {
325 /*Nothing to cleanup. (No dynamically allocated memory in 'ext')*/
326 } else if(sign == LV_SIGNAL_GET_TYPE) {
327 lv_obj_type_t * buf = param;
328 uint8_t i;
329 for(i = 0; i < LV_MAX_ANCESTOR_NUM - 1; i++) { /*Find the last set data*/
330 if(buf->type[i] == NULL) break;
331 }
332 buf->type[i] = "lv_tileview";
333 }
334
335 return res;
336 }
337
338 /**
339 * Signal function of the tileview scrollable
340 * @param tileview pointer to the scrollable part of the tileview object
341 * @param sign a signal type from lv_signal_t enum
342 * @param param pointer to a signal specific variable
343 * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
344 */
lv_tileview_scrl_signal(lv_obj_t * scrl,lv_signal_t sign,void * param)345 static lv_res_t lv_tileview_scrl_signal(lv_obj_t * scrl, lv_signal_t sign, void * param)
346 {
347
348 lv_res_t res;
349
350 /* Include the ancient signal function */
351 res = ancestor_scrl_signal(scrl, sign, param);
352 if(res != LV_RES_OK) return res;
353
354 lv_obj_t * tileview = lv_obj_get_parent(scrl);
355 const lv_style_t * style_bg = lv_tileview_get_style(tileview, LV_TILEVIEW_STYLE_MAIN);
356
357 /*Apply constraint on moving of the tileview*/
358 if(sign == LV_SIGNAL_CORD_CHG) {
359 lv_indev_t * indev = lv_indev_get_act();
360 if(indev) {
361 lv_tileview_ext_t * ext = lv_obj_get_ext_attr(tileview);
362
363 /*Set horizontal drag constraint if no vertical constraint an dragged to valid x
364 * direction */
365 if(ext->drag_ver == 0 &&
366 ((ext->drag_right_en && indev->proc.types.pointer.drag_sum.x <= -LV_INDEV_DEF_DRAG_LIMIT) ||
367 (ext->drag_left_en && indev->proc.types.pointer.drag_sum.x >= LV_INDEV_DEF_DRAG_LIMIT))) {
368 ext->drag_hor = 1;
369 }
370 /*Set vertical drag constraint if no horizontal constraint an dragged to valid y
371 * direction */
372 if(ext->drag_hor == 0 &&
373 ((ext->drag_bottom_en && indev->proc.types.pointer.drag_sum.y <= -LV_INDEV_DEF_DRAG_LIMIT) ||
374 (ext->drag_top_en && indev->proc.types.pointer.drag_sum.y >= LV_INDEV_DEF_DRAG_LIMIT))) {
375 ext->drag_ver = 1;
376 }
377
378 #if LV_USE_ANIMATION
379 if(ext->drag_hor) {
380 ext->page.edge_flash.top_ip = 0;
381 ext->page.edge_flash.bottom_ip = 0;
382 }
383
384 if(ext->drag_ver) {
385 ext->page.edge_flash.right_ip = 0;
386 ext->page.edge_flash.left_ip = 0;
387 }
388 #endif
389
390 lv_coord_t x = lv_obj_get_x(scrl);
391 lv_coord_t y = lv_obj_get_y(scrl);
392 lv_coord_t h = lv_obj_get_height(tileview);
393 lv_coord_t w = lv_obj_get_width(tileview);
394 if(ext->drag_top_en == 0) {
395 if(y > -(ext->act_id.y * h) && indev->proc.types.pointer.vect.y > 0 && ext->drag_hor == 0) {
396 #if LV_USE_ANIMATION
397 if(ext->page.edge_flash.enabled && ext->page.edge_flash.left_ip == 0 &&
398 ext->page.edge_flash.right_ip == 0 && ext->page.edge_flash.top_ip == 0 &&
399 ext->page.edge_flash.bottom_ip == 0) {
400 ext->page.edge_flash.top_ip = 1;
401 lv_page_start_edge_flash(tileview);
402 }
403 #endif
404
405 lv_obj_set_y(scrl, -ext->act_id.y * h + style_bg->body.padding.top);
406 }
407 }
408 if(ext->drag_bottom_en == 0 && indev->proc.types.pointer.vect.y < 0 && ext->drag_hor == 0) {
409 if(y < -(ext->act_id.y * h)) {
410 #if LV_USE_ANIMATION
411 if(ext->page.edge_flash.enabled && ext->page.edge_flash.left_ip == 0 &&
412 ext->page.edge_flash.right_ip == 0 && ext->page.edge_flash.top_ip == 0 &&
413 ext->page.edge_flash.bottom_ip == 0) {
414 ext->page.edge_flash.bottom_ip = 1;
415 lv_page_start_edge_flash(tileview);
416 }
417 #endif
418 }
419
420 lv_obj_set_y(scrl, -ext->act_id.y * h + style_bg->body.padding.top);
421 }
422 if(ext->drag_left_en == 0) {
423 if(x > -(ext->act_id.x * w) && indev->proc.types.pointer.vect.x > 0 && ext->drag_ver == 0) {
424 #if LV_USE_ANIMATION
425 if(ext->page.edge_flash.enabled && ext->page.edge_flash.left_ip == 0 &&
426 ext->page.edge_flash.right_ip == 0 && ext->page.edge_flash.top_ip == 0 &&
427 ext->page.edge_flash.bottom_ip == 0) {
428 ext->page.edge_flash.left_ip = 1;
429 lv_page_start_edge_flash(tileview);
430 }
431 #endif
432
433 lv_obj_set_x(scrl, -ext->act_id.x * w + style_bg->body.padding.left);
434 }
435 }
436 if(ext->drag_right_en == 0 && indev->proc.types.pointer.vect.x < 0 && ext->drag_ver == 0) {
437 if(x < -(ext->act_id.x * w)) {
438 #if LV_USE_ANIMATION
439 if(ext->page.edge_flash.enabled && ext->page.edge_flash.left_ip == 0 &&
440 ext->page.edge_flash.right_ip == 0 && ext->page.edge_flash.top_ip == 0 &&
441 ext->page.edge_flash.bottom_ip == 0) {
442 ext->page.edge_flash.right_ip = 1;
443 lv_page_start_edge_flash(tileview);
444 }
445 #endif
446 }
447
448 lv_obj_set_x(scrl, -ext->act_id.x * w + style_bg->body.padding.top);
449 }
450
451 /*Apply the drag constraints*/
452 if(ext->drag_ver == 0)
453 lv_obj_set_y(scrl, -ext->act_id.y * lv_obj_get_height(tileview) + style_bg->body.padding.top);
454 if(ext->drag_hor == 0)
455 lv_obj_set_x(scrl, -ext->act_id.x * lv_obj_get_width(tileview) + style_bg->body.padding.left);
456 }
457 }
458 return res;
459 }
460
tileview_scrl_event_cb(lv_obj_t * scrl,lv_event_t event)461 static void tileview_scrl_event_cb(lv_obj_t * scrl, lv_event_t event)
462 {
463 lv_obj_t * tileview = lv_obj_get_parent(scrl);
464
465 /*Initialize some variables on PRESS*/
466 if(event == LV_EVENT_PRESSED) {
467 lv_tileview_ext_t * ext = lv_obj_get_ext_attr(tileview);
468 ext->drag_hor = 0;
469 ext->drag_ver = 0;
470 set_valid_drag_dirs(tileview);
471 }
472 /*Animate the tabview to the correct location on RELEASE*/
473 else if(event == LV_EVENT_PRESS_LOST || event == LV_EVENT_RELEASED) {
474 /* If the element was dragged and it moved the tileview finish the drag manually to
475 * let the tileview to finish the move.*/
476 lv_indev_t * indev = lv_indev_get_act();
477 lv_tileview_ext_t * ext = lv_obj_get_ext_attr(tileview);
478 if(lv_indev_is_dragging(indev) && (ext->drag_hor || ext->drag_ver)) {
479 indev->proc.types.pointer.drag_in_prog = 0;
480 }
481
482 drag_end_handler(tileview);
483 }
484 }
485
486 /**
487 * Called when the user releases an element of the tileview after dragging it.
488 * @param tileview pointer to a tileview object
489 */
drag_end_handler(lv_obj_t * tileview)490 static void drag_end_handler(lv_obj_t * tileview)
491 {
492 lv_tileview_ext_t * ext = lv_obj_get_ext_attr(tileview);
493 lv_indev_t * indev = lv_indev_get_act();
494 lv_point_t point_act;
495 lv_indev_get_point(indev, &point_act);
496 lv_obj_t * scrl = lv_page_get_scrl(tileview);
497 lv_point_t p;
498
499 p.x = -(scrl->coords.x1 - lv_obj_get_width(tileview) / 2);
500 p.y = -(scrl->coords.y1 - lv_obj_get_height(tileview) / 2);
501
502 /*From the drag vector (drag throw) predict the end position*/
503 if(ext->drag_hor) {
504 lv_point_t vect;
505 lv_indev_get_vect(indev, &vect);
506 lv_coord_t predict = 0;
507
508 while(vect.x != 0) {
509 predict += vect.x;
510 vect.x = vect.x * (100 - LV_INDEV_DEF_DRAG_THROW) / 100;
511 }
512
513 p.x -= predict;
514 } else if(ext->drag_ver) {
515 lv_point_t vect;
516 lv_indev_get_vect(indev, &vect);
517 lv_coord_t predict = 0;
518
519 while(vect.y != 0) {
520 predict += vect.y;
521 vect.y = vect.y * (100 - LV_INDEV_DEF_DRAG_THROW) / 100;
522 }
523
524 p.y -= predict;
525 }
526
527 /*Get the index of the tile*/
528 p.x = p.x / lv_obj_get_width(tileview);
529 p.y = p.y / lv_obj_get_height(tileview);
530
531 /*Max +- move*/
532 lv_coord_t x_move = p.x - ext->act_id.x;
533 lv_coord_t y_move = p.y - ext->act_id.y;
534 if(x_move < -1) x_move = -1;
535 if(x_move > 1) x_move = 1;
536 if(y_move < -1) y_move = -1;
537 if(y_move > 1) y_move = 1;
538
539 /*Set the new tile*/
540 lv_tileview_set_tile_act(tileview, ext->act_id.x + x_move, ext->act_id.y + y_move, true);
541 }
542
set_valid_drag_dirs(lv_obj_t * tileview)543 static bool set_valid_drag_dirs(lv_obj_t * tileview)
544 {
545
546 lv_tileview_ext_t * ext = lv_obj_get_ext_attr(tileview);
547 if(ext->valid_pos == NULL) return false;
548
549 ext->drag_bottom_en = 0;
550 ext->drag_top_en = 0;
551 ext->drag_left_en = 0;
552 ext->drag_right_en = 0;
553
554 uint16_t i;
555 for(i = 0; i < ext->valid_pos_cnt; i++) {
556 if(ext->valid_pos[i].x == ext->act_id.x && ext->valid_pos[i].y == ext->act_id.y - 1) ext->drag_top_en = 1;
557 if(ext->valid_pos[i].x == ext->act_id.x && ext->valid_pos[i].y == ext->act_id.y + 1) ext->drag_bottom_en = 1;
558 if(ext->valid_pos[i].x == ext->act_id.x - 1 && ext->valid_pos[i].y == ext->act_id.y) ext->drag_left_en = 1;
559 if(ext->valid_pos[i].x == ext->act_id.x + 1 && ext->valid_pos[i].y == ext->act_id.y) ext->drag_right_en = 1;
560 }
561
562 return true;
563 }
564
565 #endif
566