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