]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/demolist.qc
Avoid loading lists of demos, screenshots and music tracks when the menu starts,...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / demolist.qc
1 #ifndef DEMOLIST_H
2 #define DEMOLIST_H
3 #include "listbox.qc"
4 CLASS(XonoticDemoList, XonoticListBox)
5         METHOD(XonoticDemoList, configureXonoticDemoList, void(entity));
6         ATTRIB(XonoticDemoList, rowsPerItem, float, 1)
7         METHOD(XonoticDemoList, resizeNotify, void(entity, vector, vector, vector, vector));
8         METHOD(XonoticDemoList, drawListBoxItem, void(entity, int, vector, bool, bool));
9         METHOD(XonoticDemoList, getDemos, void(entity));
10         METHOD(XonoticDemoList, startDemo, void(entity));
11         METHOD(XonoticDemoList, timeDemo, void(entity));
12         METHOD(XonoticDemoList, demoName, string(entity, float));
13         METHOD(XonoticDemoList, doubleClickListBoxItem, void(entity, float, vector));
14         METHOD(XonoticDemoList, keyDown, float(entity, float, float, float));
15         METHOD(XonoticDemoList, destroy, void(entity));
16         METHOD(XonoticDemoList, showNotify, void(entity));
17
18         ATTRIB(XonoticDemoList, listDemo, float, -1)
19         ATTRIB(XonoticDemoList, realFontSize, vector, '0 0 0')
20         ATTRIB(XonoticDemoList, columnNameOrigin, float, 0)
21         ATTRIB(XonoticDemoList, columnNameSize, float, 0)
22         ATTRIB(XonoticDemoList, realUpperMargin, float, 0)
23         ATTRIB(XonoticDemoList, origin, vector, '0 0 0')
24         ATTRIB(XonoticDemoList, itemAbsSize, vector, '0 0 0')
25
26         ATTRIB(XonoticDemoList, filterString, string, string_null)
27 ENDCLASS(XonoticDemoList)
28
29 #ifndef IMPLEMENTATION
30 // public:
31 entity demolist; // for reference elsewhere
32 entity makeXonoticDemoList();
33 #endif
34 void DemoList_Refresh_Click(entity btn, entity me);
35 void DemoList_Filter_Change(entity box, entity me);
36 #endif
37
38 #ifdef IMPLEMENTATION
39
40 entity makeXonoticDemoList()
41 {
42         entity me;
43         me = NEW(XonoticDemoList);
44         me.configureXonoticDemoList(me);
45         return me;
46 }
47
48 void XonoticDemoList_configureXonoticDemoList(entity me)
49 {
50         me.configureXonoticListBox(me);
51 }
52
53 string XonoticDemoList_demoName(entity me, float i)
54 {
55         string s;
56         s = bufstr_get(me.listDemo, i);
57
58         if(substring(s, 0, 1) == "/")
59                 s = substring(s, 1, strlen(s) - 1);  // remove the first forward slash
60
61         return s;
62 }
63
64 // if subdir is true look in subdirectories too (1 level)
65 void getDemos_for_ext(entity me, string ext, float subdir)
66 {
67         string s;
68         if (subdir)
69                 s="demos/*/";
70         else
71                 s="demos/";
72         if(me.filterString)
73                 s=strcat(s, me.filterString, ext);
74         else
75                 s=strcat(s, "*", ext);
76
77         float list, i, n;
78         list = search_begin(s, false, true);
79         if(list >= 0)
80         {
81                 n = search_getsize(list);
82                 for(i = 0; i < n; ++i)
83                 {
84                         s = search_getfilename(list, i); // get initial full file name
85                         s = substring(s, 6, (strlen(s) - 6 - 4)); // remove "demos/" prefix and ".dem" suffix
86                         s = strdecolorize(s); // remove any pre-existing colors
87                         if(subdir)
88                         {
89                                 s = strreplace("/", "^7/", s); // clear colors at the forward slash
90                                 s = strcat("/", rgb_to_hexcolor(SKINCOLOR_DEMOLIST_SUBDIR), s); // add a forward slash for sorting, then color
91                                 bufstr_add(me.listDemo, s, true);
92                         }
93                         else { bufstr_add(me.listDemo, s, true); }
94                 }
95                 search_end(list);
96         }
97
98         if (subdir)
99                 getDemos_for_ext(me, ext, false);
100 }
101
102 void XonoticDemoList_getDemos(entity me)
103 {
104         if (me.listDemo >= 0)
105                 buf_del(me.listDemo);
106         me.listDemo = buf_create();
107         if (me.listDemo < 0)
108         {
109                 me.nItems = 0;
110                 return;
111         }
112         getDemos_for_ext(me, ".dem", true);
113         me.nItems = buf_getsize(me.listDemo);
114         if(me.nItems > 0)
115                 buf_sort(me.listDemo, 128, false);
116 }
117
118 void XonoticDemoList_destroy(entity me)
119 {
120         if(me.nItems > 0)
121                 buf_del(me.listDemo);
122 }
123
124 void XonoticDemoList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
125 {
126         me.itemAbsSize = '0 0 0';
127         SUPER(XonoticDemoList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
128
129         me.realFontSize_y = me.fontSize / (me.itemAbsSize_y = (absSize.y * me.itemHeight));
130         me.realFontSize_x = me.fontSize / (me.itemAbsSize_x = (absSize.x * (1 - me.controlWidth)));
131         me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
132
133         me.columnNameOrigin = me.realFontSize.x;
134         me.columnNameSize = 1 - 2 * me.realFontSize.x;
135 }
136
137 void XonoticDemoList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
138 {
139         string s;
140         if(isSelected)
141                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
142         else if(isFocused)
143         {
144                 me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
145                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
146         }
147
148         s = me.demoName(me,i);
149         s = draw_TextShortenToWidth(s, me.columnNameSize, 0, me.realFontSize);
150         draw_Text(me.realUpperMargin * eY + (me.columnNameOrigin + 0.00 * (me.columnNameSize - draw_TextWidth(s, 0, me.realFontSize))) * eX, s, me.realFontSize, SKINCOLOR_TEXT, SKINALPHA_TEXT, 1);
151 }
152
153 void XonoticDemoList_showNotify(entity me)
154 {
155         me.getDemos(me);
156 }
157
158 void DemoList_Refresh_Click(entity btn, entity me)
159 {
160         me.getDemos(me);
161         me.setSelected(me, 0); //always select the first element after a list update
162 }
163
164 void DemoList_Filter_Change(entity box, entity me)
165 {
166         if(me.filterString)
167                 strunzone(me.filterString);
168
169         if(box.text != "")
170         {
171                 if (strstrofs(box.text, "*", 0) >= 0 || strstrofs(box.text, "?", 0) >= 0)
172                         me.filterString = strzone(box.text);
173                 else
174                         me.filterString = strzone(strcat("*", box.text, "*"));
175         }
176         else
177                 me.filterString = string_null;
178
179         me.getDemos(me);
180 }
181
182 void XonoticDemoList_startDemo(entity me)
183 {
184         string s;
185         s = me.demoName(me, me.selectedItem);
186         s = strdecolorize(s);
187
188         localcmd("playdemo \"demos/", s, ".dem\" \nwait \ntogglemenu\n");
189 }
190
191 void XonoticDemoList_timeDemo(entity me)
192 {
193         string s;
194         s = me.demoName(me, me.selectedItem);
195         s = strdecolorize(s);
196
197         localcmd("timedemo \"demos/", s, ".dem\" \nwait \ntogglemenu\n");
198 }
199
200 void DemoConfirm_ListClick_Check_Gamestatus(entity me)
201 {
202         if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER))) // we're not in a match, lets watch the demo
203         {
204                 me.startDemo(me);
205         }
206         else // already in a match, player has to confirm
207         {
208                 DialogOpenButton_Click_withCoords(
209                         me,
210                         main.demostartconfirmDialog,
211                         boxToGlobal(eY * (me.selectedItem * me.itemHeight - me.scrollPos), me.origin, me.size),
212                         boxToGlobalSize(eY * me.itemHeight + eX * (1 - me.controlWidth), me.size)
213                 );
214         }
215 }
216
217 void XonoticDemoList_doubleClickListBoxItem(entity me, float i, vector where)
218 {
219         DemoConfirm_ListClick_Check_Gamestatus(me);
220 }
221
222 float XonoticDemoList_keyDown(entity me, float scan, float ascii, float shift)
223 {
224         if(scan == K_ENTER || scan == K_KP_ENTER)
225         {
226                 DemoConfirm_ListClick_Check_Gamestatus(me);
227                 return 1;
228         }
229         else
230         {
231                 return SUPER(XonoticDemoList).keyDown(me, scan, ascii, shift);
232         }
233 }
234 #endif
235