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