]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/menu-div0test/nexuiz/maplist.c
add server browser, and somewhat broken font. When will Ubuntu fix perl-fu?
[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, g_maplistCache, string, string_null)
24         METHOD(NexuizMapList, g_maplistCacheToggle, void(entity, float))
25         METHOD(NexuizMapList, g_maplistCacheQuery, float(entity, float))
26 ENDCLASS(NexuizMapList)
27 entity makeNexuizMapList();
28 #endif
29
30 #ifdef IMPLEMENTATION
31 entity makeNexuizMapList()
32 {
33         entity me;
34         me = spawnNexuizMapList();
35         me.configureNexuizMapList(me);
36         return me;
37 }
38 void configureNexuizMapListNexuizMapList(entity me)
39 {
40         me.configureNexuizListBox(me);
41         me.refilter(me);
42 }
43
44 float g_maplistCacheQueryNexuizMapList(entity me, float i)
45 {
46         return stof(substring(me.g_maplistCache, i, 1));
47 }
48 void g_maplistCacheToggleNexuizMapList(entity me, float i)
49 {
50         string a, b, c, s;
51         s = me.g_maplistCache;
52         if not(s)
53                 return;
54         b = substring(s, i, 1);
55         if(b == "0")
56                 b = "1";
57         else if(b == "1")
58                 b = "0";
59         else
60                 return; // nothing happens
61         a = substring(s, 0, i);
62         c = substring(s, i+1, strlen(s) - (i+1));
63         strunzone(s);
64         me.g_maplistCache = strzone(strcat(a, b, c));
65 }
66
67 void resizeNotifyNexuizMapList(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
68 {
69         vector itemAbsSize;
70         itemAbsSize = '0 0 0';
71
72         resizeNotifyNexuizListBox(me, relOrigin, relSize, absOrigin, absSize);
73
74         me.realFontSize_y = me.fontSize / (itemAbsSize_y = (absSize_y * me.itemHeight));
75         me.realFontSize_x = me.fontSize / (itemAbsSize_x = (absSize_x * (1 - me.controlWidth)));
76         me.realUpperMargin1 = 0.5 * (1 - 2.5 * me.realFontSize_y);
77         me.realUpperMargin2 = me.realUpperMargin1 + 1.5 * me.realFontSize_y;
78
79         me.columnPreviewOrigin = 0;
80         me.columnPreviewSize = itemAbsSize_y / itemAbsSize_x * 4 / 3;
81         me.columnNameOrigin = me.columnPreviewOrigin + me.columnPreviewSize + me.realFontSize_x;
82         me.columnNameSize = 1 - me.columnPreviewSize - 2 * me.realFontSize_x;
83
84         me.checkMarkSize = (eX * (itemAbsSize_y / itemAbsSize_x) + eY) * 0.5;
85         me.checkMarkOrigin = eY + eX * (me.columnPreviewOrigin + me.columnPreviewSize) - me.checkMarkSize;
86 }
87 void clickListBoxItemNexuizMapList(entity me, float i, vector where)
88 {
89         if(i == me.lastClickedMap)
90                 if(time < me.lastClickedTime + 0.3)
91                 {
92                         // DOUBLE CLICK!
93                         // insert/remove into/from maplist
94                         me.g_maplistCacheToggle(me, i);
95                         // TODO also update the actual cvar
96                 }
97         me.lastClickedMap = i;
98         me.lastClickedTime = time;
99 }
100 void drawListBoxItemNexuizMapList(entity me, float i, vector absSize, float isSelected)
101 {
102         // layout: Ping, Map name, Map name, NP, TP, MP
103         string s;
104         float p;
105         vector theColor;
106         float theAlpha;
107         float included;
108
109         theColor = '1 1 1';
110
111         if(!MapInfo_Get_ByID(i))
112                 return;
113
114         included = me.g_maplistCacheQuery(me, i);
115         if(included)
116                 theAlpha = 1;
117         else
118                 theAlpha = 0.4;
119
120         if(isSelected)
121                 draw_Fill('0 0 0', '1 1 0', '0 0 1', 0.5);
122         else if(included)
123                 draw_Fill('0 0 0', '1 1 0', '0 0 0', 0.5);
124
125         s = ftos(p);
126         draw_Picture(me.columnPreviewOrigin * eX, strcat("/maps/", MapInfo_Map_bspname), me.columnPreviewSize * eX + eY, '1 1 1', theAlpha);
127         if(included)
128                 draw_Picture(me.checkMarkOrigin, "checkmark", me.checkMarkSize, '1 1 1', 1);
129         s = draw_TextShortenToWidth(strcat(MapInfo_Map_bspname, ": ", MapInfo_Map_title), me.columnNameSize / me.realFontSize_x, 0);
130         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);
131         s = draw_TextShortenToWidth(MapInfo_Map_author, me.columnNameSize / me.realFontSize_x, 0);
132         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);
133 }
134
135 void refilterNexuizMapList(entity me)
136 {
137         float i, j, n;
138         string s;
139         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures());
140         me.nItems = MapInfo_count;
141         if(me.g_maplistCache)
142                 strunzone(me.g_maplistCache);
143         s = "0";
144         for(i = 1; i < MapInfo_count; i *= 2)
145                 s = strcat(s, s);
146         n = tokenize(cvar_string("g_maplist"));
147         for(i = 0; i < n; ++i)
148         {
149                 j = MapInfo_FindName(argv(i));
150                 if(j >= 0)
151                         s = strcat(
152                                 substring(s, 0, j),
153                                 "1",
154                                 substring(s, j+1, MapInfo_count - (j+1))
155                         );
156         }
157         me.g_maplistCache = strzone(s);
158 }
159 #endif