]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/menu/xonotic/playlist.c
Make the play button resume the track if paused
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / playlist.c
index c1545b18e9dffa83b8fb9c0fc615c12ed9767666..80fe3132e25eb9a9bea30353bdc38203d35678fe 100644 (file)
@@ -7,14 +7,15 @@ CLASS(XonoticPlayList) EXTENDS(XonoticListBox)
        METHOD(XonoticPlayList, drawListBoxItem, void(entity, float, vector, float))
        METHOD(XonoticPlayList, stopSound, void(entity))
        METHOD(XonoticPlayList, startSound, void(entity, float))
+       METHOD(XonoticPlayList, resumeSound, void(entity))
        METHOD(XonoticPlayList, pauseSound, void(entity))
        METHOD(XonoticPlayList, clickListBoxItem, void(entity, float, vector))
        METHOD(XonoticPlayList, keyDown, float(entity, float, float, float))
        METHOD(XonoticPlayList, mouseDrag, float(entity, vector))
 
        METHOD(XonoticPlayList, addToPlayList, void(entity, string))
-       METHOD(XonoticPlayList, removeFromPlayList, void(entity, string))
-       ATTRIB(XonoticPlayList, playingTrack, string, string_null)
+       METHOD(XonoticPlayList, removeSelectedFromPlayList, void(entity))
+       ATTRIB(XonoticPlayList, playingTrack, float, -1)
 
        ATTRIB(XonoticPlayList, realFontSize, vector, '0 0 0')
        ATTRIB(XonoticPlayList, columnNameOrigin, float, 0)
@@ -30,6 +31,8 @@ CLASS(XonoticPlayList) EXTENDS(XonoticListBox)
 ENDCLASS(XonoticPlayList)
 
 entity makeXonoticPlayList();
+void PlayList_Remove(entity btn, entity me);
+void PlayList_Remove_All(entity btn, entity me);
 void StopSound_Click(entity btn, entity me);
 void StartSound_Click(entity btn, entity me);
 void PauseSound_Click(entity btn, entity me);
@@ -86,7 +89,7 @@ void XonoticPlayList_addToPlayList(entity me, string track)
        cvar_set("music_playlist_list0", strcat(cvar_string("music_playlist_list0"), " ", track));
 }
 
-void XonoticPlayList_removeFromPlayList(entity me, string track)
+void XonoticPlayList_removeSelectedFromPlayList(entity me)
 {
        float i, cpt = FALSE;
        string s = "";
@@ -95,28 +98,47 @@ void XonoticPlayList_removeFromPlayList(entity me, string track)
                return;
        for(i = 0; i < me.nItems; ++i)
        {
-               if(argv(i) == track)
+               if(i == me.selectedItem)
                {
+                       if(i == me.nItems - 1)
+                               me.setSelected(me, me.selectedItem - 1);
                        if(cvar("music_playlist_index") == 0 || cvar("music_playlist_index") == 999)
                        {
                                if(cvar("music_playlist_current0") == i)
                                        cpt = TRUE; // current playing track (we can't start next track here because startSound calls tokenize_console)
-                               else
-                               {
-                                       if(cvar("music_playlist_current0") > i)
-                                               cvar_set("music_playlist_current0", ftos(cvar("music_playlist_current0") - 1));
-                               }
+                               else if(cvar("music_playlist_current0") > i)
+                                       cvar_set("music_playlist_current0", ftos(cvar("music_playlist_current0") - 1));
                        }
                        continue;
                }
                s = strcat(s, " ", argv(i));
        }
+       // we must stop the current playing track if it has been removed
+       // otherwise pause/play button will resume from another track
        if(s == "")
+       {
                cvar_set("music_playlist_list0", "");
+               if(cpt)
+                       me.stopSound(me);
+       }
        else
-               cvar_set("music_playlist_list0", substring(s, 1, strlen(s))); //remove initial space
-       if(cpt)
-               me.startSound(me, 0); // stop current playing track otherwise pause/play button will resume from another track
+       {
+               cvar_set("music_playlist_list0", substring(s, 1, strlen(s))); // remove initial space
+               if(cpt)
+                       me.startSound(me, 0);
+       }
+}
+
+void PlayList_Remove(entity btn, entity me)
+{
+       me.removeSelectedFromPlayList(me);
+}
+
+void PlayList_Remove_All(entity btn, entity me)
+{
+       cvar_set("music_playlist_list0", "");
+       me.stopSound(me);
+       me.selectedItem = 0;
 }
 
 float XonoticPlayList_mouseDrag(entity me, vector pos)
@@ -144,6 +166,10 @@ float XonoticPlayList_mouseDrag(entity me, vector pos)
 void XonoticPlayList_draw(entity me)
 {
        me.nItems = tokenize_console(cvar_string("music_playlist_list0"));
+       if(cvar("music_playlist_index") == 0 || cvar("music_playlist_index") == 999)
+               me.playingTrack = cvar("music_playlist_current0");
+       else
+               me.playingTrack = -1;
        SUPER(XonoticPlayList).draw(me);
 }
 
