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