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