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