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