]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/menu/nexuiz/maplist.c
reverting the preparations, we first need a stringwidth wrapper, and some things...
[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 ENDCLASS(NexuizMapList)
45 entity makeNexuizMapList();
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 destroyNexuizMapList(entity me)
53 {
54         MapInfo_Shutdown();
55 }
56
57 entity makeNexuizMapList()
58 {
59         entity me;
60         me = spawnNexuizMapList();
61         me.configureNexuizMapList(me);
62         return me;
63 }
64
65 void configureNexuizMapListNexuizMapList(entity me)
66 {
67         me.configureNexuizListBox(me);
68         me.refilter(me);
69 }
70
71 void loadCvarsNexuizMapList(entity me)
72 {
73         me.refilter(me);
74 }
75
76 float g_maplistCacheQueryNexuizMapList(entity me, float i)
77 {
78         return stof(substring(me.g_maplistCache, i, 1));
79 }
80 void g_maplistCacheToggleNexuizMapList(entity me, float i)
81 {
82         string a, b, c, s, bspname;
83         float n;
84         s = me.g_maplistCache;
85         if not(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 not((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 drawNexuizMapList(entity me)
115 {
116         if(me.startButton)
117                 me.startButton.disabled = ((me.selectedItem < 0) || (me.selectedItem >= me.nItems));
118         drawListBox(me);
119 }
120
121 void resizeNotifyNexuizMapList(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
122 {
123         me.itemAbsSize = '0 0 0';
124         resizeNotifyNexuizListBox(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 clickListBoxItemNexuizMapList(entity me, float i, vector where)
141 {
142         if(where_x <= me.columnPreviewOrigin + me.columnPreviewSize)
143         {
144                 if(where_x >= 0)
145                         me.g_maplistCacheToggle(me, i);
146         }
147         if(where_x >= me.columnNameOrigin)
148                 if(where_x <= 1)
149                         {
150                                 if(i == me.lastClickedMap)
151                                         if(time < me.lastClickedTime + 0.3)
152                                         {
153                                                 // DOUBLE CLICK!
154                                                 // pop up map info screen
155                                                 main.mapInfoDialog.loadMapInfo(main.mapInfoDialog, i, me);
156                                                 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));
157                                                 return;
158                                         }
159                                 me.lastClickedMap = i;
160                                 me.lastClickedTime = time;
161                         }
162 }
163
164 void drawListBoxItemNexuizMapList(entity me, float i, vector absSize, float isSelected)
165 {
166         // layout: Ping, Map name, Map name, NP, TP, MP
167         string s;
168         float p;
169         float theAlpha;
170         float included;
171
172         if(!MapInfo_Get_ByID(i))
173                 return;
174
175         included = me.g_maplistCacheQuery(me, i);
176         if(included || isSelected)
177                 theAlpha = SKINALPHA_MAPLIST_INCLUDEDFG;
178         else
179                 theAlpha = SKINALPHA_MAPLIST_NOTINCLUDEDFG;
180
181         if(isSelected)
182                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
183         else if(included)
184                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_MAPLIST_INCLUDEDBG, SKINALPHA_MAPLIST_INCLUDEDBG);
185
186         s = ftos(p);
187         draw_Picture(me.columnPreviewOrigin * eX, strcat("/maps/", MapInfo_Map_bspname), me.columnPreviewSize * eX + eY, '1 1 1', theAlpha);
188         if(included)
189                 draw_Picture(me.checkMarkOrigin, "checkmark", me.checkMarkSize, '1 1 1', 1);
190         s = draw_TextShortenToWidth(strcat(MapInfo_Map_bspname, ": ", MapInfo_Map_title), me.columnNameSize / me.realFontSize_x, 0);
191         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);
192         s = draw_TextShortenToWidth(MapInfo_Map_author, me.columnNameSize / me.realFontSize_x, 0);
193         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);
194
195         MapInfo_ClearTemps();
196 }
197
198 void refilterNexuizMapList(entity me)
199 {
200         float i, j, n;
201         string s;
202         float gt, f;
203         gt = MapInfo_CurrentGametype();
204         f = MapInfo_CurrentFeatures();
205         MapInfo_FilterGametype(gt, f, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
206         me.nItems = MapInfo_count;
207         for(i = 0; i < MapInfo_count; ++i)
208                 draw_PreloadPicture(strcat("/maps/", MapInfo_BSPName_ByID(i)));
209         if(me.g_maplistCache)
210                 strunzone(me.g_maplistCache);
211         s = "0";
212         for(i = 1; i < MapInfo_count; i *= 2)
213                 s = strcat(s, s);
214         n = tokenize_console(cvar_string("g_maplist"));
215         for(i = 0; i < n; ++i)
216         {
217                 j = MapInfo_FindName(argv(i));
218                 if(j >= 0)
219                         s = strcat(
220                                 substring(s, 0, j),
221                                 "1",
222                                 substring(s, j+1, MapInfo_count - (j+1))
223                         );
224         }
225         me.g_maplistCache = strzone(s);
226         if(gt != me.lastGametype || f != me.lastFeatures)
227         {
228                 me.lastGametype = gt;
229                 me.lastFeatures = f;
230                 me.setSelected(me, 0);
231         }
232 }
233
234 void refilterCallbackNexuizMapList(entity me, entity cb)
235 {
236         me.refilter(me);
237 }
238
239 void MapList_All(entity btn, entity me)
240 {
241         float i;
242         string s;
243         MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, 0, MAPINFO_FLAG_FORBIDDEN, 0); // all
244         s = "";
245         for(i = 0; i < MapInfo_count; ++i)
246                 s = strcat(s, " ", MapInfo_BSPName_ByID(i));
247         cvar_set("g_maplist", substring(s, 1, strlen(s) - 1));
248         me.refilter(me);
249 }
250
251 void MapList_None(entity btn, entity me)
252 {
253         cvar_set("g_maplist", "");
254         me.refilter(me);
255 }
256
257 void MapList_LoadMap(entity btn, entity me)
258 {
259         string m;
260         float i;
261
262         i = me.selectedItem;
263
264         if(btn.parent.instanceOfNexuizMapInfoDialog)
265         {
266                 i = btn.parent.currentMapIndex;
267                 Dialog_Close(btn, btn.parent);
268         }
269
270         if(i >= me.nItems || i < 0)
271                 return;
272
273         m = MapInfo_BSPName_ByID(i);
274         if not(m)
275         {
276                 print("Huh? Can't play this (m is NULL). Refiltering so this won't happen again.\n");
277                 return;
278         }
279         if(MapInfo_CheckMap(m))
280         {
281                 localcmd("\nmenu_loadmap_prepare\n");
282                 if(cvar("menu_use_default_hostname"))
283                         localcmd("hostname \"", strdecolorize(cvar_string("_cl_name")), "'s Nexuiz server\"\n");
284                 MapInfo_LoadMap(m);
285         }
286         else
287         {
288                 print("Huh? Can't play this (invalid game type). Refiltering so this won't happen again.\n");
289                 me.refilter(me);
290         }
291 }
292
293 float keyDownNexuizMapList(entity me, float scan, float ascii, float shift)
294 {
295         string ch, save;
296         if(scan == K_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_SPACE)
303         {
304                 me.g_maplistCacheToggle(me, me.selectedItem);
305         }
306         else if(ascii == 43) // +
307         {
308                 if not(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 keyDownListBox(me, scan, ascii, shift);
350         return 1;
351 }
352
353 #endif