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