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