Merge branch 'terencehill/vehicle_hud_fix' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / campaign.qc
1 #include "../../common/campaign_common.qh"
2
3 #ifndef CAMPAIGN_H
4 #define CAMPAIGN_H
5 #include "listbox.qc"
6 CLASS(XonoticCampaignList, XonoticListBox)
7         METHOD(XonoticCampaignList, configureXonoticCampaignList, void(entity))
8         ATTRIB(XonoticCampaignList, rowsPerItem, float, 10)
9         METHOD(XonoticCampaignList, draw, void(entity))
10         METHOD(XonoticCampaignList, drawListBoxItem, void(entity, int, vector, bool, bool))
11         METHOD(XonoticCampaignList, doubleClickListBoxItem, void(entity, float, vector))
12         METHOD(XonoticCampaignList, resizeNotify, void(entity, vector, vector, vector, vector))
13         METHOD(XonoticCampaignList, setSelected, void(entity, float))
14         METHOD(XonoticCampaignList, keyDown, float(entity, float, float, float))
15         METHOD(XonoticCampaignList, campaignGo, void(entity, float))
16         METHOD(XonoticCampaignList, destroy, void(entity))
17
18         ATTRIB(XonoticCampaignList, campaignGlob, float, 0)
19         ATTRIB(XonoticCampaignList, realFontSize, vector, '0 0 0')
20         ATTRIB(XonoticCampaignList, columnPreviewOrigin, float, 0)
21         ATTRIB(XonoticCampaignList, columnPreviewSize, float, 0)
22         ATTRIB(XonoticCampaignList, columnNameOrigin, float, 0)
23         ATTRIB(XonoticCampaignList, columnNameSize, float, 0)
24         ATTRIB(XonoticCampaignList, columnCheckMarkOrigin, float, 0)
25         ATTRIB(XonoticCampaignList, columnCheckMarkSize, float, 0)
26         ATTRIB(XonoticCampaignList, checkMarkOrigin, vector, '0 0 0')
27         ATTRIB(XonoticCampaignList, checkMarkSize, vector, '0 0 0')
28         ATTRIB(XonoticCampaignList, realUpperMargin1, float, 0)
29         ATTRIB(XonoticCampaignList, realUpperMargin2, float, 0)
30
31         ATTRIB(XonoticCampaignList, origin, vector, '0 0 0')
32         ATTRIB(XonoticCampaignList, itemAbsSize, vector, '0 0 0')
33         ATTRIB(XonoticCampaignList, emptyLineHeight, float, 0.5)
34
35         ATTRIB(XonoticCampaignList, campaignIndex, float, 0)
36         ATTRIB(XonoticCampaignList, cvarName, string, string_null)
37         METHOD(XonoticCampaignList, loadCvars, void(entity))
38         METHOD(XonoticCampaignList, saveCvars, void(entity))
39
40         ATTRIB(XonoticCampaignList, buttonNext, entity, NULL)
41         ATTRIB(XonoticCampaignList, buttonPrev, entity, NULL)
42         ATTRIB(XonoticCampaignList, labelTitle, entity, NULL)
43 ENDCLASS(XonoticCampaignList)
44 entity makeXonoticCampaignList();
45 void CampaignList_LoadMap(entity btn, entity me);
46 void MultiCampaign_Next(entity btn, entity me);
47 void MultiCampaign_Prev(entity btn, entity me);
48 #endif
49
50 #ifdef IMPLEMENTATION
51 string campaign_longdesc_wrapped[CAMPAIGN_MAX_ENTRIES];
52
53 void rewrapCampaign(float w, float l0, float emptyheight, vector theFontSize)
54 {
55         int i, j;
56         int n;
57         float l;
58         string r, s;
59         for(i = 0; i < campaign_entries; ++i)
60         {
61                 l = l0;
62                 if(campaign_longdesc_wrapped[i])
63                         strunzone(campaign_longdesc_wrapped[i]);
64                 n = tokenizebyseparator(campaign_longdesc[i], "\n");
65                 r = "";
66                 for(j = 0; j < n; ++j)
67                 {
68                         s = argv(j);
69                         if(s == "")
70                         {
71                                 l -= emptyheight;
72                                 r = strcat(r, "\n");
73                                 continue;
74                         }
75
76                         getWrappedLine_remaining = s;
77                         while(getWrappedLine_remaining)
78                         {
79                                 s = getWrappedLine(w, theFontSize, draw_TextWidth_WithoutColors);
80                                 if(--l < 0) goto toolong;
81                                 r = strcat(r, s, "\n");
82                         }
83                 }
84                 goto nottoolong;
85 :toolong
86                 while(substring(r, strlen(r) - 1, 1) == "\n")
87                         r = substring(r, 0, strlen(r) - 1);
88                 r = strcat(r, "...\n");
89 :nottoolong
90                 campaign_longdesc_wrapped[i] = strzone(substring(r, 0, strlen(r) - 1));
91         }
92 }
93
94 entity makeXonoticCampaignList()
95 {
96         entity me;
97         me = NEW(XonoticCampaignList);
98         me.configureXonoticCampaignList(me);
99         return me;
100 }
101 void XonoticCampaignList_configureXonoticCampaignList(entity me)
102 {
103         me.configureXonoticListBox(me);
104         me.campaignGlob = search_begin("maps/campaign*.txt", true, true);
105         me.loadCvars(me);
106         me.campaignGo(me, 0); // takes care of enabling/disabling buttons too
107 }
108
109 void XonoticCampaignList_destroy(entity me)
110 {
111         if(me.campaignGlob >= 0)
112                 search_end(me.campaignGlob);
113 }
114
115 void XonoticCampaignList_loadCvars(entity me)
116 {
117         // read campaign cvars
118         if(campaign_name)
119                 strunzone(campaign_name);
120         if(me.cvarName)
121                 strunzone(me.cvarName);
122         campaign_name = strzone(cvar_string("g_campaign_name"));
123         me.cvarName = strzone(strcat("g_campaign", campaign_name, "_index"));
124         registercvar(me.cvarName, "", 0); // saved by server QC anyway
125         CampaignFile_Unload();
126         CampaignFile_Load(0, CAMPAIGN_MAX_ENTRIES);
127         me.campaignIndex = bound(0, cvar(me.cvarName), campaign_entries);
128         cvar_set(me.cvarName, ftos(me.campaignIndex));
129         if(me.columnNameSize)
130                 rewrapCampaign(me.columnNameSize, me.rowsPerItem - 3, me.emptyLineHeight, me.realFontSize);
131         me.nItems = min(me.campaignIndex + 2, campaign_entries);
132         me.selectedItem = min(me.campaignIndex, me.nItems - 1);
133         me.scrollPos = me.nItems * me.itemHeight - 1;
134         if(me.labelTitle)
135                 me.labelTitle.setText(me.labelTitle, campaign_title);
136 }
137
138 void XonoticCampaignList_saveCvars(entity me)
139 {
140         // write campaign cvars
141         // no reason to do this!
142         // cvar_set("g_campaign_name", campaign_name);
143         // cvar_set(me.cvarName, ftos(me.campaignIndex)); // NOTE: only server QC does that!
144 }
145
146 void XonoticCampaignList_campaignGo(entity me, float step)
147 {
148         float canNext, canPrev;
149         string s;
150         float i, j, n;
151
152         canNext = canPrev = 0;
153
154         if(me.campaignGlob >= 0)
155         {
156                 n = search_getsize(me.campaignGlob);
157                 if(n > 0)
158                 {
159                         j = -1;
160                         s = strcat("maps/campaign", campaign_name, ".txt");
161                         for(i = 0; i < n; ++i)
162                         {
163                                 if(search_getfilename(me.campaignGlob, i) == s)
164                                         j = i;
165                         }
166                         if(j < 0)
167                         {
168                                 s = strcat("maps/campaign", cvar_defstring("g_campaign_name"), ".txt");
169                                 for(i = 0; i < n; ++i)
170                                 {
171                                         if(search_getfilename(me.campaignGlob, i) == s)
172                                                 j = i;
173                                 }
174                         }
175                         if(j < 0)
176                         {
177                                 if(step >= 0)
178                                         j = 0;
179                                 else
180                                         j = n - 1;
181                         }
182                         else
183                                 j = mod(j + step, n);
184                         s = search_getfilename(me.campaignGlob, j);
185                         s = substring(s, 13, strlen(s) - 17);
186                         cvar_set("g_campaign_name", s);
187                         me.loadCvars(me);
188                         canNext = (j != n - 1);
189                         canPrev = (j != 0);
190                 }
191         }
192
193         if(me.buttonNext)
194                 me.buttonNext.disabled = !canNext;
195         if(me.buttonPrev)
196                 me.buttonPrev.disabled = !canPrev;
197 }
198
199 void MultiCampaign_Next(entity btn, entity me)
200 {
201         me.campaignGo(me, +1);
202 }
203 void MultiCampaign_Prev(entity btn, entity me)
204 {
205         me.campaignGo(me, -1);
206 }
207
208 void XonoticCampaignList_draw(entity me)
209 {
210         if(cvar(me.cvarName) != me.campaignIndex || cvar_string("g_campaign_name") != campaign_name)
211                 me.loadCvars(me);
212         SUPER(XonoticCampaignList).draw(me);
213 }
214
215 void XonoticCampaignList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
216 {
217         me.itemAbsSize = '0 0 0';
218         SUPER(XonoticCampaignList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
219
220         me.realFontSize_y = me.fontSize / (me.itemAbsSize_y = (absSize.y * me.itemHeight));
221         me.realFontSize_x = me.fontSize / (me.itemAbsSize_x = (absSize.x * (1 - me.controlWidth)));
222         me.realUpperMargin1 = 0.5 * me.realFontSize.y;
223         me.realUpperMargin2 = me.realUpperMargin1 + 2 * me.realFontSize.y;
224
225         me.checkMarkSize = (eX * (me.itemAbsSize.y / me.itemAbsSize.x) + eY) * 0.5;
226
227         me.columnPreviewOrigin = 0;
228         me.columnPreviewSize = me.itemAbsSize.y / me.itemAbsSize.x * 4 / 3;
229         me.columnCheckMarkSize = me.checkMarkSize.x;
230         me.columnNameSize = 1 - me.columnPreviewSize - me.columnCheckMarkSize - 4 * me.realFontSize.x;
231         me.columnNameOrigin = me.columnPreviewOrigin + me.columnPreviewSize + me.realFontSize.x;
232         me.columnCheckMarkOrigin = me.columnNameOrigin + me.columnNameSize + me.realFontSize.x * 2;
233
234         me.checkMarkOrigin = eY + eX * (me.columnCheckMarkOrigin + me.columnCheckMarkSize) - me.checkMarkSize;
235
236         rewrapCampaign(me.columnNameSize, me.rowsPerItem - 3, me.emptyLineHeight, me.realFontSize);
237 }
238 void XonoticCampaignList_doubleClickListBoxItem(entity me, float i, vector where)
239 {
240         CampaignList_LoadMap(me, me);
241 }
242 void XonoticCampaignList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
243 {
244         string s;
245         vector theColor;
246         float theAlpha;
247         float j, n;
248         vector o;
249
250         if(i < me.campaignIndex)
251         {
252                 theAlpha = SKINALPHA_CAMPAIGN_SELECTABLE;
253                 theColor = SKINCOLOR_CAMPAIGN_SELECTABLE;
254         }
255         else if(i == me.campaignIndex)
256         {
257                 theAlpha = SKINALPHA_CAMPAIGN_CURRENT;
258                 theColor = SKINCOLOR_CAMPAIGN_CURRENT;
259         }
260         else
261         {
262                 theAlpha = SKINALPHA_CAMPAIGN_FUTURE;
263                 theColor = SKINCOLOR_CAMPAIGN_FUTURE;
264         }
265
266         if(isSelected)
267                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
268         else if(isFocused)
269         {
270                 me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
271                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
272         }
273
274         if(draw_PictureSize(strcat("/maps/", campaign_mapname[i])) == '0 0 0')
275                 draw_Picture(me.columnPreviewOrigin * eX, "nopreview_map", me.columnPreviewSize * eX + eY, '1 1 1', theAlpha);
276         else
277                 draw_Picture(me.columnPreviewOrigin * eX, strcat("/maps/", campaign_mapname[i]), me.columnPreviewSize * eX + eY, '1 1 1', theAlpha);
278
279         if(i < me.campaignIndex)
280                 draw_Picture(me.checkMarkOrigin, "checkmark", me.checkMarkSize, '1 1 1', 1);
281         if(i <= me.campaignIndex)
282                 s = campaign_shortdesc[i]; // fteqcc sucks
283         else
284                 s = "???";
285         s = draw_TextShortenToWidth(sprintf(_("Level %d: %s"), i+1, s), me.columnNameSize, 0, me.realFontSize);
286         draw_Text(me.realUpperMargin1 * eY + (me.columnNameOrigin + 0.00 * (me.columnNameSize - draw_TextWidth(s, 0, me.realFontSize))) * eX, s, me.realFontSize, theColor, theAlpha, 0);
287
288         if(i <= me.campaignIndex)
289         {
290                 s = campaign_longdesc_wrapped[i];
291                 n = tokenizebyseparator(s, "\n");
292                 o = me.realUpperMargin2 * eY + me.columnNameOrigin * eX;
293                 for(j = 0; j < n; ++j)
294                         if(argv(j) != "")
295                         {
296                                 draw_Text(o, argv(j), me.realFontSize, theColor, theAlpha * SKINALPHA_CAMPAIGN_DESCRIPTION, 0);
297                                 o.y += me.realFontSize.y;
298                         }
299                         else
300                                 o.y += me.realFontSize.y * me.emptyLineHeight;
301         }
302 }
303 void CampaignList_LoadMap(entity btn, entity me)
304 {
305         if(me.selectedItem >= me.nItems || me.selectedItem < 0)
306                 return;
307         CampaignSetup(me.selectedItem);
308 }
309
310 void XonoticCampaignList_setSelected(entity me, float i)
311 {
312         // prevent too late items from being played
313         SUPER(XonoticCampaignList).setSelected(me, min(i, me.campaignIndex));
314 }
315
316 float XonoticCampaignList_keyDown(entity me, float scan, float ascii, float shift)
317 {
318         if(scan == K_ENTER || scan == K_SPACE || scan == K_KP_ENTER)
319                 CampaignList_LoadMap(me, me);
320         else
321                 return SUPER(XonoticCampaignList).keyDown(me, scan, ascii, shift);
322         return 1;
323 }
324 #endif