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