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