]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/maplist.qc
add ctrl-f hotkey; fix variable name consistency
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / maplist.qc
1 #ifdef INTERFACE
2 CLASS(XonoticMapList) EXTENDS(XonoticListBox)
3         METHOD(XonoticMapList, configureXonoticMapList, void(entity))
4         ATTRIB(XonoticMapList, rowsPerItem, float, 4)
5         METHOD(XonoticMapList, draw, void(entity))
6         METHOD(XonoticMapList, drawListBoxItem, void(entity, float, vector, float))
7         METHOD(XonoticMapList, clickListBoxItem, void(entity, float, vector))
8         METHOD(XonoticMapList, doubleClickListBoxItem, void(entity, float, vector))
9         METHOD(XonoticMapList, resizeNotify, void(entity, vector, vector, vector, vector))
10         METHOD(XonoticMapList, refilter, void(entity))
11         METHOD(XonoticMapList, refilterCallback, void(entity, entity))
12         METHOD(XonoticMapList, keyDown, float(entity, float, float, float))
13
14         ATTRIB(XonoticMapList, realFontSize, vector, '0 0 0')
15         ATTRIB(XonoticMapList, columnPreviewOrigin, float, 0)
16         ATTRIB(XonoticMapList, columnPreviewSize, float, 0)
17         ATTRIB(XonoticMapList, columnNameOrigin, float, 0)
18         ATTRIB(XonoticMapList, columnNameSize, float, 0)
19         ATTRIB(XonoticMapList, checkMarkOrigin, vector, '0 0 0')
20         ATTRIB(XonoticMapList, checkMarkSize, vector, '0 0 0')
21         ATTRIB(XonoticMapList, realUpperMargin1, float, 0)
22         ATTRIB(XonoticMapList, realUpperMargin2, float, 0)
23
24         ATTRIB(XonoticMapList, lastGametype, float, 0)
25         ATTRIB(XonoticMapList, lastFeatures, float, 0)
26
27         ATTRIB(XonoticMapList, origin, vector, '0 0 0')
28         ATTRIB(XonoticMapList, itemAbsSize, vector, '0 0 0')
29
30         ATTRIB(XonoticMapList, g_maplistCache, string, string_null)
31         METHOD(XonoticMapList, g_maplistCacheToggle, void(entity, float))
32         METHOD(XonoticMapList, g_maplistCacheQuery, float(entity, float))
33
34         ATTRIB(XonoticMapList, stringFilter, string, string_null)
35         ATTRIB(XonoticMapList, stringFilterBox, entity, NULL)
36
37         ATTRIB(XonoticMapList, startButton, entity, NULL)
38
39         METHOD(XonoticMapList, loadCvars, void(entity))
40
41         ATTRIB(XonoticMapList, typeToSearchString, string, string_null)
42         ATTRIB(XonoticMapList, typeToSearchTime, float, 0)
43
44         METHOD(XonoticMapList, destroy, void(entity))
45
46         ATTRIB(XonoticListBox, alphaBG, float, 0)
47 ENDCLASS(XonoticMapList)
48 entity makeXonoticMapList();
49 entity makeXonoticMapListStringFilterBox(entity me, float doEditColorCodes, string theCvar);
50 void MapList_StringFilter_Change(entity box, entity me);
51 void MapList_All(entity btn, entity me);
52 void MapList_None(entity btn, entity me);
53 void MapList_LoadMap(entity btn, entity me);
54 #endif
55
56 #ifdef IMPLEMENTATION
57 void XonoticMapList_destroy(entity me)
58 {
59         MapInfo_Shutdown();
60 }
61
62 entity makeXonoticMapListStringFilterBox(entity me, float doEditColorCodes, string theCvar)
63 {
64         me.mapListBox.stringFilterBox = makeXonoticInputBox(doEditColorCodes, theCvar);
65         return me.mapListBox.stringFilterBox;
66 }
67 entity makeXonoticMapList()
68 {
69         entity me;
70         me = spawnXonoticMapList();
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, float i, vector absSize, float isSelected)
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 if(included)
198                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_MAPLIST_INCLUDEDBG, SKINALPHA_MAPLIST_INCLUDEDBG);
199
200         if(draw_PictureSize(strcat("/maps/", MapInfo_Map_bspname)) == '0 0 0')
201                 draw_Picture(me.columnPreviewOrigin * eX, "nopreview_map", me.columnPreviewSize * eX + eY, '1 1 1', theAlpha);
202         else
203                 draw_Picture(me.columnPreviewOrigin * eX, strcat("/maps/", MapInfo_Map_bspname), me.columnPreviewSize * eX + eY, '1 1 1', theAlpha);
204
205         if(included)
206                 draw_Picture(me.checkMarkOrigin, "checkmark", me.checkMarkSize, '1 1 1', 1);
207         s = draw_TextShortenToWidth(strdecolorize(MapInfo_Map_titlestring), me.columnNameSize, 0, me.realFontSize);
208         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);
209         s = draw_TextShortenToWidth(strdecolorize(MapInfo_Map_author), me.columnNameSize, 0,  me.realFontSize);
210         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);
211
212         MapInfo_ClearTemps();
213 }
214
215 void XonoticMapList_refilter(entity me)
216 {
217         float i, j, n;
218         string s;
219         float gt, f;
220         gt = MapInfo_CurrentGametype();
221         f = MapInfo_CurrentFeatures();
222         // TODO consider consolidating the two functions
223         if (me.stringFilter)
224                 MapInfo_FilterGametypeAndString(gt, f, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0, me.stringFilter);
225         else
226                 MapInfo_FilterGametype(gt, f, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
227         me.nItems = MapInfo_count;
228
229         for(i = 0; i < MapInfo_count; ++i)
230                 draw_PreloadPicture(strcat("/maps/", MapInfo_BSPName_ByID(i)));
231         if(me.g_maplistCache)
232                 strunzone(me.g_maplistCache);
233         s = "0";
234         for(i = 1; i < MapInfo_count; i *= 2)
235                 s = strcat(s, s);
236         n = tokenize_console(cvar_string("g_maplist"));
237         for(i = 0; i < n; ++i)
238         {
239                 j = MapInfo_FindName(argv(i));
240                 if(j >= 0)
241                         s = strcat(
242                                 substring(s, 0, j),
243                                 "1",
244                                 substring(s, j+1, MapInfo_count - (j+1))
245                         );
246         }
247         me.g_maplistCache = strzone(s);
248         if(gt != me.lastGametype || f != me.lastFeatures)
249         {
250                 me.lastGametype = gt;
251                 me.lastFeatures = f;
252                 me.setSelected(me, 0);
253         }
254 }
255
256 void XonoticMapList_refilterCallback(entity me, entity cb)
257 {
258         me.refilter(me);
259 }
260 void MapList_StringFilter_Change(entity box, entity me)
261 {
262         if(me.stringFilter)
263                 strunzone(me.stringFilter);
264         if(box.text != "")
265                 me.stringFilter = strzone(box.text);
266         else
267                 me.stringFilter = string_null;
268         
269         me.refilter(me);
270 }
271
272 void MapList_All(entity btn, entity me)
273 {
274         float i;
275         string s;
276         if (me.stringFilter)
277                 MapInfo_FilterGametypeAndString(MAPINFO_TYPE_ALL, 0, 0, MapInfo_ForbiddenFlags(), 0, me.stringFilter);
278         else
279                 MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, 0, MapInfo_ForbiddenFlags(), 0); // all
280         s = "";
281         for(i = 0; i < MapInfo_count; ++i)
282                 s = strcat(s, " ", MapInfo_BSPName_ByID(i));
283                 
284         cvar_set("g_maplist", substring(s, 1, strlen(s) - 1));
285         me.refilter(me);
286 }
287
288 void MapList_None(entity btn, entity me)
289 {
290         cvar_set("g_maplist", "");
291         me.refilter(me);
292 }
293
294 void MapList_LoadMap(entity btn, entity me)
295 {
296         string m;
297         float i;
298
299         i = me.selectedItem;
300
301         if(btn.parent.instanceOfXonoticMapInfoDialog)
302         {
303                 i = btn.parent.currentMapIndex;
304                 Dialog_Close(btn, btn.parent);
305         }
306
307         if(i >= me.nItems || i < 0)
308                 return;
309
310         m = MapInfo_BSPName_ByID(i);
311         if (!m)
312         {
313                 print(_("Huh? Can't play this (m is NULL). Refiltering so this won't happen again.\n"));
314                 me.refilter(me);
315                 return;
316         }
317         if(MapInfo_CheckMap(m))
318         {
319                 localcmd("\nmenu_loadmap_prepare\n");
320                 if(cvar("menu_use_default_hostname"))
321                         localcmd("hostname \"", sprintf(_("%s's Xonotic Server"), strdecolorize(cvar_string("_cl_name"))), "\"\n");
322                 MapInfo_LoadMap(m, 1);
323         }
324         else
325         {
326                 print(_("Huh? Can't play this (invalid game type). Refiltering so this won't happen again.\n"));
327                 me.refilter(me);
328                 return;
329         }
330 }
331
332 float XonoticMapList_keyDown(entity me, float scan, float ascii, float shift)
333 {
334         string ch, save;
335         if(me.nItems <= 0)
336                 return SUPER(XonoticMapList).keyDown(me, scan, ascii, shift);
337         if(scan == K_MOUSE2 || scan == K_SPACE || scan == K_ENTER || scan == K_KP_ENTER)
338         {
339                 // pop up map info screen
340                 m_play_click_sound(MENU_SOUND_OPEN);
341                 main.mapInfoDialog.loadMapInfo(main.mapInfoDialog, me.selectedItem, me);
342                 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));
343         }
344         else if(scan == K_MOUSE3 || scan == K_INS || scan == K_KP_INS)
345         {
346                 m_play_click_sound(MENU_SOUND_SELECT);
347                 me.g_maplistCacheToggle(me, me.selectedItem);
348         }
349         else if(ascii == 43) // +
350         {
351                 if (!me.g_maplistCacheQuery(me, me.selectedItem))
352                 {
353                         m_play_click_sound(MENU_SOUND_SELECT);
354                         me.g_maplistCacheToggle(me, me.selectedItem);
355                 }
356         }
357         else if(ascii == 45) // -
358         {
359                 if(me.g_maplistCacheQuery(me, me.selectedItem))
360                 {
361                         m_play_click_sound(MENU_SOUND_SELECT);
362                         me.g_maplistCacheToggle(me, me.selectedItem);
363                 }
364         }
365         else if(scan == K_BACKSPACE)
366         {
367                 if(time < me.typeToSearchTime)
368                 {
369                         save = substring(me.typeToSearchString, 0, strlen(me.typeToSearchString) - 1);
370                         if(me.typeToSearchString)
371                                 strunzone(me.typeToSearchString);
372                         me.typeToSearchString = strzone(save);
373                         me.typeToSearchTime = time + 0.5;
374                         if(strlen(me.typeToSearchString))
375                         {
376                                 MapInfo_FindName(me.typeToSearchString);
377                                 if(MapInfo_FindName_firstResult >= 0)
378                                         me.setSelected(me, MapInfo_FindName_firstResult);
379                         }
380                 }
381         }
382         else if(ascii >= 32 && ascii != 127)
383         {
384                 ch = chr(ascii);
385                 if(time > me.typeToSearchTime)
386                         save = ch;
387                 else
388                         save = strcat(me.typeToSearchString, ch);
389                 if(me.typeToSearchString)
390                         strunzone(me.typeToSearchString);
391                 me.typeToSearchString = strzone(save);
392                 me.typeToSearchTime = time + 0.5;
393                 MapInfo_FindName(me.typeToSearchString);
394                 if(MapInfo_FindName_firstResult >= 0)
395                         me.setSelected(me, MapInfo_FindName_firstResult);
396         }
397         else if(scan == 102 && ascii == 6 && shift == 2) // ctrl-f ("F"ind)
398         {
399                 me.parent.setFocus(me.parent, me.stringFilterBox);
400         }
401         else
402                 return SUPER(XonoticMapList).keyDown(me, scan, ascii, shift);
403         return 1;
404 }
405 #endif