Debian Codegraph @

src/gui/intro.cpp

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #define _USE_MATH_DEFINES
21 #include "gui.h"
22 #include "../ta-log.h"
23 #include "imgui_internal.h"
24 #include <fmt/printf.h>
25
26 void FurnaceGUI::drawImage(ImDrawList* dl, FurnaceGUIImages image, const ImVec2& pos, const ImVec2& scale, double rotate, const ImVec2& uvMin, const ImVec2& uvMax, const ImVec4& imgColor) {
27 FurnaceGUIImage* imgI=getImage(image);
28 FurnaceGUITexture* img=getTexture(image);
29
30 if (img==NULL) return;
31
32 float squareSize=MAX(introMax.x-introMin.x,introMax.y-introMin.y);
33 float uDiff=uvMax.x-uvMin.x;
34 float vDiff=uvMax.y-uvMin.y;
35
36 ImVec2 rectMin=ImVec2(
37 -imgI->width*0.5*scale.x*(squareSize/2048.0)*uDiff,
38 -imgI->height*0.5*scale.y*(squareSize/2048.0)*vDiff
39 );
40 ImVec2 rectMax=ImVec2(
41 -rectMin.x,
42 -rectMin.y
43 );
44
45 ImVec2 posAbs=ImLerp(
46 ImVec2(introMin.x+((introMax.x-introMin.x)-squareSize)*0.5,introMin.y+((introMax.y-introMin.y)-squareSize)*0.5),
47 ImVec2(introMin.x+((introMax.x-introMin.x)+squareSize)*0.5,introMin.y+((introMax.y-introMin.y)+squareSize)*0.5),
48 pos
49 );
50
51 ImVec2 quad0=ImVec2(
52 posAbs.x+rectMin.x*cos(rotate)-rectMin.y*sin(rotate),
53 posAbs.y+rectMin.x*sin(rotate)+rectMin.y*cos(rotate)
54 );
55 ImVec2 quad1=ImVec2(
56 posAbs.x+rectMax.x*cos(rotate)-rectMin.y*sin(rotate),
57 posAbs.y+rectMax.x*sin(rotate)+rectMin.y*cos(rotate)
58 );
59 ImVec2 quad2=ImVec2(
60 posAbs.x+rectMax.x*cos(rotate)-rectMax.y*sin(rotate),
61 posAbs.y+rectMax.x*sin(rotate)+rectMax.y*cos(rotate)
62 );
63 ImVec2 quad3=ImVec2(
64 posAbs.x+rectMin.x*cos(rotate)-rectMax.y*sin(rotate),
65 posAbs.y+rectMin.x*sin(rotate)+rectMax.y*cos(rotate)
66 );
67
68 float uScale=rend->getTextureU(img);
69 float vScale=rend->getTextureV(img);
70
71 ImVec2 uv0=ImVec2(uvMin.x*uScale,uvMin.y*vScale);
72 ImVec2 uv1=ImVec2(uvMax.x*uScale,uvMin.y*vScale);
73 ImVec2 uv2=ImVec2(uvMax.x*uScale,uvMax.y*vScale);
74 ImVec2 uv3=ImVec2(uvMin.x*uScale,uvMax.y*vScale);
75
76 ImU32 colorConverted=ImGui::GetColorU32(imgColor);
77
78 dl->AddImageQuad(rend->getTextureID(img),quad0,quad1,quad2,quad3,uv0,uv1,uv2,uv3,colorConverted);
79 }
80
81 void FurnaceGUI::endIntroTune() {
82 if (introStopped) return;
83 logV("ending intro");
84 stop();
85 if (curFileName.empty()) {
86 e->createNewFromDefaults();
87 } else { // load pending song
88 if (load(curFileName)>0) {
89 showError(fmt::sprintf(_("Error while loading file! (%s)"),lastError));
90 curFileName="";
91 e->createNewFromDefaults();
92 }
93 }
94 undoHist.clear();
95 redoHist.clear();
96 modified=false;
97 curNibble=false;
98 orderNibble=false;
99 orderCursor=-1;
100 samplePos=0;
101 updateSampleTex=true;
102 selStart=SelectionPoint();
103 selEnd=SelectionPoint();
104 cursor=SelectionPoint();
105 updateWindowTitle();
106 updateScroll(0);
107 introStopped=true;
108 }
109
110 void FurnaceGUI::drawIntro(double introTime, bool monitor) {
111 if (monitor) {
112 return;
113 }
114 if (introPos<(shortIntro?1.0:11.0) || monitor) {
115 if (!monitor) {
116 WAKE_UP;
117 nextWindow=GUI_WINDOW_NOTHING;
118 ImGui::SetNextWindowPos(ImVec2(0,0));
119 ImGui::SetNextWindowSize(ImVec2(canvasW,canvasH));
120 if (introPos<0.1) ImGui::SetNextWindowFocus();
121 }
122 if (ImGui::Begin(monitor?"IntroMon X":"Intro",NULL,monitor?globalWinFlags:(ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoDocking|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoBackground),monitor?_("IntroMon X"):_("Intro"))) {
123 if (monitor) {
124 if (ImGui::Button(_("Preview"))) {
125 introPos=0;
126 tutorial.introPlayed=false;
127 shortIntro=false;
128 introSkipDo=false;
129 introSkip=0.0;
130 e->setOrder(0);
131 e->setRepeatPattern(false);
132 play();
133 }
134 ImGui::SameLine();
135 ImGui::TextColored(ImVec4(1.0,introTime<10.0?1.0:0.0,introTime<10.0?1.0:0.0,1.0),"%.2f",introTime);
136 ImGui::SameLine();
137 ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
138 ImGui::ProgressBar(introTime/11.0,ImVec2(-FLT_MIN,0),"##IntroP");
139 }
140
141 ImDrawList* dl=monitor?ImGui::GetWindowDrawList():ImGui::GetForegroundDrawList();
142 ImVec2 top=monitor?ImGui::GetCursorScreenPos():ImVec2(0.0f,0.0f);
143 ImVec2 bottom=monitor?ImGui::GetContentRegionAvail():ImVec2(canvasW,canvasH);
144 if (monitor) {
145 bottom.x+=top.x;
146 bottom.y+=top.y;
147 }
148
149 introMin=top;
150 introMax=bottom;
151
152 if (monitor) dl->PushClipRect(top,bottom);
153
154 if (shortIntro && !monitor) {
155 // background
156 float bgAlpha=CLAMP((1.0-introTime)*4.0,0.0,1.0);
157 bgAlpha=3.0*pow(bgAlpha,2.0)-2.0*pow(bgAlpha,3.0);
158 ImU32 bgColor=ImGui::GetColorU32(ImVec4(0.0f,0.0f,0.0f,bgAlpha));
159
160 dl->AddRectFilled(top,bottom,bgColor);
161
162 drawImage(dl,GUI_IMAGE_INTROBG,ImVec2(0.125,0.25-(introTime)*0.05),ImVec2(18.0,18.0),0.0,ImVec2(0.0,0.0),ImVec2(1.0,1.0),ImVec4(0.4,0.8,1.0,0.4*CLAMP(introTime*2.0,0.0,1.0)*bgAlpha));
163 drawImage(dl,GUI_IMAGE_INTROBG,ImVec2(0.4,0.25-(introTime)*0.08),ImVec2(18.0,18.0),0.0,ImVec2(0.0,0.0),ImVec2(1.0,1.0),ImVec4(0.1,0.2,1.0,0.2*CLAMP(introTime*3.0,0.0,1.0)*bgAlpha));
164 drawImage(dl,GUI_IMAGE_INTROBG,ImVec2(0.7,0.25-(introTime)*0.03),ImVec2(20.0,20.0),0.0,ImVec2(0.0,0.0),ImVec2(1.0,1.0),ImVec4(0.7,1.0,0.7,0.1*CLAMP(introTime*3.0,0.0,1.0)*bgAlpha));
165
166 drawImage(dl,GUI_IMAGE_LOGO,ImVec2(0.5,0.5+pow(1.0-CLAMP(introTime*2.0,0.0,1.0),4.0)*0.125),ImVec2(0.67,0.67),0.0f,ImVec2(0.0,0.0),ImVec2(1.0,1.0),ImVec4(1.0,1.0,1.0,CLAMP(introTime*3.0,0.0,1.0)*bgAlpha));
167 } else {
168 // preload textures
169 getTexture(GUI_IMAGE_TALOGO);
170 getTexture(GUI_IMAGE_TACHIP);
171 getTexture(GUI_IMAGE_LOGO);
172 getTexture(GUI_IMAGE_INTROBG,GUI_BLEND_MODE_ADD);
173
174 if (monitor) {
175 ImVec2 textPos=ImLerp(top,bottom,ImVec2(0.5,0.5));
176 textPos.x-=ImGui::CalcTextSize(_("SORRY NOTHING")).x*0.5;
177 textPos.y-=ImGui::CalcTextSize(_("SORRY NOTHING")).y*0.5;
178 dl->AddText(textPos,ImGui::GetColorU32(uiColors[GUI_COLOR_TEXT]),_("SORRY NOTHING"));
179 }
180
181 if (introSkip<0.5 || monitor) {
182 // background
183 float bgAlpha=CLAMP(11.0-introTime,0.0,1.0);
184 bgAlpha=3.0*pow(bgAlpha,2.0)-2.0*pow(bgAlpha,3.0);
185 ImU32 bgColor=ImGui::GetColorU32(ImVec4(0.0f,0.0f,0.0f,bgAlpha));
186
187 dl->AddRectFilled(top,bottom,bgColor);
188
189 // part 1 - talogo
190 if (introTime<2.3) {
191 drawImage(dl,GUI_IMAGE_TALOGO,ImVec2(0.5,0.5),ImVec2(0.67,0.67),0.0f,ImVec2(0.0,0.0),ImVec2(1.0,1.0),ImVec4(1.0,1.0,1.0,MAX(0.01,1.0-pow(MAX(0.0,1.0-introTime*2.0),3.0))));
192
193 for (int i=0; i<16; i++) {
194 double chipCenter=0.22+pow(MAX(0.0,1.5-introTime*0.8-((double)i/36.0)),2.0)+pow(sin(-introTime*2.2-(double)i*0.44),24)*0.05;
195 ImVec2 chipPos=ImVec2(
196 0.5+chipCenter*cos(2.0*M_PI*(double)i/16.0-pow(introTime,2.2)),
197 0.5+chipCenter*sin(2.0*M_PI*(double)i/16.0-pow(introTime,2.2))
198 );
199 drawImage(dl,GUI_IMAGE_TACHIP,chipPos,ImVec2(0.25,0.25),0.0f,ImVec2(0.0,0.0),ImVec2(1.0,0.5),ImVec4(1.0,1.0,1.0,1.0));
200 }
201 }
202
203 // background after part 1
204 if (introTime>2.3) {
205 float s1a=CLAMP((introTime-3.2)*1.3,0.0f,1.0f);
206 float s2a=CLAMP((introTime-4.5)*1.0,0.0f,1.0f);
207 float addition=(3*pow(s1a,2)-2*pow(s1a,3)+(3*pow(s2a,2)-2*pow(s2a,3))*1.5)*3.5;
208 drawImage(dl,GUI_IMAGE_INTROBG,ImVec2(0.125,0.25-(introTime+addition)*0.05),ImVec2(18.0,18.0),0.0,ImVec2(0.0,0.0),ImVec2(1.0,1.0),ImVec4(0.2,0.1+0.7*s1a,1.0*s1a,0.5*bgAlpha));
209 drawImage(dl,GUI_IMAGE_INTROBG,ImVec2(0.4,0.25-(introTime+addition)*0.08),ImVec2(18.0,18.0),0.0,ImVec2(0.0,0.0),ImVec2(1.0,1.0),ImVec4(0.1*s1a,0.2+0.4*s1a,1.0*s1a,0.6*bgAlpha));
210 drawImage(dl,GUI_IMAGE_INTROBG,ImVec2(0.7,0.25-(introTime+addition)*0.03),ImVec2(20.0,20.0),0.0,ImVec2(0.0,0.0),ImVec2(1.0,1.0),ImVec4(0.2+0.5*s1a,0.2,1.0,(0.5-0.4*s1a)*bgAlpha));
211 }
212
213 const double fallPatX[]={
214 0.0,
215 272.0,
216 470.0,
217 742.0,
218 1013.0
219 };
220
221 // part 2 - falling patterns
222 if (introTime>2.3 && introTime<4.5) {
223 for (int i=0; i<48; i++) {
224 ImVec2 uv0=ImVec2(
225 fallPatX[i&3],
226 (double)(i>>2)*36.0
227 );
228 ImVec2 uv1=ImVec2(
229 fallPatX[1+(i&3)]-2.0,
230 uv0.y+36.0
231 );
232 uv0.x/=1024.0;
233 uv0.y/=512.0;
234 uv1.x/=1024.0;
235 uv1.y/=512.0;
236
237 bool left=(i%6)>=3;
238 double t=(introTime-2.5)*(0.77+(cos(i+7)*0.05));
239 double alteration=pow(t,0.4)-(0.55*t)+0.55*pow(t,6.0);
240
241 drawImage(dl,GUI_IMAGE_PAT,ImVec2((left?0:1)+sin(cos(i*3.67))*0.35+((alteration+((introTime-2.3)*(0.08*(double)(1+(i&3)))))*(left?1.0:-1.0)),0.5+sin(i*6.74)*0.3-pow(CLAMP(introTime-3.0,0.0,1.0),4.0)*1.5),ImVec2(1.5,1.5),0.0f,uv0,uv1,ImVec4(1.0,1.0,1.0,1.0));
242 }
243 }
244
245 // transition
246 float transitionPos=CLAMP(introTime*4.0-8,-1.5,3.5);
247 dl->AddQuadFilled(
248 ImVec2(
249 top.x+(transitionPos-1.5)*(bottom.x-top.x),
250 top.y
251 ),
252 ImVec2(
253 top.x+(transitionPos)*(bottom.x-top.x),
254 top.y
255 ),
256 ImVec2(
257 top.x+(transitionPos-0.2)*(bottom.x-top.x),
258 bottom.y
259 ),
260 ImVec2(
261 top.x+(transitionPos-1.7)*(bottom.x-top.x),
262 bottom.y
263 ),
264 ImGui::GetColorU32(ImVec4(0.35,0.4,0.5,1.0))
265 );
266
267 // part 3 - falling chips
268 if (introTime>3.0 && introTime<6.0) {
269 for (int i=0; i<40; i++) {
270 float blah=(introTime-4.25)*1.3;
271 ImVec2 chipPos=ImVec2(
272 0.5+sin(i)*0.4,
273 0.1-(1.1*pow(blah,2.0)-1.3*pow(blah,2.0)+pow(blah,5.0))+i*0.02+((introTime-3.75)*1.3*(fabs(sin(i*1.3))*0.28))
274 );
275 drawImage(dl,GUI_IMAGE_TACHIP,chipPos,ImVec2(0.33,0.33),0.5*M_PI,ImVec2(0.0,0.0),ImVec2(1.0,0.5),ImVec4(1.0,1.0,1.0,1.0));
276 }
277 }
278
279 // part 4 - logo end
280 if (introTime>5.0) {
281 drawImage(
282 dl,
283 GUI_IMAGE_WORDMARK,
284 ImVec2(0.36+0.3*(1.0-pow(1.0-CLAMP(introTime-6.0,0.0,1.0),6.0)),0.5+pow(1.0-CLAMP(introTime-5.0,0.0,1.0),4.0)),
285 ImVec2(1.0,1.0),
286 0.0f,
287 ImVec2(pow(1.0-CLAMP(introTime-6.0,0.0,1.0),8.0),0.0),
288 ImVec2(1.0,1.0),
289 ImVec4(1.0,1.0,1.0,bgAlpha)
290 );
291 drawImage(dl,GUI_IMAGE_LOGO,ImVec2(0.5-0.25*(1.0-pow(1.0-CLAMP(introTime-6.0,0.0,1.0),6.0)),0.5+pow(1.0-CLAMP(introTime-5.0,0.0,1.0),4.0)),ImVec2(0.67,0.67),0.0f,ImVec2(0.0,0.0),ImVec2(1.0,1.0),ImVec4(1.0,1.0,1.0,bgAlpha));
292 }
293 }
294
295 // intro skip fade
296 if (!monitor) {
297 if (introSkipDo) {
298 introSkip+=ImGui::GetIO().DeltaTime;
299 if (introSkip>=0.5) {
300 if (!shortIntro) endIntroTune();
301 introPos=0.1;
302 if (introSkip>=0.75) introPos=12.0;
303 }
304 } else {
305 introSkip-=ImGui::GetIO().DeltaTime*4.0f;
306 if (introSkip<0.0) introSkip=0.0;
307 }
308
309 dl->AddRectFilled(top,bottom,ImGui::GetColorU32(ImVec4(0.0,0.0,0.0,CLAMP(introSkip*2.0,0.0,1.0)-CLAMP((introSkip-0.5)*4,0.0,1.0))));
310 if (introSkip<0.5) dl->AddText(ImVec2(8.0*dpiScale,8.0*dpiScale),ImGui::GetColorU32(ImVec4(1.0,1.0,1.0,CLAMP(introSkip*8.0,0.0,1.0))),_("hold to skip"));
311 }
312 }
313
314 if (monitor) dl->PopClipRect();
315
316 // workaround to texture issue
317 dl->AddText(ImVec2(bottom.x-1,bottom.y-1),ImGui::ColorConvertFloat4ToU32(ImVec4(0.0,0.0,0.1,0.01)),"A");
318 }
319 ImGui::End();
320
321 if (mustClear<=0 && !monitor) {
322 if (!shortIntro && introPos<=0.0) {
323 e->setOrder(0);
324 e->setRepeatPattern(false);
325 play();
326 }
327 if (introPos>=10.0 && !shortIntro) endIntroTune();
328 introPos+=ImGui::GetIO().DeltaTime;
329 if (introPos>=(shortIntro?1.0:11.0)) {
330 introPos=12.0;
331 tutorial.introPlayed=true;
332 commitTutorial();
333 }
334 }
335 } else if (!shortIntro) {
336 endIntroTune();
337 }
338 }