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