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