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