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