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