Debian Codegraph @

src/gui/cursor.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 "../ta-log.h"
22 #include "actionUtil.h"
23
24 void FurnaceGUI::startSelection(int xCoarse, int xFine, int y, int ord, bool fullRow) {
25 DETERMINE_FIRST_LAST;
26
27 if (xCoarse!=selStart.xCoarse || xFine!=selStart.xFine || y!=selStart.y || ord!=selStart.order) {
28 curNibble=false;
29 }
30
31 if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left) && !fullRow && settings.doubleClickColumn) {
32 if (cursor.xCoarse==xCoarse && cursor.xFine==xFine && cursor.y==y && cursor.order==ord) {
33 // select entire channel
34 selStart.xCoarse=xCoarse;
35 selStart.xFine=0;
36 selStart.y=0;
37 selStart.order=ord;
38 selEnd.xCoarse=xCoarse;
39 selEnd.xFine=2+e->curPat[selEnd.xCoarse].effectCols*2;
40 selEnd.y=e->curSubSong->patLen-1;
41 selEnd.order=ord;
42
43 finishSelection();
44 return;
45 }
46 }
47
48 if (((settings.dragMovesSelection==1 || settings.dragMovesSelection==3 || settings.dragMovesSelection==5) || ((settings.dragMovesSelection==2 || settings.dragMovesSelection==4) && (ImGui::IsKeyDown(ImGuiKey_LeftCtrl) || ImGui::IsKeyDown(ImGuiKey_RightCtrl)))) && !fullRow) {
49 if (xCoarse>=selStart.xCoarse && (xFine>=selStart.xFine || xCoarse>selStart.xCoarse) && (ord>selStart.order || y>=selStart.y) &&
50 xCoarse<=selEnd.xCoarse && (xFine<=selEnd.xFine || xCoarse<selEnd.xCoarse) && (ord<selEnd.order || y<=selEnd.y)) {
51 dragging=true;
52 selecting=true;
53 selectingFull=false;
54 dragSourceX=xCoarse;
55 dragSourceXFine=xFine;
56 dragSourceY=y;
57 dragSourceOrder=ord;
58 dragDestinationX=xCoarse;
59 dragDestinationXFine=xFine;
60 dragDestinationY=y;
61 dragDestinationOrder=ord;
62 dragStart=selStart;
63 dragEnd=selEnd;
64 return;
65 }
66 }
67
68 if (fullRow) {
69 selStart.xCoarse=firstChannel;
70 selStart.xFine=0;
71 selEnd.xCoarse=lastChannel-1;
72 selEnd.xFine=2+e->curPat[selEnd.xCoarse].effectCols*2;
73 selStart.y=y;
74 selEnd.y=y;
75 selStart.order=ord;
76 selEnd.order=ord;
77 } else {
78 if (xCoarse!=cursor.xCoarse || y!=cursor.y || ord!=cursor.order) {
79 makeCursorUndo();
80 }
81 cursor.xCoarse=xCoarse;
82 cursor.xFine=xFine;
83 cursor.y=y;
84 cursor.order=ord;
85 selStart.xCoarse=xCoarse;
86 selStart.xFine=xFine;
87 selStart.y=y;
88 selStart.order=ord;
89 selEnd.xCoarse=xCoarse;
90 selEnd.xFine=xFine;
91 selEnd.y=y;
92 selEnd.order=ord;
93 }
94 selecting=true;
95 selectingFull=fullRow;
96 e->setMidiBaseChan(cursor.xCoarse);
97 }
98
99 void FurnaceGUI::updateSelection(int xCoarse, int xFine, int y, int ord, bool fullRow) {
100 if (!selecting) return;
101 if (dragging) {
102 dragDestinationX=xCoarse;
103 if (dragStart.xFine>=3 && dragStart.xCoarse==dragEnd.xCoarse) dragDestinationXFine=(dragSourceXFine&1)?((xFine-1)|1):((xFine+1)&(~1));
104 dragDestinationY=y;
105 dragDestinationOrder=ord;
106 cursorDrag.xCoarse=xCoarse;
107 cursorDrag.xFine=xFine;
108 cursorDrag.y=y;
109 cursorDrag.order=ord;
110
111 int len=dragEnd.xCoarse-dragStart.xCoarse+1;
112 if (len<0) len=0;
113
114 DETERMINE_FIRST_LAST;
115
116 if (dragStart.xCoarse+(dragDestinationX-dragSourceX)<firstChannel) {
117 dragDestinationX=dragSourceX-dragStart.xCoarse;
118 }
119
120 if (dragEnd.xCoarse+(dragDestinationX-dragSourceX)>=lastChannel) {
121 dragDestinationX=lastChannel-(dragEnd.xCoarse-dragSourceX)-1;
122 }
123
124 if (dragStart.xFine>=3 && dragStart.xCoarse==dragEnd.xCoarse) {
125 if (dragEnd.xFine+(dragDestinationXFine-dragSourceXFine)>(2+e->curPat[dragDestinationX].effectCols*2)) {
126 dragDestinationXFine=(2+e->curPat[dragDestinationX].effectCols*2)-dragEnd.xFine+dragSourceXFine;
127 }
128 if (dragStart.xFine+(dragDestinationXFine-dragSourceXFine)<3) {
129 dragDestinationXFine=3-dragStart.xFine+dragSourceXFine;
130 }
131 }
132
133 int dragStartYAbs=dragStart.y+(dragStart.order*e->curSubSong->patLen);
134 int dragEndYAbs=dragEnd.y+(dragEnd.order*e->curSubSong->patLen);
135 int dragSourceYAbs=dragSourceY+(dragSourceOrder*e->curSubSong->patLen);
136 int dragDestinationYAbs=dragDestinationY+(dragDestinationOrder*e->curSubSong->patLen);
137
138 if (e->curSubSong->patLen>0) {
139 int newSelStartYAbs=dragStartYAbs+(dragDestinationYAbs-dragSourceYAbs);
140 int newSelEndYAbs=dragEndYAbs+(dragDestinationYAbs-dragSourceYAbs);
141
142 logV("SS: %d SE: %d",newSelStartYAbs,newSelEndYAbs);
143
144 if (newSelStartYAbs<0) {
145 int newDragDestination=dragSourceYAbs-dragStartYAbs;
146 newSelStartYAbs=0;
147 newSelEndYAbs=dragEndYAbs-dragStartYAbs;
148 dragDestinationY=newDragDestination%e->curSubSong->patLen;
149 dragDestinationOrder=newDragDestination/e->curSubSong->patLen;
150 }
151
152 if (newSelEndYAbs>=(e->curSubSong->ordersLen*e->curSubSong->patLen)) {
153 int newDragDestination=(e->curSubSong->ordersLen*e->curSubSong->patLen)-(dragEndYAbs-dragSourceYAbs)-1;
154 newSelStartYAbs=(e->curSubSong->ordersLen*e->curSubSong->patLen)-1-(dragEndYAbs-dragStartYAbs);
155 newSelEndYAbs=(e->curSubSong->ordersLen*e->curSubSong->patLen)-1;
156 dragDestinationY=newDragDestination%e->curSubSong->patLen;
157 dragDestinationOrder=newDragDestination/e->curSubSong->patLen;
158 }
159
160 selStart.xCoarse=dragStart.xCoarse+(dragDestinationX-dragSourceX);
161 selStart.xFine=dragStart.xFine+(dragDestinationXFine-dragSourceXFine);
162 selStart.y=newSelStartYAbs%e->curSubSong->patLen;
163 selStart.order=newSelStartYAbs/e->curSubSong->patLen;
164 selEnd.xCoarse=dragEnd.xCoarse+(dragDestinationX-dragSourceX);
165 selEnd.xFine=dragEnd.xFine+(dragDestinationXFine-dragSourceXFine);
166 selEnd.y=newSelEndYAbs%e->curSubSong->patLen;
167 selEnd.order=newSelEndYAbs/e->curSubSong->patLen;
168 }
169 } else {
170 if (selectingFull) {
171 DETERMINE_LAST;
172 selEnd.xCoarse=lastChannel-1;
173 selEnd.xFine=2+e->curPat[selEnd.xCoarse].effectCols*2;
174 selEnd.y=y;
175 selEnd.order=ord;
176 } else {
177 selEnd.xCoarse=xCoarse;
178 selEnd.xFine=xFine;
179 selEnd.y=y;
180 selEnd.order=ord;
181 }
182 }
183 }
184
185 void FurnaceGUI::finishSelection() {
186 // swap points if needed
187 if (selEnd.order<selStart.order) {
188 selEnd.order^=selStart.order;
189 selStart.order^=selEnd.order;
190 selEnd.order^=selStart.order;
191 selEnd.y^=selStart.y;
192 selStart.y^=selEnd.y;
193 selEnd.y^=selStart.y;
194 } else if (selEnd.order==selStart.order && selEnd.y<selStart.y) {
195 selEnd.y^=selStart.y;
196 selStart.y^=selEnd.y;
197 selEnd.y^=selStart.y;
198 }
199 if (selEnd.xCoarse<selStart.xCoarse) {
200 selEnd.xCoarse^=selStart.xCoarse;
201 selStart.xCoarse^=selEnd.xCoarse;
202 selEnd.xCoarse^=selStart.xCoarse;
203
204 selEnd.xFine^=selStart.xFine;
205 selStart.xFine^=selEnd.xFine;
206 selEnd.xFine^=selStart.xFine;
207 } else if (selEnd.xCoarse==selStart.xCoarse && selEnd.xFine<selStart.xFine) {
208 selEnd.xFine^=selStart.xFine;
209 selStart.xFine^=selEnd.xFine;
210 selEnd.xFine^=selStart.xFine;
211 }
212 selecting=false;
213 selectingFull=false;
214 mobilePatSel=false;
215
216 if (dragging) {
217 if (dragSourceX==dragDestinationX && dragSourceY==dragDestinationY && dragSourceXFine==dragDestinationXFine && dragSourceOrder==dragDestinationOrder) {
218 cursor=cursorDrag;
219 selStart=cursorDrag;
220 selEnd=cursorDrag;
221 } else { // perform drag
222 doDrag(settings.dragMovesSelection==3 || settings.dragMovesSelection==4 || (settings.dragMovesSelection==5 && (ImGui::IsKeyDown(ImGuiKey_LeftCtrl) || ImGui::IsKeyDown(ImGuiKey_RightCtrl))));
223 }
224
225 dragging=false;
226 }
227
228 // boundary check
229 int chanCount=e->getTotalChannelCount();
230
231 if (selStart.xCoarse<0) selStart.xCoarse=0;
232 if (selStart.xCoarse>=chanCount) selStart.xCoarse=chanCount-1;
233 if (selStart.y<0) selStart.y=0;
234 if (selStart.y>=e->curSubSong->patLen) selStart.y=e->curSubSong->patLen-1;
235 if (selStart.order<0) selStart.order=0;
236 if (selStart.order>=e->curSubSong->ordersLen) selStart.order=e->curSubSong->ordersLen-1;
237 if (selEnd.xCoarse<0) selEnd.xCoarse=0;
238 if (selEnd.xCoarse>=chanCount) selEnd.xCoarse=chanCount-1;
239 if (selEnd.y<0) selEnd.y=0;
240 if (selEnd.y>=e->curSubSong->patLen) selEnd.y=e->curSubSong->patLen-1;
241 if (selEnd.order<0) selEnd.order=0;
242 if (selEnd.order>=e->curSubSong->ordersLen) selEnd.order=e->curSubSong->ordersLen-1;
243 if (cursor.xCoarse<0) cursor.xCoarse=0;
244 if (cursor.xCoarse>=chanCount) cursor.xCoarse=chanCount-1;
245 if (cursor.y<0) cursor.y=0;
246 if (cursor.y>=e->curSubSong->patLen) cursor.y=e->curSubSong->patLen-1;
247 if (cursor.order<0) cursor.order=0;
248 if (cursor.order>=e->curSubSong->ordersLen) cursor.order=e->curSubSong->ordersLen-1;
249
250 if (e->curSubSong->chanCollapse[selStart.xCoarse]==3) {
251 selStart.xFine=0;
252 }
253 if (e->curSubSong->chanCollapse[selEnd.xCoarse] && selEnd.xFine>=(3-e->curSubSong->chanCollapse[selEnd.xCoarse])) {
254 selEnd.xFine=2+e->curPat[selEnd.xCoarse].effectCols*2;
255 }
256 if (selStart.xFine<0) {
257 selStart.xFine=0;
258 }
259 if (selEnd.xFine<0) {
260 selEnd.xFine=0;
261 }
262 if (selStart.xFine>(2+e->curPat[selStart.xCoarse].effectCols*2)) {
263 selStart.xFine=2+e->curPat[selStart.xCoarse].effectCols*2;
264 }
265 if (selEnd.xFine>(2+e->curPat[selEnd.xCoarse].effectCols*2)) {
266 selEnd.xFine=2+e->curPat[selEnd.xCoarse].effectCols*2;
267 }
268
269 // change order if necessary
270 if (curOrder!=cursor.order) {
271 if (!e->isPlaying() || !followPattern) {
272 setOrder(cursor.order);
273 }
274 updateScroll(cursor.y);
275 }
276
277 logV(_("finish selection: %d.%d,%d.%d - %d.%d,%d.%d"),selStart.xCoarse,selStart.xFine,selStart.order,selStart.y,selEnd.xCoarse,selEnd.xFine,selEnd.order,selEnd.y);
278
279 e->setMidiBaseChan(cursor.xCoarse);
280 }
281
282 void FurnaceGUI::moveCursor(int x, int y, bool select) {
283 if (y>=editStepCoarse || y<=-editStepCoarse || x<=-5 || x>=5) {
284 makeCursorUndo();
285 }
286 if (!select) {
287 finishSelection();
288 }
289
290 DETERMINE_FIRST_LAST;
291
292 curNibble=false;
293 if (x!=0) {
294 demandScrollX=true;
295 if (x>0) {
296 for (int i=0; i<x; i++) {
297 if (++cursor.xFine>=(e->curSubSong->chanCollapse[cursor.xCoarse]?(4-e->curSubSong->chanCollapse[cursor.xCoarse]):(3+e->curPat[cursor.xCoarse].effectCols*2))) {
298 cursor.xFine=0;
299 if (++cursor.xCoarse>=lastChannel) {
300 if (settings.wrapHorizontal!=0 && !select) {
301 cursor.xCoarse=firstChannel;
302 if (settings.wrapHorizontal==2) y++;
303 } else {
304 cursor.xCoarse=lastChannel-1;
305 cursor.xFine=e->curSubSong->chanCollapse[cursor.xCoarse]?(3-e->curSubSong->chanCollapse[cursor.xCoarse]):(2+e->curPat[cursor.xCoarse].effectCols*2);
306 }
307 } else {
308 while (!e->curSubSong->chanShow[cursor.xCoarse]) {
309 cursor.xCoarse++;
310 if (cursor.xCoarse>=e->getTotalChannelCount()) break;
311 }
312 }
313 }
314 }
315 } else {
316 for (int i=0; i<-x; i++) {
317 if (--cursor.xFine<0) {
318 if (--cursor.xCoarse<firstChannel) {
319 if (settings.wrapHorizontal!=0 && !select) {
320 cursor.xCoarse=lastChannel-1;
321 cursor.xFine=2+e->curPat[cursor.xCoarse].effectCols*2;
322 if (settings.wrapHorizontal==2) y--;
323 } else {
324 cursor.xCoarse=firstChannel;
325 cursor.xFine=0;
326 }
327 } else {
328 while (!e->curSubSong->chanShow[cursor.xCoarse]) {
329 cursor.xCoarse--;
330 if (cursor.xCoarse<0) break;
331 }
332 if (e->curSubSong->chanCollapse[cursor.xCoarse]) {
333 cursor.xFine=3-e->curSubSong->chanCollapse[cursor.xCoarse];
334 } else {
335 cursor.xFine=2+e->curPat[cursor.xCoarse].effectCols*2;
336 }
337 }
338 }
339 }
340 }
341 }
342 if (y!=0) {
343 if (y>0) {
344 for (int i=0; i<y; i++) {
345 cursor.y++;
346 if (cursor.y>=e->curSubSong->patLen) {
347 if (settings.wrapVertical!=0 && !select) {
348 cursor.y=0;
349 if (settings.wrapVertical>1) {
350 if (!e->isPlaying() || !followPattern) {
351 if (curOrder<(e->curSubSong->ordersLen-1)) {
352 setOrder(curOrder+1);
353 cursor.order=curOrder;
354 } else if (settings.wrapVertical==3) {
355 setOrder(0);
356 cursor.order=curOrder;
357 } else {
358 cursor.y=e->curSubSong->patLen-1;
359 }
360 } else {
361 cursor.y=e->curSubSong->patLen-1;
362 }
363 }
364 } else {
365 cursor.y=e->curSubSong->patLen-1;
366 }
367 }
368 }
369 } else {
370 for (int i=0; i<-y; i++) {
371 cursor.y--;
372 if (cursor.y<0) {
373 if (settings.wrapVertical!=0 && !select) {
374 cursor.y=e->curSubSong->patLen-1;
375 if (settings.wrapVertical>1) {
376 if (!e->isPlaying() || !followPattern) {
377 if (curOrder>0) {
378 setOrder(curOrder-1);
379 cursor.order=curOrder;
380 } else if (settings.wrapVertical==3) {
381 setOrder(e->curSubSong->ordersLen-1);
382 cursor.order=curOrder;
383 } else {
384 cursor.y=0;
385 }
386 } else {
387 cursor.y=0;
388 }
389 }
390 } else {
391 cursor.y=0;
392 }
393 }
394 }
395 }
396 }
397 if (!select) {
398 selStart=cursor;
399 }
400 selEnd=cursor;
401 if (!settings.cursorMoveNoScroll) {
402 updateScroll(cursor.y);
403 }
404 e->setMidiBaseChan(cursor.xCoarse);
405 }
406
407 void FurnaceGUI::moveCursorPrevChannel(bool overflow) {
408 makeCursorUndo();
409 finishSelection();
410 curNibble=false;
411
412 DETERMINE_FIRST_LAST;
413
414 do {
415 cursor.xCoarse--;
416 if (cursor.xCoarse<0) break;
417 } while (!e->curSubSong->chanShow[cursor.xCoarse]);
418 if (cursor.xCoarse<firstChannel) {
419 if (overflow) {
420 cursor.xCoarse=lastChannel-1;
421 } else {
422 cursor.xCoarse=firstChannel;
423 }
424 }
425 e->setMidiBaseChan(cursor.xCoarse);
426
427 int xFineMax=(e->curSubSong->chanCollapse[cursor.xCoarse]?(4-e->curSubSong->chanCollapse[cursor.xCoarse]):(3+e->curPat[cursor.xCoarse].effectCols*2));
428 if (cursor.xFine<0) cursor.xFine=0;
429 if (cursor.xFine>=xFineMax) cursor.xFine=xFineMax-1;
430
431 selStart=cursor;
432 selEnd=cursor;
433 demandScrollX=true;
434 }
435
436 void FurnaceGUI::moveCursorNextChannel(bool overflow) {
437 makeCursorUndo();
438 finishSelection();
439 curNibble=false;
440
441 DETERMINE_FIRST_LAST;
442
443 do {
444 cursor.xCoarse++;
445 if (cursor.xCoarse>=e->getTotalChannelCount()) break;
446 } while (!e->curSubSong->chanShow[cursor.xCoarse]);
447 if (cursor.xCoarse>=lastChannel) {
448 if (overflow) {
449 cursor.xCoarse=firstChannel;
450 } else {
451 cursor.xCoarse=lastChannel-1;
452 }
453 }
454 e->setMidiBaseChan(cursor.xCoarse);
455
456 int xFineMax=(e->curSubSong->chanCollapse[cursor.xCoarse]?(4-e->curSubSong->chanCollapse[cursor.xCoarse]):(3+e->curPat[cursor.xCoarse].effectCols*2));
457 if (cursor.xFine<0) cursor.xFine=0;
458 if (cursor.xFine>=xFineMax) cursor.xFine=xFineMax-1;
459
460 selStart=cursor;
461 selEnd=cursor;
462 demandScrollX=true;
463 }
464
465 void FurnaceGUI::moveCursorTop(bool select) {
466 makeCursorUndo();
467 if (!select) {
468 finishSelection();
469 }
470 curNibble=false;
471 if (cursor.y==0) {
472 DETERMINE_FIRST;
473 cursor.xCoarse=firstChannel;
474 cursor.xFine=0;
475 demandScrollX=true;
476 } else {
477 cursor.y=0;
478 }
479 if (!select) {
480 selStart=cursor;
481 }
482 selEnd=cursor;
483 e->setMidiBaseChan(cursor.xCoarse);
484 updateScroll(cursor.y);
485 }
486
487 void FurnaceGUI::moveCursorBottom(bool select) {
488 makeCursorUndo();
489 if (!select) {
490 finishSelection();
491 }
492 curNibble=false;
493 if (cursor.y==e->curSubSong->patLen-1) {
494 DETERMINE_LAST;
495 cursor.xCoarse=lastChannel-1;
496 if (cursor.xCoarse<0) cursor.xCoarse=0;
497 cursor.xFine=2+e->curPat[cursor.xCoarse].effectCols*2;
498 demandScrollX=true;
499 } else {
500 cursor.y=e->curSubSong->patLen-1;
501 }
502 if (!select) {
503 selStart=cursor;
504 }
505 selEnd=cursor;
506 e->setMidiBaseChan(cursor.xCoarse);
507 updateScroll(cursor.y);
508 }
509
510 void FurnaceGUI::editAdvance() {
511 finishSelection();
512 cursor.y+=editStep;
513 int hangPrevention=0;
514 while (cursor.y>=e->curSubSong->patLen) {
515 if (++hangPrevention>500) {
516 showError("BUG: about to hang when advancing cursor.\nplease report this issue immediately!");
517 break;
518 }
519 switch (settings.wrapVertical) {
520 case 1: // wrap
521 cursor.y-=e->curSubSong->patLen;
522 break;
523 case 2: // wrap + next pattern
524 if (curOrder<(e->curSubSong->ordersLen-1)) {
525 cursor.y-=e->curSubSong->patLen;
526 setOrder(curOrder+1);
527 cursor.order=curOrder;
528 } else {
529 cursor.y=e->curSubSong->patLen-1;
530 }
531 break;
532 case 3: // wrap + next pattern (wrap around)
533 cursor.y-=e->curSubSong->patLen;
534 if (curOrder<(e->curSubSong->ordersLen-1)) {
535 setOrder(curOrder+1);
536 } else {
537 setOrder(0);
538 }
539 cursor.order=curOrder;
540 break;
541 default: // don't wrap
542 cursor.y=e->curSubSong->patLen-1;
543 break;
544 }
545 }
546 selStart=cursor;
547 selEnd=cursor;
548 updateScroll(cursor.y);
549 }