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