]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/demolist.qc
Add strfree to reduce explicit use of strunzone/string_null
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / demolist.qc
1 #include "demolist.qh"
2
3 #include "inputbox.qh"
4
5 entity makeXonoticDemoList()
6 {
7         entity me;
8         me = NEW(XonoticDemoList);
9         me.configureXonoticDemoList(me);
10         return me;
11 }
12
13 void XonoticDemoList_configureXonoticDemoList(entity me)
14 {
15         me.configureXonoticListBox(me);
16         me.nItems = 0;
17 }
18
19 string XonoticDemoList_demoName(entity me, float i)
20 {
21         string s;
22         s = bufstr_get(me.listDemo, i);
23
24         if(substring(s, 0, 1) == "/")
25                 s = substring(s, 1, strlen(s) - 1);  // remove the first forward slash
26
27         return s;
28 }
29
30 // if subdir is true look in subdirectories too (1 level)
31 void getDemos_for_ext(entity me, string ext, float subdir)
32 {
33         string s;
34         if (subdir)
35                 s="demos/*/";
36         else
37                 s="demos/";
38         if(me.filterString)
39                 s=strcat(s, me.filterString, ext);
40         else
41                 s=strcat(s, "*", ext);
42
43         float list, i, n;
44         list = search_begin(s, false, true);
45         if(list >= 0)
46         {
47                 n = search_getsize(list);
48                 for(i = 0; i < n; ++i)
49                 {
50                         s = search_getfilename(list, i); // get initial full file name
51                         s = substring(s, 6, (strlen(s) - 6 - 4)); // remove "demos/" prefix and ".dem" suffix
52                         s = strdecolorize(s); // remove any pre-existing colors
53                         if(subdir)
54                         {
55                                 s = strreplace("/", "^7/", s); // clear colors at the forward slash
56                                 s = strcat("/", rgb_to_hexcolor(SKINCOLOR_DEMOLIST_SUBDIR), s); // add a forward slash for sorting, then color
57                                 bufstr_add(me.listDemo, s, true);
58                         }
59                         else { bufstr_add(me.listDemo, s, true); }
60                 }
61                 search_end(list);
62         }
63
64         if (subdir)
65                 getDemos_for_ext(me, ext, false);
66 }
67
68 void XonoticDemoList_getDemos(entity me)
69 {
70         if (me.listDemo >= 0)
71                 buf_del(me.listDemo);
72         me.listDemo = buf_create();
73         if (me.listDemo < 0)
74         {
75                 me.nItems = 0;
76                 return;
77         }
78         getDemos_for_ext(me, ".dem", true);
79         me.nItems = buf_getsize(me.listDemo);
80         if(me.nItems > 0)
81                 buf_sort(me.listDemo, 128, false);
82 }
83
84 void XonoticDemoList_destroy(entity me)
85 {
86         if(me.nItems > 0)
87                 buf_del(me.listDemo);
88 }
89
90 void XonoticDemoList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
91 {
92         me.itemAbsSize = '0 0 0';
93         SUPER(XonoticDemoList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
94
95         me.realFontSize_y = me.fontSize / (me.itemAbsSize_y = (absSize.y * me.itemHeight));
96         me.realFontSize_x = me.fontSize / (me.itemAbsSize_x = (absSize.x * (1 - me.controlWidth)));
97         me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
98
99         me.columnNameOrigin = me.realFontSize.x;
100         me.columnNameSize = 1 - 2 * me.realFontSize.x;
101 }
102
103 void XonoticDemoList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
104 {
105         string s;
106         if(isSelected)
107                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
108         else if(isFocused)
109         {
110                 me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
111                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
112         }
113
114         s = me.demoName(me,i);
115         s = draw_TextShortenToWidth(s, me.columnNameSize, 0, me.realFontSize);
116         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);
117 }
118
119 void XonoticDemoList_showNotify(entity me)
120 {
121         me.getDemos(me);
122 }
123
124 void DemoList_Refresh_Click(entity btn, entity me)
125 {
126         me.getDemos(me);
127         me.setSelected(me, 0); //always select the first element after a list update
128 }
129
130 void DemoList_Filter_Change(entity box, entity me)
131 {
132         strfree(me.filterString);
133
134         if(box.text != "")
135         {
136                 if (strstrofs(box.text, "*", 0) >= 0 || strstrofs(box.text, "?", 0) >= 0)
137                         me.filterString = strzone(box.text);
138                 else
139                         me.filterString = strzone(strcat("*", box.text, "*"));
140         }
141         else
142                 me.filterString = string_null;
143
144         me.getDemos(me);
145 }
146
147 void XonoticDemoList_startDemo(entity me)
148 {
149         string s;
150         s = me.demoName(me, me.selectedItem);
151         s = strdecolorize(s);
152
153         localcmd("playdemo \"demos/", s, ".dem\" \nwait \ntogglemenu\n");
154 }
155
156 void XonoticDemoList_timeDemo(entity me)
157 {
158         string s;
159         s = me.demoName(me, me.selectedItem);
160         s = strdecolorize(s);
161
162         localcmd("timedemo \"demos/", s, ".dem\" \nwait \ntogglemenu\n");
163 }
164
165 void DemoConfirm_ListClick_Check_Gamestatus(entity me)
166 {
167         if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER))) // we're not in a match, lets watch the demo
168         {
169                 me.startDemo(me);
170         }
171         else // already in a match, player has to confirm
172         {
173                 DialogOpenButton_Click_withCoords(
174                         me,
175                         main.demostartconfirmDialog,
176                         boxToGlobal(eY * (me.selectedItem * me.itemHeight - me.scrollPos), me.origin, me.size),
177                         boxToGlobalSize(eY * me.itemHeight + eX * (1 - me.controlWidth), me.size)
178                 );
179         }
180 }
181
182 void XonoticDemoList_doubleClickListBoxItem(entity me, float i, vector where)
183 {
184         DemoConfirm_ListClick_Check_Gamestatus(me);
185 }
186
187 float XonoticDemoList_keyDown(entity me, float scan, float ascii, float shift)
188 {
189         if(scan == K_ENTER || scan == K_KP_ENTER)
190         {
191                 DemoConfirm_ListClick_Check_Gamestatus(me);
192                 return 1;
193         }
194         else
195         {
196                 return SUPER(XonoticDemoList).keyDown(me, scan, ascii, shift);
197         }
198 }