]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/menu/nexuiz/maplist.c
committing stringwidth change - from now on, always provide a fontsize argument to...
[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, refilterCallback, void(entity, entity))
11         METHOD(NexuizMapList, keyDown, float(entity, float, float, float))
12
13         ATTRIB(NexuizMapList, realFontSize, vector, '0 0 0')
14         ATTRIB(NexuizMapList, columnPreviewOrigin, float, 0)
15         ATTRIB(NexuizMapList, columnPreviewSize, float, 0)
16         ATTRIB(NexuizMapList, columnNameOrigin, float, 0)
17         ATTRIB(NexuizMapList, columnNameSize, float, 0)
18         ATTRIB(NexuizMapList, checkMarkOrigin, vector, '0 0 0')
19         ATTRIB(NexuizMapList, checkMarkSize, vector, '0 0 0')
20         ATTRIB(NexuizMapList, realUpperMargin1, float, 0)
21         ATTRIB(NexuizMapList, realUpperMargin2, float, 0)
22
23         ATTRIB(NexuizMapList, lastClickedMap, float, -1)
24         ATTRIB(NexuizMapList, lastClickedTime, float, 0)
25
26         ATTRIB(NexuizMapList, lastGametype, float, 0)
27         ATTRIB(NexuizMapList, lastFeatures, float, 0)
28
29         ATTRIB(NexuizMapList, origin, vector, '0 0 0')
30         ATTRIB(NexuizMapList, itemAbsSize, vector, '0 0 0')
31
32         ATTRIB(NexuizMapList, g_maplistCache, string, string_null)
33         METHOD(NexuizMapList, g_maplistCacheToggle, void(entity, float))
34         METHOD(NexuizMapList, g_maplistCacheQuery, float(entity, float))
35
36         ATTRIB(NexuizMapList, startButton, entity, NULL)
37
38         METHOD(NexuizMapList, loadCvars, void(entity))
39
40         ATTRIB(NexuizMapList, typeToSearchString, string, string_null)
41         ATTRIB(NexuizMapList, typeToSearchTime, float, 0)
42
43         METHOD(NexuizMapList, destroy, void(entity))
44
45         ATTRIB(NexuizListBox, alphaBG, float, 0)
46 ENDCLASS(NexuizMapList)
47 entity makeNexuizMapList();
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 destroyNexuizMapList(entity me)
55 {
56         MapInfo_Shutdown();
57 }
58
59 entity makeNexuizMapList()
60 {
61         entity me;
62         me = spawnNexuizMapList();
63         me.configureNexuizMapList(me);
64         return me;
65 }
66
67 void configureNexuizMapListNexuizMapList(entity me)
68 {
69         me.configureNexuizListBox(me);
70         me.refilter(me);
71 }
72
73 void loadCvarsNexuizMapList(entity me)
74 {
75         me.refilter(me);
76 }
77
78 float g_maplistCacheQueryNexuizMapList(entity me, float i)
79 {
80         return stof(substring(me.g_maplistCache, i, 1));
81 }
82 void g_maplistCacheToggleNexuizMapList(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 drawNexuizMapList(entity me)
117 {
118         if(me.startButton)
119                 me.startButton.disabled = ((me.selectedItem < 0) || (me.selectedItem >= me.nItems));
120         drawListBox(me);
121 }
122
123 void resizeNotifyNexuizMapList(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
124 {
125         me.itemAbsSize = '0 0 0';
126         resizeNotifyNexuizListBox(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 clickListBoxItemNexuizMapList(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 drawListBoxItemNexuizMapList(entity me, float i, vector absSize, float isSelected)
167 {
168         // layout: Ping, Map name, Map name, NP, TP, MP
169         string s;
170         float p;
171         float theAlpha;
172         float included;
173
174         if(!MapInfo_Get_ByID(i))
175                 return;
176
177         included = me.g_maplistCacheQuery(me, i);
178         if(included || isSelected)
179                 theAlpha = SKINALPHA_MAPLIST_INCLUDEDFG;
180         else
181                 theAlpha = SKINALPHA_MAPLIST_NOTINCLUDEDFG;
182
183         if(isSelected)
184                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
185         else if(included)
186                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_MAPLIST_INCLUDEDBG, SKINALPHA_MAPLIST_INCLUDEDBG);
187
188         s = ftos(p);
189         draw_Picture(me.columnPreviewOrigin * eX, strcat("/maps/", MapInfo_Map_bspname), me.columnPreviewSize * eX + eY, '1 1 1', theAlpha);
190         if(included)
191                 draw_Picture(me.checkMarkOrigin, "checkmark", me.checkMarkSize, '1 1 1', 1);
192         s = draw_TextShortenToWidth(strcat(MapInfo_Map_bspname, ": ", MapInfo_Map_title), me.columnNameSize, 0, me.realFontSize);
193         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);
194         s = draw_TextShortenToWidth(MapInfo_Map_author, me.columnNameSize, 0,  me.realFontSize);
195         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);
196
197         MapInfo_ClearTemps();
198 }
199
200 void refilterNexuizMapList(entity me)
201 {
202         float i, j, n;
203         string s;
204         float gt, f;
205         gt = MapInfo_CurrentGametype();
206         f = MapInfo_CurrentFeatures();
207         MapInfo_FilterGametype(gt, f, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
208         me.nItems = MapInfo_count;
209         for(i = 0; i < MapInfo_count; ++i)
210                 draw_PreloadPicture(strcat("/maps/", MapInfo_BSPName_ByID(i)));
211         if(me.g_maplistCache)
212                 strunzone(me.g_maplistCache);
213         s = "0";
214         for(i = 1; i < MapInfo_count; i *= 2)
215                 s = strcat(s, s);
216         n = tokenize_console(cvar_string("g_maplist"));
217         for(i = 0; i < n; ++i)
218         {
219                 j = MapInfo_FindName(argv(i));
220                 if(j >= 0)
221                         s = strcat(
222                                 substring(s, 0, j),
223                                 "1",
224                                 substring(s, j+1, MapInfo_count - (j+1))
225                         );
226         }
227         me.g_maplistCache = strzone(s);
228         if(gt != me.lastGametype || f != me.lastFeatures)
229         {
230                 me.lastGametype = gt;
231                 me.lastFeatures = f;
232                 me.setSelected(me, 0);
233         }
234 }
235
236 void refilterCallbackNexuizMapList(entity me, entity cb)
237 {
238         me.refilter(me);
239 }
240
241 void MapList_All(entity btn, entity me)
242 {
243         float i;
244         string s;
245         MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, 0, MAPINFO_FLAG_FORBIDDEN, 0); // all
246         s = "";
247         for(i = 0; i < MapInfo_count; ++i)
248                 s = strcat(s, " ", MapInfo_BSPName_ByID(i));
249         cvar_set("g_maplist", substring(s, 1, strlen(s) - 1));
250         me.refilter(me);
251 }
252
253 void MapList_None(entity btn, entity me)
254 {
255         cvar_set("g_maplist", "");
256         me.refilter(me);
257 }
258
259 void MapList_LoadMap(entity btn, entity me)
260 {
261         string m;
262         float i;
263
264         i = me.selectedItem;
265
266         if(btn.parent.instanceOfNexuizMapInfoDialog)
267         {
268                 i = btn.parent.currentMapIndex;
269                 Dialog_Close(btn, btn.parent);
270         }
271
272         if(i >= me.nItems || i < 0)
273                 return;
274
275         m = MapInfo_BSPName_ByID(i);
276         if not(m)
277         {
278                 print("Huh? Can't play this (m is NULL). Refiltering so this won't happen again.\n");
279                 return;
280         }
281         if(MapInfo_CheckMap(m))
282         {
283                 localcmd("\nmenu_loadmap_prepare\n");
284                 if(cvar("menu_use_default_hostname"))
285                         localcmd("hostname \"", strdecolorize(cvar_string("_cl_name")), "'s Nexuiz server\"\n");
286                 MapInfo_LoadMap(m);
287         }
288         else
289         {
290                 print("Huh? Can't play this (invalid game type). Refiltering so this won't happen again.\n");
291                 me.refilter(me);
292         }
293 }
294
295 float keyDownNexuizMapList(entity me, float scan, float ascii, float shift)
296 {
297         string ch, save;
298         if(scan == K_ENTER)
299         {
300                 // pop up map info screen
301                 main.mapInfoDialog.loadMapInfo(main.mapInfoDialog, me.selectedItem, me);
302                 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));
303         }
304         else if(scan == K_SPACE)
305         {
306                 me.g_maplistCacheToggle(me, me.selectedItem);
307         }
308         else if(ascii == 43) // +
309         {
310                 if not(me.g_maplistCacheQuery(me, me.selectedItem))
311                         me.g_maplistCacheToggle(me, me.selectedItem);
312         }
313         else if(ascii == 45) // -
314         {
315                 if(me.g_maplistCacheQuery(me, me.selectedItem))
316                         me.g_maplistCacheToggle(me, me.selectedItem);
317         }
318         else if(scan == K_BACKSPACE)
319         {
320                 if(time < me.typeToSearchTime)
321                 {
322                         save = substring(me.typeToSearchString, 0, strlen(me.typeToSearchString) - 1);
323                         if(me.typeToSearchString)
324                                 strunzone(me.typeToSearchString);
325                         me.typeToSearchString = strzone(save);
326                         me.typeToSearchTime = time + 0.5;
327                         if(strlen(me.typeToSearchString))
328                         {
329                                 MapInfo_FindName(me.typeToSearchString);
330                                 if(MapInfo_FindName_firstResult >= 0)
331                                         me.setSelected(me, MapInfo_FindName_firstResult);
332                         }
333                 }
334         }
335         else if(ascii >= 32 && ascii != 127)
336         {
337                 ch = chr(ascii);
338                 if(time > me.typeToSearchTime)
339                         save = ch;
340                 else
341                         save = strcat(me.typeToSearchString, ch);
342                 if(me.typeToSearchString)
343                         strunzone(me.typeToSearchString);
344                 me.typeToSearchString = strzone(save);
345                 me.typeToSearchTime = time + 0.5;
346                 MapInfo_FindName(me.typeToSearchString);
347                 if(MapInfo_FindName_firstResult >= 0)
348                         me.setSelected(me, MapInfo_FindName_firstResult);
349         }
350         else
351                 return keyDownListBox(me, scan, ascii, shift);
352         return 1;
353 }
354
355 #endif