]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/maplist.qc
Listbox / Picker: Implement item fading in a different way so that it gets influenced...
[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, bool))
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, bool isFocused)
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(isFocused)
185                 {
186                         me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
187                         draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
188                 }
189         }
190
191         if(draw_PictureSize(strcat("/maps/", MapInfo_Map_bspname)) == '0 0 0')
192                 draw_Picture(me.columnPreviewOrigin * eX, "nopreview_map", me.columnPreviewSize * eX + eY, '1 1 1', theAlpha);
193         else
194                 draw_Picture(me.columnPreviewOrigin * eX, strcat("/maps/", MapInfo_Map_bspname), me.columnPreviewSize * eX + eY, '1 1 1', theAlpha);
195
196         if(included)
197                 draw_Picture(me.checkMarkOrigin, "checkmark", me.checkMarkSize, '1 1 1', 1);
198         s = draw_TextShortenToWidth(strdecolorize(MapInfo_Map_titlestring), me.columnNameSize, 0, me.realFontSize);
199         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);
200         s = draw_TextShortenToWidth(strdecolorize(MapInfo_Map_author), me.columnNameSize, 0,  me.realFontSize);
201         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);
202
203         MapInfo_ClearTemps();
204 }
205
206 void XonoticMapList_refilter(entity me)
207 {
208         float i, j, n;
209         string s;
210         float gt, f;
211         gt = MapInfo_CurrentGametype();
212         f = MapInfo_CurrentFeatures();
213         MapInfo_FilterGametype(gt, f, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
214         me.nItems = MapInfo_count;
215         for(i = 0; i < MapInfo_count; ++i)
216                 draw_PreloadPicture(strcat("/maps/", MapInfo_BSPName_ByID(i)));
217         if(me.g_maplistCache)
218                 strunzone(me.g_maplistCache);
219         s = "0";
220         for(i = 1; i < MapInfo_count; i *= 2)
221                 s = strcat(s, s);
222         n = tokenize_console(cvar_string("g_maplist"));
223         for(i = 0; i < n; ++i)
224         {
225                 j = MapInfo_FindName(argv(i));
226                 if(j >= 0)
227                         s = strcat(
228                                 substring(s, 0, j),
229                                 "1",
230                                 substring(s, j+1, MapInfo_count - (j+1))
231                         );
232         }
233         me.g_maplistCache = strzone(s);
234         if(gt != me.lastGametype || f != me.lastFeatures)
235         {
236                 me.lastGametype = gt;
237                 me.lastFeatures = f;
238                 me.setSelected(me, 0);
239         }
240 }
241
242 void XonoticMapList_refilterCallback(entity me, entity cb)
243 {
244         me.refilter(me);
245 }
246
247 void MapList_All(entity btn, entity me)
248 {
249         float i;
250         string s;
251         MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, 0, MapInfo_ForbiddenFlags(), 0); // all
252         s = "";
253         for(i = 0; i < MapInfo_count; ++i)
254                 s = strcat(s, " ", MapInfo_BSPName_ByID(i));
255         cvar_set("g_maplist", substring(s, 1, strlen(s) - 1));
256         me.refilter(me);
257 }
258
259 void MapList_None(entity btn, entity me)
260 {
261         cvar_set("g_maplist", "");
262         me.refilter(me);
263 }
264
265 void MapList_LoadMap(entity btn, entity me)
266 {
267         string m;
268         float i;
269
270         i = me.selectedItem;
271
272         if(btn.parent.instanceOfXonoticMapInfoDialog)
273         {
274                 i = btn.parent.currentMapIndex;
275                 Dialog_Close(btn, btn.parent);
276         }
277
278         if(i >= me.nItems || i < 0)
279                 return;
280
281         m = MapInfo_BSPName_ByID(i);
282         if (!m)
283         {
284                 print(_("Huh? Can't play this (m is NULL). Refiltering so this won't happen again.\n"));
285                 me.refilter(me);
286                 return;
287         }
288         if(MapInfo_CheckMap(m))
289         {
290                 localcmd("\nmenu_loadmap_prepare\n");
291                 if(cvar("menu_use_default_hostname"))
292                         localcmd("hostname \"", sprintf(_("%s's Xonotic Server"), strdecolorize(cvar_string("_cl_name"))), "\"\n");
293                 MapInfo_LoadMap(m, 1);
294         }
295         else
296         {
297                 print(_("Huh? Can't play this (invalid game type). Refiltering so this won't happen again.\n"));
298                 me.refilter(me);
299                 return;
300         }
301 }
302
303 float XonoticMapList_keyDown(entity me, float scan, float ascii, float shift)
304 {
305         string ch, save;
306         if(me.nItems <= 0)
307                 return SUPER(XonoticMapList).keyDown(me, scan, ascii, shift);
308         if(scan == K_MOUSE2 || scan == K_SPACE || scan == K_ENTER || scan == K_KP_ENTER)
309         {
310                 // pop up map info screen
311                 m_play_click_sound(MENU_SOUND_OPEN);
312                 main.mapInfoDialog.loadMapInfo(main.mapInfoDialog, me.selectedItem, me);
313                 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));
314         }
315         else if(scan == K_MOUSE3 || scan == K_INS || scan == K_KP_INS)
316         {
317                 m_play_click_sound(MENU_SOUND_SELECT);
318                 me.g_maplistCacheToggle(me, me.selectedItem);
319         }
320         else if(ascii == 43) // +
321         {
322                 if (!me.g_maplistCacheQuery(me, me.selectedItem))
323                 {
324                         m_play_click_sound(MENU_SOUND_SELECT);
325                         me.g_maplistCacheToggle(me, me.selectedItem);
326                 }
327         }
328         else if(ascii == 45) // -
329         {
330                 if(me.g_maplistCacheQuery(me, me.selectedItem))
331                 {
332                         m_play_click_sound(MENU_SOUND_SELECT);
333                         me.g_maplistCacheToggle(me, me.selectedItem);
334                 }
335         }
336         else if(scan == K_BACKSPACE)
337         {
338                 if(time < me.typeToSearchTime)
339                 {
340                         save = substring(me.typeToSearchString, 0, strlen(me.typeToSearchString) - 1);
341                         if(me.typeToSearchString)
342                                 strunzone(me.typeToSearchString);
343                         me.typeToSearchString = strzone(save);
344                         me.typeToSearchTime = time + 0.5;
345                         if(strlen(me.typeToSearchString))
346                         {
347                                 MapInfo_FindName(me.typeToSearchString);
348                                 if(MapInfo_FindName_firstResult >= 0)
349                                         me.setSelected(me, MapInfo_FindName_firstResult);
350                         }
351                 }
352         }
353         else if(ascii >= 32 && ascii != 127)
354         {
355                 ch = chr(ascii);
356                 if(time > me.typeToSearchTime)
357                         save = ch;
358                 else
359                         save = strcat(me.typeToSearchString, ch);
360                 if(me.typeToSearchString)
361                         strunzone(me.typeToSearchString);
362                 me.typeToSearchString = strzone(save);
363                 me.typeToSearchTime = time + 0.5;
364                 MapInfo_FindName(me.typeToSearchString);
365                 if(MapInfo_FindName_firstResult >= 0)
366                         me.setSelected(me, MapInfo_FindName_firstResult);
367         }
368         else
369                 return SUPER(XonoticMapList).keyDown(me, scan, ascii, shift);
370         return 1;
371 }
372
373 #endif