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