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