]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/maplist.qc
Sort menu classes
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / maplist.qc
1 #ifndef MAPLIST_H
2 #define MAPLIST_H
3 CLASS(XonoticMapList, XonoticListBox)
4         METHOD(XonoticMapList, configureXonoticMapList, void(entity))
5         ATTRIB(XonoticMapList, rowsPerItem, float, 4)
6         METHOD(XonoticMapList, draw, void(entity))
7         METHOD(XonoticMapList, drawListBoxItem, void(entity, float, vector, float))
8         METHOD(XonoticMapList, clickListBoxItem, void(entity, float, vector))
9         METHOD(XonoticMapList, doubleClickListBoxItem, void(entity, float, vector))
10         METHOD(XonoticMapList, resizeNotify, void(entity, vector, vector, vector, vector))
11         METHOD(XonoticMapList, refilter, void(entity))
12         METHOD(XonoticMapList, refilterCallback, void(entity, entity))
13         METHOD(XonoticMapList, keyDown, float(entity, float, float, float))
14
15         ATTRIB(XonoticMapList, realFontSize, vector, '0 0 0')
16         ATTRIB(XonoticMapList, columnPreviewOrigin, float, 0)
17         ATTRIB(XonoticMapList, columnPreviewSize, float, 0)
18         ATTRIB(XonoticMapList, columnNameOrigin, float, 0)
19         ATTRIB(XonoticMapList, columnNameSize, float, 0)
20         ATTRIB(XonoticMapList, checkMarkOrigin, vector, '0 0 0')
21         ATTRIB(XonoticMapList, checkMarkSize, vector, '0 0 0')
22         ATTRIB(XonoticMapList, realUpperMargin1, float, 0)
23         ATTRIB(XonoticMapList, realUpperMargin2, float, 0)
24
25         ATTRIB(XonoticMapList, lastGametype, float, 0)
26         ATTRIB(XonoticMapList, lastFeatures, float, 0)
27
28         ATTRIB(XonoticMapList, origin, vector, '0 0 0')
29         ATTRIB(XonoticMapList, itemAbsSize, vector, '0 0 0')
30
31         ATTRIB(XonoticMapList, g_maplistCache, string, string_null)
32         METHOD(XonoticMapList, g_maplistCacheToggle, void(entity, float))
33         METHOD(XonoticMapList, g_maplistCacheQuery, float(entity, float))
34
35         ATTRIB(XonoticMapList, startButton, entity, NULL)
36
37         METHOD(XonoticMapList, loadCvars, void(entity))
38
39         ATTRIB(XonoticMapList, typeToSearchString, string, string_null)
40         ATTRIB(XonoticMapList, typeToSearchTime, float, 0)
41
42         METHOD(XonoticMapList, destroy, void(entity))
43
44         ATTRIB(XonoticMapList, alphaBG, float, 0)
45 ENDCLASS(XonoticMapList)
46 entity makeXonoticMapList();
47 void MapList_All(entity btn, entity me);
48 void MapList_None(entity btn, entity me);
49 void MapList_LoadMap(entity btn, entity me);
50 #endif
51
52 #ifdef IMPLEMENTATION
53 void XonoticMapList_destroy(entity me)
54 {
55         MapInfo_Shutdown();
56 }
57
58 entity makeXonoticMapList()
59 {
60         entity me;
61         me = NEW(XonoticMapList);
62         me.configureXonoticMapList(me);
63         return me;
64 }
65
66 void XonoticMapList_configureXonoticMapList(entity me)
67 {
68         me.configureXonoticListBox(me);
69         me.refilter(me);
70 }
71
72 void XonoticMapList_loadCvars(entity me)
73 {
74         me.refilter(me);
75 }
76
77 float XonoticMapList_g_maplistCacheQuery(entity me, float i)
78 {
79         return stof(substring(me.g_maplistCache, i, 1));
80 }
81 void XonoticMapList_g_maplistCacheToggle(entity me, float i)
82 {
83         string a, b, c, s, bspname;
84         float n;
85         s = me.g_maplistCache;
86         if (!s)
87                 return;
88         b = substring(s, i, 1);
89         if(b == "0")
90                 b = "1";
91         else if(b == "1")
92                 b = "0";
93         else
94                 return; // nothing happens
95         a = substring(s, 0, i);
96         c = substring(s, i+1, strlen(s) - (i+1));
97         strunzone(s);
98         me.g_maplistCache = strzone(strcat(a, b, c));
99         // TODO also update the actual cvar
100         if (!((bspname = MapInfo_BSPName_ByID(i))))
101                 return;
102         if(b == "1")
103                 cvar_set("g_maplist", strcat(bspname, " ", cvar_string("g_maplist")));
104         else
105         {
106                 s = "";
107                 n = tokenize_console(cvar_string("g_maplist"));
108                 for(i = 0; i < n; ++i)
109                         if(argv(i) != bspname)
110                                 s = strcat(s, " ", argv(i));
111                 cvar_set("g_maplist", substring(s, 1, strlen(s) - 1));
112         }
113 }
114
115 void XonoticMapList_draw(entity me)
116 {
117         if(me.startButton)
118                 me.startButton.disabled = ((me.selectedItem < 0) || (me.selectedItem >= me.nItems));
119         SUPER(XonoticMapList).draw(me);
120 }
121
122 void XonoticMapList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
123 {
124         me.itemAbsSize = '0 0 0';
125         SUPER(XonoticMapList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
126
127         me.realFontSize_y = me.fontSize / (me.itemAbsSize_y = (absSize.y * me.itemHeight));
128         me.realFontSize_x = me.fontSize / (me.itemAbsSize_x = (absSize.x * (1 - me.controlWidth)));
129         me.realUpperMargin1 = 0.5 * (1 - 2.5 * me.realFontSize.y);
130         me.realUpperMargin2 = me.realUpperMargin1 + 1.5 * me.realFontSize.y;
131
132         me.columnPreviewOrigin = 0;
133         me.columnPreviewSize = me.itemAbsSize.y / me.itemAbsSize.x * 4 / 3;
134         me.columnNameOrigin = me.columnPreviewOrigin + me.columnPreviewSize + me.realFontSize.x;
135         me.columnNameSize = 1 - me.columnPreviewSize - 2 * me.realFontSize.x;
136
137         me.checkMarkSize = (eX * (me.itemAbsSize.y / me.itemAbsSize.x) + eY) * 0.5;
138         me.checkMarkOrigin = eY + eX * (me.columnPreviewOrigin + me.columnPreviewSize) - me.checkMarkSize;
139 }
140
141 void XonoticMapList_clickListBoxItem(entity me, float i, vector where)
142 {
143         if(where.x <= me.columnPreviewOrigin + me.columnPreviewSize)
144                 if(where.x >= 0)
145                 {
146                         m_play_click_sound(MENU_SOUND_SELECT);
147                         me.g_maplistCacheToggle(me, i);
148                 }
149 }
150
151 void XonoticMapList_doubleClickListBoxItem(entity me, float i, vector where)
152 {
153         if(where.x >= me.columnNameOrigin)
154                 if(where.x <= 1)
155                 {
156                         // pop up map info screen
157                         m_play_click_sound(MENU_SOUND_OPEN);
158                         main.mapInfoDialog.loadMapInfo(main.mapInfoDialog, i, me);
159                         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));
160                 }
161 }
162
163 void XonoticMapList_drawListBoxItem(entity me, float i, vector absSize, float isSelected)
164 {
165         // layout: Ping, Map name, Map name, NP, TP, MP
166         string s;
167         float theAlpha;
168         float included;
169
170         if(!MapInfo_Get_ByID(i))
171                 return;
172
173         included = me.g_maplistCacheQuery(me, i);
174         if(included || isSelected)
175                 theAlpha = SKINALPHA_MAPLIST_INCLUDEDFG;
176         else
177                 theAlpha = SKINALPHA_MAPLIST_NOTINCLUDEDFG;
178
179         if(isSelected)
180                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
181         else if(included)
182                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_MAPLIST_INCLUDEDBG, SKINALPHA_MAPLIST_INCLUDEDBG);
183
184         if(draw_PictureSize(strcat("/maps/", MapInfo_Map_bspname)) == '0 0 0')
185                 draw_Picture(me.columnPreviewOrigin * eX, "nopreview_map", me.columnPreviewSize * eX + eY, '1 1 1', theAlpha);
186         else
187                 draw_Picture(me.columnPreviewOrigin * eX, strcat("/maps/", MapInfo_Map_bspname), me.columnPreviewSize * eX + eY, '1 1 1', theAlpha);
188
189         if(included)
190                 draw_Picture(me.checkMarkOrigin, "checkmark", me.checkMarkSize, '1 1 1', 1);
191         s = draw_TextShortenToWidth(strdecolorize(MapInfo_Map_titlestring), me.columnNameSize, 0, me.realFontSize);
192         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);
193         s = draw_TextShortenToWidth(strdecolorize(MapInfo_Map_author), me.columnNameSize, 0,  me.realFontSize);
194         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);
195
196         MapInfo_ClearTemps();
197 }
198
199 void XonoticMapList_refilter(entity me)
200 {
201         float i, j, n;
202         string s;
203         float gt, f;
204         gt = MapInfo_CurrentGametype();
205         f = MapInfo_CurrentFeatures();
206         MapInfo_FilterGametype(gt, f, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
207         me.nItems = MapInfo_count;
208         for(i = 0; i < MapInfo_count; ++i)
209                 draw_PreloadPicture(strcat("/maps/", MapInfo_BSPName_ByID(i)));
210         if(me.g_maplistCache)
211                 strunzone(me.g_maplistCache);
212         s = "0";
213         for(i = 1; i < MapInfo_count; i *= 2)
214                 s = strcat(s, s);
215         n = tokenize_console(cvar_string("g_maplist"));
216         for(i = 0; i < n; ++i)
217         {
218                 j = MapInfo_FindName(argv(i));
219                 if(j >= 0)
220                         s = strcat(
221                                 substring(s, 0, j),
222                                 "1",
223                                 substring(s, j+1, MapInfo_count - (j+1))
224                         );
225         }
226         me.g_maplistCache = strzone(s);
227         if(gt != me.lastGametype || f != me.lastFeatures)
228         {
229                 me.lastGametype = gt;
230                 me.lastFeatures = f;
231                 me.setSelected(me, 0);
232         }
233 }
234
235 void XonoticMapList_refilterCallback(entity me, entity cb)
236 {
237         me.refilter(me);
238 }
239
240 void MapList_All(entity btn, entity me)
241 {
242         float i;
243         string s;
244         MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, 0, MapInfo_ForbiddenFlags(), 0); // all
245         s = "";
246         for(i = 0; i < MapInfo_count; ++i)
247                 s = strcat(s, " ", MapInfo_BSPName_ByID(i));
248         cvar_set("g_maplist", substring(s, 1, strlen(s) - 1));
249         me.refilter(me);
250 }
251
252 void MapList_None(entity btn, entity me)
253 {
254         cvar_set("g_maplist", "");
255         me.refilter(me);
256 }
257
258 void MapList_LoadMap(entity btn, entity me)
259 {
260         string m;
261         float i;
262
263         i = me.selectedItem;
264
265         if(btn.parent.instanceOfXonoticMapInfoDialog)
266         {
267                 i = btn.parent.currentMapIndex;
268                 Dialog_Close(btn, btn.parent);
269         }
270
271         if(i >= me.nItems || i < 0)
272                 return;
273
274         m = MapInfo_BSPName_ByID(i);
275         if (!m)
276         {
277                 print(_("Huh? Can't play this (m is NULL). Refiltering so this won't happen again.\n"));
278                 me.refilter(me);
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 \"", sprintf(_("%s's Xonotic Server"), strdecolorize(cvar_string("_cl_name"))), "\"\n");
286                 MapInfo_LoadMap(m, 1);
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                 return;
293         }
294 }
295
296 float XonoticMapList_keyDown(entity me, float scan, float ascii, float shift)
297 {
298         string ch, save;
299         if(me.nItems <= 0)
300                 return SUPER(XonoticMapList).keyDown(me, scan, ascii, shift);
301         if(scan == K_MOUSE2 || scan == K_SPACE || scan == K_ENTER || scan == K_KP_ENTER)
302         {
303                 // pop up map info screen
304                 m_play_click_sound(MENU_SOUND_OPEN);
305                 main.mapInfoDialog.loadMapInfo(main.mapInfoDialog, me.selectedItem, me);
306                 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));
307         }
308         else if(scan == K_MOUSE3 || scan == K_INS || scan == K_KP_INS)
309         {
310                 m_play_click_sound(MENU_SOUND_SELECT);
311                 me.g_maplistCacheToggle(me, me.selectedItem);
312         }
313         else if(ascii == 43) // +
314         {
315                 if (!me.g_maplistCacheQuery(me, me.selectedItem))
316                 {
317                         m_play_click_sound(MENU_SOUND_SELECT);
318                         me.g_maplistCacheToggle(me, me.selectedItem);
319                 }
320         }
321         else if(ascii == 45) // -
322         {
323                 if(me.g_maplistCacheQuery(me, me.selectedItem))
324                 {
325                         m_play_click_sound(MENU_SOUND_SELECT);
326                         me.g_maplistCacheToggle(me, me.selectedItem);
327                 }
328         }
329         else if(scan == K_BACKSPACE)
330         {
331                 if(time < me.typeToSearchTime)
332                 {
333                         save = substring(me.typeToSearchString, 0, strlen(me.typeToSearchString) - 1);
334                         if(me.typeToSearchString)
335                                 strunzone(me.typeToSearchString);
336                         me.typeToSearchString = strzone(save);
337                         me.typeToSearchTime = time + 0.5;
338                         if(strlen(me.typeToSearchString))
339                         {
340                                 MapInfo_FindName(me.typeToSearchString);
341                                 if(MapInfo_FindName_firstResult >= 0)
342                                         me.setSelected(me, MapInfo_FindName_firstResult);
343                         }
344                 }
345         }
346         else if(ascii >= 32 && ascii != 127)
347         {
348                 ch = chr(ascii);
349                 if(time > me.typeToSearchTime)
350                         save = ch;
351                 else
352                         save = strcat(me.typeToSearchString, ch);
353                 if(me.typeToSearchString)
354                         strunzone(me.typeToSearchString);
355                 me.typeToSearchString = strzone(save);
356                 me.typeToSearchTime = time + 0.5;
357                 MapInfo_FindName(me.typeToSearchString);
358                 if(MapInfo_FindName_firstResult >= 0)
359                         me.setSelected(me, MapInfo_FindName_firstResult);
360         }
361         else
362                 return SUPER(XonoticMapList).keyDown(me, scan, ascii, shift);
363         return 1;
364 }
365
366 #endif