From 557e07876de0086b70e301b104547bf35ec57959 Mon Sep 17 00:00:00 2001 From: quou Date: Tue, 4 Jun 2024 21:42:04 +1000 Subject: Loading album art, seeking, pause/play. --- main.c | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 140 insertions(+), 4 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 94f78b5..2b0e763 100644 --- a/main.c +++ b/main.c @@ -6,14 +6,44 @@ Library lib; Player pl; +UIButton* play_button; +UISlider* seek_slider; +UIImageDisplay* cover_image; + +void ui_update_play(void) { + UI_FREE(play_button->label); + play_button->label = UIStringCopy( + pl.play? "Pause": "Play", + -1 + ); + UIElementRefresh((UIElement*)play_button); + UIImageDisplaySetContent( + cover_image, + pl.cover, + album_cover_w, + album_cover_h, + album_cover_w * 4 + ); + UIElementRepaint((UIElement*)cover_image, 0); +} + +void ui_update_seek(void) { + float p; + if (!pl.song) return; + p = (float)pl.cs / (float)pl.ms; + seek_slider->position = p; + UIElementRefresh((UIElement*)seek_slider); +} int libtab_msg( - UIElement *el, + UIElement* el, UIMessage msg, int di, - void *dp + void* dp ) { Song* song; + (void)di; + (void)dp; if (msg == UI_MSG_TABLE_GET_ITEM) { UITableGetItem *m = (UITableGetItem*)dp; song = &lib.songs[lib.indices[m->index]]; @@ -56,18 +86,88 @@ int libtab_msg( el->window->cursorY ); song = &lib.songs[lib.indices[hit]]; - init_player(&pl); play_song(&pl, song); + ui_update_play(); + } + return 0; +} + +int playpause_msg( + UIElement* el, + UIMessage msg, + int di, + void* dp +) { + (void)el; + (void)di; + (void)dp; + if (msg == UI_MSG_CLICKED) { + if (!pl.song) return 0; + pl.play = !pl.play; + ui_update_play(); + } + return 0; +} + +int start_msg( + UIElement* el, + UIMessage msg, + int di, + void* dp +) { + (void)el; + (void)di; + (void)dp; + if (msg == UI_MSG_CLICKED) { + if (!pl.song) return 0; + play_seek(&pl, 0.0f); + } + return 0; +} + +int end_msg( + UIElement* el, + UIMessage msg, + int di, + void* dp +) { + (void)el; + (void)di; + (void)dp; + if (msg == UI_MSG_CLICKED) { + if (!pl.song) return 0; + play_seek(&pl, 1.0f); } return 0; } +int seek_msg( + UIElement* el, + UIMessage msg, + int di, + void* dp +) { + UISlider* sl; + (void)el; + (void)di; + (void)dp; + if (msg == UI_MSG_VALUE_CHANGED) { + sl = (UISlider*)el; + play_seek(&pl, sl->position); + } else if (msg == UI_MSG_ANIMATE) { + ui_update_seek(); + } + + return 0; +} + int prog_main(void* mem) { Arena liba; UIWindow* wi; UISplitPane* split1, * split2, * split3; UIPanel* plib, * pctrl, * plist, * pqueue; UITable* libtab; + int r; memory_init(mem); init_arena( &liba, @@ -75,6 +175,7 @@ int prog_main(void* mem) { library_memory_size ); build_library(&liba, &lib, library_path); + init_player(&pl); UIInitialise(); wi = UIWindowCreate( 0, @@ -102,6 +203,39 @@ int prog_main(void* mem) { pctrl = UIPanelCreate(&split3->e, UI_PANEL_GRAY); pctrl->gap = 5; pctrl->border = UI_RECT_1(5); + cover_image = UIImageDisplayCreate( + &pctrl->e, + 0, + (unsigned*)pl.cover, + album_cover_w, + album_cover_h, + album_cover_w + ); + play_button = UIButtonCreate( + &pctrl->e, + 0, + "Play", + -1 + ); + play_button->e.messageUser = playpause_msg; + UIButtonCreate( + &pctrl->e, + 0, + "Start", + -1 + )->e.messageUser = start_msg; + UIButtonCreate( + &pctrl->e, + 0, + "End", + -1 + )->e.messageUser = end_msg; + seek_slider = UISliderCreate( + &pctrl->e, + UI_ELEMENT_H_FILL + ); + UIElementAnimate(&seek_slider->e, false); + seek_slider->e.messageUser = seek_msg; pqueue = UIPanelCreate(&split3->e, UI_PANEL_GRAY); pqueue->gap = 5; @@ -140,5 +274,7 @@ int prog_main(void* mem) { libtab->e.messageUser = libtab_msg; UITableResizeColumns(libtab); - return UIMessageLoop(); + r = UIMessageLoop(); + deinit_player(&pl); + return r; } -- cgit v1.2.3-54-g00ecf