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