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