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