]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/soundlist.c
Merge remote-tracking branch 'origin/master' into terencehill/music_player
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / soundlist.c
1 #ifdef INTERFACE
2 CLASS(XonoticSoundList) EXTENDS(XonoticListBox)
3         METHOD(XonoticSoundList, configureXonoticSoundList, void(entity))
4         ATTRIB(XonoticSoundList, rowsPerItem, float, 1)
5         METHOD(XonoticSoundList, resizeNotify, void(entity, vector, vector, vector, vector))
6         METHOD(XonoticSoundList, drawListBoxItem, void(entity, float, vector, float))
7         METHOD(XonoticSoundList, getSounds, void(entity))
8         METHOD(XonoticSoundList, soundName, string(entity, float))
9         METHOD(XonoticSoundList, clickListBoxItem, void(entity, float, vector))
10         METHOD(XonoticSoundList, keyDown, float(entity, float, float, float))
11         METHOD(XonoticSoundList, destroy, void(entity))
12         METHOD(XonoticSoundList, showNotify, void(entity))
13
14         ATTRIB(XonoticSoundList, listSound, float, -1)
15         ATTRIB(XonoticSoundList, realFontSize, vector, '0 0 0')
16         ATTRIB(XonoticSoundList, columnNameOrigin, float, 0)
17         ATTRIB(XonoticSoundList, columnNameSize, float, 0)
18         ATTRIB(XonoticSoundList, realUpperMargin, float, 0)
19         ATTRIB(XonoticSoundList, origin, vector, '0 0 0')
20         ATTRIB(XonoticSoundList, itemAbsSize, vector, '0 0 0')
21
22         ATTRIB(XonoticSoundList, lastClickedSound, float, -1)
23         ATTRIB(XonoticSoundList, lastClickedTime, float, 0)
24         ATTRIB(XonoticSoundList, filterString, string, string_null)
25         ATTRIB(XonoticSoundList, playlist, entity, world)
26 ENDCLASS(XonoticSoundList)
27
28 entity makeXonoticSoundList();
29 void SoundList_Filter_Change(entity box, entity me);
30 void SoundList_Menu_Track_Change(entity box, entity me);
31 void SoundList_Menu_Track_Reset(entity box, entity me);
32 #endif
33
34 #ifdef IMPLEMENTATION
35
36 entity makeXonoticSoundList()
37 {
38         entity me;
39         me = spawnXonoticSoundList();
40         me.configureXonoticSoundList(me);
41         return me;
42 }
43
44 void XonoticSoundList_configureXonoticSoundList(entity me)
45 {
46         me.configureXonoticListBox(me);
47         me.getSounds(me);
48 }
49
50 string XonoticSoundList_soundName(entity me, float i )
51 {
52         string s;
53         s = search_getfilename(me.listSound, i);
54         s = substring(s, 15, strlen(s) - 15 - 4);  // sound/cdtracks/, .ogg
55         return s;
56 }
57
58
59 void XonoticSoundList_getSounds(entity me)
60 {
61         string s;
62         float i;
63
64         if(me.filterString)
65                 //subdirectory in filterString allowed
66                 s=strcat("sound/cdtracks/*", me.filterString, "*.ogg");
67         else
68                 s="sound/cdtracks/*.ogg";
69
70         if(me.listSound >= 0)
71                 search_end(me.listSound);
72
73         me.listSound = search_begin(s, FALSE, TRUE);
74
75         if(me.listSound < 0)
76                 me.nItems=0;
77         else
78                 me.nItems=search_getsize(me.listSound);
79 }
80
81 void XonoticSoundList_destroy(entity me)
82 {
83         search_end(me.listSound);
84 }
85
86 void XonoticSoundList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
87 {
88         me.itemAbsSize = '0 0 0';
89         SUPER(XonoticSoundList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
90
91         me.realFontSize_y = me.fontSize / (me.itemAbsSize_y = (absSize_y * me.itemHeight));
92         me.realFontSize_x = me.fontSize / (me.itemAbsSize_x = (absSize_x * (1 - me.controlWidth)));
93         me.realUpperMargin = 0.5 * (1 - me.realFontSize_y);
94
95         me.columnNameOrigin = me.realFontSize_x;
96         me.columnNameSize = 1 - 2 * me.realFontSize_x;
97 }
98
99 void XonoticSoundList_drawListBoxItem(entity me, float i, vector absSize, float isSelected)
100 {
101         string s;
102         if(isSelected)
103                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
104
105         s = me.soundName(me,i);
106         if(s == cvar_defstring("menu_cdtrack"))
107                 s = strcat(s, " [default menu track]");
108         else if(s == cvar_string("menu_cdtrack"))
109                 s = strcat(s, " [current menu track]");
110         s = draw_TextShortenToWidth(s, me.columnNameSize, 0, me.realFontSize);
111         draw_Text(me.realUpperMargin * eY + me.columnNameOrigin * eX, s, me.realFontSize, '1 1 1', SKINALPHA_TEXT, 0);
112 }
113
114 void XonoticSoundList_showNotify(entity me)
115 {
116         me.getSounds(me);
117 }
118
119 void SoundList_Menu_Track_Change(entity box, entity me)
120 {
121         cvar_set("menu_cdtrack", me.soundName(me,me.selectedItem));
122 }
123
124 void SoundList_Menu_Track_Reset(entity box, entity me)
125 {
126         cvar_set("menu_cdtrack", cvar_defstring("menu_cdtrack"));
127 }
128
129 void SoundList_Filter_Change(entity box, entity me)
130 {
131         if(me.filterString)
132                 strunzone(me.filterString);
133
134         if(box.text != "")
135                 me.filterString = strzone(box.text);
136         else
137                 me.filterString = string_null;
138
139         me.getSounds(me);
140 }
141
142 void XonoticSoundList_clickListBoxItem(entity me, float i, vector where)
143 {
144         if(i == me.lastClickedSound)
145                 if(time < me.lastClickedTime + 0.3)
146                 {
147                         // DOUBLE CLICK!
148                         me.setSelected(me, i);
149                         me.playlist.addToPlayList(me.playlist, me.soundName(me, i));
150                 }
151         me.lastClickedSound = i;
152         me.lastClickedTime = time;
153 }
154
155 float XonoticSoundList_keyDown(entity me, float scan, float ascii, float shift)
156 {
157         if(scan == K_ENTER || scan == K_KP_ENTER || scan == K_SPACE) {
158                 me.playlist.addToPlayList(me.playlist, me.soundName(me, me.selectedItem));
159                 return 1;
160         }
161         else if(scan == K_DEL || scan == K_KP_DEL) {
162                 me.playlist.removeFromPlayList(me.playlist, me.soundName(me, me.selectedItem));
163                 return 1;
164         }
165         else
166                 return SUPER(XonoticSoundList).keyDown(me, scan, ascii, shift);
167 }
168 #endif
169