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_demo_music_list.c (7068B)

      1 /**
      2  * @file lv_demo_music_list.c
      3  *
      4  */
      5 
      6 /*********************
      7  *      INCLUDES
      8  *********************/
      9 #include "lv_demo_music_list.h"
     10 #if LV_USE_DEMO_MUSIC
     11 
     12 #include "lv_demo_music_main.h"
     13 
     14 /*********************
     15  *      DEFINES
     16  *********************/
     17 
     18 /**********************
     19  *      TYPEDEFS
     20  **********************/
     21 
     22 /**********************
     23  *  STATIC PROTOTYPES
     24  **********************/
     25 static lv_obj_t * add_list_btn(lv_obj_t * parent, uint32_t track_id);
     26 static void btn_click_event_cb(lv_event_t * e);
     27 
     28 /**********************
     29  *  STATIC VARIABLES
     30  **********************/
     31 static lv_obj_t * list;
     32 static const lv_font_t * font_small;
     33 static const lv_font_t * font_medium;
     34 static lv_style_t style_scrollbar;
     35 static lv_style_t style_btn;
     36 static lv_style_t style_btn_pr;
     37 static lv_style_t style_btn_chk;
     38 static lv_style_t style_btn_dis;
     39 static lv_style_t style_title;
     40 static lv_style_t style_artist;
     41 static lv_style_t style_time;
     42 LV_IMG_DECLARE(img_lv_demo_music_btn_list_play);
     43 LV_IMG_DECLARE(img_lv_demo_music_btn_list_pause);
     44 
     45 /**********************
     46  *      MACROS
     47  **********************/
     48 
     49 /**********************
     50  *   GLOBAL FUNCTIONS
     51  **********************/
     52 
     53 lv_obj_t * _lv_demo_music_list_create(lv_obj_t * parent)
     54 {
     55 #if LV_DEMO_MUSIC_LARGE
     56     font_small = &lv_font_montserrat_16;
     57     font_medium = &lv_font_montserrat_22;
     58 #else
     59     font_small = &lv_font_montserrat_12;
     60     font_medium = &lv_font_montserrat_16;
     61 #endif
     62 
     63     lv_style_init(&style_scrollbar);
     64     lv_style_set_width(&style_scrollbar,  4);
     65     lv_style_set_bg_opa(&style_scrollbar, LV_OPA_COVER);
     66     lv_style_set_bg_color(&style_scrollbar, lv_color_hex3(0xeee));
     67     lv_style_set_radius(&style_scrollbar, LV_RADIUS_CIRCLE);
     68     lv_style_set_pad_right(&style_scrollbar, 4);
     69 
     70     static const lv_coord_t grid_cols[] = {LV_GRID_CONTENT, LV_GRID_FR(1), LV_GRID_CONTENT, LV_GRID_TEMPLATE_LAST};
     71 #if LV_DEMO_MUSIC_LARGE
     72     static const lv_coord_t grid_rows[] = {35,  30, LV_GRID_TEMPLATE_LAST};
     73 #else
     74     static const lv_coord_t grid_rows[] = {22,  17, LV_GRID_TEMPLATE_LAST};
     75 #endif
     76     lv_style_init(&style_btn);
     77     lv_style_set_bg_opa(&style_btn, LV_OPA_TRANSP);
     78     lv_style_set_grid_column_dsc_array(&style_btn, grid_cols);
     79     lv_style_set_grid_row_dsc_array(&style_btn, grid_rows);
     80     lv_style_set_grid_row_align(&style_btn, LV_GRID_ALIGN_CENTER);
     81     lv_style_set_layout(&style_btn, LV_LAYOUT_GRID);
     82 #if LV_DEMO_MUSIC_LARGE
     83     lv_style_set_pad_right(&style_btn, 30);
     84 #else
     85     lv_style_set_pad_right(&style_btn, 20);
     86 #endif
     87     lv_style_init(&style_btn_pr);
     88     lv_style_set_bg_opa(&style_btn_pr, LV_OPA_COVER);
     89     lv_style_set_bg_color(&style_btn_pr,  lv_color_hex(0x4c4965));
     90 
     91     lv_style_init(&style_btn_chk);
     92     lv_style_set_bg_opa(&style_btn_chk, LV_OPA_COVER);
     93     lv_style_set_bg_color(&style_btn_chk, lv_color_hex(0x4c4965));
     94 
     95     lv_style_init(&style_btn_dis);
     96     lv_style_set_text_opa(&style_btn_dis, LV_OPA_40);
     97     lv_style_set_img_opa(&style_btn_dis, LV_OPA_40);
     98 
     99     lv_style_init(&style_title);
    100     lv_style_set_text_font(&style_title, font_medium);
    101     lv_style_set_text_color(&style_title, lv_color_hex(0xffffff));
    102 
    103     lv_style_init(&style_artist);
    104     lv_style_set_text_font(&style_artist, font_small);
    105     lv_style_set_text_color(&style_artist, lv_color_hex(0xb1b0be));
    106 
    107     lv_style_init(&style_time);
    108     lv_style_set_text_font(&style_time, font_medium);
    109     lv_style_set_text_color(&style_time, lv_color_hex(0xffffff));
    110 
    111     /*Create an empty transparent container*/
    112     list = lv_obj_create(parent);
    113     lv_obj_remove_style_all(list);
    114     lv_obj_set_size(list, LV_HOR_RES, LV_VER_RES - LV_DEMO_MUSIC_HANDLE_SIZE);
    115     lv_obj_set_y(list, LV_DEMO_MUSIC_HANDLE_SIZE);
    116     lv_obj_add_style(list, &style_scrollbar, LV_PART_SCROLLBAR);
    117     lv_obj_set_flex_flow(list, LV_FLEX_FLOW_COLUMN);
    118 
    119     uint32_t track_id;
    120     for(track_id = 0; _lv_demo_music_get_title(track_id); track_id++) {
    121         add_list_btn(list,  track_id);
    122     }
    123 
    124 #if LV_DEMO_MUSIC_SQUARE || LV_DEMO_MUSIC_ROUND
    125     lv_obj_set_scroll_snap_y(list, LV_SCROLL_SNAP_CENTER);
    126 #endif
    127 
    128     _lv_demo_music_list_btn_check(0, true);
    129 
    130     return list;
    131 }
    132 
    133 void _lv_demo_music_list_btn_check(uint32_t track_id, bool state)
    134 {
    135     lv_obj_t * btn = lv_obj_get_child(list, track_id);
    136     lv_obj_t * icon = lv_obj_get_child(btn, 0);
    137 
    138     if(state) {
    139         lv_obj_add_state(btn, LV_STATE_CHECKED);
    140         lv_img_set_src(icon, &img_lv_demo_music_btn_list_pause);
    141         lv_obj_scroll_to_view(btn, LV_ANIM_ON);
    142     }
    143     else {
    144         lv_obj_clear_state(btn, LV_STATE_CHECKED);
    145         lv_img_set_src(icon, &img_lv_demo_music_btn_list_play);
    146     }
    147 }
    148 
    149 /**********************
    150  *   STATIC FUNCTIONS
    151  **********************/
    152 
    153 static lv_obj_t * add_list_btn(lv_obj_t * parent, uint32_t track_id)
    154 {
    155     uint32_t t = _lv_demo_music_get_track_length(track_id);
    156     char time[32];
    157     lv_snprintf(time, sizeof(time), "%"LV_PRIu32":%02"LV_PRIu32, t / 60, t % 60);
    158     const char * title = _lv_demo_music_get_title(track_id);
    159     const char * artist = _lv_demo_music_get_artist(track_id);
    160 
    161     lv_obj_t * btn = lv_obj_create(parent);
    162     lv_obj_remove_style_all(btn);
    163 #if LV_DEMO_MUSIC_LARGE
    164     lv_obj_set_size(btn, lv_pct(100), 110);
    165 #else
    166     lv_obj_set_size(btn, lv_pct(100), 60);
    167 #endif
    168 
    169     lv_obj_add_style(btn, &style_btn, 0);
    170     lv_obj_add_style(btn, &style_btn_pr, LV_STATE_PRESSED);
    171     lv_obj_add_style(btn, &style_btn_chk, LV_STATE_CHECKED);
    172     lv_obj_add_style(btn, &style_btn_dis, LV_STATE_DISABLED);
    173     lv_obj_add_event_cb(btn, btn_click_event_cb, LV_EVENT_CLICKED, NULL);
    174 
    175     if(track_id >= 3) {
    176         lv_obj_add_state(btn, LV_STATE_DISABLED);
    177     }
    178 
    179     lv_obj_t * icon = lv_img_create(btn);
    180     lv_img_set_src(icon, &img_lv_demo_music_btn_list_play);
    181     lv_obj_set_grid_cell(icon, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_CENTER, 0, 2);
    182 
    183     lv_obj_t * title_label = lv_label_create(btn);
    184     lv_label_set_text(title_label, title);
    185     lv_obj_set_grid_cell(title_label, LV_GRID_ALIGN_START, 1, 1, LV_GRID_ALIGN_CENTER, 0, 1);
    186     lv_obj_add_style(title_label, &style_title, 0);
    187 
    188     lv_obj_t * artist_label = lv_label_create(btn);
    189     lv_label_set_text(artist_label, artist);
    190     lv_obj_add_style(artist_label, &style_artist, 0);
    191     lv_obj_set_grid_cell(artist_label, LV_GRID_ALIGN_START, 1, 1, LV_GRID_ALIGN_CENTER, 1, 1);
    192 
    193     lv_obj_t * time_label = lv_label_create(btn);
    194     lv_label_set_text(time_label, time);
    195     lv_obj_add_style(time_label, &style_time, 0);
    196     lv_obj_set_grid_cell(time_label, LV_GRID_ALIGN_END, 2, 1, LV_GRID_ALIGN_CENTER, 0, 2);
    197 
    198     LV_IMG_DECLARE(img_lv_demo_music_list_border);
    199     lv_obj_t * border = lv_img_create(btn);
    200     lv_img_set_src(border, &img_lv_demo_music_list_border);
    201     lv_obj_set_width(border, lv_pct(120));
    202     lv_obj_align(border, LV_ALIGN_BOTTOM_MID, 0, 0);
    203     lv_obj_add_flag(border, LV_OBJ_FLAG_IGNORE_LAYOUT);
    204 
    205     return btn;
    206 }
    207 
    208 
    209 static void btn_click_event_cb(lv_event_t * e)
    210 {
    211     lv_obj_t * btn = lv_event_get_target(e);
    212 
    213     uint32_t idx = lv_obj_get_child_id(btn);
    214 
    215     _lv_demo_music_play(idx);
    216 }
    217 #endif /*LV_USE_DEMO_MUSIC*/
    218