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