1 /**
2  * @file lv_tileview.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "lv_tileview_private.h"
10 #include "../../core/lv_obj_class_private.h"
11 #include "../../indev/lv_indev.h"
12 #include "../../indev/lv_indev_private.h"
13 #if LV_USE_TILEVIEW
14 
15 /*********************
16  *      DEFINES
17  *********************/
18 
19 /**********************
20  *      TYPEDEFS
21  **********************/
22 
23 /**********************
24  *  STATIC PROTOTYPES
25  **********************/
26 static void lv_tileview_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
27 static void lv_tileview_tile_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
28 static void tileview_event_cb(lv_event_t * e);
29 
30 /**********************
31  *  STATIC VARIABLES
32  **********************/
33 
34 const lv_obj_class_t lv_tileview_class = {
35     .constructor_cb = lv_tileview_constructor,
36     .base_class = &lv_obj_class,
37     .instance_size = sizeof(lv_tileview_t),
38     .name = "tileview",
39 };
40 
41 const lv_obj_class_t lv_tileview_tile_class = {
42     .constructor_cb = lv_tileview_tile_constructor,
43     .base_class = &lv_obj_class,
44     .instance_size = sizeof(lv_tileview_tile_t),
45     .name = "tile",
46 };
47 
48 /**********************
49  *      MACROS
50  **********************/
51 
52 /**********************
53  *   GLOBAL FUNCTIONS
54  **********************/
55 
lv_tileview_create(lv_obj_t * parent)56 lv_obj_t * lv_tileview_create(lv_obj_t * parent)
57 {
58     LV_LOG_INFO("begin");
59     lv_obj_t * obj = lv_obj_class_create_obj(&lv_tileview_class, parent);
60     lv_obj_class_init_obj(obj);
61     return obj;
62 }
63 
64 /*======================
65  * Add/remove functions
66  *=====================*/
67 
lv_tileview_add_tile(lv_obj_t * tv,uint8_t col_id,uint8_t row_id,lv_dir_t dir)68 lv_obj_t * lv_tileview_add_tile(lv_obj_t * tv, uint8_t col_id, uint8_t row_id, lv_dir_t dir)
69 {
70     LV_LOG_INFO("begin");
71 
72     lv_obj_t * obj = lv_obj_class_create_obj(&lv_tileview_tile_class, tv);
73     lv_obj_class_init_obj(obj);
74     lv_obj_set_pos(obj, lv_pct(col_id * 100), lv_pct(row_id * 100));
75 
76     lv_tileview_tile_t * tile = (lv_tileview_tile_t *)obj;
77     tile->dir = dir;
78 
79     if(col_id == 0 && row_id == 0) {
80         lv_obj_set_scroll_dir(tv, dir);
81     }
82     return obj;
83 }
84 
lv_tileview_set_tile(lv_obj_t * obj,lv_obj_t * tile_obj,lv_anim_enable_t anim_en)85 void lv_tileview_set_tile(lv_obj_t * obj, lv_obj_t * tile_obj, lv_anim_enable_t anim_en)
86 {
87     int32_t tx = lv_obj_get_x(tile_obj);
88     int32_t ty = lv_obj_get_y(tile_obj);
89 
90     lv_tileview_tile_t * tile = (lv_tileview_tile_t *)tile_obj;
91     lv_tileview_t * tv = (lv_tileview_t *) obj;
92     tv->tile_act = (lv_obj_t *)tile;
93 
94     lv_obj_set_scroll_dir(obj, tile->dir);
95     lv_obj_scroll_to(obj, tx, ty, anim_en);
96 }
97 
lv_tileview_set_tile_by_index(lv_obj_t * tv,uint32_t col_id,uint32_t row_id,lv_anim_enable_t anim_en)98 void lv_tileview_set_tile_by_index(lv_obj_t * tv, uint32_t col_id, uint32_t row_id, lv_anim_enable_t anim_en)
99 {
100     lv_obj_update_layout(tv);
101 
102     int32_t w = lv_obj_get_content_width(tv);
103     int32_t h = lv_obj_get_content_height(tv);
104 
105     int32_t tx = col_id * w;
106     int32_t ty = row_id * h;
107 
108     uint32_t i;
109     for(i = 0; i < lv_obj_get_child_count(tv); i++) {
110         lv_obj_t * tile_obj = lv_obj_get_child(tv, i);
111         int32_t x = lv_obj_get_x(tile_obj);
112         int32_t y = lv_obj_get_y(tile_obj);
113         if(x == tx && y == ty) {
114             lv_tileview_set_tile(tv, tile_obj, anim_en);
115             return;
116         }
117     }
118 
119     LV_LOG_WARN("No tile found with at (%d,%d) index", (int)col_id, (int)row_id);
120 }
121 
lv_tileview_get_tile_active(lv_obj_t * obj)122 lv_obj_t * lv_tileview_get_tile_active(lv_obj_t * obj)
123 {
124     lv_tileview_t * tv = (lv_tileview_t *) obj;
125     return tv->tile_act;
126 }
127 
128 /**********************
129  *   STATIC FUNCTIONS
130  **********************/
131 
lv_tileview_constructor(const lv_obj_class_t * class_p,lv_obj_t * obj)132 static void lv_tileview_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
133 {
134     LV_UNUSED(class_p);
135     lv_obj_set_size(obj, LV_PCT(100), LV_PCT(100));
136     lv_obj_add_event_cb(obj, tileview_event_cb, LV_EVENT_ALL, NULL);
137     lv_obj_add_flag(obj, LV_OBJ_FLAG_SCROLL_ONE);
138     lv_obj_set_scroll_snap_x(obj, LV_SCROLL_SNAP_CENTER);
139     lv_obj_set_scroll_snap_y(obj, LV_SCROLL_SNAP_CENTER);
140 
141 }
142 
lv_tileview_tile_constructor(const lv_obj_class_t * class_p,lv_obj_t * obj)143 static void lv_tileview_tile_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
144 {
145 
146     LV_UNUSED(class_p);
147     lv_obj_set_size(obj, LV_PCT(100), LV_PCT(100));
148     lv_obj_update_layout(obj);  /*Be sure the size is correct*/
149 }
150 
tileview_event_cb(lv_event_t * e)151 static void tileview_event_cb(lv_event_t * e)
152 {
153     lv_event_code_t code = lv_event_get_code(e);
154     lv_obj_t * obj = lv_event_get_current_target(e);
155     lv_tileview_t * tv = (lv_tileview_t *) obj;
156 
157     if(code == LV_EVENT_SCROLL_END) {
158         lv_indev_t * indev = lv_indev_active();
159         if(indev && indev->state == LV_INDEV_STATE_PRESSED) {
160             return;
161         }
162 
163         int32_t w = lv_obj_get_content_width(obj);
164         int32_t h = lv_obj_get_content_height(obj);
165 
166         lv_point_t scroll_end;
167         lv_obj_get_scroll_end(obj, &scroll_end);
168         int32_t left = scroll_end.x;
169         int32_t top = scroll_end.y;
170 
171         int32_t tx = ((left + (w / 2)) / w) * w;
172         int32_t ty = ((top + (h / 2)) / h) * h;
173 
174         lv_dir_t dir = LV_DIR_ALL;
175         uint32_t i;
176         for(i = 0; i < lv_obj_get_child_count(obj); i++) {
177             lv_obj_t * tile_obj = lv_obj_get_child(obj, i);
178             int32_t x = lv_obj_get_x(tile_obj);
179             int32_t y = lv_obj_get_y(tile_obj);
180             if(x == tx && y == ty) {
181                 lv_tileview_tile_t * tile = (lv_tileview_tile_t *)tile_obj;
182                 tv->tile_act = (lv_obj_t *)tile;
183                 dir = tile->dir;
184                 lv_obj_send_event(obj, LV_EVENT_VALUE_CHANGED, NULL);
185                 break;
186             }
187         }
188         lv_obj_set_scroll_dir(obj, dir);
189     }
190 }
191 #endif /*LV_USE_TILEVIEW*/
192