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