]> 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 45111678605c61316426c360e0ea3dd2f64c3241..80fe3132e25eb9a9bea30353bdc38203d35678fe 100644 (file)
@@ -7,18 +7,21 @@ 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)
        ATTRIB(XonoticPlayList, columnNameSize, float, 0)
+       ATTRIB(XonoticPlayList, columnNumberOrigin, float, 0)
+       ATTRIB(XonoticPlayList, columnNumberSize, float, 0)
        ATTRIB(XonoticPlayList, realUpperMargin, float, 0)
        ATTRIB(XonoticPlayList, origin, vector, '0 0 0')
        ATTRIB(XonoticPlayList, itemAbsSize, vector, '0 0 0')
@@ -28,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);
@@ -60,8 +65,11 @@ void XonoticPlayList_resizeNotify(entity me, vector relOrigin, vector relSize, v
        me.realFontSize_x = me.fontSize / (me.itemAbsSize_x = (absSize_x * (1 - me.controlWidth)));
        me.realUpperMargin = 0.5 * (1 - me.realFontSize_y);
 
-       me.columnNameOrigin = me.realFontSize_x;
-       me.columnNameSize = 1 - 2 * me.realFontSize_x;
+       me.columnNumberOrigin = 0;
+       me.columnNumberSize = 3 * me.realFontSize_x;
+
+       me.columnNameOrigin = me.columnNumberSize + me.realFontSize_x;
+       me.columnNameSize = 1 - me.columnNameOrigin - me.realFontSize_x;
 }
 
 void XonoticPlayList_addToPlayList(entity me, string track)
@@ -81,28 +89,56 @@ 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;
+       float i, cpt = FALSE;
        string s = "";
        me.nItems = tokenize_console(cvar_string("music_playlist_list0"));
        if(me.nItems == 0)
                return;
        for(i = 0; i < me.nItems; ++i)
        {
-               if(argv(i) == track)
-               if(cvar("music_playlist_current0") != i) // forbid removing the current playing track, otherwise pause button will resume from another track
+               if(i == me.selectedItem)
                {
-                       if(cvar("music_playlist_current0") > i)
-                               cvar_set("music_playlist_current0", ftos(cvar("music_playlist_current0") - 1));
+                       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));
+                       }
                        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
+       {
+               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)
@@ -130,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);
 }
 
@@ -139,27 +179,28 @@ 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))
-                       draw_Text(me.realUpperMargin * eY, chr(0xE000 + 141), me.realFontSize, '1 1 1', SKINALPHA_TEXT, 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);
        }
 
-       s = argv(i);
-       s = strcat(ftos(i+1), ") ", s);
-       s = draw_TextShortenToWidth(s, me.columnNameSize, 0, me.realFontSize);
+       s = ftos(i+1);
+       draw_CenterText(me.realUpperMargin * eY + (me.columnNumberOrigin + 0.5 * me.columnNumberSize) * eX, s, me.realFontSize, '1 1 1', SKINALPHA_TEXT, 0);
+
+       s = draw_TextShortenToWidth(argv(i), me.columnNameSize, 0, me.realFontSize);
        draw_Text(me.realUpperMargin * eY + me.columnNameOrigin * eX, s, me.realFontSize, '1 1 1', SKINALPHA_TEXT, 0);
 }
 
 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");
        }
 }
@@ -175,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));
@@ -207,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)
@@ -248,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