]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/maplist.qc
73ce4616be8d3ef5fd64d97e7f816bd8e20ce3fd
[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.itemAbsSize.y = absSize.y * me.itemHeight;
92         me.itemAbsSize.x = absSize.x * (1 - me.controlWidth);
93         me.realFontSize.y = me.fontSize / me.itemAbsSize.y;
94         me.realFontSize.x = me.fontSize / me.itemAbsSize.x;
95         me.realUpperMargin1 = 0.5 * (1 - 2.5 * me.realFontSize.y);
96         me.realUpperMargin2 = me.realUpperMargin1 + 1.5 * me.realFontSize.y;
97
98         me.columnPreviewOrigin = 0;
99         me.columnPreviewSize = me.itemAbsSize.y / me.itemAbsSize.x * 4 / 3;
100         me.columnNameOrigin = me.columnPreviewOrigin + me.columnPreviewSize + me.realFontSize.x;
101         me.columnNameSize = 1 - me.columnPreviewSize - 2 * me.realFontSize.x;
102
103         me.checkMarkSize = (eX * (me.itemAbsSize.y / me.itemAbsSize.x) + eY) * 0.5;
104         me.checkMarkOrigin = eY + eX * (me.columnPreviewOrigin + me.columnPreviewSize) - me.checkMarkSize;
105 }
106
107 void XonoticMapList_clickListBoxItem(entity me, float i, vector where)
108 {
109         if(where.x <= me.columnPreviewOrigin + me.columnPreviewSize)
110                 if(where.x >= 0)
111                 {
112                         m_play_click_sound(MENU_SOUND_SELECT);
113                         me.g_maplistCacheToggle(me, i);
114                 }
115 }
116
117 void XonoticMapList_doubleClickListBoxItem(entity me, float i, vector where)
118 {
119         if(where.x >= me.columnNameOrigin)
120                 if(where.x <= 1)
121                 {
122                         // pop up map info screen
123                         m_play_click_sound(MENU_SOUND_OPEN);
124                         main.mapInfoDialog.loadMapInfo(main.mapInfoDialog, i, me);
125                         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));
126                 }
127 }
128
129 void XonoticMapList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
130 {
131         // layout: Ping, Map name, Map name, NP, TP, MP
132         string s;
133         float theAlpha;
134         float included;
135
136         if(!MapInfo_Get_ByID(i))
137                 return;
138
139         included = me.g_maplistCacheQuery(me, i);
140         if(included || isSelected)
141                 theAlpha = SKINALPHA_MAPLIST_INCLUDEDFG;
142         else
143                 theAlpha = SKINALPHA_MAPLIST_NOTINCLUDEDFG;
144
145         if(isSelected)
146                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
147         else
148         {
149                 if(included)
150                         draw_Fill('0 0 0', '1 1 0', SKINCOLOR_MAPLIST_INCLUDEDBG, SKINALPHA_MAPLIST_INCLUDEDBG);
151                 if(isFocused)
152                 {
153                         me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
154                         draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
155                 }
156         }
157
158         if(draw_PictureSize(strcat("/maps/", MapInfo_Map_bspname)) == '0 0 0')
159         {
160                 if(draw_PictureSize(strcat("/levelshots/", MapInfo_Map_bspname)) == '0 0 0')
161                         draw_Picture(me.columnPreviewOrigin * eX, "nopreview_map", me.columnPreviewSize * eX + eY, '1 1 1', theAlpha);
162                 else
163                         draw_Picture(me.columnPreviewOrigin * eX, strcat("/levelshots/", MapInfo_Map_bspname), me.columnPreviewSize * eX + eY, '1 1 1', theAlpha);
164         }
165         else
166                 draw_Picture(me.columnPreviewOrigin * eX, strcat("/maps/", MapInfo_Map_bspname), me.columnPreviewSize * eX + eY, '1 1 1', theAlpha);
167
168         if(included)
169                 draw_Picture(me.checkMarkOrigin, "checkmark", me.checkMarkSize, '1 1 1', 1);
170         s = draw_TextShortenToWidth(strdecolorize(MapInfo_Map_titlestring), me.columnNameSize, 0, me.realFontSize);
171         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);
172         s = draw_TextShortenToWidth(strdecolorize(MapInfo_Map_author), me.columnNameSize, 0,  me.realFontSize);
173         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);
174
175         MapInfo_ClearTemps();
176 }
177
178 void XonoticMapList_refilter(entity me)
179 {
180         float i, j, n;
181         string s;
182         Gametype gt = MapInfo_CurrentGametype();
183         int f = MapInfo_CurrentFeatures();
184         MapInfo_FilterGametype(gt, f, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
185         if (me.stringFilter)
186                 MapInfo_FilterString(me.stringFilter);
187         me.nItems = MapInfo_count;
188
189         for(i = 0; i < MapInfo_count; ++i)
190                 draw_PreloadPicture(strcat("/maps/", MapInfo_BSPName_ByID(i)));
191         s = "0";
192         for(i = 1; i < MapInfo_count; i *= 2)
193                 s = strcat(s, s);
194         n = tokenize_console(cvar_string("g_maplist"));
195         for(i = 0; i < n; ++i)
196         {
197                 j = MapInfo_FindName(argv(i));
198                 if(j >= 0)
199                 {
200                         // double check that the two mapnames are "identical", not just share the same prefix
201                         if (strlen(MapInfo_BSPName_ByID(j)) == strlen(argv(i)))
202                                 s = strcat(
203                                         substring(s, 0, j),
204                                         "1",
205                                         substring(s, j+1, MapInfo_count - (j+1))
206                                 );
207                 }
208         }
209         strcpy(me.g_maplistCache, s);
210         if(gt != me.lastGametype || f != me.lastFeatures)
211         {
212                 me.lastGametype = gt;
213                 me.lastFeatures = f;
214                 me.setSelected(me, 0);
215         }
216 }
217
218 void XonoticMapList_refilterCallback(entity me, entity cb)
219 {
220         me.refilter(me);
221 }
222
223 void MapList_StringFilterBox_Change(entity box, entity me)
224 {
225         strfree(me.stringFilter);
226         if(box.text != "")
227                 me.stringFilter = strzone(box.text);
228
229         me.refilter(me);
230 }
231
232 void MapList_Add_Shown(entity btn, entity me)
233 {
234         float i, n;
235         n = strlen(me.g_maplistCache);
236         for (i = 0 ; i < n; i++)
237         {
238                 if (!me.g_maplistCacheQuery(me, i))
239                         me.g_maplistCacheToggle(me, i);
240         }
241         me.refilter(me);
242 }
243
244 void MapList_Remove_Shown(entity btn, entity me)
245 {
246         float i, n;
247         n = strlen(me.g_maplistCache);
248         for (i = 0 ; i < n; i++)
249         {
250                 if (me.g_maplistCacheQuery(me, i))
251                         me.g_maplistCacheToggle(me, i);
252         }
253         me.refilter(me);
254 }
255
256 void MapList_Add_All(entity btn, entity me)
257 {
258         float i;
259         string s;
260         _MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, 0, MapInfo_ForbiddenFlags(), 0); // all
261         s = "";
262         for(i = 0; i < MapInfo_count; ++i)
263                 s = strcat(s, " ", MapInfo_BSPName_ByID(i));
264         cvar_set("g_maplist", substring(s, 1, strlen(s) - 1));
265         me.refilter(me);
266 }
267
268 void MapList_Remove_All(entity btn, entity me)
269 {
270         cvar_set("g_maplist", "");
271         me.refilter(me);
272 }
273
274 void MapList_LoadMap(entity btn, entity me)
275 {
276         string m;
277         float i;
278
279         i = me.selectedItem;
280
281         if(btn.parent.instanceOfXonoticMapInfoDialog)
282         {
283                 i = btn.parent.currentMapIndex;
284                 Dialog_Close(btn, btn.parent);
285         }
286
287         if(i >= me.nItems || i < 0)
288                 return;
289
290         m = MapInfo_BSPName_ByID(i);
291         if (!m)
292         {
293                 LOG_INFO(_("Huh? Can't play this (m is NULL). Refiltering so this won't happen again."));
294                 me.refilter(me);
295                 return;
296         }
297         if(MapInfo_CheckMap(m))
298         {
299                 localcmd("\nmenu_loadmap_prepare\n");
300                 if(cvar("menu_use_default_hostname"))
301                         localcmd("hostname \"", sprintf(_("%s's Xonotic Server"), strdecolorize(cvar_string("_cl_name"))), "\"\n");
302                 MapInfo_LoadMap(m, 1);
303         }
304         else
305         {
306                 LOG_INFO(_("Huh? Can't play this (invalid game type). Refiltering so this won't happen again."));
307                 me.refilter(me);
308                 return;
309         }
310 }
311
312 float XonoticMapList_keyDown(entity me, float scan, float ascii, float shift)
313 {
314         string ch, save;
315         if(me.nItems <= 0)
316                 return SUPER(XonoticMapList).keyDown(me, scan, ascii, shift);
317         if(scan == K_MOUSE2 || scan == K_SPACE || scan == K_ENTER || scan == K_KP_ENTER)
318         {
319                 // pop up map info screen
320                 m_play_click_sound(MENU_SOUND_OPEN);
321                 main.mapInfoDialog.loadMapInfo(main.mapInfoDialog, me.selectedItem, me);
322                 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));
323         }
324         else if(scan == K_MOUSE3 || scan == K_INS || scan == K_KP_INS)
325         {
326                 m_play_click_sound(MENU_SOUND_SELECT);
327                 me.g_maplistCacheToggle(me, me.selectedItem);
328         }
329         else if(ascii == 43) // +
330         {
331                 if (!me.g_maplistCacheQuery(me, me.selectedItem))
332                 {
333                         m_play_click_sound(MENU_SOUND_SELECT);
334                         me.g_maplistCacheToggle(me, me.selectedItem);
335                 }
336         }
337         else if(ascii == 45) // -
338         {
339                 if(me.g_maplistCacheQuery(me, me.selectedItem))
340                 {
341                         m_play_click_sound(MENU_SOUND_SELECT);
342                         me.g_maplistCacheToggle(me, me.selectedItem);
343                 }
344         }
345         else if(scan == K_BACKSPACE)
346         {
347                 if(time < me.typeToSearchTime)
348                 {
349                         save = substring(me.typeToSearchString, 0, strlen(me.typeToSearchString) - 1);
350                         strcpy(me.typeToSearchString, save);
351                         me.typeToSearchTime = time + 0.5;
352                         if(strlen(me.typeToSearchString))
353                         {
354                                 MapInfo_FindName(me.typeToSearchString);
355                                 if(MapInfo_FindName_firstResult >= 0)
356                                         me.setSelected(me, MapInfo_FindName_firstResult);
357                         }
358                 }
359         }
360         else if(ascii >= 32 && ascii != 127)
361         {
362                 ch = chr(ascii);
363                 if(time > me.typeToSearchTime)
364                         save = ch;
365                 else
366                         save = strcat(me.typeToSearchString, ch);
367                 strcpy(me.typeToSearchString, save);
368                 me.typeToSearchTime = time + 0.5;
369                 MapInfo_FindName(me.typeToSearchString);
370                 if(MapInfo_FindName_firstResult >= 0)
371                         me.setSelected(me, MapInfo_FindName_firstResult);
372         }
373         else if((shift & S_CTRL) && scan == 'f') // ctrl-f (as in "F"ind)
374         {
375                 me.parent.setFocus(me.parent, me.stringFilterBox);
376         }
377         else if((shift & S_CTRL) && scan == 'u') // ctrl-u (remove stringFilter line
378         {
379                 me.stringFilterBox.setText(me.stringFilterBox, "");
380                 MapList_StringFilterBox_Change(me.stringFilterBox, me);
381         }
382         else
383                 return SUPER(XonoticMapList).keyDown(me, scan, ascii, shift);
384         return 1;
385 }
386
387 float MapList_StringFilterBox_keyDown(entity me, float scan, float ascii, float shift)
388 {
389         // in this section, note that onChangeEntity has the ref to mapListBox
390         // we make use of that, instead of extending a class to add one more attrib
391         switch(scan)
392         {
393                 case K_KP_ENTER:
394                 case K_ENTER:
395                         // move the focus to the mapListBox
396                         me.onChangeEntity.parent.setFocus(me.onChangeEntity.parent, me.onChangeEntity);
397                         return 1;
398                 case K_KP_UPARROW:
399                 case K_UPARROW:
400                 case K_KP_DOWNARROW:
401                 case K_DOWNARROW:
402                         // pass the event to the mapListBox (to scroll up and down)
403                         return me.onChangeEntity.keyDown(me.onChangeEntity, scan, ascii, shift);
404         }
405         return SUPER(XonoticInputBox).keyDown(me, scan, ascii, shift);
406 }