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