]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/menu-div0test/nexuiz/maplist.c
add map info dialog
[divverent/nexuiz.git] / data / qcsrc / menu-div0test / 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, drawListBoxItem, void(entity, float, vector, float))
6         METHOD(NexuizMapList, clickListBoxItem, void(entity, float, vector))
7         METHOD(NexuizMapList, resizeNotify, void(entity, vector, vector, vector, vector))
8         METHOD(NexuizMapList, refilter, void(entity))
9
10         ATTRIB(NexuizMapList, realFontSize, vector, '0 0 0')
11         ATTRIB(NexuizMapList, columnPreviewOrigin, float, 0)
12         ATTRIB(NexuizMapList, columnPreviewSize, float, 0)
13         ATTRIB(NexuizMapList, columnNameOrigin, float, 0)
14         ATTRIB(NexuizMapList, columnNameSize, float, 0)
15         ATTRIB(NexuizMapList, checkMarkOrigin, vector, '0 0 0')
16         ATTRIB(NexuizMapList, checkMarkSize, vector, '0 0 0')
17         ATTRIB(NexuizMapList, realUpperMargin1, float, 0)
18         ATTRIB(NexuizMapList, realUpperMargin2, float, 0)
19
20         ATTRIB(NexuizMapList, lastClickedMap, float, -1)
21         ATTRIB(NexuizMapList, lastClickedTime, float, 0)
22
23         ATTRIB(NexuizMapList, lastGametype, float, 0)
24         ATTRIB(NexuizMapList, lastFeatures, float, 0)
25
26         ATTRIB(NexuizMapList, origin, vector, '0 0 0')
27         ATTRIB(NexuizMapList, itemAbsSize, vector, '0 0 0')
28
29         ATTRIB(NexuizMapList, g_maplistCache, string, string_null)
30         METHOD(NexuizMapList, g_maplistCacheToggle, void(entity, float))
31         METHOD(NexuizMapList, g_maplistCacheQuery, float(entity, float))
32 ENDCLASS(NexuizMapList)
33 entity makeNexuizMapList();
34 void MapList_All(entity btn, entity me);
35 void MapList_None(entity btn, entity me);
36 #endif
37
38 #ifdef IMPLEMENTATION
39 entity makeNexuizMapList()
40 {
41         entity me;
42         me = spawnNexuizMapList();
43         me.configureNexuizMapList(me);
44         return me;
45 }
46 void configureNexuizMapListNexuizMapList(entity me)
47 {
48         me.configureNexuizListBox(me);
49         me.refilter(me);
50 }
51
52 float g_maplistCacheQueryNexuizMapList(entity me, float i)
53 {
54         return stof(substring(me.g_maplistCache, i, 1));
55 }
56 void g_maplistCacheToggleNexuizMapList(entity me, float i)
57 {
58         string a, b, c, s, bspname;
59         float n;
60         s = me.g_maplistCache;
61         if not(s)
62                 return;
63         b = substring(s, i, 1);
64         if(b == "0")
65                 b = "1";
66         else if(b == "1")
67                 b = "0";
68         else
69                 return; // nothing happens
70         a = substring(s, 0, i);
71         c = substring(s, i+1, strlen(s) - (i+1));
72         strunzone(s);
73         me.g_maplistCache = strzone(strcat(a, b, c));
74         // TODO also update the actual cvar
75         if not((bspname = MapInfo_BSPName_ByID(i)))
76                 return;
77         if(b == "1")
78                 cvar_set("g_maplist", strcat(bspname, " ", cvar_string("g_maplist")));
79         else
80         {
81                 s = "";
82                 n = tokenize(cvar_string("g_maplist"));
83                 for(i = 0; i < n; ++i)
84                         if(argv(i) != bspname)
85                                 s = strcat(s, " ", argv(i));
86                 cvar_set("g_maplist", substring(s, 1, strlen(s) - 1));
87         }
88 }
89
90 void resizeNotifyNexuizMapList(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
91 {
92         me.origin = absOrigin;
93         me.itemAbsSize = '0 0 0';
94         resizeNotifyNexuizListBox(me, relOrigin, relSize, absOrigin, absSize);
95
96         me.realFontSize_y = me.fontSize / (me.itemAbsSize_y = (absSize_y * me.itemHeight));
97         me.realFontSize_x = me.fontSize / (me.itemAbsSize_x = (absSize_x * (1 - me.controlWidth)));
98         me.realUpperMargin1 = 0.5 * (1 - 2.5 * me.realFontSize_y);
99         me.realUpperMargin2 = me.realUpperMargin1 + 1.5 * me.realFontSize_y;
100
101         me.columnPreviewOrigin = 0;
102         me.columnPreviewSize = me.itemAbsSize_y / me.itemAbsSize_x * 4 / 3;
103         me.columnNameOrigin = me.columnPreviewOrigin + me.columnPreviewSize + me.realFontSize_x;
104         me.columnNameSize = 1 - me.columnPreviewSize - 2 * me.realFontSize_x;
105
106         me.checkMarkSize = (eX * (me.itemAbsSize_y / me.itemAbsSize_x) + eY) * 0.5;
107         me.checkMarkOrigin = eY + eX * (me.columnPreviewOrigin + me.columnPreviewSize) - me.checkMarkSize;
108 }
109 void clickListBoxItemNexuizMapList(entity me, float i, vector where)
110 {
111         if(where_x <= me.columnPreviewOrigin + me.columnPreviewSize)
112         {
113                 if(where_x >= 0)
114                         me.g_maplistCacheToggle(me, i);
115         }
116         if(where_x >= me.columnNameOrigin)
117                 if(where_x <= 1)
118                         {
119                                 if(i == me.lastClickedMap)
120                                         if(time < me.lastClickedTime + 0.3)
121                                         {
122                                                 // DOUBLE CLICK!
123                                                 // pop up map info screen
124                                                 main.mapInfoDialog.loadMapInfo(main.mapInfoDialog, i);
125                                                 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));
126                                                 return;
127                                         }
128                                 me.lastClickedMap = i;
129                                 me.lastClickedTime = time;
130                         }
131 }
132 void drawListBoxItemNexuizMapList(entity me, float i, vector absSize, float isSelected)
133 {
134         // layout: Ping, Map name, Map name, NP, TP, MP
135         string s;
136         float p;
137         vector theColor;
138         float theAlpha;
139         float included;
140
141         theColor = '1 1 1';
142
143         if(!MapInfo_Get_ByID(i))
144                 return;
145
146         included = me.g_maplistCacheQuery(me, i);
147         if(included)
148                 theAlpha = 1;
149         else
150                 theAlpha = 0.4;
151
152         if(isSelected)
153                 draw_Fill('0 0 0', '1 1 0', '0 0 1', 0.5);
154         else if(included)
155                 draw_Fill('0 0 0', '1 1 0', '0 0 0', 0.5);
156
157         s = ftos(p);
158         draw_Picture(me.columnPreviewOrigin * eX, strcat("/maps/", MapInfo_Map_bspname), me.columnPreviewSize * eX + eY, '1 1 1', theAlpha);
159         if(included)
160                 draw_Picture(me.checkMarkOrigin, "checkmark", me.checkMarkSize, '1 1 1', 1);
161         s = draw_TextShortenToWidth(strcat(MapInfo_Map_bspname, ": ", MapInfo_Map_title), me.columnNameSize / me.realFontSize_x, 0);
162         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);
163         s = draw_TextShortenToWidth(MapInfo_Map_author, me.columnNameSize / me.realFontSize_x, 0);
164         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);
165 }
166
167 void refilterNexuizMapList(entity me)
168 {
169         float i, j, n;
170         string s;
171         float gt, f;
172         gt = MapInfo_CurrentGametype();
173         f = MapInfo_CurrentFeatures();
174         MapInfo_FilterGametype(gt, f);
175         me.nItems = MapInfo_count;
176         for(i = 0; i < MapInfo_count; ++i)
177                 draw_PreloadPicture(strcat("/maps/", MapInfo_BSPName_ByID(i)));
178         if(me.g_maplistCache)
179                 strunzone(me.g_maplistCache);
180         s = "0";
181         for(i = 1; i < MapInfo_count; i *= 2)
182                 s = strcat(s, s);
183         n = tokenize(cvar_string("g_maplist"));
184         for(i = 0; i < n; ++i)
185         {
186                 j = MapInfo_FindName(argv(i));
187                 if(j >= 0)
188                         s = strcat(
189                                 substring(s, 0, j),
190                                 "1",
191                                 substring(s, j+1, MapInfo_count - (j+1))
192                         );
193         }
194         me.g_maplistCache = strzone(s);
195         if(gt != me.lastGametype || f != me.lastFeatures)
196         {
197                 me.lastGametype = gt;
198                 me.lastFeatures = f;
199                 me.setSelected(me, 0);
200         }
201 }
202 void MapList_All(entity btn, entity me)
203 {
204         float i;
205         string s;
206         MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0); // all
207         s = "";
208         for(i = 0; i < MapInfo_count; ++i)
209                 s = strcat(s, " ", MapInfo_BSPName_ByID(i));
210         cvar_set("g_maplist", substring(s, 1, strlen(s) - 1));
211         me.refilter(me);
212 }
213 void MapList_None(entity btn, entity me)
214 {
215         cvar_set("g_maplist", "");
216         me.refilter(me);
217 }
218 #endif