@@ -153,10 +179,10 @@ void XonoticPlayList_drawListBoxItem(entity me, float i, vector absSize, float i
        if(isSelected)
                draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
 
-       if(argv(i) == me.playingTrack)
+       if(i == me.playingTrack)
        {
                float f = cvar("music_playlist_sampleposition0");
-               if(f == 0 || (((time * 2) & 1) && f > 0))
+               if(f <= 0 || (((time * 2) & 1) && f > 0))
                        draw_Text(me.realUpperMargin * eY + (me.columnNumberOrigin + me.columnNumberSize) * eX, chr(0xE000 + 141), me.realFontSize, '1 1 1', SKINALPHA_TEXT, 0);
        }
 
@@ -170,11 +196,11 @@ void XonoticPlayList_drawListBoxItem(entity me, float i, vector absSize, float i
 void XonoticPlayList_stopSound(entity me)
 {
        // STOP: list 0 is disabled by setting the index to -1
-       // we set sampleposition0 to -1 to indicate that music is stopped
-       if(cvar("music_playlist_index") != -1) // == 0 doesn't work when paused
+       // we set sampleposition0 to 0 to forget the position that the engine saves in this frame (for this reason we need to wait a frame)
+       if(cvar("music_playlist_index") == 0 || cvar("music_playlist_index") == 999)
        {
                cvar_set("music_playlist_index", "-1");
-               localcmd("\nwait; music_playlist_sampleposition0 -1\n");
+               localcmd("\nwait; music_playlist_sampleposition0 0\n");
                localcmd("\ndefer 3 \"cd play $menu_cdtrack\"\n");
        }
 }
@@ -190,17 +216,27 @@ void XonoticPlayList_startSound(entity me, float offset)
        me.nItems = tokenize_console(cvar_string("music_playlist_list0"));
        if(offset)
        {
+               if(cvar("music_playlist_index") == -1)
+                       return;
                f = bound(0, cvar("music_playlist_current0") + offset, me.nItems - 1);
                if(f == cvar("music_playlist_current0"))
                        return;
        }
        else
+       {
                f = me.selectedItem;
-       if(me.playingTrack)
-               strunzone(me.playingTrack);
-       me.playingTrack = strzone(argv(f));
+               // if it was paused then resume
+               if(f == cvar("music_playlist_current0"))
+               if(cvar("music_playlist_index") == 999)
+               {
+                       me.resumeSound(me);
+                       return;
+               }
+               // if it was not paused then proceed with restart
+       }
+
        // START: list 0 is disabled by setting the index to 999
-       // we set current0 to the selected track and sampleposition0 to 0 to forget value saved by the engine
+       // we set current0 to the selected track and sampleposition0 to 0 to forget the position that the engine saves in this frame (for this reason we need to wait a frame)
        // then we switch back to list 0
        cvar_set("music_playlist_index", "999");
        cvar_set("music_playlist_current0", ftos(f));
@@ -222,16 +258,20 @@ void NextSound_Click(entity btn, entity me)
        me.startSound(me, +1);
 }
 
+void XonoticPlayList_resumeSound(entity me)
+{
+       // RESUME: list 0 is enabled by setting the index to 0
+       // (we reset sampleposition0 to 0 to mark the track as in playing back state)
+       if(cvar("music_playlist_index") == 999)
+               localcmd("\nmusic_playlist_index 0; wait; music_playlist_sampleposition0 0\n");
+}
 void XonoticPlayList_pauseSound(entity me)
 {
        // PAUSE: list 0 is disabled by setting the index to 999
        // (we know the track is paused because the engine sets sampleposition0 to remember current position)
-       // RESUME: list 0 is enabled by setting the index to 0
-       // (we reset sampleposition0 to 0 to mark the track as in playing back state)
        if(cvar("music_playlist_index") == 0)
                localcmd("\nmusic_playlist_index 999\n");
-       else
-               localcmd("\nmusic_playlist_index 0; wait; music_playlist_sampleposition0 0\n");
+       else me.resumeSound(me);
 }
 
 void PauseSound_Click(entity btn, entity me)
@@ -263,8 +303,7 @@ float XonoticPlayList_keyDown(entity me, float scan, float ascii, float shift)
                return 1;
        }
        else if(scan == K_DEL || scan == K_KP_DEL || scan == K_BACKSPACE || scan == K_MOUSE3) {
-               me.nItems = tokenize_console(cvar_string("music_playlist_list0"));
-               me.removeFromPlayList(me, argv(me.selectedItem));
+               me.removeSelectedFromPlayList(me);
                return 1;
        }
        else