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_misc/lv_math.h"
15 #include "../lv_misc/lv_debug.h"
16 #include "../lv_themes/lv_theme.h"
17
18 /*********************
19 * DEFINES
20 *********************/
21 #define LV_OBJX_NAME "lv_tileview"
22
23 #if LV_USE_ANIMATION
24 #ifndef LV_TILEVIEW_DEF_ANIM_TIME
25 #define LV_TILEVIEW_DEF_ANIM_TIME 300 /*Animation time loading a tile [ms] (0: no animation) */
26 #endif
27 #else
28 #undef LV_TILEVIEW_DEF_ANIM_TIME
29 #define LV_TILEVIEW_DEF_ANIM_TIME 0 /*No animations*/
30 #endif
31
32 /**********************
33 * TYPEDEFS
34 **********************/
35
36 /**********************
37 * STATIC PROTOTYPES
38 **********************/
39 static lv_res_t lv_tileview_signal(lv_obj_t * tileview, lv_signal_t sign, void * param);
40 static lv_res_t lv_tileview_scrl_signal(lv_obj_t * scrl, lv_signal_t sign, void * param);
41 static void drag_end_handler(lv_obj_t * tileview);
42 static bool set_valid_drag_dirs(lv_obj_t * tileview);
43
44 /**********************
45 * STATIC VARIABLES
46 **********************/
47 static lv_signal_cb_t ancestor_signal;
48 static lv_signal_cb_t ancestor_scrl_signal;
49 static lv_design_cb_t ancestor_design;
50
51 /**********************
52 * MACROS
53 **********************/
54
55 /**********************
56 * GLOBAL FUNCTIONS
57 **********************/
58
59 /**
60 * Create a tileview object
61 * @param par pointer to an object, it will be the parent of the new tileview
62 * @param copy pointer to a tileview object, if not NULL then the new object will be copied from it
63 * @return pointer to the created tileview
64 */
lv_tileview_create(lv_obj_t * par,const lv_obj_t * copy)65 lv_obj_t * lv_tileview_create(lv_obj_t * par, const lv_obj_t * copy)
66 {
67 LV_LOG_TRACE("tileview create started");
68
69 /*Create the ancestor of tileview*/
70 lv_obj_t * new_tileview = lv_page_create(par, copy);
71 LV_ASSERT_MEM(new_tileview);
72 if(new_tileview == NULL) return NULL;
73
74 /*Allocate the tileview type specific extended data*/
75 lv_tileview_ext_t * ext = lv_obj_allocate_ext_attr(new_tileview, sizeof(lv_tileview_ext_t));
76 LV_ASSERT_MEM(ext);
77 if(ext == NULL) {
78 lv_obj_del(new_tileview);
79 return NULL;
80 }
81
82 if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_tileview);
83 if(ancestor_scrl_signal == NULL) ancestor_scrl_signal = lv_obj_get_signal_cb(lv_page_get_scrollable(new_tileview));
84 if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(new_tileview);
85
86 /*Initialize the allocated 'ext' */
87 #if LV_USE_ANIMATION
88 ext->anim_time = LV_TILEVIEW_DEF_ANIM_TIME;
89 #endif
90 ext->act_id.x = 0;
91 ext->act_id.y = 0;
92 ext->valid_pos = NULL;
93 ext->valid_pos_cnt = 0;
94
95 /*The signal and design functions are not copied so set them here*/
96 lv_obj_set_signal_cb(new_tileview, lv_tileview_signal);
97 lv_obj_set_signal_cb(lv_page_get_scrollable(new_tileview), lv_tileview_scrl_signal);
98
99 /*Init the new tileview*/
100 if(copy == NULL) {
101 /* Set a size which fits into the parent.
102 * Don't use `par` directly because if the tileview is created on a page it is moved to the
103 * scrollable so the parent has changed */
104 lv_coord_t w;
105 lv_coord_t h;
106 if(par) {
107 w = lv_obj_get_width_fit(lv_obj_get_parent(new_tileview));
108 h = lv_obj_get_height_fit(lv_obj_get_parent(new_tileview));
109 }
110 else {
111 w = lv_disp_get_hor_res(NULL);
112 h = lv_disp_get_ver_res(NULL);
113 }
114
115 lv_obj_set_size(new_tileview, w, h);
116 lv_obj_set_drag_throw(lv_page_get_scrollable(new_tileview), true);
117 lv_obj_set_drag_dir(lv_page_get_scrollable(new_tileview), LV_DRAG_DIR_ONE);
118
119 lv_page_set_scrollable_fit(new_tileview, LV_FIT_MAX);
120
121 lv_obj_reset_style_list(new_tileview, LV_PAGE_PART_SCROLLABLE);
122 lv_theme_apply(new_tileview, LV_THEME_TILEVIEW);
123 }
124 /*Copy an existing tileview*/
125 else {
126 lv_tileview_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
127 ext->act_id.x = copy_ext->act_id.x;
128 ext->act_id.y = copy_ext->act_id.y;
129 ext->valid_pos = copy_ext->valid_pos;
130 ext->valid_pos_cnt = copy_ext->valid_pos_cnt;
131 #if LV_USE_ANIMATION
132 ext->anim_time = copy_ext->anim_time;
133 #endif
134
135 /*Refresh the style with new signal function*/
136 lv_obj_refresh_style(new_tileview, LV_OBJ_PART_ALL, LV_STYLE_PROP_ALL);
137 }
138
139 LV_LOG_INFO("tileview created");
140
141 return new_tileview;
142 }
143
144 /*======================
145 * Add/remove functions
146 *=====================*/
147
148 /**
149 * Register an object on the tileview. The register object will able to slide the tileview
150 * @param tileview pointer to a Tileview object
151 * @param element pointer to an object
152 */
lv_tileview_add_element(lv_obj_t * tileview,lv_obj_t * element)153 void lv_tileview_add_element(lv_obj_t * tileview, lv_obj_t * element)
154 {
155 LV_UNUSED(tileview);
156 LV_ASSERT_OBJ(tileview, LV_OBJX_NAME);
157 LV_ASSERT_NULL(tileview);
158
159 lv_page_glue_obj(element, true);
160 }
161
162 /*=====================
163 * Setter functions
164 *====================*/
165
166 /**
167 * Set the valid position's indices. The scrolling will be possible only to these positions.
168 * @param tileview pointer to a Tileview object
169 * @param valid_pos array width the indices. E.g. `lv_point_t p[] = {{0,0}, {1,0}, {1,1}`.
170 * Only the pointer is saved so can't be a local variable.
171 * @param valid_pos_cnt number of elements in `valid_pos` array
172 */
lv_tileview_set_valid_positions(lv_obj_t * tileview,const lv_point_t valid_pos[],uint16_t valid_pos_cnt)173 void lv_tileview_set_valid_positions(lv_obj_t * tileview, const lv_point_t valid_pos[], uint16_t valid_pos_cnt)
174 {
175 LV_ASSERT_OBJ(tileview, LV_OBJX_NAME);
176 LV_ASSERT_NULL(valid_pos);
177
178 lv_tileview_ext_t * ext = lv_obj_get_ext_attr(tileview);
179 ext->valid_pos = valid_pos;
180 ext->valid_pos_cnt = valid_pos_cnt;
181
182 set_valid_drag_dirs(tileview);
183
184 /*If valid pos. is selected do nothing*/
185 uint16_t i;
186 for(i = 0; i < valid_pos_cnt; i++) {
187 if(valid_pos[i].x == ext->act_id.x && valid_pos[i].y == ext->act_id.y) {
188 return;
189 }
190 }
191
192 /*Set a valid position if now an invalid is selected*/
193 if(valid_pos_cnt > 0) {
194 lv_tileview_set_tile_act(tileview, valid_pos[0].x, valid_pos[0].y, LV_ANIM_OFF);
195 }
196 }
197
198 /**
199 * Set the tile to be shown
200 * @param tileview pointer to a tileview object
201 * @param x column id (0, 1, 2...)
202 * @param y line id (0, 1, 2...)
203 * @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately
204 */
lv_tileview_set_tile_act(lv_obj_t * tileview,lv_coord_t x,lv_coord_t y,lv_anim_enable_t anim)205 void lv_tileview_set_tile_act(lv_obj_t * tileview, lv_coord_t x, lv_coord_t y, lv_anim_enable_t anim)
206 {
207 LV_ASSERT_OBJ(tileview, LV_OBJX_NAME);
208
209 #if LV_USE_ANIMATION == 0
210 anim = LV_ANIM_OFF;
211 #endif
212
213 lv_tileview_ext_t * ext = lv_obj_get_ext_attr(tileview);
214
215 uint32_t tile_id;
216 bool valid = false;
217 for(tile_id = 0; tile_id < ext->valid_pos_cnt; tile_id++) {
218 if(ext->valid_pos[tile_id].x == x && ext->valid_pos[tile_id].y == y) {
219 valid = true;
220 break;
221 }
222 }
223
224 if(valid == false) return; /*Don't load not valid tiles*/
225
226 ext->act_id.x = x;
227 ext->act_id.y = y;
228
229 lv_coord_t x_coord = -x * lv_obj_get_width(tileview);
230 lv_coord_t y_coord = -y * lv_obj_get_height(tileview);
231 lv_obj_t * scrl = lv_page_get_scrollable(tileview);
232 if(anim) {
233 #if LV_USE_ANIMATION
234 lv_coord_t x_act = lv_obj_get_x(scrl);
235 lv_coord_t y_act = lv_obj_get_y(scrl);
236
237 lv_anim_t a;
238 lv_anim_init(&a);
239 lv_anim_set_var(&a, scrl);
240 lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_x);
241 lv_anim_set_time(&a, ext->anim_time);
242
243
244 if(x_coord != x_act) {
245 lv_anim_set_values(&a, x_act, x_coord);
246 lv_anim_start(&a);
247 }
248
249 if(y_coord != y_act) {
250 lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_y);
251 lv_anim_set_values(&a, y_act, y_coord);
252 lv_anim_start(&a);
253 }
254 #endif
255 }
256 else {
257 lv_obj_set_pos(scrl, x_coord, y_coord);
258 }
259
260 lv_res_t res;
261 res = lv_event_send(tileview, LV_EVENT_VALUE_CHANGED, &tile_id);
262 if(res != LV_RES_OK) return; /*Prevent the tile loading*/
263
264 set_valid_drag_dirs(tileview);
265 }
266
267 /*=====================
268 * Getter functions
269 *====================*/
270
271 /*
272 * New object specific "get" functions come here
273 */
274 /**
275 * Get the tile to be shown
276 * @param tileview pointer to a tileview object
277 * @param x column id (0, 1, 2...)
278 * @param y line id (0, 1, 2...)
279 */
lv_tileview_get_tile_act(lv_obj_t * tileview,lv_coord_t * x,lv_coord_t * y)280 void lv_tileview_get_tile_act(lv_obj_t * tileview, lv_coord_t * x, lv_coord_t * y)
281 {
282 lv_tileview_ext_t * ext = lv_obj_get_ext_attr(tileview);
283
284 *x = ext->act_id.x;
285 *y = ext->act_id.y;
286 }
287
288 /*=====================
289 * Other functions
290 *====================*/
291
292 /*
293 * New object specific "other" functions come here
294 */
295
296 /**********************
297 * STATIC FUNCTIONS
298 **********************/
299
300 /**
301 * Signal function of the tileview
302 * @param tileview pointer to a tileview object
303 * @param sign a signal type from lv_signal_t enum
304 * @param param pointer to a signal specific variable
305 * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
306 */
lv_tileview_signal(lv_obj_t * tileview,lv_signal_t sign,void * param)307 static lv_res_t lv_tileview_signal(lv_obj_t * tileview, lv_signal_t sign, void * param)
308 {
309 lv_res_t res;
310
311 /* Include the ancient signal function */
312 res = ancestor_signal(tileview, sign, param);
313 if(res != LV_RES_OK) return res;
314 if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, LV_OBJX_NAME);
315
316 if(sign == LV_SIGNAL_CLEANUP) {
317 /*Nothing to cleanup. (No dynamically allocated memory in 'ext')*/
318 }
319
320 return res;
321 }
322
323 /**
324 * Signal function of the tileview scrollable
325 * @param tileview pointer to the scrollable part of the tileview object
326 * @param sign a signal type from lv_signal_t enum
327 * @param param pointer to a signal specific variable
328 * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
329 */
lv_tileview_scrl_signal(lv_obj_t * scrl,lv_signal_t sign,void * param)330 static lv_res_t lv_tileview_scrl_signal(lv_obj_t * scrl, lv_signal_t sign, void * param)
331 {
332
333 lv_res_t res;
334
335 /* Include the ancient signal function */
336 res = ancestor_scrl_signal(scrl, sign, param);
337 if(res != LV_RES_OK) return res;
338 if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, "");
339
340 lv_obj_t * tileview = lv_obj_get_parent(scrl);
341
342 if(sign == LV_SIGNAL_DRAG_BEGIN) {
343 set_valid_drag_dirs(tileview);
344 }
345 else if(sign == LV_SIGNAL_DRAG_THROW_BEGIN) {
346 drag_end_handler(tileview);
347
348 res = lv_indev_finish_drag(lv_indev_get_act());
349 if(res != LV_RES_OK) return res;
350 }
351 /*Apply constraint on moving of the tileview*/
352 else if(sign == LV_SIGNAL_COORD_CHG) {
353 lv_indev_t * indev = lv_indev_get_act();
354 if(indev) {
355 lv_tileview_ext_t * ext = lv_obj_get_ext_attr(tileview);
356
357 lv_coord_t x = lv_obj_get_x(scrl);
358 lv_coord_t y = lv_obj_get_y(scrl);
359 lv_coord_t h = lv_obj_get_height(tileview);
360 lv_coord_t w = lv_obj_get_width(tileview);
361 lv_coord_t top = lv_obj_get_style_pad_top(tileview, LV_TILEVIEW_PART_BG);
362 lv_coord_t left = lv_obj_get_style_pad_left(tileview, LV_TILEVIEW_PART_BG);
363 if(!ext->drag_top_en && y > -(ext->act_id.y * h) && indev->proc.types.pointer.vect.y > 0) {
364 lv_page_start_edge_flash(tileview, LV_PAGE_EDGE_TOP);
365 lv_obj_set_y(scrl, -ext->act_id.y * h + top);
366 }
367 if(!ext->drag_bottom_en && indev->proc.types.pointer.vect.y < 0 && y < -(ext->act_id.y * h)) {
368 lv_page_start_edge_flash(tileview, LV_PAGE_EDGE_BOTTOM);
369 lv_obj_set_y(scrl, -ext->act_id.y * h + top);
370 }
371
372 if(!ext->drag_left_en && x > -(ext->act_id.x * w) && indev->proc.types.pointer.vect.x > 0) {
373 lv_page_start_edge_flash(tileview, LV_PAGE_EDGE_LEFT);
374 lv_obj_set_x(scrl, -ext->act_id.x * w + left);
375 }
376
377 if(!ext->drag_right_en && indev->proc.types.pointer.vect.x < 0 && x < -(ext->act_id.x * w)) {
378 lv_page_start_edge_flash(tileview, LV_PAGE_EDGE_RIGHT);
379 lv_obj_set_x(scrl, -ext->act_id.x * w + left);
380 }
381
382 /*Apply the drag constraints*/
383 lv_drag_dir_t drag_dir = indev->proc.types.pointer.drag_dir;
384 if(drag_dir == LV_DRAG_DIR_HOR)
385 lv_obj_set_y(scrl, -ext->act_id.y * lv_obj_get_height(tileview) + top);
386 else if(drag_dir == LV_DRAG_DIR_VER)
387 lv_obj_set_x(scrl, -ext->act_id.x * lv_obj_get_width(tileview) + left);
388 }
389 }
390 return res;
391 }
392
393 /**
394 * Called when the user releases an element of the tileview after dragging it.
395 * @param tileview pointer to a tileview object
396 */
drag_end_handler(lv_obj_t * tileview)397 static void drag_end_handler(lv_obj_t * tileview)
398 {
399 lv_tileview_ext_t * ext = lv_obj_get_ext_attr(tileview);
400 lv_indev_t * indev = lv_indev_get_act();
401 lv_point_t point_act;
402 lv_indev_get_point(indev, &point_act);
403 lv_obj_t * scrl = lv_page_get_scrollable(tileview);
404 lv_point_t p;
405
406 p.x = -(lv_obj_get_x(scrl) - lv_obj_get_width(tileview) / 2);
407 p.y = -(lv_obj_get_y(scrl) - lv_obj_get_height(tileview) / 2);
408
409 lv_drag_dir_t drag_dir = indev->proc.types.pointer.drag_dir;
410 /*From the drag vector (drag throw) predict the end position*/
411 if(drag_dir & LV_DRAG_DIR_HOR) {
412 lv_point_t vect;
413 lv_indev_get_vect(indev, &vect);
414 lv_coord_t predict = 0;
415
416 while(vect.x != 0) {
417 predict += vect.x;
418 vect.x = vect.x * (100 - LV_INDEV_DEF_DRAG_THROW) / 100;
419 }
420
421 p.x -= predict;
422
423 }
424 else if(drag_dir & LV_DRAG_DIR_VER) {
425 lv_point_t vect;
426 lv_indev_get_vect(indev, &vect);
427 lv_coord_t predict = 0;
428
429 while(vect.y != 0) {
430 predict += vect.y;
431 vect.y = vect.y * (100 - LV_INDEV_DEF_DRAG_THROW) / 100;
432 }
433
434 p.y -= predict;
435 }
436
437 /*Get the index of the tile*/
438 p.x = p.x / lv_obj_get_width(tileview);
439 p.y = p.y / lv_obj_get_height(tileview);
440
441 /*Max +- move*/
442 lv_coord_t x_move = p.x - ext->act_id.x;
443 lv_coord_t y_move = p.y - ext->act_id.y;
444 if(x_move < -1) x_move = -1;
445 if(x_move > 1) x_move = 1;
446 if(y_move < -1) y_move = -1;
447 if(y_move > 1) y_move = 1;
448
449 /*Set the new tile*/
450 lv_tileview_set_tile_act(tileview, ext->act_id.x + x_move, ext->act_id.y + y_move, true);
451 }
452
set_valid_drag_dirs(lv_obj_t * tileview)453 static bool set_valid_drag_dirs(lv_obj_t * tileview)
454 {
455
456 lv_tileview_ext_t * ext = lv_obj_get_ext_attr(tileview);
457 if(ext->valid_pos == NULL) return false;
458
459 ext->drag_bottom_en = 0;
460 ext->drag_top_en = 0;
461 ext->drag_left_en = 0;
462 ext->drag_right_en = 0;
463
464 uint16_t i;
465 for(i = 0; i < ext->valid_pos_cnt; i++) {
466 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;
467 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;
468 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;
469 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;
470 }
471
472 return true;
473 }
474
475 #endif
476