]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/playlist.qc
9730e84172f9436024fdf55ad1037cba1295244d
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / playlist.qc
1 #ifdef INTERFACE
2 CLASS(XonoticPlayList) EXTENDS(XonoticListBox)
3         METHOD(XonoticPlayList, configureXonoticPlayList, void(entity))
4         ATTRIB(XonoticPlayList, rowsPerItem, float, 1)
5         METHOD(XonoticPlayList, resizeNotify, void(entity, vector, vector, vector, vector))
6         METHOD(XonoticPlayList, draw, void(entity))
7         METHOD(XonoticPlayList, drawListBoxItem, void(entity, int, vector, bool, float))
8         METHOD(XonoticPlayList, stopSound, void(entity))
9         METHOD(XonoticPlayList, startSound, void(entity, float))
10         METHOD(XonoticPlayList, resumeSound, void(entity))
11         METHOD(XonoticPlayList, pauseSound, void(entity))
12         METHOD(XonoticPlayList, doubleClickListBoxItem, void(entity, float, vector))
13         METHOD(XonoticPlayList, keyDown, float(entity, float, float, float))
14         METHOD(XonoticPlayList, mouseDrag, float(entity, vector))
15
16         METHOD(XonoticPlayList, addToPlayList, void(entity, string))
17         METHOD(XonoticPlayList, removeSelectedFromPlayList, void(entity))
18         ATTRIB(XonoticPlayList, playingTrack, float, -1)
19
20         ATTRIB(XonoticPlayList, realFontSize, vector, '0 0 0')
21         ATTRIB(XonoticPlayList, columnNameOrigin, float, 0)
22         ATTRIB(XonoticPlayList, columnNameSize, float, 0)
23         ATTRIB(XonoticPlayList, columnNumberOrigin, float, 0)
24         ATTRIB(XonoticPlayList, columnNumberSize, float, 0)
25         ATTRIB(XonoticPlayList, realUpperMargin, float, 0)
26         ATTRIB(XonoticPlayList, origin, vector, '0 0 0')
27         ATTRIB(XonoticPlayList, itemAbsSize, vector, '0 0 0')
28 ENDCLASS(XonoticPlayList)
29
30 entity makeXonoticPlayList();
31 void PlayList_Remove(entity btn, entity me);
32 void PlayList_Remove_All(entity btn, entity me);
33 void StopSound_Click(entity btn, entity me);
34 void StartSound_Click(entity btn, entity me);
35 void PauseSound_Click(entity btn, entity me);
36 void PrevSound_Click(entity btn, entity me);
37 void NextSound_Click(entity btn, entity me);
38 #endif
39
40 #ifdef IMPLEMENTATION
41
42 entity makeXonoticPlayList()
43 {
44         entity me;
45         me = spawnXonoticPlayList();
46         me.configureXonoticPlayList(me);
47         return me;
48 }
49
50 void XonoticPlayList_configureXonoticPlayList(entity me)
51 {
52         me.nItems = tokenize_console(cvar_string("music_playlist_list0"));
53         me.configureXonoticListBox(me);
54 }
55
56 void XonoticPlayList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
57 {
58         me.itemAbsSize = '0 0 0';
59         SUPER(XonoticPlayList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
60
61         me.realFontSize_y = me.fontSize / (me.itemAbsSize_y = (absSize.y * me.itemHeight));
62         me.realFontSize_x = me.fontSize / (me.itemAbsSize_x = (absSize.x * (1 - me.controlWidth)));
63         me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
64
65         me.columnNumberOrigin = 0;
66         me.columnNumberSize = 3 * me.realFontSize.x;
67
68         me.columnNameOrigin = me.columnNumberSize + me.realFontSize.x;
69         me.columnNameSize = 1 - me.columnNameOrigin - me.realFontSize.x;
70 }
71
72 void XonoticPlayList_addToPlayList(entity me, string track)
73 {
74         me.nItems = tokenize_console(cvar_string("music_playlist_list0"));
75         if(me.nItems == 0)
76         {
77                 cvar_set("music_playlist_list0", track);
78                 return;
79         }
80         float i;
81         for(i = 0; i < me.nItems; ++i)
82         {
83                 if(argv(i) == track)
84                         return; // track is already in playlist
85         }
86         cvar_set("music_playlist_list0", strcat(cvar_string("music_playlist_list0"), " ", track));
87 }
88
89 void XonoticPlayList_removeSelectedFromPlayList(entity me)
90 {
91         float i, cpt = false;
92         string s = "";
93         me.nItems = tokenize_console(cvar_string("music_playlist_list0"));
94         if(me.nItems == 0)
95                 return;
96         for(i = 0; i < me.nItems; ++i)
97         {
98                 if(i == me.selectedItem)
99                 {
100                         if(i == me.nItems - 1)
101                                 me.setSelected(me, me.selectedItem - 1);
102                         if(cvar("music_playlist_index") == 0 || cvar("music_playlist_index") == 999)
103                         {
104                                 if(cvar("music_playlist_current0") == i)
105                                         cpt = true; // current playing track (we can't start next track here because startSound calls tokenize_console)
106                                 else if(cvar("music_playlist_current0") > i)
107                                         cvar_set("music_playlist_current0", ftos(cvar("music_playlist_current0") - 1));
108                         }
109                         continue;
110                 }
111                 s = strcat(s, " ", argv(i));
112         }
113         // we must stop the current playing track if it has been removed
114         // otherwise pause/play button will resume from another track
115         if(s == "")
116         {
117                 cvar_set("music_playlist_list0", "");
118                 if(cpt)
119                         me.stopSound(me);
120         }
121         else
122         {
123                 cvar_set("music_playlist_list0", substring(s, 1, strlen(s))); // remove initial space
124                 if(cpt)
125                         me.startSound(me, 0);
126         }
127 }
128
129 void PlayList_Remove(entity btn, entity me)
130 {
131         me.removeSelectedFromPlayList(me);
132 }
133
134 void PlayList_Remove_All(entity btn, entity me)
135 {
136         cvar_set("music_playlist_list0", "");
137         me.stopSound(me);
138         me.selectedItem = 0;
139 }
140
141 float XonoticPlayList_mouseDrag(entity me, vector pos)
142 {
143         float f, i;
144         i = me.selectedItem;
145         f = SUPER(XonoticPlayList).mouseDrag(me, pos);
146
147         if(me.pressed != 1) // don't change priority if the person is just scrolling
148         {
149                 if(me.selectedItem != i)
150                 {
151                         cvar_set("music_playlist_list0", swapInPriorityList(cvar_string("music_playlist_list0"), me.selectedItem, i));
152                         float c = cvar("music_playlist_current0");
153                         if(c == i)
154                                 cvar_set("music_playlist_current0", ftos(me.selectedItem));
155                         else if(c == me.selectedItem)
156                                 cvar_set("music_playlist_current0", ftos(i));
157                 }
158         }
159
160         return f;
161 }
162
163 void XonoticPlayList_draw(entity me)
164 {
165         me.nItems = tokenize_console(cvar_string("music_playlist_list0"));
166         if(cvar("music_playlist_index") == 0 || cvar("music_playlist_index") == 999)
167                 me.playingTrack = cvar("music_playlist_current0");
168         else
169                 me.playingTrack = -1;
170         SUPER(XonoticPlayList).draw(me);
171 }
172
173 void XonoticPlayList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, float highlightedTime)
174 {
175         string s;
176         if(isSelected)
177                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
178         else if(highlightedTime)
179                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, getHighlightAlpha(SKINALPHA_LISTBOX_SELECTED * 0.1, highlightedTime));
180
181         if(i == me.playingTrack)
182         {
183                 float f = cvar("music_playlist_sampleposition0");
184                 if(f <= 0 || (((time * 2) & 1) && f > 0))
185                         draw_Text(me.realUpperMargin * eY + (me.columnNumberOrigin + me.columnNumberSize) * eX, "\xE2\x96\xB6", me.realFontSize, '1 1 1', SKINALPHA_TEXT, 0);
186         }
187
188         s = ftos(i+1);
189         draw_CenterText(me.realUpperMargin * eY + (me.columnNumberOrigin + 0.5 * me.columnNumberSize) * eX, s, me.realFontSize, '1 1 1', SKINALPHA_TEXT, 0);
190
191         s = draw_TextShortenToWidth(argv(i), me.columnNameSize, 0, me.realFontSize);
192         draw_Text(me.realUpperMargin * eY + me.columnNameOrigin * eX, s, me.realFontSize, '1 1 1', SKINALPHA_TEXT, 0);
193 }
194
195 void XonoticPlayList_stopSound(entity me)
196 {
197         // STOP: list 0 is disabled by setting the index to -1
198         // 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)
199         if(cvar("music_playlist_index") == 0 || cvar("music_playlist_index") == 999)
200         {
201                 cvar_set("music_playlist_index", "-1");
202                 localcmd("\nwait; music_playlist_sampleposition0 0\n");
203                 localcmd("\ndefer 3 \"cd play $menu_cdtrack\"\n");
204         }
205 }
206
207 void StopSound_Click(entity btn, entity me)
208 {
209         me.stopSound(me);
210 }
211
212 void XonoticPlayList_startSound(entity me, float offset)
213 {
214         float f;
215         me.nItems = tokenize_console(cvar_string("music_playlist_list0"));
216         if(offset)
217         {
218                 if(cvar("music_playlist_index") == -1)
219                         return;
220                 f = bound(0, cvar("music_playlist_current0") + offset, me.nItems - 1);
221                 if(f == cvar("music_playlist_current0"))
222                         return;
223         }
224         else
225         {
226                 f = me.selectedItem;
227                 // if it was paused then resume
228                 if(f == cvar("music_playlist_current0"))
229                 if(cvar("music_playlist_index") == 999)
230                 {
231                         me.resumeSound(me);
232                         return;
233                 }
234                 // if it was not paused then proceed with restart
235         }
236
237         // START: list 0 is disabled by setting the index to 999
238         // 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)
239         // then we switch back to list 0
240         cvar_set("music_playlist_index", "999");
241         cvar_set("music_playlist_current0", ftos(f));
242         localcmd("\nwait; music_playlist_sampleposition0 0; wait; music_playlist_index 0\n");
243 }
244
245 void StartSound_Click(entity btn, entity me)
246 {
247         me.startSound(me, 0);
248 }
249
250 void PrevSound_Click(entity btn, entity me)
251 {
252         me.startSound(me, -1);
253 }
254
255 void NextSound_Click(entity btn, entity me)
256 {
257         me.startSound(me, +1);
258 }
259
260 void XonoticPlayList_resumeSound(entity me)
261 {
262         // RESUME: list 0 is enabled by setting the index to 0
263         // (we reset sampleposition0 to 0 to mark the track as in playing back state)
264         if(cvar("music_playlist_index") == 999)
265                 localcmd("\nmusic_playlist_index 0; wait; music_playlist_sampleposition0 0\n");
266 }
267 void XonoticPlayList_pauseSound(entity me)
268 {
269         // PAUSE: list 0 is disabled by setting the index to 999
270         // (we know the track is paused because the engine sets sampleposition0 to remember current position)
271         if(cvar("music_playlist_index") == 0)
272                 localcmd("\nmusic_playlist_index 999\n");
273         else me.resumeSound(me);
274 }
275
276 void PauseSound_Click(entity btn, entity me)
277 {
278         me.pauseSound(me);
279 }
280
281 void XonoticPlayList_doubleClickListBoxItem(entity me, float i, vector where)
282 {
283         me.startSound(me, 0);
284 }
285
286 float XonoticPlayList_keyDown(entity me, float scan, float ascii, float shift)
287 {
288         if(scan == K_ENTER || scan == K_KP_ENTER) {
289                 me.startSound(me, 0);
290                 return 1;
291         }
292         else if(scan == K_SPACE) {
293                 me.pauseSound(me);
294                 return 1;
295         }
296         else if(scan == K_DEL || scan == K_KP_DEL || scan == K_BACKSPACE || scan == K_MOUSE3) {
297                 me.removeSelectedFromPlayList(me);
298                 return 1;
299         }
300         else
301                 return SUPER(XonoticPlayList).keyDown(me, scan, ascii, shift);
302 }
303 #endif
304