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