Debian Codegraph @

src/gui/orders.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 #include "gui.h"
21 #include <fmt/printf.h>
22 #include <imgui.h>
23 #include "IconsFontAwesome4.h"
24 #include "imgui_internal.h"
25 #include "../ta-log.h"
26
27 void FurnaceGUI::drawMobileOrderSel() {
28 if (!portrait) return;
29
30 if (!orderScrollLocked) {
31 if (orderScroll>(float)curOrder-0.005f && orderScroll<(float)curOrder+0.005f) {
32 orderScroll=curOrder;
33 } else if (orderScroll<curOrder) {
34 orderScroll+=MAX(0.05f,(curOrder-orderScroll)*0.2f);
35 if (orderScroll>curOrder) orderScroll=curOrder;
36 WAKE_UP;
37 } else {
38 orderScroll-=MAX(0.05f,(orderScroll-curOrder)*0.2f);
39 if (orderScroll<curOrder) orderScroll=curOrder;
40 WAKE_UP;
41 }
42 }
43
44 ImGui::SetNextWindowPos(ImVec2(0.0f,mobileMenuPos*-0.65*canvasH));
45 ImGui::SetNextWindowSize(ImVec2(canvasW,0.12*canvasW));
46 if (ImGui::Begin("OrderSel",NULL,globalWinFlags)) {
47 ImDrawList* dl=ImGui::GetWindowDrawList();
48 ImGuiWindow* window=ImGui::GetCurrentWindow();
49 ImGuiStyle& style=ImGui::GetStyle();
50
51 ImVec2 size=ImGui::GetContentRegionAvail();
52
53 ImVec2 minArea=window->DC.CursorPos;
54 ImVec2 maxArea=ImVec2(
55 minArea.x+size.x,
56 minArea.y+size.y
57 );
58 ImRect rect=ImRect(minArea,maxArea);
59 ImGui::ItemSize(size,style.FramePadding.y);
60 if (ImGui::ItemAdd(rect,ImGui::GetID("OrderSelW"))) {
61 ImVec2 centerPos=ImLerp(minArea,maxArea,ImVec2(0.5,0.5));
62
63 for (int i=0; i<e->curSubSong->ordersLen; i++) {
64 ImVec2 pos=centerPos;
65 ImVec4 color=uiColors[GUI_COLOR_TEXT];
66 pos.x+=(i-orderScroll)*40.0*dpiScale;
67 if (pos.x<-200.0*dpiScale) continue;
68 if (pos.x>canvasW+200.0*dpiScale) break;
69 String text=fmt::sprintf("%.2X",i);
70 float targetSize=size.y-fabs(i-orderScroll)*2.0*dpiScale;
71 if (targetSize<8.0*dpiScale) targetSize=8.0*dpiScale;
72 color.w*=CLAMP(2.0f*(targetSize/size.y-0.5f),0.0f,1.0f);
73
74 ImGui::PushFont(bigFont);
75 ImVec2 textSize=ImGui::CalcTextSize(text.c_str());
76 ImGui::PopFont();
77
78 pos.x-=textSize.x*0.5*(targetSize/textSize.y);
79 pos.y-=targetSize*0.5;
80
81 dl->AddText(bigFont,targetSize,pos,ImGui::GetColorU32(color),text.c_str());
82 }
83 }
84 if (ImGui::IsItemClicked()) {
85 orderScrollSlideOrigin=ImGui::GetMousePos().x+orderScroll*40.0f*dpiScale;
86 orderScrollRealOrigin=ImGui::GetMousePos();
87 orderScrollLocked=true;
88 orderScrollTolerance=true;
89 }
90
91 // time
92 if (e->isPlaying() && settings.playbackTime) {
93 int totalTicks=e->getTotalTicks();
94 int totalSeconds=e->getTotalSeconds();
95 String info="";
96
97 if (totalSeconds==0x7fffffff) {
98 info="∞";
99 } else {
100 if (totalSeconds>=86400) {
101 int totalDays=totalSeconds/86400;
102 int totalYears=totalDays/365;
103 totalDays%=365;
104 int totalMonths=totalDays/30;
105 totalDays%=30;
106
107 info+=fmt::sprintf("%dy",totalYears);
108 info+=fmt::sprintf("%dm",totalMonths);
109 info+=fmt::sprintf("%dd",totalDays);
110 }
111
112 if (totalSeconds>=3600) {
113 info+=fmt::sprintf("%.2d:",(totalSeconds/3600)%24);
114 }
115
116 info+=fmt::sprintf("%.2d:%.2d.%.2d",(totalSeconds/60)%60,totalSeconds%60,totalTicks/10000);
117 }
118
119 ImVec2 textSize=ImGui::CalcTextSize(info.c_str());
120
121 dl->AddRectFilled(ImVec2(11.0f*dpiScale,(size.y*0.5)-(5.0f*dpiScale)),ImVec2((21.0f*dpiScale)+textSize.x,(size.y*0.5)+textSize.y+(5.0f*dpiScale)),ImGui::GetColorU32(ImGuiCol_WindowBg));
122 dl->AddRect(ImVec2(11.0f*dpiScale,(size.y*0.5)-(5.0f*dpiScale)),ImVec2((21.0f*dpiScale)+textSize.x,(size.y*0.5)+textSize.y+(5.0f*dpiScale)),ImGui::GetColorU32(ImGuiCol_Border),0,0,dpiScale);
123 dl->AddText(ImVec2(16.0f*dpiScale,(size.y)*0.5),ImGui::GetColorU32(ImGuiCol_Text),info.c_str());
124 }
125 }
126 ImGui::End();
127 }
128
129 #define NEXT_BUTTON \
130 if (++buttonColumn>=buttonColumns) { \
131 buttonColumn=0; \
132 } else { \
133 ImGui::SameLine(); \
134 }
135
136 void FurnaceGUI::drawOrderButtons() {
137 int buttonColumns=(settings.orderButtonPos==0)?8:1;
138 int buttonColumn=0;
139
140 while (buttonColumns<8 && ((int)(8/buttonColumns)*ImGui::GetFrameHeightWithSpacing())>ImGui::GetContentRegionAvail().y) {
141 buttonColumns++;
142 }
143
144 if (ImGui::Button(ICON_FA_PLUS)) { handleUnimportant
145 // add order row (new)
146 doAction(GUI_ACTION_ORDERS_ADD);
147 }
148 if (ImGui::IsItemHovered()) {
149 ImGui::SetTooltip(_("Add new order"));
150 }
151 NEXT_BUTTON;
152
153 pushDestColor();
154 if (ImGui::Button(ICON_FA_MINUS)) { handleUnimportant
155 // remove this order row
156 doAction(GUI_ACTION_ORDERS_REMOVE);
157 }
158 popDestColor();
159 if (ImGui::IsItemHovered()) {
160 ImGui::SetTooltip(_("Remove order"));
161 }
162 NEXT_BUTTON;
163
164 if (ImGui::Button(ICON_FA_FILES_O)) { handleUnimportant
165 // duplicate order row
166 doAction(GUI_ACTION_ORDERS_DUPLICATE);
167 }
168 if (ImGui::IsItemClicked(ImGuiMouseButton_Right)) {
169 doAction(GUI_ACTION_ORDERS_DEEP_CLONE);
170 }
171 if (ImGui::IsItemHovered()) {
172 ImGui::SetTooltip(_("Duplicate order (right-click to deep clone)"));
173 }
174 NEXT_BUTTON;
175
176 if (ImGui::Button(ICON_FA_ANGLE_UP)) { handleUnimportant
177 // move order row up
178 doAction(GUI_ACTION_ORDERS_MOVE_UP);
179 }
180 if (ImGui::IsItemHovered()) {
181 ImGui::SetTooltip(_("Move order up"));
182 }
183 NEXT_BUTTON;
184
185 if (ImGui::Button(ICON_FA_ANGLE_DOWN)) { handleUnimportant
186 // move order row down
187 doAction(GUI_ACTION_ORDERS_MOVE_DOWN);
188 }
189 if (ImGui::IsItemHovered()) {
190 ImGui::SetTooltip(_("Move order down"));
191 }
192 NEXT_BUTTON;
193
194 if (ImGui::Button(ICON_FA_ANGLE_DOUBLE_DOWN)) { handleUnimportant
195 // duplicate order row at end
196 doAction(GUI_ACTION_ORDERS_DUPLICATE_END);
197 }
198 if (ImGui::IsItemClicked(ImGuiMouseButton_Right)) {
199 doAction(GUI_ACTION_ORDERS_DEEP_CLONE_END);
200 }
201 if (ImGui::IsItemHovered()) {
202 ImGui::SetTooltip(_("Place copy of current order at end of song (right-click to deep clone)"));
203 }
204 NEXT_BUTTON;
205
206 if (ImGui::Button(changeAllOrders?ICON_FA_LINK"##ChangeAll":ICON_FA_CHAIN_BROKEN"##ChangeAll")) { handleUnimportant
207 // whether to change one or all orders in a row
208 changeAllOrders=!changeAllOrders;
209 }
210 if (ImGui::IsItemHovered()) {
211 if (changeAllOrders) {
212 ImGui::SetTooltip(_("Order change mode: entire row"));
213 } else {
214 ImGui::SetTooltip(_("Order change mode: one"));
215 }
216 }
217 NEXT_BUTTON;
218
219 if (orderEditMode==0 && mobileUI) {
220 orderEditMode=1;
221 }
222
223 const char* orderEditModeLabel="?##OrderEditMode";
224 if (orderEditMode==3) {
225 orderEditModeLabel=ICON_FA_ARROWS_V "##OrderEditMode";
226 } else if (orderEditMode==2) {
227 orderEditModeLabel=ICON_FA_ARROWS_H "##OrderEditMode";
228 } else if (orderEditMode==1) {
229 orderEditModeLabel=ICON_FA_I_CURSOR "##OrderEditMode";
230 } else {
231 orderEditModeLabel=ICON_FA_MOUSE_POINTER "##OrderEditMode";
232 }
233 if (ImGui::Button(orderEditModeLabel)) { handleUnimportant
234 orderEditMode++;
235 if (orderEditMode>3) orderEditMode=mobileUI?1:0;
236 curNibble=false;
237 }
238 if (ImGui::IsItemHovered()) {
239 if (orderEditMode==3) {
240 ImGui::SetTooltip(_("Order edit mode: Select and type (scroll vertically)"));
241 } else if (orderEditMode==2) {
242 ImGui::SetTooltip(_("Order edit mode: Select and type (scroll horizontally)"));
243 } else if (orderEditMode==1) {
244 ImGui::SetTooltip(_("Order edit mode: Select and type (don't scroll)"));
245 } else {
246 ImGui::SetTooltip(_("Order edit mode: Click to change"));
247 }
248 }
249 }
250
251 void FurnaceGUI::drawOrders() {
252 static char selID[4096];
253 if (nextWindow==GUI_WINDOW_ORDERS) {
254 ordersOpen=true;
255 ImGui::SetNextWindowFocus();
256 nextWindow=GUI_WINDOW_NOTHING;
257 }
258 if (!ordersOpen) return;
259 if (mobileUI) {
260 patWindowPos=(portrait?ImVec2(0.0f,(mobileMenuPos*-0.65*canvasH)):ImVec2((0.16*canvasH)+0.5*canvasW*mobileMenuPos,0.0f));
261 patWindowSize=(portrait?ImVec2(canvasW,canvasH-(0.16*canvasW)-(pianoOpen?(0.4*canvasW):0.0f)):ImVec2(canvasW-(0.16*canvasH),canvasH-(pianoOpen?(0.3*canvasH):0.0f)));
262 ImGui::SetNextWindowPos(patWindowPos);
263 ImGui::SetNextWindowSize(patWindowSize);
264 } else {
265 //ImGui::SetNextWindowSizeConstraints(ImVec2(440.0f*dpiScale,400.0f*dpiScale),ImVec2(canvasW,canvasH));
266 }
267 if (ImGui::Begin("Orders",&ordersOpen,globalWinFlags|ImGuiWindowFlags_NoScrollbar|ImGuiWindowFlags_NoScrollWithMouse,_("Orders"))) {
268 if (ImGui::BeginTable("OrdColumn",(settings.orderButtonPos==0)?1:2,ImGuiTableFlags_BordersInnerV)) {
269 if (settings.orderButtonPos==2) {
270 ImGui::TableSetupColumn("c0",ImGuiTableColumnFlags_WidthStretch);
271 ImGui::TableSetupColumn("c1",ImGuiTableColumnFlags_WidthFixed);
272 } else if (settings.orderButtonPos==1) {
273 ImGui::TableSetupColumn("c0",ImGuiTableColumnFlags_WidthFixed);
274 ImGui::TableSetupColumn("c1",ImGuiTableColumnFlags_WidthStretch);
275 }
276
277 ImVec2 prevSpacing=ImGui::GetStyle().ItemSpacing;
278 if (settings.orderButtonPos!=0) {
279 ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing,ImVec2(1.0f*dpiScale,1.0f*dpiScale));
280 }
281
282 ImGui::TableNextRow();
283
284 if (settings.orderButtonPos<2) {
285 ImGui::TableNextColumn();
286 drawOrderButtons();
287 }
288
289 if (settings.orderButtonPos==0) {
290 ImGui::TableNextRow();
291 }
292
293 ImGui::TableNextColumn();
294
295 int displayChans=0;
296 for (int i=0; i<e->getTotalChannelCount(); i++) {
297 if (e->curSubSong->chanShow[i]) displayChans++;
298 }
299 ImGui::PushFont(patFont);
300 bool tooSmall=((displayChans+1)>((ImGui::GetContentRegionAvail().x)/(ImGui::CalcTextSize("AA").x+2.0*ImGui::GetStyle().ItemInnerSpacing.x)));
301 float yHeight=ImGui::GetContentRegionAvail().y;
302 float lineHeight=(ImGui::GetTextLineHeight()+4*dpiScale);
303 if (e->isPlaying() || haveHitBounds) {
304 if (followOrders) {
305 float nextOrdScroll=(playOrder+1)*lineHeight-((yHeight-(tooSmall?ImGui::GetStyle().ScrollbarSize:0.0f))/2.0f);
306 if (nextOrdScroll<0.0f) nextOrdScroll=0.0f;
307 ImGui::SetNextWindowScroll(ImVec2(-1.0f,nextOrdScroll));
308 }
309 }
310 ImVec2 clipBegin=ImGui::GetCursorScreenPos();
311 ImVec2 clipEnd=clipBegin+ImGui::GetContentRegionAvail();
312 if (ImGui::BeginTable("OrdersTable",1+displayChans,(tooSmall?ImGuiTableFlags_SizingFixedFit:ImGuiTableFlags_SizingStretchSame)|ImGuiTableFlags_ScrollX|ImGuiTableFlags_ScrollY)) {
313 if (tooSmall) {
314 // set up cell sizes? I don't know
315 }
316 ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing,prevSpacing);
317 ImGui::TableSetupScrollFreeze(1,1);
318 ImGui::TableNextRow(0,lineHeight);
319 ImGui::TableNextColumn();
320 ImGui::PushStyleColor(ImGuiCol_Text,uiColors[GUI_COLOR_ORDER_ROW_INDEX]);
321 for (int i=0; i<e->getTotalChannelCount(); i++) {
322 if (!e->curSubSong->chanShow[i]) continue;
323 ImGui::TableNextColumn();
324 ImGui::TextNoHashHide("%s",e->getChannelShortName(i));
325 }
326 // OH MY FREAKING. just let me sleep.
327 clipBegin.y+=lineHeight;
328 ImGui::PopStyleColor();
329 for (int i=0; i<e->curSubSong->ordersLen; i++) {
330 ImGui::TableNextRow(0,lineHeight);
331 if (playOrder==i && e->isPlaying()) ImGui::TableSetBgColor(ImGuiTableBgTarget_RowBg0,ImGui::GetColorU32(uiColors[GUI_COLOR_ORDER_ACTIVE]));
332 ImGui::TableNextColumn();
333 if (curOrder==i) {
334 if (ImGui::GetCurrentWindowRead()->ScrollbarY) {
335 clipEnd.x-=ImGui::GetStyle().ScrollbarSize;
336 }
337 // draw a border
338 ImGui::PushClipRect(clipBegin,clipEnd,false);
339 ImDrawList* dl=ImGui::GetWindowDrawList();
340 ImVec2 rBegin=ImGui::GetCursorScreenPos();
341 rBegin.y-=ImGui::GetStyle().CellPadding.y;
342 ImVec2 rEnd=ImVec2(clipEnd.x,rBegin.y+lineHeight);
343 dl->AddRect(rBegin,rEnd,ImGui::GetColorU32(uiColors[GUI_COLOR_ORDER_SELECTED]),2.0f*dpiScale);
344 ImGui::PopClipRect();
345 }
346 ImGui::PushStyleColor(ImGuiCol_Text,uiColors[GUI_COLOR_ORDER_ROW_INDEX]);
347 bool highlightLoop=(i>=loopOrder && i<=loopEnd);
348 if (highlightLoop) ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg,ImGui::GetColorU32(uiColors[GUI_COLOR_SONG_LOOP]));
349 if (settings.orderRowsBase==1) {
350 snprintf(selID,4096,"%.2X##O_S%.2x",i,i);
351 } else {
352 snprintf(selID,4096,"%d##O_S%.2x",i,i);
353 }
354 if (ImGui::Selectable(selID)) {
355 setOrder(i);
356 curNibble=false;
357 orderCursor=-1;
358
359 if (orderEditMode==0) {
360 handleUnimportant;
361 }
362
363 if (cursor.xCoarse==selStart.xCoarse && cursor.xFine==selStart.xFine && cursor.y==selStart.y && cursor.order==selStart.order &&
364 cursor.xCoarse==selEnd.xCoarse && cursor.xFine==selEnd.xFine && cursor.y==selEnd.y && cursor.order==selEnd.order) {
365 cursor.order=curOrder;
366 selStart=cursor;
367 selEnd=cursor;
368 }
369 }
370 ImGui::PopStyleColor();
371 for (int j=0; j<e->getTotalChannelCount(); j++) {
372 if (!e->curSubSong->chanShow[j]) continue;
373 if (!ImGui::TableNextColumn()) continue;
374 DivPattern* pat=e->curPat[j].getPattern(e->curOrders->ord[j][i],false);
375 /*if (!pat->name.empty()) {
376 snprintf(selID,4096,"%s##O_%.2x_%.2x",pat->name.c_str(),j,i);
377 } else {*/
378 snprintf(selID,4096,"%.2X##O_%.2x_%.2x",e->curOrders->ord[j][i],j,i);
379 //}
380
381 ImGui::PushStyleColor(ImGuiCol_Text,(curOrder==i || e->curOrders->ord[j][i]==e->curOrders->ord[j][curOrder])?uiColors[GUI_COLOR_ORDER_SIMILAR]:uiColors[GUI_COLOR_ORDER_INACTIVE]);
382 if (ImGui::Selectable(selID,settings.ordersCursor?(cursor.xCoarse==j && curOrder!=i):false)) {
383 if (curOrder==i) {
384 if (orderEditMode==0) {
385 prepareUndo(GUI_UNDO_CHANGE_ORDER);
386 e->lockSave([this,i,j]() {
387 if (changeAllOrders) {
388 for (int k=0; k<e->getTotalChannelCount(); k++) {
389 if (e->curOrders->ord[k][i]<(unsigned char)(DIV_MAX_PATTERNS-1)) e->curOrders->ord[k][i]++;
390 }
391 } else {
392 if (e->curOrders->ord[j][i]<(unsigned char)(DIV_MAX_PATTERNS-1)) e->curOrders->ord[j][i]++;
393 }
394 });
395 e->walkSong(loopOrder,loopRow,loopEnd);
396 makeUndo(GUI_UNDO_CHANGE_ORDER);
397 } else {
398 orderCursor=j;
399 curNibble=false;
400 }
401 } else {
402 setOrder(i);
403 e->walkSong(loopOrder,loopRow,loopEnd);
404 if (orderEditMode!=0) {
405 orderCursor=j;
406 curNibble=false;
407 }
408
409 // i wonder whether this is necessary
410 if (cursor.xCoarse==selStart.xCoarse && cursor.xFine==selStart.xFine && cursor.y==selStart.y && cursor.order==selStart.order &&
411 cursor.xCoarse==selEnd.xCoarse && cursor.xFine==selEnd.xFine && cursor.y==selEnd.y && cursor.order==selEnd.order) {
412 cursor.order=curOrder;
413 selStart=cursor;
414 selEnd=cursor;
415 }
416 }
417
418 if (orderEditMode==0) {
419 handleUnimportant;
420 }
421 }
422 ImGui::PopStyleColor();
423 if (orderEditMode!=0 && curOrder==i && orderCursor==j) {
424 // draw a border
425 ImDrawList* dl=ImGui::GetWindowDrawList();
426 dl->AddRect(ImGui::GetItemRectMin(),ImGui::GetItemRectMax(),ImGui::GetColorU32(uiColors[GUI_COLOR_TEXT]),2.0f*dpiScale);
427 }
428 if (!pat->name.empty() && ImGui::IsItemHovered()) {
429 ImGui::SetTooltip("%s",pat->name.c_str());
430 }
431 if (ImGui::IsItemClicked(ImGuiMouseButton_Right)) {
432 if (curOrder==i) {
433 if (orderEditMode==0) {
434 prepareUndo(GUI_UNDO_CHANGE_ORDER);
435 e->lockSave([this,i,j]() {
436 if (changeAllOrders) {
437 for (int k=0; k<e->getTotalChannelCount(); k++) {
438 if (e->curOrders->ord[k][i]>0) e->curOrders->ord[k][i]--;
439 }
440 } else {
441 if (e->curOrders->ord[j][i]>0) e->curOrders->ord[j][i]--;
442 }
443 });
444 e->walkSong(loopOrder,loopRow,loopEnd);
445 makeUndo(GUI_UNDO_CHANGE_ORDER);
446 } else {
447 orderCursor=j;
448 curNibble=false;
449 }
450 } else {
451 setOrder(i);
452 e->walkSong(loopOrder,loopRow,loopEnd);
453 if (orderEditMode!=0) {
454 orderCursor=j;
455 curNibble=false;
456 }
457
458 if (cursor.xCoarse==selStart.xCoarse && cursor.xFine==selStart.xFine && cursor.y==selStart.y && cursor.order==selStart.order &&
459 cursor.xCoarse==selEnd.xCoarse && cursor.xFine==selEnd.xFine && cursor.y==selEnd.y && cursor.order==selEnd.order) {
460 cursor.order=curOrder;
461 selStart=cursor;
462 selEnd=cursor;
463 }
464 }
465 }
466 }
467 }
468 ImGui::PopStyleVar();
469 ImGui::EndTable();
470 }
471 ImGui::PopFont();
472
473 if (settings.orderButtonPos==2) {
474 ImGui::TableNextColumn();
475 drawOrderButtons();
476 }
477
478 if (settings.orderButtonPos!=0) {
479 ImGui::PopStyleVar();
480 }
481
482 ImGui::EndTable();
483 }
484 }
485 if (ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows)) curWindow=GUI_WINDOW_ORDERS;
486 ImGui::End();
487 }