]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/maplist.qc
Rename menu files to be consistent
[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                         me.g_maplistCacheToggle(me, i);
145 }
146
147 void XonoticMapList_doubleClickListBoxItem(entity me, float i, vector where)
148 {
149         if(where_x >= me.columnNameOrigin)
150                 if(where_x <= 1)
151                 {
152                         // pop up map info screen
153                         main.mapInfoDialog.loadMapInfo(main.mapInfoDialog, i, me);
154                         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));
155                 }
156 }
157
158 void XonoticMapList_drawListBoxItem(entity me, float i, vector absSize, float isSelected)
159 {
160         // layout: Ping, Map name, Map name, NP, TP, MP
161         string s;
162         float theAlpha;
163         float included;
164
165         if(!MapInfo_Get_ByID(i))
166                 return;
167
168         included = me.g_maplistCacheQuery(me, i);
169         if(included || isSelected)
170                 theAlpha = SKINALPHA_MAPLIST_INCLUDEDFG;
171         else
172                 theAlpha = SKINALPHA_MAPLIST_NOTINCLUDEDFG;
173
174         if(isSelected)
175                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
176         else if(included)
177                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_MAPLIST_INCLUDEDBG, SKINALPHA_MAPLIST_INCLUDEDBG);
178
179         if(draw_PictureSize(strcat("/maps/", MapInfo_Map_bspname)) == '0 0 0')
180                 draw_Picture(me.columnPreviewOrigin * eX, "nopreview_map", me.columnPreviewSize * eX + eY, '1 1 1', theAlpha);
181         else
182                 draw_Picture(me.columnPreviewOrigin * eX, strcat("/maps/", MapInfo_Map_bspname), me.columnPreviewSize * eX + eY, '1 1 1', theAlpha);
183
184         if(included)
185                 draw_Picture(me.checkMarkOrigin, "checkmark", me.checkMarkSize, '1 1 1', 1);
186         s = draw_TextShortenToWidth(strdecolorize(MapInfo_Map_titlestring), me.columnNameSize, 0, me.realFontSize);
187         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);
188         s = draw_TextShortenToWidth(strdecolorize(MapInfo_Map_author), me.columnNameSize, 0,  me.realFontSize);
189         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);
190
191         MapInfo_ClearTemps();
192 }
193
194 void XonoticMapList_refilter(entity me)
195 {
196         float i, j, n;
197         string s;
198         float gt, f;
199         gt = MapInfo_CurrentGametype();
200         f = MapInfo_CurrentFeatures();
201         MapInfo_FilterGametype(gt, f, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
202         me.nItems = MapInfo_count;
203         for(i = 0; i < MapInfo_count; ++i)
204                 draw_PreloadPicture(strcat("/maps/", MapInfo_BSPName_ByID(i)));
205         if(me.g_maplistCache)
206                 strunzone(me.g_maplistCache);
207         s = "0";
208         for(i = 1; i < MapInfo_count; i *= 2)
209                 s = strcat(s, s);
210         n = tokenize_console(cvar_string("g_maplist"));
211         for(i = 0; i < n; ++i)
212         {
213                 j = MapInfo_FindName(argv(i));
214                 if(j >= 0)
215                         s = strcat(
216                                 substring(s, 0, j),
217                                 "1",
218                                 substring(s, j+1, MapInfo_count - (j+1))
219                         );
220         }
221         me.g_maplistCache = strzone(s);
222         if(gt != me.lastGametype || f != me.lastFeatures)
223         {
224                 me.lastGametype = gt;
225                 me.lastFeatures = f;
226                 me.setSelected(me, 0);
227         }
228 }
229
230 void XonoticMapList_refilterCallback(entity me, entity cb)
231 {
232         me.refilter(me);
233 }
234
235 void MapList_All(entity btn, entity me)
236 {
237         float i;
238         string s;
239         MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, 0, MapInfo_ForbiddenFlags(), 0); // all
240         s = "";
241         for(i = 0; i < MapInfo_count; ++i)
242                 s = strcat(s, " ", MapInfo_BSPName_ByID(i));
243         cvar_set("g_maplist", substring(s, 1, strlen(s) - 1));
244         me.refilter(me);
245 }
246
247 void MapList_None(entity btn, entity me)
248 {
249         cvar_set("g_maplist", "");
250         me.refilter(me);
251 }
252
253 void MapList_LoadMap(entity btn, entity me)
254 {
255         string m;
256         float i;
257
258         i = me.selectedItem;
259
260         if(btn.parent.instanceOfXonoticMapInfoDialog)
261         {
262                 i = btn.parent.currentMapIndex;
263                 Dialog_Close(btn, btn.parent);
264         }
265
266         if(i >= me.nItems || i < 0)
267                 return;
268
269         m = MapInfo_BSPName_ByID(i);
270         if (!m)
271         {
272                 print(_("Huh? Can't play this (m is NULL). Refiltering so this won't happen again.\n"));
273                 me.refilter(me);
274                 return;
275         }
276         if(MapInfo_CheckMap(m))
277         {
278                 localcmd("\nmenu_loadmap_prepare\n");
279                 if(cvar("menu_use_default_hostname"))
280                         localcmd("hostname \"", sprintf(_("%s's Xonotic Server"), strdecolorize(cvar_string("_cl_name"))), "\"\n");
281                 MapInfo_LoadMap(m, 1);
282         }
283         else
284         {
285                 print(_("Huh? Can't play this (invalid game type). Refiltering so this won't happen again.\n"));
286                 me.refilter(me);
287                 return;
288         }
289 }
290
291 float XonoticMapList_keyDown(entity me, float scan, float ascii, float shift)
292 {
293         string ch, save;
294         if(me.nItems <= 0)
295                 return SUPER(XonoticMapList).keyDown(me, scan, ascii, shift);
296         if(scan == K_MOUSE2 || scan == K_SPACE || scan == K_ENTER || scan == K_KP_ENTER)
297         {
298                 // pop up map info screen
299                 main.mapInfoDialog.loadMapInfo(main.mapInfoDialog, me.selectedItem, me);
300                 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));
301         }
302         else if(scan == K_MOUSE3 || scan == K_INS || scan == K_KP_INS)
303         {
304                 me.g_maplistCacheToggle(me, me.selectedItem);
305         }
306         else if(ascii == 43) // +
307         {
308                 if (!me.g_maplistCacheQuery(me, me.selectedItem))
309                         me.g_maplistCacheToggle(me, me.selectedItem);
310         }
311         else if(ascii == 45) // -
312         {
313                 if(me.g_maplistCacheQuery(me, me.selectedItem))
314                         me.g_maplistCacheToggle(me, me.selectedItem);
315         }
316         else if(scan == K_BACKSPACE)
317         {
318                 if(time < me.typeToSearchTime)
319                 {
320                         save = substring(me.typeToSearchString, 0, strlen(me.typeToSearchString) - 1);
321                         if(me.typeToSearchString)
322                                 strunzone(me.typeToSearchString);
323                         me.typeToSearchString = strzone(save);
324                         me.typeToSearchTime = time + 0.5;
325                         if(strlen(me.typeToSearchString))
326                         {
327                                 MapInfo_FindName(me.typeToSearchString);
328                                 if(MapInfo_FindName_firstResult >= 0)
329                                         me.setSelected(me, MapInfo_FindName_firstResult);
330                         }
331                 }
332         }
333         else if(ascii >= 32 && ascii != 127)
334         {
335                 ch = chr(ascii);
336                 if(time > me.typeToSearchTime)
337                         save = ch;
338                 else
339                         save = strcat(me.typeToSearchString, ch);
340                 if(me.typeToSearchString)
341                         strunzone(me.typeToSearchString);
342                 me.typeToSearchString = strzone(save);
343                 me.typeToSearchTime = time + 0.5;
344                 MapInfo_FindName(me.typeToSearchString);
345                 if(MapInfo_FindName_firstResult >= 0)
346                         me.setSelected(me, MapInfo_FindName_firstResult);
347         }
348         else
349                 return SUPER(XonoticMapList).keyDown(me, scan, ascii, shift);
350         return 1;
351 }
352
353 #endif