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