ob_cardview.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. /**
  2. * @file ob_card.c
  3. *
  4. */
  5. /*********************
  6. * INCLUDES
  7. *********************/
  8. #include "lv_conf.h"
  9. #include "config.h"
  10. #if USE_OB_CARDVIEW != 0
  11. #include "ob_cardview.h"
  12. #include "lvgl/lvgl.h"
  13. /*********************
  14. * DEFINES
  15. *********************/
  16. #if USE_LV_ANIMATION
  17. # ifndef OB_CARDVIEW_ANIM_TIME
  18. # define OB_CARDVIEW_ANIM_TIME 300 /*Animation time of focusing to the a list element [ms] (0: no animation) */
  19. # endif
  20. #else
  21. # undef OB_CARDVIEW_ANIM_TIME
  22. # define OB_CARDVIEW_ANIM_TIME 0 /*No animations*/
  23. #endif
  24. /**********************
  25. * TYPEDEFS
  26. **********************/
  27. /**********************
  28. * STATIC PROTOTYPES
  29. **********************/
  30. static bool ob_cardview_design(lv_obj_t * cardview, const lv_area_t * mask, lv_design_mode_t mode);
  31. static lv_res_t ob_cardview_signal(lv_obj_t * cardview, lv_signal_t sign, void * param);
  32. static lv_res_t cardpage_signal(lv_obj_t * card_page, lv_signal_t sign, void * param);
  33. static lv_res_t cardpage_scrl_signal(lv_obj_t * card_scrl, lv_signal_t sign, void * param);
  34. static void cardpage_pressed_hadler(lv_obj_t * cardview, lv_obj_t * cardpage);
  35. static void cardpage_pressing_hadler(lv_obj_t * cardview, lv_obj_t * cardpage);
  36. static void cardpage_press_lost_hadler(lv_obj_t * cardview, lv_obj_t * cardpage);
  37. static void cardview_realign(lv_obj_t * cardview);
  38. /**********************
  39. * STATIC VARIABLES
  40. **********************/
  41. static lv_design_func_t ancestor_design;
  42. static lv_signal_func_t ancestor_signal;
  43. static lv_signal_func_t page_signal;
  44. static lv_signal_func_t page_scrl_signal;
  45. /**********************
  46. * MACROS
  47. **********************/
  48. /**********************
  49. * GLOBAL FUNCTIONS
  50. **********************/
  51. /**
  52. * Create a Tab view object
  53. * @param par pointer to an object, it will be the parent of the new card
  54. * @param copy pointer to a card object, if not NULL then the new object will be copied from it
  55. * @return pointer to the created card
  56. */
  57. lv_obj_t * ob_cardview_create(lv_obj_t * par, lv_obj_t * copy)
  58. {
  59. /*Create the ancestor of card*/
  60. lv_obj_t * new_cardview = lv_obj_create(par, copy);
  61. lv_mem_assert(new_cardview);
  62. if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_func(new_cardview);
  63. if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_func(new_cardview);
  64. /*Allocate the card type specific extended data*/
  65. ob_cardview_ext_t * ext = lv_obj_allocate_ext_attr(new_cardview, sizeof(ob_cardview_ext_t));
  66. lv_mem_assert(ext);
  67. /*Initialize the allocated 'ext' */
  68. ext->drag_hor = 0;
  69. ext->draging = 0;
  70. ext->slide_enable = 1;
  71. ext->card_cur = 0;
  72. ext->point_last.x = 0;
  73. ext->point_last.y = 0;
  74. ext->content = NULL;
  75. ext->card_load_action = NULL;
  76. ext->anim_time = OB_CARDVIEW_ANIM_TIME;
  77. ext->card_cnt = 0;
  78. ext->bullet_size_act = 8;
  79. ext->bullet_size_ina = 5;
  80. ext->style_bullet_act = &lv_style_pretty_color;
  81. ext->style_bullet_ina = &lv_style_pretty;
  82. /*The signal and design functions are not copied so set them here*/
  83. lv_obj_set_signal_func(new_cardview, ob_cardview_signal);
  84. lv_obj_set_design_func(new_cardview, ob_cardview_design);
  85. /*Init the new card*/
  86. if(copy == NULL) {
  87. lv_obj_set_size(new_cardview, LV_HOR_RES, LV_VER_RES);
  88. ext->content = lv_cont_create(new_cardview, NULL);
  89. lv_cont_set_fit(ext->content, true, false);
  90. lv_cont_set_layout(ext->content, LV_LAYOUT_ROW_T);
  91. lv_cont_set_style(ext->content, &lv_style_transp_tight);
  92. lv_obj_set_height(ext->content, LV_VER_RES);
  93. /*Set the default styles*/
  94. lv_theme_t *th = lv_theme_get_current();
  95. if(th) {
  96. ob_cardview_set_style(new_cardview, OB_CARDVIEW_STYLE_BG, th->cardview.bg);
  97. ob_cardview_set_style(new_cardview, OB_CARDVIEW_STYLE_BULLET_ACT, th->cardview.bullet.act);
  98. ob_cardview_set_style(new_cardview, OB_CARDVIEW_STYLE_BULLET_INA, th->cardview.bullet.ina);
  99. } else {
  100. ob_cardview_set_style(new_cardview, OB_CARDVIEW_STYLE_BG, &lv_style_plain);
  101. }
  102. }
  103. /*Copy an existing card view*/
  104. else {
  105. }
  106. return new_cardview;
  107. }
  108. /*======================
  109. * Add/remove functions
  110. *=====================*/
  111. /**
  112. * Add a card to the end
  113. */
  114. lv_obj_t * ob_cardview_append_card(lv_obj_t * cardview)
  115. {
  116. ob_cardview_ext_t * ext = lv_obj_get_ext_attr(cardview);
  117. /*Create the container page*/
  118. lv_obj_t * h_new = lv_page_create(ext->content, NULL);
  119. lv_obj_set_size(h_new, lv_obj_get_width(cardview), lv_obj_get_height(ext->content));
  120. //lv_page_set_sb_mode(h, LV_SB_MODE_AUTO); // onion.io: disabled scrollbars
  121. lv_page_set_sb_mode(h_new, LV_SB_MODE_OFF);
  122. lv_page_set_style(h_new, LV_PAGE_STYLE_BG, &lv_style_transp_fit);
  123. lv_page_set_style(h_new, LV_PAGE_STYLE_SCRL, &lv_style_transp_fit);
  124. if(page_signal == NULL) page_signal = lv_obj_get_signal_func(h_new);
  125. if(page_scrl_signal == NULL) page_scrl_signal = lv_obj_get_signal_func(lv_page_get_scrl(h_new));
  126. lv_obj_set_signal_func(h_new, cardpage_signal);
  127. lv_obj_set_signal_func(lv_page_get_scrl(h_new), cardpage_scrl_signal);
  128. ext->card_cnt++;
  129. /*Set the first card as active*/
  130. if(ext->card_cnt == 1) {
  131. ext->card_cur = 0;
  132. ob_cardview_set_card_act(cardview, 0, false);
  133. cardview_realign(cardview); /*To set the proper btns height*/
  134. }
  135. return h_new;
  136. }
  137. /**
  138. * Add a new card with the given name
  139. * @param cardview pointer to Tab view object where to ass the new card
  140. * @return pointer to the created page object (lv_page). You can create your content here
  141. */
  142. lv_obj_t * ob_cardview_add_card(lv_obj_t * cardview, int pos)
  143. {
  144. ob_cardview_ext_t * ext = lv_obj_get_ext_attr(cardview);
  145. /*Move all existing objects with `id < pos` to a temporary parent (required to handle `pos`)*/
  146. lv_obj_t * h = lv_obj_get_child_back(ext->content, NULL);
  147. int id;
  148. lv_obj_t * h_next;
  149. for (id = 0; h != NULL && id < pos; id++) {
  150. h_next = lv_obj_get_child_back(ext->content, h);
  151. lv_obj_set_parent(h, cardview);
  152. h = h_next;
  153. }
  154. /*Create the container page*/
  155. lv_obj_t * h_new = lv_page_create(cardview, NULL);
  156. lv_obj_set_size(h_new, lv_obj_get_width(cardview), lv_obj_get_height(ext->content));
  157. //lv_page_set_sb_mode(h, LV_SB_MODE_AUTO); // onion.io: disabled scrollbars
  158. lv_page_set_sb_mode(h_new, LV_SB_MODE_OFF);
  159. lv_page_set_style(h_new, LV_PAGE_STYLE_BG, &lv_style_transp_fit);
  160. lv_page_set_style(h_new, LV_PAGE_STYLE_SCRL, &lv_style_transp_fit);
  161. if(page_signal == NULL) page_signal = lv_obj_get_signal_func(h_new);
  162. if(page_scrl_signal == NULL) page_scrl_signal = lv_obj_get_signal_func(lv_page_get_scrl(h_new));
  163. lv_obj_set_signal_func(h_new, cardpage_signal);
  164. lv_obj_set_signal_func(lv_page_get_scrl(h_new), cardpage_scrl_signal);
  165. /*Move the remaining cards to the temp. parent too.*/
  166. for (; h != NULL; id++) {
  167. h_next = lv_obj_get_child_back(ext->content, h);
  168. lv_obj_set_parent(h, cardview);
  169. h = h_next;
  170. }
  171. /*Move back all cards now in the correct order*/
  172. h = lv_obj_get_child_back(cardview, NULL);
  173. while(h != NULL) {
  174. h_next = lv_obj_get_child_back(cardview, h);
  175. if(h != ext->content) lv_obj_set_parent(h, ext->content);
  176. h = h_next;
  177. }
  178. ext->card_cnt++;
  179. /*Set the first card as active*/
  180. if(ext->card_cnt == 1) {
  181. ext->card_cur = 0;
  182. ob_cardview_set_card_act(cardview, 0, false);
  183. cardview_realign(cardview); /*To set the proper btns height*/
  184. }
  185. return h_new;
  186. }
  187. /**
  188. * Delete a card from the card view
  189. */
  190. void ob_cardview_delete_card(lv_obj_t * cardview, lv_obj_t * card)
  191. {
  192. //printf("ob_cardview_delete_card");
  193. char * cardId = lv_obj_get_free_ptr(card);
  194. free(cardId);
  195. lv_obj_del(card);
  196. ob_cardview_ext_t * ext = lv_obj_get_ext_attr(cardview);
  197. ext->card_cnt--;
  198. }
  199. /*=====================
  200. * Setter functions
  201. *====================*/
  202. /**
  203. * Set a new card
  204. * @param cardview pointer to Tab view object
  205. * @param id index of a card to load
  206. * @param anim_en true: set with sliding animation; false: set immediately
  207. */
  208. void ob_cardview_set_card_act(lv_obj_t * cardview, uint16_t id, bool anim_en)
  209. {
  210. ob_cardview_ext_t * ext = lv_obj_get_ext_attr(cardview);
  211. lv_style_t * style = lv_obj_get_style(ext->content);
  212. if(id >= ext->card_cnt) id = ext->card_cnt - 1;
  213. if(ext->card_load_action) ext->card_load_action(cardview, id);
  214. ext->card_cur = id;
  215. lv_coord_t cont_x = -(lv_obj_get_width(cardview) * id + style->body.padding.inner * id + style->body.padding.hor);
  216. if(ext->anim_time == 0 || anim_en == false) {
  217. lv_obj_set_x(ext->content, cont_x);
  218. } else {
  219. #if USE_LV_ANIMATION
  220. lv_anim_t a;
  221. a.var = ext->content;
  222. a.start = lv_obj_get_x(ext->content);
  223. a.end = cont_x;
  224. a.fp = (lv_anim_fp_t)lv_obj_set_x;
  225. a.path = lv_anim_path_linear;
  226. a.end_cb = NULL;
  227. a.act_time = 0;
  228. a.time = ext->anim_time;
  229. a.playback = 0;
  230. a.playback_pause = 0;
  231. a.repeat = 0;
  232. a.repeat_pause = 0;
  233. lv_anim_create(&a);
  234. #endif
  235. }
  236. }
  237. /**
  238. * Set an action to call when a card is loaded (Good to create content only if required)
  239. * ob_cardview_get_act() still gives the current (old) card (to remove content from here)
  240. * @param cardview pointer to a cardview object
  241. * @param action pointer to a function to call when a btn is loaded
  242. */
  243. void ob_cardview_set_card_load_action(lv_obj_t *cardview, ob_cardview_action_t action)
  244. {
  245. ob_cardview_ext_t * ext = lv_obj_get_ext_attr(cardview);
  246. ext->card_load_action = action;
  247. }
  248. /**
  249. * Enable horizontal sliding with touch pad
  250. * @param cardview pointer to Tab view object
  251. * @param en true: enable sliding; false: disable sliding
  252. */
  253. void ob_cardview_set_sliding(lv_obj_t * cardview, bool en)
  254. {
  255. ob_cardview_ext_t * ext = lv_obj_get_ext_attr(cardview);
  256. ext->slide_enable = en == false ? 0 : 1;
  257. }
  258. /**
  259. * Set the animation time of card view when a new card is loaded
  260. * @param cardview pointer to Tab view object
  261. * @param anim_time_ms time of animation in milliseconds
  262. */
  263. void ob_cardview_set_anim_time(lv_obj_t * cardview, uint16_t anim_time)
  264. {
  265. ob_cardview_ext_t * ext = lv_obj_get_ext_attr(cardview);
  266. ext->anim_time = anim_time;
  267. }
  268. /**
  269. * Set the size of the bullets which indicated the pages
  270. * @param cardview pointer to Card view object
  271. * @param size_act size of the active bullet
  272. * @param size_ina size of the inactive bullet
  273. */
  274. void ob_cardview_set_bullet_size(lv_obj_t * cardview, lv_coord_t size_act, lv_coord_t size_ina)
  275. {
  276. ob_cardview_ext_t * ext = lv_obj_get_ext_attr(cardview);
  277. ext->bullet_size_act = size_act;
  278. ext->bullet_size_ina = size_ina;
  279. lv_obj_invalidate(cardview);
  280. }
  281. /**
  282. * Set the style of a card view
  283. * @param cardview pointer to a tan view object
  284. * @param type which style should be set
  285. * @param style pointer to the new style
  286. */
  287. void ob_cardview_set_style(lv_obj_t *cardview, ob_cardview_style_t type, lv_style_t *style)
  288. {
  289. ob_cardview_ext_t *ext = lv_obj_get_ext_attr(cardview);
  290. switch(type) {
  291. case OB_CARDVIEW_STYLE_BG:
  292. lv_obj_set_style(cardview, style);
  293. break;
  294. case OB_CARDVIEW_STYLE_BULLET_ACT:
  295. ext->style_bullet_act = style;
  296. lv_obj_invalidate(cardview);
  297. break;
  298. case OB_CARDVIEW_STYLE_BULLET_INA:
  299. ext->style_bullet_ina = style;
  300. lv_obj_invalidate(cardview);
  301. break;
  302. }
  303. }
  304. /*=====================
  305. * Getter functions
  306. *====================*/
  307. /**
  308. * Get the index of the currently active card
  309. * @param cardview pointer to Tab view object
  310. * @return the active btn index
  311. */
  312. uint16_t ob_cardview_get_card_act(lv_obj_t * cardview)
  313. {
  314. ob_cardview_ext_t * ext = lv_obj_get_ext_attr(cardview);
  315. return ext->card_cur;
  316. }
  317. /**
  318. * Get the free_num index of the currently active card
  319. * @param cardview pointer to Card view object
  320. * @return the active card free_num index
  321. */
  322. uint16_t ob_cardview_get_card_act_fn(lv_obj_t * cardview)
  323. {
  324. ob_cardview_ext_t * ext = lv_obj_get_ext_attr(cardview);
  325. uint16_t activeCardId = ext->card_cur;
  326. uint16_t i = 0;
  327. lv_obj_t * page = lv_obj_get_child_back(ext->content, NULL);
  328. while(page != NULL && i != activeCardId) {
  329. i++;
  330. page = lv_obj_get_child_back(ext->content, page);
  331. }
  332. if(i == activeCardId && page != NULL) return lv_obj_get_free_num(page);
  333. return 0;
  334. }
  335. /**
  336. * Get the number of cards
  337. * @param cardview pointer to Tab view object
  338. * @return btn count
  339. */
  340. uint16_t ob_cardview_get_card_count(lv_obj_t * cardview)
  341. {
  342. ob_cardview_ext_t * ext = lv_obj_get_ext_attr(cardview);
  343. return ext->card_cnt;
  344. }
  345. /**
  346. * Get the page (content area) of a card
  347. * @param cardview pointer to Tab view object
  348. * @param id index of the card (>= 0)
  349. * @return pointer to page (lv_page) object
  350. */
  351. lv_obj_t * ob_cardview_get_card(lv_obj_t * cardview, uint16_t id)
  352. {
  353. ob_cardview_ext_t * ext = lv_obj_get_ext_attr(cardview);
  354. uint16_t i = -1;
  355. lv_obj_t * page = lv_obj_get_child_back(ext->content, NULL);
  356. // implementation where id is ordering
  357. // while(page != NULL && i != id) {
  358. // i++;
  359. // page = lv_obj_get_child_back(ext->content, page);
  360. // }
  361. // implementation where id is retrieved from the free_num
  362. while(page != NULL) {
  363. i = lv_obj_get_free_num(page);
  364. if (i == id) {
  365. // found the match
  366. break;
  367. }
  368. // grab the next card
  369. page = lv_obj_get_child_back(ext->content, page);
  370. }
  371. if(i == id) return page;
  372. return NULL;
  373. }
  374. /**
  375. * Get the page (content area) of a card by specifying a matching free_ptr string value
  376. * @param cardview pointer to Tab view object
  377. * @param searchKey string value of free_ptr of matching card
  378. * @return pointer to page (lv_page) object
  379. */
  380. lv_obj_t * ob_cardview_get_card_by_fp_string(lv_obj_t * cardview, char* searchKey)
  381. {
  382. ob_cardview_ext_t * ext = lv_obj_get_ext_attr(cardview);
  383. lv_obj_t * page = lv_obj_get_child_back(ext->content, NULL);
  384. char* pageFp = NULL;
  385. while(page != NULL) {
  386. // grab the free_ptr
  387. pageFp = lv_obj_get_free_ptr(page);
  388. if (pageFp != NULL) {
  389. if (strcmp(pageFp, searchKey) == 0) {
  390. // found the match
  391. break;
  392. }
  393. }
  394. // grab the next card
  395. page = lv_obj_get_child_back(ext->content, page);
  396. }
  397. return page;
  398. }
  399. /**
  400. * Get the card load action
  401. * @param cardview pointer to a cardview object
  402. * @param return the current btn load action
  403. */
  404. ob_cardview_action_t ob_cardview_get_card_load_action(lv_obj_t *cardview)
  405. {
  406. ob_cardview_ext_t * ext = lv_obj_get_ext_attr(cardview);
  407. return ext->card_load_action;
  408. }
  409. /**
  410. * Get horizontal sliding is enabled or not
  411. * @param cardview pointer to Tab view object
  412. * @return true: enable sliding; false: disable sliding
  413. */
  414. bool ob_cardview_get_sliding(lv_obj_t * cardview)
  415. {
  416. ob_cardview_ext_t *ext = lv_obj_get_ext_attr(cardview);
  417. return ext->slide_enable ? true : false;
  418. }
  419. /**
  420. * Get the animation time of card view when a new card is loaded
  421. * @param cardview pointer to Tab view object
  422. * @return time of animation in milliseconds
  423. */
  424. uint16_t ob_cardview_get_anim_time(lv_obj_t * cardview)
  425. {
  426. ob_cardview_ext_t * ext = lv_obj_get_ext_attr(cardview);
  427. return ext->anim_time;
  428. }
  429. /**
  430. * Get the size of the active bullet
  431. * @param cardview pointer to Card view object
  432. * @return size of the bullets (inactive bullets have half size)
  433. */
  434. lv_coord_t ob_cardview_get_bullet_size_act(lv_obj_t * cardview)
  435. {
  436. ob_cardview_ext_t * ext = lv_obj_get_ext_attr(cardview);
  437. return ext->bullet_size_act;
  438. }
  439. /**
  440. * Get the size of the active bullet
  441. * @param cardview pointer to Card view object
  442. * @return size of the bullets (inactive bullets have half size)
  443. */
  444. lv_coord_t ob_cardview_get_bullet_size_ina(lv_obj_t * cardview)
  445. {
  446. ob_cardview_ext_t * ext = lv_obj_get_ext_attr(cardview);
  447. return ext->bullet_size_ina;
  448. }
  449. /**
  450. * Get a style of a card view
  451. * @param cardview pointer to a ab view object
  452. * @param type which style should be get
  453. * @return style pointer to a style
  454. */
  455. lv_style_t * ob_cardview_get_style(lv_obj_t *cardview, ob_cardview_style_t type)
  456. {
  457. ob_cardview_ext_t *ext = lv_obj_get_ext_attr(cardview);
  458. switch (type) {
  459. case OB_CARDVIEW_STYLE_BG: return lv_obj_get_style(cardview);
  460. case OB_CARDVIEW_STYLE_BULLET_ACT: return ext->style_bullet_act;
  461. case OB_CARDVIEW_STYLE_BULLET_INA: return ext->style_bullet_ina;
  462. default: return NULL;
  463. }
  464. /*To avoid warning*/
  465. return NULL;
  466. }
  467. /**
  468. * Get highest free_num of all cards in carview
  469. * @param cardview pointer to a card view object
  470. * @param type which style should be get
  471. * @return value of highest free_num
  472. */
  473. uint16_t ob_cardview_get_max_free_num(lv_obj_t *cardview) {
  474. ob_cardview_ext_t * ext = lv_obj_get_ext_attr(cardview);
  475. uint16_t i = 0;
  476. lv_obj_t * page = lv_obj_get_child_back(ext->content, NULL);
  477. while(page != NULL) {
  478. if (lv_obj_get_free_num(page) > i) {
  479. i = lv_obj_get_free_num(page);
  480. }
  481. page = lv_obj_get_child_back(ext->content, page);
  482. }
  483. return i;
  484. }
  485. /**********************
  486. * STATIC FUNCTIONS
  487. **********************/
  488. static bool ob_cardview_design(lv_obj_t * cardview, const lv_area_t * mask, lv_design_mode_t mode)
  489. {
  490. if(mode == LV_DESIGN_COVER_CHK || mode == LV_DESIGN_DRAW_MAIN) {
  491. return ancestor_design(cardview, mask, mode);
  492. }
  493. else if (mode == LV_DESIGN_DRAW_POST) {
  494. ob_cardview_ext_t * ext = lv_obj_get_ext_attr(cardview);
  495. uint16_t card_cnt = ob_cardview_get_card_count(cardview);
  496. uint16_t card_act_id = ob_cardview_get_card_act(cardview);
  497. lv_coord_t w = lv_obj_get_width(cardview);
  498. lv_obj_t * content = ext->content;
  499. lv_coord_t h_space = ext->style_bullet_act->body.padding.hor;
  500. lv_coord_t v_space = ext->style_bullet_act->body.padding.ver;
  501. lv_coord_t bullet_area_w = h_space * (card_cnt- 1);
  502. lv_coord_t x_base = w / 2 - bullet_area_w / 2;
  503. lv_coord_t y_base;
  504. /*If v_space > 0 align from the top if < 0 align from the bottom*/
  505. if(v_space > 0) {
  506. y_base = content->coords.y1 + v_space;
  507. } else {
  508. v_space = -v_space;
  509. y_base = content->coords.y2 - v_space;
  510. }
  511. lv_area_t area;
  512. int i;
  513. for(i = 0; i < card_cnt; i++) {
  514. if(i == card_act_id) {
  515. area.y1 = y_base - ext->bullet_size_act / 2;
  516. area.y2 = area.y1 + ext->bullet_size_act;
  517. area.x1 = x_base + i * h_space - ext->bullet_size_act / 2;
  518. area.x2 = area.x1 + ext->bullet_size_act;
  519. lv_draw_rect(&area, mask, ext->style_bullet_act);
  520. } else {
  521. area.y1 = y_base - ext->bullet_size_ina / 2;
  522. area.y2 = area.y1 + ext->bullet_size_ina;
  523. area.x1 = x_base + i * h_space - ext->bullet_size_ina / 2;
  524. area.x2 = area.x1 + ext->bullet_size_ina;
  525. lv_draw_rect(&area, mask, ext->style_bullet_ina);
  526. }
  527. }
  528. }
  529. return true;
  530. }
  531. /**
  532. * Signal function of the Tab view
  533. * @param cardview pointer to a Tab view object
  534. * @param sign a signal type from lv_signal_t enum
  535. * @param param pointer to a signal specific variable
  536. * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
  537. */
  538. static lv_res_t ob_cardview_signal(lv_obj_t * cardview, lv_signal_t sign, void * param)
  539. {
  540. lv_res_t res;
  541. /* Include the ancient signal function */
  542. res = ancestor_signal(cardview, sign, param);
  543. if(res != LV_RES_OK) return res;
  544. ob_cardview_ext_t * ext = lv_obj_get_ext_attr(cardview);
  545. if(sign == LV_SIGNAL_CLEANUP) {
  546. ext->content= NULL;
  547. }
  548. else if(sign == LV_SIGNAL_CORD_CHG) {
  549. if(ext->content != NULL &&
  550. (lv_obj_get_width(cardview) != lv_area_get_width(param) ||
  551. lv_obj_get_height(cardview) != lv_area_get_height(param)))
  552. {
  553. cardview_realign(cardview);
  554. }
  555. }
  556. return res;
  557. }
  558. /**
  559. * Signal function of a card's page
  560. * @param card pointer to a card page object
  561. * @param sign a signal type from lv_signal_t enum
  562. * @param param pointer to a signal specific variable
  563. * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
  564. */
  565. static lv_res_t cardpage_signal(lv_obj_t * card_page, lv_signal_t sign, void * param)
  566. {
  567. lv_res_t res;
  568. /* Include the ancient signal function */
  569. res = page_signal(card_page, sign, param);
  570. if(res != LV_RES_OK) return res;
  571. lv_obj_t * cont = lv_obj_get_parent(card_page);
  572. lv_obj_t * cardview = lv_obj_get_parent(cont);
  573. if(ob_cardview_get_sliding(cardview) == false) return res;
  574. if(sign == LV_SIGNAL_PRESSED) {
  575. cardpage_pressed_hadler(cardview, card_page);
  576. }
  577. else if(sign == LV_SIGNAL_PRESSING) {
  578. cardpage_pressing_hadler(cardview, card_page);
  579. }
  580. else if(sign == LV_SIGNAL_RELEASED || sign == LV_SIGNAL_PRESS_LOST) {
  581. cardpage_press_lost_hadler(cardview, card_page);
  582. }
  583. return res;
  584. }
  585. /**
  586. * Signal function of the card page's scrollable object
  587. * @param card_scrl pointer to a card page's scrollable object
  588. * @param sign a signal type from lv_signal_t enum
  589. * @param param pointer to a signal specific variable
  590. * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
  591. */
  592. static lv_res_t cardpage_scrl_signal(lv_obj_t * card_scrl, lv_signal_t sign, void * param)
  593. {
  594. lv_res_t res;
  595. /* Include the ancient signal function */
  596. res = page_scrl_signal(card_scrl, sign, param);
  597. if(res != LV_RES_OK) return res;
  598. lv_obj_t * card_page = lv_obj_get_parent(card_scrl);
  599. lv_obj_t * cont = lv_obj_get_parent(card_page);
  600. lv_obj_t * cardview = lv_obj_get_parent(cont);
  601. if(ob_cardview_get_sliding(cardview) == false) return res;
  602. if(sign == LV_SIGNAL_PRESSED) {
  603. cardpage_pressed_hadler(cardview, card_page);
  604. }
  605. else if(sign == LV_SIGNAL_PRESSING) {
  606. cardpage_pressing_hadler(cardview, card_page);
  607. }
  608. else if(sign == LV_SIGNAL_RELEASED || sign == LV_SIGNAL_PRESS_LOST) {
  609. cardpage_press_lost_hadler(cardview, card_page);
  610. }
  611. return res;
  612. }
  613. /**
  614. * Called when a card's page or scrollable object is pressed
  615. * @param cardview pointer to the btn view object
  616. * @param cardpage pointer to the page of a btn
  617. */
  618. static void cardpage_pressed_hadler(lv_obj_t * cardview, lv_obj_t * cardpage)
  619. {
  620. (void)cardpage;
  621. ob_cardview_ext_t * ext = lv_obj_get_ext_attr(cardview);
  622. lv_indev_t * indev = lv_indev_get_act();
  623. lv_indev_get_point(indev, &ext->point_last);
  624. }
  625. /**
  626. * Called when a card's page or scrollable object is being pressed
  627. * @param cardview pointer to the btn view object
  628. * @param cardpage pointer to the page of a btn
  629. */
  630. static void cardpage_pressing_hadler(lv_obj_t * cardview, lv_obj_t * cardpage)
  631. {
  632. ob_cardview_ext_t * ext = lv_obj_get_ext_attr(cardview);
  633. lv_indev_t * indev = lv_indev_get_act();
  634. lv_point_t point_act;
  635. lv_indev_get_point(indev, &point_act);
  636. lv_coord_t x_diff = point_act.x - ext->point_last.x;
  637. lv_coord_t y_diff = point_act.y - ext->point_last.y;
  638. if(ext->draging == 0) {
  639. if(x_diff >= LV_INDEV_DRAG_LIMIT || x_diff<= -LV_INDEV_DRAG_LIMIT) {
  640. ext->drag_hor = 1;
  641. ext->draging = 1;
  642. lv_obj_set_drag(lv_page_get_scrl(cardpage), false);
  643. } else if(y_diff >= LV_INDEV_DRAG_LIMIT || y_diff <= -LV_INDEV_DRAG_LIMIT) {
  644. ext->drag_hor = 0;
  645. ext->draging = 1;
  646. }
  647. }
  648. if(ext->drag_hor) {
  649. lv_obj_set_x(ext->content, lv_obj_get_x(ext->content) + point_act.x - ext->point_last.x);
  650. ext->point_last.x = point_act.x;
  651. ext->point_last.y = point_act.y;
  652. }
  653. }
  654. /**
  655. * Called when a card's page or scrollable object is released or the press id lost
  656. * @param cardview pointer to the btn view object
  657. * @param cardpage pointer to the page of a btn
  658. */
  659. static void cardpage_press_lost_hadler(lv_obj_t * cardview, lv_obj_t * cardpage)
  660. {
  661. ob_cardview_ext_t * ext = lv_obj_get_ext_attr(cardview);
  662. ext->drag_hor = 0;
  663. ext->draging = 0;
  664. lv_obj_set_drag(lv_page_get_scrl(cardpage), true);
  665. lv_indev_t * indev = lv_indev_get_act();
  666. lv_point_t point_act;
  667. lv_indev_get_point(indev, &point_act);
  668. lv_point_t vect;
  669. lv_indev_get_vect(indev, &vect);
  670. lv_coord_t x_predict = 0;
  671. while(vect.x != 0) {
  672. x_predict += vect.x;
  673. vect.x = vect.x * (100 - LV_INDEV_DRAG_THROW) / 100;
  674. }
  675. lv_coord_t page_x1 = cardpage->coords.x1 - cardview->coords.x1 + x_predict;
  676. lv_coord_t page_x2 = page_x1 + lv_obj_get_width(cardpage);
  677. lv_coord_t treshold = lv_obj_get_width(cardview) / 2;
  678. uint16_t card_cur = ext->card_cur;
  679. if(page_x1 > treshold) {
  680. if(card_cur != 0) card_cur--;
  681. } else if(page_x2 < treshold) {
  682. if(card_cur < ext->card_cnt - 1) card_cur++;
  683. }
  684. ob_cardview_set_card_act(cardview, card_cur, true);
  685. }
  686. /**
  687. * Realign and resize the elements of Tab view
  688. * @param cardview pointer to a Tab view object
  689. */
  690. static void cardview_realign(lv_obj_t * cardview)
  691. {
  692. ob_cardview_ext_t * ext = lv_obj_get_ext_attr(cardview);
  693. if(ext->card_cnt != 0) {
  694. }
  695. lv_obj_set_height(ext->content, lv_obj_get_height(cardview));
  696. lv_obj_t * pages = lv_obj_get_child(ext->content, NULL);
  697. while(pages != NULL) {
  698. if(lv_obj_get_signal_func(pages) == cardpage_signal) { /*Be sure to adjust only the pages (user can other things)*/
  699. lv_obj_set_size(pages, lv_obj_get_width(cardview), lv_obj_get_height(ext->content));
  700. }
  701. pages = lv_obj_get_child(ext->content, pages);
  702. }
  703. ob_cardview_set_card_act(cardview, ext->card_cur, false);
  704. }
  705. #endif