Merge branch 'master' into terencehill/menu_listbox_changes
[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.setSelected(me, min(me.campaignIndex, me.nItems - 1));
133         if(me.nItems - 1 > me.campaignIndex)
134                 me.scrollToItem(me, me.nItems - 1);
135         if(me.labelTitle)
136                 me.labelTitle.setText(me.labelTitle, campaign_title);
137 }
138
139 void XonoticCampaignList_saveCvars(entity me)
140 {
141         // write campaign cvars
142         // no reason to do this!
143         // cvar_set("g_campaign_name", campaign_name);
144         // cvar_set(me.cvarName, ftos(me.campaignIndex)); // NOTE: only server QC does that!
145 }
146
147 void XonoticCampaignList_campaignGo(entity me, float step)
148 {
149         float canNext, canPrev;
150         string s;
151         float i, j, n;
152
153         canNext = canPrev = 0;
154
155         if(me.campaignGlob >= 0)
156         {
157                 n = search_getsize(me.campaignGlob);
158                 if(n > 0)
159                 {
160                         j = -1;
161                         s = strcat("maps/campaign", campaign_name, ".txt");
162                         for(i = 0; i < n; ++i)
163                         {
164                                 if(search_getfilename(me.campaignGlob, i) == s)
165                                         j = i;
166                         }
167                         if(j < 0)
168                         {
169                                 s = strcat("maps/campaign", cvar_defstring("g_campaign_name"), ".txt");
170                                 for(i = 0; i < n; ++i)
171                                 {
172                                         if(search_getfilename(me.campaignGlob, i) == s)
173                                                 j = i;
174                                 }
175                         }
176                         if(j < 0)
177                         {
178                                 if(step >= 0)
179                                         j = 0;
180                                 else
181                                         j = n - 1;
182                         }
183                         else
184                                 j = mod(j + step, n);
185                         s = search_getfilename(me.campaignGlob, j);
186                         s = substring(s, 13, strlen(s) - 17);
187                         cvar_set("g_campaign_name", s);
188                         me.loadCvars(me);
189                         canNext = (j != n - 1);
190                         canPrev = (j != 0);
191                 }
192         }
193
194         if(me.buttonNext)
195                 me.buttonNext.disabled = !canNext;
196         if(me.buttonPrev)
197                 me.buttonPrev.disabled = !canPrev;
198 }
199
200 void MultiCampaign_Next(entity btn, entity me)
201 {
202         me.campaignGo(me, +1);
203 }
204 void MultiCampaign_Prev(entity btn, entity me)
205 {
206         me.campaignGo(me, -1);
207 }
208
209 void XonoticCampaignList_draw(entity me)
210 {
211         if(cvar(me.cvarName) != me.campaignIndex || cvar_string("g_campaign_name") != campaign_name)
212                 me.loadCvars(me);
213         SUPER(XonoticCampaignList).draw(me);
214 }
215
216 void XonoticCampaignList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
217 {
218         me.itemAbsSize = '0 0 0';
219         SUPER(XonoticCampaignList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
220
221         me.realFontSize_y = me.fontSize / (me.itemAbsSize_y = (absSize.y * me.itemHeight));
222         me.realFontSize_x = me.fontSize / (me.itemAbsSize_x = (absSize.x * (1 - me.controlWidth)));
223         me.realUpperMargin1 = 0.5 * me.realFontSize.y;
224         me.realUpperMargin2 = me.realUpperMargin1 + 2 * me.realFontSize.y;
225
226         me.checkMarkSize = (eX * (me.itemAbsSize.y / me.itemAbsSize.x) + eY) * 0.5;
227
228         me.columnPreviewOrigin = 0;
229         me.columnPreviewSize = me.itemAbsSize.y / me.itemAbsSize.x * 4 / 3;
230         me.columnCheckMarkSize = me.checkMarkSize.x;
231         me.columnNameSize = 1 - me.columnPreviewSize - me.columnCheckMarkSize - 4 * me.realFontSize.x;
232         me.columnNameOrigin = me.columnPreviewOrigin + me.columnPreviewSize + me.realFontSize.x;
233         me.columnCheckMarkOrigin = me.columnNameOrigin + me.columnNameSize + me.realFontSize.x * 2;
234
235         me.checkMarkOrigin = eY + eX * (me.columnCheckMarkOrigin + me.columnCheckMarkSize) - me.checkMarkSize;
236
237         rewrapCampaign(me.columnNameSize, me.rowsPerItem - 3, me.emptyLineHeight, me.realFontSize);
238 }
239 void XonoticCampaignList_doubleClickListBoxItem(entity me, float i, vector where)
240 {
241         CampaignList_LoadMap(me, me);
242 }
243 void XonoticCampaignList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
244 {
245         string s;
246         vector theColor;
247         float theAlpha;
248         float j, n;
249         vector o;
250
251         if(i < me.campaignIndex)
252         {
253                 theAlpha = SKINALPHA_CAMPAIGN_SELECTABLE;
254                 theColor = SKINCOLOR_CAMPAIGN_SELECTABLE;
255         }
256         else if(i == me.campaignIndex)
257         {
258                 theAlpha = SKINALPHA_CAMPAIGN_CURRENT;
259                 theColor = SKINCOLOR_CAMPAIGN_CURRENT;
260         }
261         else
262         {
263                 theAlpha = SKINALPHA_CAMPAIGN_FUTURE;
264                 theColor = SKINCOLOR_CAMPAIGN_FUTURE;
265         }
266
267         if(isSelected)
268                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
269         else if(isFocused)
270         {
271                 me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
272                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
273         }
274
275         if(draw_PictureSize(strcat("/maps/", campaign_mapname[i])) == '0 0 0')
276                 draw_Picture(me.columnPreviewOrigin * eX, "nopreview_map", me.columnPreviewSize * eX + eY, '1 1 1', theAlpha);
277         else
278                 draw_Picture(me.columnPreviewOrigin * eX, strcat("/maps/", campaign_mapname[i]), me.columnPreviewSize * eX + eY, '1 1 1', theAlpha);
279
280         if(i < me.campaignIndex)
281                 draw_Picture(me.checkMarkOrigin, "checkmark", me.checkMarkSize, '1 1 1', 1);
282         if(i <= me.campaignIndex)
283                 s = campaign_shortdesc[i]; // fteqcc sucks
284         else
285                 s = "???";
286         s = draw_TextShortenToWidth(sprintf(_("Level %d: %s"), i+1, s), me.columnNameSize, 0, me.realFontSize);
287         draw_Text(me.realUpperMargin1 * eY + (me.columnNameOrigin + 0.00 * (me.columnNameSize - draw_TextWidth(s, 0, me.realFontSize))) * eX, s, me.realFontSize, theColor, theAlpha, 0);
288
289         if(i <= me.campaignIndex)
290         {
291                 s = campaign_longdesc_wrapped[i];
292                 n = tokenizebyseparator(s, "\n");
293                 o = me.realUpperMargin2 * eY + me.columnNameOrigin * eX;
294                 for(j = 0; j < n; ++j)
295                         if(argv(j) != "")
296                         {
297                                 draw_Text(o, argv(j), me.realFontSize, theColor, theAlpha * SKINALPHA_CAMPAIGN_DESCRIPTION, 0);
298                                 o.y += me.realFontSize.y;
299                         }
300                         else
301                                 o.y += me.realFontSize.y * me.emptyLineHeight;
302         }
303 }
304 void CampaignList_LoadMap(entity btn, entity me)
305 {
306         if(me.selectedItem >= me.nItems || me.selectedItem < 0)
307                 return;
308         CampaignSetup(me.selectedItem);
309 }
310
311 void XonoticCampaignList_setSelected(entity me, float i)
312 {
313         // prevent too late items from being played
314         SUPER(XonoticCampaignList).setSelected(me, min(i, me.campaignIndex));
315 }
316
317 float XonoticCampaignList_keyDown(entity me, float scan, float ascii, float shift)
318 {
319         if(scan == K_ENTER || scan == K_SPACE || scan == K_KP_ENTER)
320                 CampaignList_LoadMap(me, me);
321         else
322                 return SUPER(XonoticCampaignList).keyDown(me, scan, ascii, shift);
323         return 1;
324 }
325 #endif