]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/maplist.qc
Add strfree to reduce explicit use of strunzone/string_null
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / maplist.qc
1 #include "maplist.qh"
2
3 #include <common/mapinfo.qh>
4 #include "dialog_multiplayer_create_mapinfo.qh"
5 #include "mainwindow.qh"
6 #include "inputbox.qh"
7
8 .bool disabled;
9
10 void XonoticMapList_destroy(entity me)
11 {
12         MapInfo_Shutdown();
13 }
14
15 entity makeXonoticMapList()
16 {
17         entity me;
18         me = NEW(XonoticMapList);
19         me.configureXonoticMapList(me);
20         return me;
21 }
22
23 entity MapList_Set_String_Filter_Box(entity me, entity e)
24 {
25         me.stringFilterBox = e;
26         return e;
27 }
28
29 void XonoticMapList_configureXonoticMapList(entity me)
30 {
31         me.configureXonoticListBox(me);
32         me.refilter(me);
33 }
34
35 void XonoticMapList_loadCvars(entity me)
36 {
37         me.refilter(me);
38 }
39
40
41 float XonoticMapList_g_maplistCacheQuery(entity me, float i)
42 {
43         return stof(substring(me.g_maplistCache, i, 1));
44 }
45 void XonoticMapList_g_maplistCacheToggle(entity me, float i)
46 {
47         string a, b, c, s, bspname;
48         float n;
49         s = me.g_maplistCache;
50         if (!s)
51                 return;
52         b = substring(s, i, 1);
53         if(b == "0")
54                 b = "1";
55         else if(b == "1")
56                 b = "0";
57         else
58                 return; // nothing happens
59         a = substring(s, 0, i);
60         c = substring(s, i+1, strlen(s) - (i+1));
61         strunzone(s);
62         me.g_maplistCache = strzone(strcat(a, b, c));
63         // TODO also update the actual cvar
64         if (!((bspname = MapInfo_BSPName_ByID(i))))
65                 return;
66         if(b == "1")
67                 cvar_set("g_maplist", strcat(bspname, " ", cvar_string("g_maplist")));
68         else
69         {
70                 s = "";
71                 n = tokenize_console(cvar_string("g_maplist"));
72                 for(i = 0; i < n; ++i)
73                         if(argv(i) != bspname)
74                                 s = strcat(s, " ", argv(i));
75                 cvar_set("g_maplist", substring(s, 1, strlen(s) - 1));
76         }
77 }
78
79 void XonoticMapList_draw(entity me)
80 {
81         if(me.startButton)
82                 me.startButton.disabled = ((me.selectedItem < 0) || (me.selectedItem >= me.nItems));
83         SUPER(XonoticMapList).draw(me);
84 }
85
86 void XonoticMapList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
87 {
88         me.itemAbsSize = '0 0 0';
89         SUPER(XonoticMapList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
90
91         me.realFontSize_y = me.fontSize / (me.itemAbsSize_y = (absSize.y * me.itemHeight));
92         me.realFontSize_x = me.fontSize / (me.itemAbsSize_x = (absSize.x * (1 - me.controlWidth)));
93         me.realUpperMargin1 = 0.5 * (1 - 2.5 * me.realFontSize.y);
94         me.realUpperMargin2 = me.realUpperMargin1 + 1.5 * me.realFontSize.y;
95
96         me.columnPreviewOrigin = 0;
97         me.columnPreviewSize = me.itemAbsSize.y / me.itemAbsSize.x * 4 / 3;
98         me.columnNameOrigin = me.columnPreviewOrigin + me.columnPreviewSize + me.realFontSize.x;
99         me.columnNameSize = 1 - me.columnPreviewSize - 2 * me.realFontSize.x;
100
101         me.checkMarkSize = (eX * (me.itemAbsSize.y / me.itemAbsSize.x) + eY) * 0.5;
102         me.checkMarkOrigin = eY + eX * (me.columnPreviewOrigin + me.columnPreviewSize) - me.checkMarkSize;
103 }
104
105 void XonoticMapList_clickListBoxItem(entity me, float i, vector where)
106 {
107         if(where.x <= me.columnPreviewOrigin + me.columnPreviewSize)
108                 if(where.x >= 0)
109                 {
110                         m_play_click_sound(MENU_SOUND_SELECT);
111                         me.g_maplistCacheToggle(me, i);
112                 }
113 }
114
115 void XonoticMapList_doubleClickListBoxItem(entity me, float i, vector where)
116 {
117         if(where.x >= me.columnNameOrigin)
118                 if(where.x <= 1)
119                 {
120                         // pop up map info screen
121                         m_play_click_sound(MENU_SOUND_OPEN);
122                         main.mapInfoDialog.loadMapInfo(main.mapInfoDialog, i, me);
123                         DialogOpenButton_Click_withCoords(NULL, main.mapInfoDialog, me.origin + eX * (me.columnNameOrigin * me.size.x) + eY * ((me.itemHeight * i - me.scrollPos) * me.size.y), eY * me.itemAbsSize.y + eX * (me.itemAbsSize.x * me.columnNameSize));
124                 }
125 }
126
127 void XonoticMapList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
128 {
129         // layout: Ping, Map name, Map name, NP, TP, MP
130         string s;
131         float theAlpha;
132         float included;
133
134         if(!MapInfo_Get_ByID(i))
135                 return;
136
137         included = me.g_maplistCacheQuery(me, i);
138         if(included || isSelected)
139                 theAlpha = SKINALPHA_MAPLIST_INCLUDEDFG;
140         else
141                 theAlpha = SKINALPHA_MAPLIST_NOTINCLUDEDFG;
142
143         if(isSelected)
144                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
145         else
146         {
147                 if(included)
148                         draw_Fill('0 0 0', '1 1 0', SKINCOLOR_MAPLIST_INCLUDEDBG, SKINALPHA_MAPLIST_INCLUDEDBG);
149                 if(isFocused)
150                 {
151                         me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
152                         draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
153                 }
154         }
155
156         if(draw_PictureSize(strcat("/maps/", MapInfo_Map_bspname)) == '0 0 0')
157                 draw_Picture(me.columnPreviewOrigin * eX, "nopreview_map", me.columnPreviewSize * eX + eY, '1 1 1', theAlpha);
158         else
159                 draw_Picture(me.columnPreviewOrigin * eX, strcat("/maps/", MapInfo_Map_bspname), me.columnPreviewSize * eX + eY, '1 1 1', theAlpha);
160
161         if(included)
162                 draw_Picture(me.checkMarkOrigin, "checkmark", me.checkMarkSize, '1 1 1', 1);
163         s = draw_TextShortenToWidth(strdecolorize(MapInfo_Map_titlestring), me.columnNameSize, 0, me.realFontSize);
164         draw_Text(me.realUpperMargin1 * eY + (me.columnNameOrigin + 0.00 * (me.columnNameSize - draw_TextWidth(s, 0, me.realFontSize))) * eX, s, me.realFontSize, SKINCOLOR_MAPLIST_TITLE, theAlpha, 0);
165         s = draw_TextShortenToWidth(strdecolorize(MapInfo_Map_author), me.columnNameSize, 0,  me.realFontSize);
166         draw_Text(me.realUpperMargin2 * eY + (me.columnNameOrigin + 1.00 * (me.columnNameSize - draw_TextWidth(s, 0, me.realFontSize))) * eX, s, me.realFontSize, SKINCOLOR_MAPLIST_AUTHOR, theAlpha, 0);
167
168         MapInfo_ClearTemps();
169 }
170
171 void XonoticMapList_refilter(entity me)
172 {
173         float i, j, n;
174         string s;
175         Gametype gt = MapInfo_CurrentGametype();
176         int f = MapInfo_CurrentFeatures();
177         MapInfo_FilterGametype(gt, f, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
178         if (me.stringFilter)
179                 MapInfo_FilterString(me.stringFilter);
180         me.nItems = MapInfo_count;
181
182         for(i = 0; i < MapInfo_count; ++i)
183                 draw_PreloadPicture(strcat("/maps/", MapInfo_BSPName_ByID(i)));
184         s = "0";
185         for(i = 1; i < MapInfo_count; i *= 2)
186                 s = strcat(s, s);
187         n = tokenize_console(cvar_string("g_maplist"));
188         for(i = 0; i < n; ++i)
189         {
190                 j = MapInfo_FindName(argv(i));
191                 if(j >= 0)
192                 {
193                         // double check that the two mapnames are "identical", not just share the same prefix
194                         if (strlen(MapInfo_BSPName_ByID(j)) == strlen(argv(i)))
195                                 s = strcat(
196                                         substring(s, 0, j),
197                                         "1",
198                                         substring(s, j+1, MapInfo_count - (j+1))
199                                 );
200                 }
201         }
202         strcpy(me.g_maplistCache, s);
203         if(gt != me.lastGametype || f != me.lastFeatures)
204         {
205                 me.lastGametype = gt;
206                 me.lastFeatures = f;
207                 me.setSelected(me, 0);
208         }
209 }
210
211 void XonoticMapList_refilterCallback(entity me, entity cb)
212 {
213         me.refilter(me);
214 }
215
216 void MapList_StringFilterBox_Change(entity box, entity me)
217 {
218         strfree(me.stringFilter);
219         if(box.text != "")
220                 me.stringFilter = strzone(box.text);
221
222         me.refilter(me);
223 }
224
225 void MapList_Add_Shown(entity btn, entity me)
226 {
227         float i, n;
228         n = strlen(me.g_maplistCache);
229         for (i = 0 ; i < n; i++)
230         {
231                 if (!me.g_maplistCacheQuery(me, i))
232                         me.g_maplistCacheToggle(me, i);
233         }
234         me.refilter(me);
235 }
236
237 void MapList_Remove_Shown(entity btn, entity me)
238 {
239         float i, n;
240         n = strlen(me.g_maplistCache);
241         for (i = 0 ; i < n; i++)
242         {
243                 if (me.g_maplistCacheQuery(me, i))
244                         me.g_maplistCacheToggle(me, i);
245         }
246         me.refilter(me);
247 }
248
249 void MapList_Add_All(entity btn, entity me)
250 {
251         float i;
252         string s;
253         _MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, 0, MapInfo_ForbiddenFlags(), 0); // all
254         s = "";
255         for(i = 0; i < MapInfo_count; ++i)
256                 s = strcat(s, " ", MapInfo_BSPName_ByID(i));
257         cvar_set("g_maplist", substring(s, 1, strlen(s) - 1));
258         me.refilter(me);
259 }
260
261 void MapList_Remove_All(entity btn, entity me)
262 {
263         cvar_set("g_maplist", "");
264         me.refilter(me);
265 }
266
267 void MapList_LoadMap(entity btn, entity me)
268 {
269         string m;
270         float i;
271
272         i = me.selectedItem;
273
274         if(btn.parent.instanceOfXonoticMapInfoDialog)
275         {
276                 i = btn.parent.currentMapIndex;
277                 Dialog_Close(btn, btn.parent);
278         }
279
280         if(i >= me.nItems || i < 0)
281                 return;
282
283         m = MapInfo_BSPName_ByID(i);
284         if (!m)
285         {
286                 LOG_INFO(_("Huh? Can't play this (m is NULL). Refiltering so this won't happen again."));
287                 me.refilter(me);
288                 return;
289         }
290         if(MapInfo_CheckMap(m))
291         {
292                 localcmd("\nmenu_loadmap_prepare\n");
293                 if(cvar("menu_use_default_hostname"))
294                         localcmd("hostname \"", sprintf(_("%s's Xonotic Server"), strdecolorize(cvar_string("_cl_name"))), "\"\n");
295                 MapInfo_LoadMap(m, 1);
296         }
297         else
298         {
299                 LOG_INFO(_("Huh? Can't play this (invalid game type). Refiltering so this won't happen again."));
300                 me.refilter(me);
301                 return;
302         }
303 }
304
305 float XonoticMapList_keyDown(entity me, float scan, float ascii, float shift)
306 {
307         string ch, save;
308         if(me.nItems <= 0)
309                 return SUPER(XonoticMapList).keyDown(me, scan, ascii, shift);
310         if(scan == K_MOUSE2 || scan == K_SPACE || scan == K_ENTER || scan == K_KP_ENTER)
311         {
312                 // pop up map info screen
313                 m_play_click_sound(MENU_SOUND_OPEN);
314                 main.mapInfoDialog.loadMapInfo(main.mapInfoDialog, me.selectedItem, me);
315                 DialogOpenButton_Click_withCoords(NULL, main.mapInfoDialog, me.origin + eX * (me.columnNameOrigin * me.size.x) + eY * ((me.itemHeight * me.selectedItem - me.scrollPos) * me.size.y), eY * me.itemAbsSize.y + eX * (me.itemAbsSize.x * me.columnNameSize));
316         }
317         else if(scan == K_MOUSE3 || scan == K_INS || scan == K_KP_INS)
318         {
319                 m_play_click_sound(MENU_SOUND_SELECT);
320                 me.g_maplistCacheToggle(me, me.selectedItem);
321         }
322         else if(ascii == 43) // +
323         {
324                 if (!me.g_maplistCacheQuery(me, me.selectedItem))
325                 {
326                         m_play_click_sound(MENU_SOUND_SELECT);
327                         me.g_maplistCacheToggle(me, me.selectedItem);
328                 }
329         }
330         else if(ascii == 45) // -
331         {
332                 if(me.g_maplistCacheQuery(me, me.selectedItem))
333                 {
334                         m_play_click_sound(MENU_SOUND_SELECT);
335                         me.g_maplistCacheToggle(me, me.selectedItem);
336                 }
337         }
338         else if(scan == K_BACKSPACE)
339         {
340                 if(time < me.typeToSearchTime)
341                 {
342                         save = substring(me.typeToSearchString, 0, strlen(me.typeToSearchString) - 1);
343                         strcpy(me.typeToSearchString, save);
344                         me.typeToSearchTime = time + 0.5;
345                         if(strlen(me.typeToSearchString))
346                         {
347                                 MapInfo_FindName(me.typeToSearchString);
348                                 if(MapInfo_FindName_firstResult >= 0)
349                                         me.setSelected(me, MapInfo_FindName_firstResult);
350                         }
351                 }
352         }
353         else if(ascii >= 32 && ascii != 127)
354         {
355                 ch = chr(ascii);
356                 if(time > me.typeToSearchTime)
357                         save = ch;
358                 else
359                         save = strcat(me.typeToSearchString, ch);
360                 strcpy(me.typeToSearchString, save);
361                 me.typeToSearchTime = time + 0.5;
362                 MapInfo_FindName(me.typeToSearchString);
363                 if(MapInfo_FindName_firstResult >= 0)
364                         me.setSelected(me, MapInfo_FindName_firstResult);
365         }
366         else if(shift & S_CTRL && scan == 'f') // ctrl-f (as in "F"ind)
367         {
368                 me.parent.setFocus(me.parent, me.stringFilterBox);
369         }
370         else if(shift & S_CTRL && scan == 'u') // ctrl-u (remove stringFilter line
371         {
372                 me.stringFilterBox.setText(me.stringFilterBox, "");
373                 MapList_StringFilterBox_Change(me.stringFilterBox, me);
374         }
375         else
376                 return SUPER(XonoticMapList).keyDown(me, scan, ascii, shift);
377         return 1;
378 }
379
380 float MapList_StringFilterBox_keyDown(entity me, float scan, float ascii, float shift)
381 {
382         // in this section, note that onChangeEntity has the ref to mapListBox
383         // we make use of that, instead of extending a class to add one more attrib
384         switch(scan)
385         {
386                 case K_KP_ENTER:
387                 case K_ENTER:
388                         // move the focus to the mapListBox
389                         me.onChangeEntity.parent.setFocus(me.onChangeEntity.parent, me.onChangeEntity);
390                         return 1;
391                 case K_KP_UPARROW:
392                 case K_UPARROW:
393                 case K_KP_DOWNARROW:
394                 case K_DOWNARROW:
395                         // pass the event to the mapListBox (to scroll up and down)
396                         return me.onChangeEntity.keyDown(me.onChangeEntity, scan, ascii, shift);
397         }
398         return SUPER(XonoticInputBox).keyDown(me, scan, ascii, shift);
399 }