]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/maplist.qc
Merge branch 'master' into matthiaskrgr/hudsetup
[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, startButton, entity, NULL)
35
36         METHOD(XonoticMapList, loadCvars, void(entity))
37
38         ATTRIB(XonoticMapList, typeToSearchString, string, string_null)
39         ATTRIB(XonoticMapList, typeToSearchTime, float, 0)
40
41         METHOD(XonoticMapList, destroy, void(entity))
42
43         ATTRIB(XonoticListBox, alphaBG, float, 0)
44 ENDCLASS(XonoticMapList)
45 entity makeXonoticMapList();
46 void MapList_All(entity btn, entity me);
47 void MapList_None(entity btn, entity me);
48 void MapList_LoadMap(entity btn, entity me);
49 #endif
50
51 #ifdef IMPLEMENTATION
52 void XonoticMapList_destroy(entity me)
53 {
54         MapInfo_Shutdown();
55 }
56
57 entity makeXonoticMapList()
58 {
59         entity me;
60         me = spawnXonoticMapList();
61         me.configureXonoticMapList(me);
62         return me;
63 }
64
65 void XonoticMapList_configureXonoticMapList(entity me)
66 {
67         me.configureXonoticListBox(me);
68         me.refilter(me);
69 }
70
71 void XonoticMapList_loadCvars(entity me)
72 {
73         me.refilter(me);
74 }
75
76 float XonoticMapList_g_maplistCacheQuery(entity me, float i)
77 {
78         return stof(substring(me.g_maplistCache, i, 1));
79 }
80 void XonoticMapList_g_maplistCacheToggle(entity me, float i)
81 {
82         string a, b, c, s, bspname;
83         float n;
84         s = me.g_maplistCache;
85         if (!s)
86                 return;
87         b = substring(s, i, 1);
88         if(b == "0")
89                 b = "1";
90         else if(b == "1")
91                 b = "0";
92         else
93                 return; // nothing happens
94         a = substring(s, 0, i);
95         c = substring(s, i+1, strlen(s) - (i+1));
96         strunzone(s);
97         me.g_maplistCache = strzone(strcat(a, b, c));
98         // TODO also update the actual cvar
99         if (!((bspname = MapInfo_BSPName_ByID(i))))
100                 return;
101         if(b == "1")
102                 cvar_set("g_maplist", strcat(bspname, " ", cvar_string("g_maplist")));
103         else
104         {
105                 s = "";
106                 n = tokenize_console(cvar_string("g_maplist"));
107                 for(i = 0; i < n; ++i)
108                         if(argv(i) != bspname)
109                                 s = strcat(s, " ", argv(i));
110                 cvar_set("g_maplist", substring(s, 1, strlen(s) - 1));
111         }
112 }
113
114 void XonoticMapList_draw(entity me)
115 {
116         if(me.startButton)
117                 me.startButton.disabled = ((me.selectedItem < 0) || (me.selectedItem >= me.nItems));
118         SUPER(XonoticMapList).draw(me);
119 }
120
121 void XonoticMapList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
122 {
123         me.itemAbsSize = '0 0 0';
124         SUPER(XonoticMapList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
125
126         me.realFontSize_y = me.fontSize / (me.itemAbsSize_y = (absSize.y * me.itemHeight));
127         me.realFontSize_x = me.fontSize / (me.itemAbsSize_x = (absSize.x * (1 - me.controlWidth)));
128         me.realUpperMargin1 = 0.5 * (1 - 2.5 * me.realFontSize.y);
129         me.realUpperMargin2 = me.realUpperMargin1 + 1.5 * me.realFontSize.y;
130
131         me.columnPreviewOrigin = 0;
132         me.columnPreviewSize = me.itemAbsSize.y / me.itemAbsSize.x * 4 / 3;
133         me.columnNameOrigin = me.columnPreviewOrigin + me.columnPreviewSize + me.realFontSize.x;
134         me.columnNameSize = 1 - me.columnPreviewSize - 2 * me.realFontSize.x;
135
136         me.checkMarkSize = (eX * (me.itemAbsSize.y / me.itemAbsSize.x) + eY) * 0.5;
137         me.checkMarkOrigin = eY + eX * (me.columnPreviewOrigin + me.columnPreviewSize) - me.checkMarkSize;
138 }
139
140 void XonoticMapList_clickListBoxItem(entity me, float i, vector where)
141 {
142         if(where.x <= me.columnPreviewOrigin + me.columnPreviewSize)
143                 if(where.x >= 0)
144                 {
145                         m_play_click_sound(MENU_SOUND_SELECT);
146                         me.g_maplistCacheToggle(me, i);
147                 }
148 }
149
150 void XonoticMapList_doubleClickListBoxItem(entity me, float i, vector where)
151 {
152         if(where.x >= me.columnNameOrigin)
153                 if(where.x <= 1)
154                 {
155                         // pop up map info screen
156                         m_play_click_sound(MENU_SOUND_OPEN);
157                         main.mapInfoDialog.loadMapInfo(main.mapInfoDialog, i, me);
158                         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));
159                 }
160 }
161
162 void XonoticMapList_drawListBoxItem(entity me, float i, vector absSize, float isSelected)
163 {
164         // layout: Ping, Map name, Map name, NP, TP, MP
165         string s;
166         float theAlpha;
167         float included;
168
169         if(!MapInfo_Get_ByID(i))
170                 return;
171
172         included = me.g_maplistCacheQuery(me, i);
173         if(included || isSelected)
174                 theAlpha = SKINALPHA_MAPLIST_INCLUDEDFG;
175         else
176                 theAlpha = SKINALPHA_MAPLIST_NOTINCLUDEDFG;
177
178         if(isSelected)
179                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
180         else if(included)
181                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_MAPLIST_INCLUDEDBG, SKINALPHA_MAPLIST_INCLUDEDBG);
182
183         if(draw_PictureSize(strcat("/maps/", MapInfo_Map_bspname)) == '0 0 0')
184                 draw_Picture(me.columnPreviewOrigin * eX, "nopreview_map", me.columnPreviewSize * eX + eY, '1 1 1', theAlpha);
185         else
186                 draw_Picture(me.columnPreviewOrigin * eX, strcat("/maps/", MapInfo_Map_bspname), me.columnPreviewSize * eX + eY, '1 1 1', theAlpha);
187
188         if(included)
189                 draw_Picture(me.checkMarkOrigin, "checkmark", me.checkMarkSize, '1 1 1', 1);
190         s = draw_TextShortenToWidth(strdecolorize(MapInfo_Map_titlestring), me.columnNameSize, 0, me.realFontSize);
191         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);
192         s = draw_TextShortenToWidth(strdecolorize(MapInfo_Map_author), me.columnNameSize, 0,  me.realFontSize);
193         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);
194
195         MapInfo_ClearTemps();
196 }
197
198 void XonoticMapList_refilter(entity me)
199 {
200         float i, j, n;
201         string s;
202         float gt, f;
203         gt = MapInfo_CurrentGametype();
204         f = MapInfo_CurrentFeatures();
205         MapInfo_FilterGametype(gt, f, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
206         me.nItems = MapInfo_count;
207         for(i = 0; i < MapInfo_count; ++i)
208                 draw_PreloadPicture(strcat("/maps/", MapInfo_BSPName_ByID(i)));
209         if(me.g_maplistCache)
210                 strunzone(me.g_maplistCache);
211         s = "0";
212         for(i = 1; i < MapInfo_count; i *= 2)
213                 s = strcat(s, s);
214         n = tokenize_console(cvar_string("g_maplist"));
215         for(i = 0; i < n; ++i)
216         {
217                 j = MapInfo_FindName(argv(i));
218                 if(j >= 0)
219                         s = strcat(
220                                 substring(s, 0, j),
221                                 "1",
222                                 substring(s, j+1, MapInfo_count - (j+1))
223                         );
224         }
225         me.g_maplistCache = strzone(s);
226         if(gt != me.lastGametype || f != me.lastFeatures)
227         {
228                 me.lastGametype = gt;
229                 me.lastFeatures = f;
230                 me.setSelected(me, 0);
231         }
232 }
233
234 void XonoticMapList_refilterCallback(entity me, entity cb)
235 {
236         me.refilter(me);
237 }
238
239 void MapList_All(entity btn, entity me)
240 {
241         float i;
242         string s;
243         MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, 0, MapInfo_ForbiddenFlags(), 0); // all
244         s = "";
245         for(i = 0; i < MapInfo_count; ++i)
246                 s = strcat(s, " ", MapInfo_BSPName_ByID(i));
247         cvar_set("g_maplist", substring(s, 1, strlen(s) - 1));
248         me.refilter(me);
249 }
250
251 void MapList_None(entity btn, entity me)
252 {
253         cvar_set("g_maplist", "");
254         me.refilter(me);
255 }
256
257 void MapList_LoadMap(entity btn, entity me)
258 {
259         string m;
260         float i;
261
262         i = me.selectedItem;
263
264         if(btn.parent.instanceOfXonoticMapInfoDialog)
265         {
266                 i = btn.parent.currentMapIndex;
267                 Dialog_Close(btn, btn.parent);
268         }
269
270         if(i >= me.nItems || i < 0)
271                 return;
272
273         m = MapInfo_BSPName_ByID(i);
274         if (!m)
275         {
276                 print(_("Huh? Can't play this (m is NULL). Refiltering so this won't happen again.\n"));
277                 me.refilter(me);
278                 return;
279         }
280         if(MapInfo_CheckMap(m))
281         {
282                 localcmd("\nmenu_loadmap_prepare\n");
283                 if(cvar("menu_use_default_hostname"))
284                         localcmd("hostname \"", sprintf(_("%s's Xonotic Server"), strdecolorize(cvar_string("_cl_name"))), "\"\n");
285                 MapInfo_LoadMap(m, 1);
286         }
287         else
288         {
289                 print(_("Huh? Can't play this (invalid game type). Refiltering so this won't happen again.\n"));
290                 me.refilter(me);
291                 return;
292         }
293 }
294
295 float XonoticMapList_keyDown(entity me, float scan, float ascii, float shift)
296 {
297         string ch, save;
298         if(me.nItems <= 0)
299                 return SUPER(XonoticMapList).keyDown(me, scan, ascii, shift);
300         if(scan == K_MOUSE2 || scan == K_SPACE || scan == K_ENTER || scan == K_KP_ENTER)
301         {
302                 // pop up map info screen
303                 m_play_click_sound(MENU_SOUND_OPEN);
304                 main.mapInfoDialog.loadMapInfo(main.mapInfoDialog, me.selectedItem, me);
305                 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));
306         }
307         else if(scan == K_MOUSE3 || scan == K_INS || scan == K_KP_INS)
308         {
309                 m_play_click_sound(MENU_SOUND_SELECT);
310                 me.g_maplistCacheToggle(me, me.selectedItem);
311         }
312         else if(ascii == 43) // +
313         {
314                 if (!me.g_maplistCacheQuery(me, me.selectedItem))
315                 {
316                         m_play_click_sound(MENU_SOUND_SELECT);
317                         me.g_maplistCacheToggle(me, me.selectedItem);
318                 }
319         }
320         else if(ascii == 45) // -
321         {
322                 if(me.g_maplistCacheQuery(me, me.selectedItem))
323                 {
324                         m_play_click_sound(MENU_SOUND_SELECT);
325                         me.g_maplistCacheToggle(me, me.selectedItem);
326                 }
327         }
328         else if(scan == K_BACKSPACE)
329         {
330                 if(time < me.typeToSearchTime)
331                 {
332                         save = substring(me.typeToSearchString, 0, strlen(me.typeToSearchString) - 1);
333                         if(me.typeToSearchString)
334                                 strunzone(me.typeToSearchString);
335                         me.typeToSearchString = strzone(save);
336                         me.typeToSearchTime = time + 0.5;
337                         if(strlen(me.typeToSearchString))
338                         {
339                                 MapInfo_FindName(me.typeToSearchString);
340                                 if(MapInfo_FindName_firstResult >= 0)
341                                         me.setSelected(me, MapInfo_FindName_firstResult);
342                         }
343                 }
344         }
345         else if(ascii >= 32 && ascii != 127)
346         {
347                 ch = chr(ascii);
348                 if(time > me.typeToSearchTime)
349                         save = ch;
350                 else
351                         save = strcat(me.typeToSearchString, ch);
352                 if(me.typeToSearchString)
353                         strunzone(me.typeToSearchString);
354                 me.typeToSearchString = strzone(save);
355                 me.typeToSearchTime = time + 0.5;
356                 MapInfo_FindName(me.typeToSearchString);
357                 if(MapInfo_FindName_firstResult >= 0)
358                         me.setSelected(me, MapInfo_FindName_firstResult);
359         }
360         else
361                 return SUPER(XonoticMapList).keyDown(me, scan, ascii, shift);
362         return 1;
363 }
364
365 #endif