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