acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

lv_tileview.c (5692B)

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