-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracker.c
2516 lines (2348 loc) · 72.4 KB
/
tracker.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <strings.h>
#include "tracker.h"
#include "console.h"
#include "chip.h"
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
typedef struct {
int x1;
int y1;
int x2;
int y2;
} Rect;
Rect rectFromPoints(const int _x1, const int _y1, const int _x2, const int _y2) {
int x1, y1, x2, y2;
if (_x1 <= _x2) {
x1 = _x1;
x2 = _x2;
} else {
x1 = _x2;
x2 = _x1;
}
if (_y1 <= _y2) {
y1 = _y1;
y2 = _y2;
} else {
y1 = _y2;
y2 = _y1;
}
return (Rect) {
x1, y1, x2, y2
};
}
Rect rectFromSize(int _x, int _y, int _w, int _h) {
return (Rect) {
_x, _y, _x + _w, _y + _h
};
}
Rect rectLine(int _x, int _y, int _len) {
return (Rect) {
_x, _y, _x + _len, _y
};
}
Rect rectRel(int _len) {
return (Rect) {
con_x(), con_y(), con_x() + _len, con_y()
};
}
Rect rectFrom(int _start) {
return (Rect) {
_start, con_y(), con_x(), con_y()
};
}
bool rectHit(Rect _rect, int _x, int _y) {
return _rect.x1 <= _x &&
_rect.x2 >= _x &&
_rect.y1 <= _y &&
_rect.y2 >= _y;
}
struct TextEdit;
typedef struct TextEdit TextEdit;
typedef struct {
Rect hitRect;
TrackerState validStates;
Action action;
TrackerState actionTrackerState;
TrackerState trackerState;
int songX;
int songY;
int selectedChannel;
int patternX;
int patternY;
int selectedPattern;
int instrumentX;
int instrumentY;
int instrumentParam;
int selectedInstrument;
int selectedTableKind;
int tableX;
int metaDataY;
TextEdit *textEdit;
} Hit;
#define NUM_HITS 65536
Hit sHits[NUM_HITS];
u16 sHitPos = 0;
void clearHits() {
sHitPos = 0;
}
Hit *addHit(Rect _hitRect, TrackerState _validStates) {
sHits[sHitPos++] = (Hit) {
_hitRect,
_validStates,
ACTION_NONE,
TRACKER_EDIT_ANY,
TRACKER_STATE_NONE,
-1, -1, -1,
-1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1,
NULL
};
return &sHits[sHitPos - 1];
}
typedef void (*TextEditChangeCallback)(TextEdit *_te, TrackerTextEditKey _exitKey);
struct TextEdit {
u8 xPos;
u8 yPos;
u8 maxWidth;
u8 visibleWidth;
int cursorPos;
u8 visibleOffset;
char *string;
char *lastString;
bool isEnabled;
bool showsEllipsis;
bool isActive;
TextEditChangeCallback changeCallback;
TextEdit *next;
TextEdit *prev;
void *userData;
};
TextEdit *TextEdit_new(TextEdit **_root, u8 _x, u8 _y, u8 _maxWidth, u8 _visibleWidth, bool _enabled,
bool _ellipsis, char _initialString[], TextEditChangeCallback _changeCallback,
void *_userData) {
TextEdit *te = malloc(sizeof(TextEdit));
if (!te) {
return NULL;
}
char *buffer = malloc(sizeof(char) * _maxWidth);
if (!buffer) {
free(te);
return NULL;
}
char *lastBuffer = malloc(sizeof(char) * _maxWidth);
if (!lastBuffer) {
free(te);
return NULL;
}
strncpy(buffer, _initialString, _maxWidth);
strncpy(lastBuffer, _initialString, _maxWidth);
int len = strlen(buffer);
int visibleOffset = (len < _visibleWidth) ? 0 : len - _visibleWidth + 1;
*te = (TextEdit) {
.xPos=_x,
.yPos=_y,
.maxWidth=_maxWidth,
.visibleWidth=_visibleWidth,
.cursorPos=len,
.visibleOffset = visibleOffset,
.string = buffer,
.lastString = lastBuffer,
.isEnabled = _enabled,
.showsEllipsis = _ellipsis,
.isActive = false,
.changeCallback = _changeCallback,
.next = *_root,
.prev = NULL,
.userData = _userData
};
*_root = te;
return te;
}
void TextEdit_delete(TextEdit **_root, TextEdit *_te) {
// Unlink
if (_te->prev == NULL) {
*_root = _te->next;
} else {
_te->prev->next = _te->next;
}
if (_te->next != NULL) {
_te->next->prev = _te->prev;
}
// Dealloc
free(_te->string);
free(_te);
}
void TextEdit_deleteAll(TextEdit **_root) {
while (_root != NULL) {
TextEdit_delete(_root, *_root);
}
}
void TextEdit_draw(TextEdit *_te) {
if (_te->isActive) {
con_gotoXY(_te->xPos, _te->yPos);
Hit *hit = addHit(rectRel(_te->visibleWidth + 1), TRACKER_EDIT_ANY);
hit->textEdit = _te;
con_setAttrib(0x0F);
con_putc(0xDE);
const int len = strlen(_te->string);
const int start = _te->visibleOffset;
const int end = _te->visibleOffset + _te->visibleWidth;
for (size_t i = start; i < end; i++) {
if (i < len) {
con_setAttrib(0x07);
con_putc(_te->string[i]);
} else {
con_setAttrib(0x80);
con_putc(0xB2);
}
}
con_setAttrib(0x0F);
con_putc(0xDD);
const int x = 1 + _te->xPos + _te->cursorPos - _te->visibleOffset;
const int y = _te->yPos;
con_setAttribXY(x, y, 0x100 | con_getAttribXY(x, y));
} else {
con_gotoXY(_te->xPos, _te->yPos);
Hit *hit = addHit(rectRel(_te->visibleWidth + 1), TRACKER_EDIT_ANY);
hit->textEdit = _te;
con_setAttrib(0x08);
con_putc(0xDE);
const int len = strlen(_te->string);
const int start = 0;
const int end = _te->visibleWidth;
for (size_t i = start; i < end; i++) {
if (i < len) {
con_setAttrib(0x07);
con_putc((_te->string)[i]);
} else {
con_setAttrib(0x80);
con_putc(0xB2);
}
}
con_setAttrib(0x08);
con_putc(0xDD);
}
} /* TextEdit_draw */
void TextEdit_drawAll(TextEdit *_root) {
while (_root != NULL) {
TextEdit_draw(_root);
_root = _root->next;
}
}
void TextEdit_pos(TextEdit *_te, int _x, int _y) {
_te->xPos = _x;
_te->yPos = _y;
}
void TextEdit_width(TextEdit *_te, int _visibleWidth) {
_te->visibleWidth = _visibleWidth;
int len = strlen(_te->lastString);
_te->visibleOffset = (len < _te->visibleWidth) ? 0 : len - _te->visibleWidth + 1;
_te->cursorPos = len;
}
void TextEdit_set(TextEdit *_te, const char *_string) {
strncpy(_te->string, _string, _te->maxWidth);
strncpy(_te->lastString, _string, _te->maxWidth);
int len = strlen(_te->lastString);
_te->visibleOffset = (len < _te->visibleWidth) ? 0 : len - _te->visibleWidth + 1;
_te->cursorPos = len;
}
const char *TextEdit_get(TextEdit *_te) {
return _te->lastString;
}
void TextEdit_activate(TextEdit *_te) {
_te->isActive = true;
strncpy(_te->string, _te->lastString, _te->maxWidth);
}
void TextEdit_deactivate(TextEdit *_te) {
_te->isActive = false;
}
void TextEdit_deactivateAll(TextEdit *_someNode) {
if (_someNode == NULL) {
return;
}
// Find root
TextEdit *node = _someNode;
while (node->prev != NULL) {
node = node->prev;
}
// Deactive all
while (node->next != NULL) {
TextEdit_deactivate(node);
node = node->next;
}
}
bool TextEdit_handleEditKey(TextEdit **_root, TrackerTextEditKey _key) {
// Find active TextEdit else return
TextEdit *te = *_root;
while (te != NULL) {
if (te->isActive) {
break;
}
te = te->next;
}
if (te == NULL) {
return false;
}
// Handle key
switch (_key) {
case TEK_ENTER:
case TEK_DOWN:
case TEK_UP: {
// Save the edit back to the reference string
strncpy(te->lastString, te->string, te->maxWidth);
te->changeCallback(te, _key);
TextEdit_deactivate(te);
return true;
}
case TEK_ESCAPE: {
strncpy(te->string, te->lastString, te->maxWidth);
int len = strlen(te->string);
te->visibleOffset = (len < te->visibleWidth) ? 0 : len - te->visibleWidth + 1;
te->cursorPos = len;
te->changeCallback(te, _key);
TextEdit_deactivate(te);
return true;
}
case TEK_HOME: {
te->cursorPos = 0;
te->visibleOffset = 0;
return true;
}
case TEK_END: {
te->cursorPos = strlen(te->string);
if (te->cursorPos - te->visibleOffset > te->visibleWidth - 1) {
te->visibleOffset = te->cursorPos - te->visibleWidth;
}
return true;
}
case TEK_RIGHT: {
int len = strlen(te->string);
if (te->cursorPos < len) {
te->cursorPos++;
if (te->cursorPos - te->visibleOffset > te->visibleWidth - 1) {
te->visibleOffset++;
}
}
return true;
}
case TEK_LEFT: {
if (te->cursorPos > 0) {
te->cursorPos--;
if (te->cursorPos - te->visibleOffset < 0) {
te->visibleOffset--;
}
}
return true;
}
case TEK_WORD_RIGHT: {
int len = strlen(te->string);
while ((te->string[te->cursorPos] != ' ') && te->cursorPos < len) {
te->cursorPos++;
if (te->cursorPos - te->visibleOffset > te->visibleWidth - 1) {
te->visibleOffset++;
}
}
while ((te->string[te->cursorPos] == ' ') && te->cursorPos < len) {
te->cursorPos++;
if (te->cursorPos - te->visibleOffset > te->visibleWidth - 1) {
te->visibleOffset++;
}
}
return true;
}
case TEK_WORD_LEFT: {
int len = strlen(te->string);
bool first = true;
while ((te->string[te->cursorPos] == ' ' || first || te->cursorPos == len) &&
te->cursorPos > 0) {
te->cursorPos--;
if (te->cursorPos - te->visibleOffset < 0) {
te->visibleOffset--;
}
first = false;
}
while ((te->string[te->cursorPos] != ' ') &&
te->cursorPos > 0) {
te->cursorPos--;
if (te->cursorPos - te->visibleOffset < 0) {
te->visibleOffset--;
}
}
while ((te->string[te->cursorPos] == ' ') &&
te->cursorPos < len &&
te->cursorPos != 0) {
te->cursorPos++;
if (te->cursorPos - te->visibleOffset > te->visibleWidth - 1) {
te->visibleOffset++;
}
}
return true;
}
case TEK_CLEAR: {
te->string[0] = '\0';
te->cursorPos = 0;
te->visibleOffset = 0;
return true;
}
case TEK_DELETE: {
int len = strlen(te->string);
if (te->cursorPos < len) {
memmove(&te->string[te->cursorPos],
&te->string[te->cursorPos + 1],
len - te->cursorPos);
}
return true;
}
case TEK_BACKSPACE: {
int len = strlen(te->string);
if (te->cursorPos > 0) {
memmove(&te->string[te->cursorPos - 1],
&te->string[te->cursorPos],
len - te->cursorPos + 1);
te->cursorPos--;
if (te->cursorPos - te->visibleOffset < 0) {
te->visibleOffset--;
}
}
return true;
}
case TEK_SPACE: {
int len = strlen(te->string);
if (len < te->maxWidth) {
memmove(&te->string[te->cursorPos + 1],
&te->string[te->cursorPos],
len - te->cursorPos + 1);
te->string[te->cursorPos] = ' ';
}
te->cursorPos++;
if (te->cursorPos - te->visibleOffset > te->visibleWidth - 1) {
te->visibleOffset++;
}
return true;
}
case TEK_INSERT: {
int len = strlen(te->string);
if (len < te->maxWidth) {
memmove(&te->string[te->cursorPos + 1],
&te->string[te->cursorPos],
len - te->cursorPos + 1);
te->string[te->cursorPos] = ' ';
}
return true;
}
default: return false;
} /* switch */
} /* TextEdit_handleEditKey */
bool TextEdit_handleAsciiKey(TextEdit **_root, int _key) {
// Find active TextEdit else return
TextEdit *te = *_root;
while (te != NULL) {
if (te->isActive) {
break;
}
te = te->next;
}
if (te == NULL) {
return false;
}
// Handle key
int len = strlen(te->string);
if (len < te->maxWidth) {
memmove(&te->string[te->cursorPos + 1],
&te->string[te->cursorPos],
len - te->cursorPos + 1);
te->string[te->cursorPos] = _key;
te->cursorPos++;
if (te->cursorPos - te->visibleOffset > te->visibleWidth - 1) {
te->visibleOffset++;
}
}
return true;
}
void TextEdit_handleHit(TextEdit *_te, int _x) {
if (_te->isActive) {
_te->cursorPos = _x - _te->xPos + _te->visibleOffset - 1;
int len = strlen(_te->string);
if (_te->cursorPos > len) {
_te->cursorPos = len;
}
if (_te->cursorPos < 0) {
_te->cursorPos = 0;
}
if (_te->cursorPos < _te->visibleOffset) {
_te->visibleOffset = _te->cursorPos;
}
if (_te->cursorPos - _te->visibleOffset > _te->visibleWidth) {
_te->visibleOffset = _te->cursorPos - _te->visibleWidth + 1;
}
} else {
TextEdit_deactivateAll(_te);
_te->isActive = true;
}
}
TextEdit *sTextEditRoot_Editor;
TextEdit *sTEInstrumentName;
#define HANDLE_ACTION(action, state) if((_action == action) && ((state & _state & sTrackerState) != 0)) \
ACTION__ ## action ## __ ## state()
#define ACTION(action, state) static void ACTION__ ## action ## __ ## state()
char *sNotenames[] = {"C-", "C#", "D-", "D#", "E-", "F-", "F#", "G-", "G#", "A-", "A#", "B-"};
u8 sOctave = 4;
char *sFilename = "";
const char *sChipName;
ChipInterface *sChip;
int sSongX = 0, sSongY = 0;
int sSelectedChannel = 0;
int sPatternX = 0, sPatternY = 0;
int sSelectedPattern = 0;
int sInstrumentX = 0, sInstrumentY = 0, sInstrumentParam = 0;
int sSelectedInstrument = 0;
int sSelectedTableKind = 0;
u16 *sSelectedTable = NULL;
int sTableX = 0;
Rect sTableRect = {0};
int sMetaDataY = 0;
TrackerState sTrackerState = TRACKER_EDIT_SONG;
bool sbEditing = false;
bool sbShowKeys = false;
u8 sPlonkNote = 0;
void tracker_onChangeInstrumentName(TextEdit *_te, TrackerTextEditKey _exitKey) {
sChip->setInstrumentName(sSelectedInstrument,
sTEInstrumentName->lastString);
if (_exitKey == TEK_UP) {
tracker_instrumentMoveUp();
}
}
void tracker_init() {
sChip->init();
con_error(sChip->loadSong(sFilename));
sSelectedPattern = sChip->getPatternNum(sSongY, sSelectedChannel);
int count = sChip->getNumTableKinds();
sSelectedTable = malloc(sizeof(u16) * count);
for (size_t i = 0; i < count; i++) {
sSelectedTable[i] = sChip->getMinTable(i);
}
sTextEditRoot_Editor = NULL;
sTEInstrumentName = TextEdit_new(&sTextEditRoot_Editor, 20, 2, 40, 16, true, false, "",
tracker_onChangeInstrumentName, NULL);
sOctave = sChip->getMaxOctave() / 2;
}
void tracker_destroy() {
sChip->shutdown();
free(sSelectedTable);
}
void tracker_setFilename(char *filename) {
sFilename = filename;
}
void *tracker_setChipName(char *chipName) {
int i = 0;
do {
sChip = chips[i];
i++;
} while (sChip != NULL && strcasecmp(chipName, sChip->getChipId()));
if (sChip != NULL) {
sChipName = sChip->getChipId();
}
return sChip;
}
void tracker_drawNote(u8 _note) {
if (_note == 0) {
con_print("---");
} else if (_note == 255) {
con_print("***");
} else {
con_printf("%s%d", sNotenames[(_note - 1) % 12], (_note - 1) / 12);
}
}
int tracker_drawPatternEditorCentered(int _x, int _y, int _height) {
Hit *hit;
if (sTrackerState == TRACKER_EDIT_PATTERN) {
con_setAttrib(0x0F);
} else {
con_setAttrib(0x07);
}
con_printXY(_x, _y, "PATTERN");
static int patternOffset = 0;
int numChannels = sChip->getNumChannels();
int width = 3;
if (sPatternY < patternOffset) {
patternOffset = sPatternY;
}
if (sPatternY >= patternOffset + _height) {
patternOffset = sPatternY - _height + 1;
}
u8 maxPatternLen = 0;
for (size_t i = 0; i < numChannels; i++) {
int patternNum = sChip->getPatternNum(sSongY, i);
if (patternNum >= 0) {
u8 patternLen = sChip->getPatternLen(patternNum);
if (patternLen > maxPatternLen) {
maxPatternLen = patternLen;
}
}
}
u8 quarter = maxPatternLen >> 2;
// Draw pattern row numbers
int heightOfRows = _height - 1;
int halfHeightOfRows = heightOfRows / 2;
for (int j = 0; j < heightOfRows; j++) {
int row = sPatternY - halfHeightOfRows + j;
if (row >= 0 && row < maxPatternLen) {
if (row == sPatternY) {
con_setAttrib(0x4F);
} else {
if (row % quarter == 0) {
con_setAttrib(0x07);
} else {
con_setAttrib(0x08);
}
}
hit = addHit(rectLine(_x, _y + j + 2, 2), TRACKER_EDIT_ANY);
hit->trackerState = TRACKER_EDIT_PATTERN;
hit->patternY = row;
con_printfXY(_x, _y + 2 + j, "%02X", row);
}
}
for (size_t i = 0; i < numChannels; i++) {
int patternNum = sChip->getPatternNum(sSongY, i);
int patternLen = sChip->getPatternLen(patternNum);
// Compute pattern width
int patternWidth = 0;
for (int j = 0; j < heightOfRows; j++) {
int row = sPatternY - halfHeightOfRows + j;
if (row >= 0 && row < patternLen) {
int w = sChip->getNumPatternDataColumns(i, patternNum, row);
int tw = 0;
for (size_t k = 0; k < w; k++) {
ChipDataType type = sChip->getPatternDataType(i, patternNum, row, k);
switch (type) {
case CDT_LABEL:
case CDT_HEX:
case CDT_ASCII: tw++;
break;
case CDT_NOTE: tw += 3;
break;
}
}
patternWidth = (patternWidth > tw) ? patternWidth : tw;
}
}
width += patternWidth + 1;
// Draw pattern data
con_setAttrib(0x07);
con_printfXY(_x + i * (patternWidth + 1) + 3, _y + 1, "%02X", patternNum);
con_setAttrib(0x08);
con_printXY(_x + i * (patternWidth + 1) + 6, _y + 1, sChip->getChannelName(i, patternWidth - 2));
for (int j = 0; j < heightOfRows; j++) {
int row = sPatternY - halfHeightOfRows + j;
if (row >= 0 && row < patternLen) {
if (sPatternY == row) {
if (sSelectedChannel == i) {
con_setAttrib(0x4F);
} else {
con_setAttrib(0x47);
}
} else {
con_setAttrib(0x07);
}
con_gotoXY(_x + i * (patternWidth + 1) + 3, _y + j + 2);
int w = sChip->getNumPatternDataColumns(i, patternNum, row);
for (size_t k = 0; k < w; k++) {
if (sPatternY == row && sSelectedChannel == i) {
con_setAttrib(0x4F);
if (k == sPatternX && sTrackerState == TRACKER_EDIT_PATTERN) {
if (sbEditing) {
con_setAttrib(0x1F4);
} else {
con_setAttrib(0xF4);
}
}
}
ChipDataType type = sChip->getPatternDataType(i, patternNum, row, k);
u8 data = sChip->getPatternData(i, patternNum, row, k);
switch (type) {
case CDT_LABEL: hit = addHit(rectRel(1), TRACKER_EDIT_ANY);
con_putc(data);
break;
case CDT_NOTE: hit = addHit(rectRel(3), TRACKER_EDIT_ANY);
tracker_drawNote(data);
break;
case CDT_HEX: hit = addHit(rectRel(1), TRACKER_EDIT_ANY);
con_putc(data + ((data < 16) ? (data < 10 ? 48 : 55) : 0));
break;
case CDT_ASCII: hit = addHit(rectRel(1), TRACKER_EDIT_ANY);
con_putc(data);
break;
}
hit->trackerState = TRACKER_EDIT_PATTERN;
hit->patternY = row;
hit->patternX = k;
hit->selectedChannel = i;
}
}
}
for (size_t j = 1; j <= _height; j++) {
con_setAttrib(j == halfHeightOfRows + 2 ? 0x48 : 0x08);
con_putcXY(_x + i * (patternWidth + 1) + 2, _y + j, 0xb3);
}
}
width--;
con_setAttrib(0x8);
for (int i = 0; i <= _height; i++) {
con_putcXY(_x + width, _y + i, 0xba);
}
return width;
} /* tracker_drawPatternEditorCentered */
int tracker_drawPatternEditor(int _x, int _y, int _height) {
Hit *hit;
if (sTrackerState == TRACKER_EDIT_PATTERN) {
con_setAttrib(0x0F);
} else {
con_setAttrib(0x07);
}
con_printXY(_x, _y, "PATTERN");
static int patternOffset = 0;
int numChannels = sChip->getNumChannels();
int width = 3;
if (sPatternY < patternOffset) {
patternOffset = sPatternY;
}
if (sPatternY >= patternOffset + _height) {
patternOffset = sPatternY - _height + 1;
}
u8 maxPatternLen = 0;
for (size_t i = 0; i < numChannels; i++) {
int patternNum = sChip->getPatternNum(sSongY, i);
int patternLen = sChip->getPatternLen(patternNum);
maxPatternLen = (patternLen > maxPatternLen) ? patternLen : maxPatternLen;
}
u8 quarter = maxPatternLen >> 2;
// Draw pattern row numbers
for (size_t j = 0; j < maxPatternLen; j++) {
if (j >= patternOffset && j - patternOffset < _height - 1) {
if (j == sPatternY) {
if (j % quarter == 0) {
con_setAttrib(0x7F);
} else {
con_setAttrib(0x70);
}
} else {
if (j % quarter == 0) {
con_setAttrib(0x0F);
} else {
con_setAttrib(0x08);
}
}
hit = addHit(rectLine(_x, _y + j + 2, 2), TRACKER_EDIT_ANY);
hit->trackerState = TRACKER_EDIT_PATTERN;
hit->patternY = j;
con_printfXY(_x, _y + j + 2, "%02X", j);
}
}
for (size_t i = 0; i < numChannels; i++) {
int patternNum = sChip->getPatternNum(sSongY, i);
int patternLen = sChip->getPatternLen(patternNum);
// Compute pattern width
int patternWidth = 0;
for (size_t j = 0; j < patternLen; j++) {
int w = sChip->getNumPatternDataColumns(i, patternNum, j);
int tw = 0;
for (size_t k = 0; k < w; k++) {
ChipDataType type = sChip->getPatternDataType(i, patternNum, j, k);
switch (type) {
case CDT_LABEL:
case CDT_HEX:
case CDT_ASCII: tw++;
break;
case CDT_NOTE: tw += 3;
break;
}
}
patternWidth = (patternWidth > tw) ? patternWidth : tw;
}
width += patternWidth + 1;
// Draw pattern data
con_setAttrib(0x07);
con_printfXY(_x + i * (patternWidth + 1) + 3, _y + 1, "%02X", patternNum);
con_setAttrib(0x08);
con_printXY(_x + i * (patternWidth + 1) + 6, _y + 1, sChip->getChannelName(i, patternWidth - 2));
for (size_t j = 0; j < patternLen; j++) {
if (sSelectedChannel == i && sPatternY == j) {
con_setAttrib(0x70);
} else {
con_setAttrib(0x07);
}
if (j >= patternOffset && j - patternOffset < _height - 1) {
con_gotoXY(_x + i * (patternWidth + 1) + 3, _y + j + 2);
int w = sChip->getNumPatternDataColumns(i, patternNum, j);
for (size_t k = 0; k < w; k++) {
if (sPatternY == j && sSelectedChannel == i) {
con_setAttrib(0x70);
if (k == sPatternX && sTrackerState == TRACKER_EDIT_PATTERN) {
con_setAttrib(0x170);
}
}
ChipDataType type = sChip->getPatternDataType(i, patternNum, j, k);
u8 data = sChip->getPatternData(i, patternNum, j, k);
switch (type) {
case CDT_LABEL: hit = addHit(rectRel(1), TRACKER_EDIT_ANY);
con_putc(data);
break;
case CDT_NOTE: hit = addHit(rectRel(3), TRACKER_EDIT_ANY);
tracker_drawNote(data);
break;
case CDT_HEX: hit = addHit(rectRel(1), TRACKER_EDIT_ANY);
con_putc(data + ((data < 16) ? (data < 10 ? 48 : 55) : 0));
break;
case CDT_ASCII: hit = addHit(rectRel(1), TRACKER_EDIT_ANY);
con_putc(data);
break;
}
hit->trackerState = TRACKER_EDIT_PATTERN;
hit->patternY = j;
hit->patternX = k;
hit->selectedChannel = i;
}
}
}
con_setAttrib(0x08);
for (size_t j = 1; j <= _height; j++) {
con_putcXY(_x + i * (patternWidth + 1) + 2, _y + j, 0xb3);
}
}
width--;
con_setAttrib(0x8);
for (int i = 0; i <= _height; i++) {
con_putcXY(_x + width, _y + i, 0xba);
}
return width;
} /* tracker_drawPatternEditor */
int tracker_drawInstrumentEditor(int _x, int _y, int _height) {
Hit *hit;
static int instOffset = 0;
if (sInstrumentY < instOffset) {
instOffset = sInstrumentY;
}
if (sInstrumentY >= instOffset + _height) {
instOffset = sInstrumentY - _height + 1;
}
int instLen = sChip->getInstrumentLen(sSelectedInstrument);
int numParams = sChip->getNumInstrumentParams(sSelectedInstrument);
// Draw line numbers
for (size_t instRow = 0; instRow < instLen; instRow++) {
if (instRow >= instOffset && instRow - instOffset < _height - 1) {
con_gotoXY(_x, _y + 3 + instRow - instOffset);
if (instRow == sInstrumentY) {
con_setAttrib(0x4F);
} else {
con_setAttrib(0x08);
}
hit = addHit(rectRel(3), TRACKER_EDIT_ANY);
hit->instrumentY = instRow;
con_printf(sChip->getInstrumentLabel(sSelectedInstrument, instRow));
con_putc(':');
}
}
// Params
int instWidth = 2;
for (size_t param = 0; param < numParams; param++) {
// Get param max width
int paramWidth = 0;
for (size_t instRow = 0; instRow < instLen; instRow++) {
int width = 0;
int numData = sChip->getNumInstrumentData(sSelectedInstrument, param, instRow);
for (size_t dataColumn = 0; dataColumn < numData; dataColumn++) {
ChipDataType type = sChip->getInstrumentDataType(sSelectedInstrument, param, instRow, dataColumn);
switch (type) {
case CDT_LABEL: width++;
break;
case CDT_NOTE: width += 3;
break;
case CDT_HEX: width++;
break;
case CDT_ASCII: width++;
break;
}
}
width++;
paramWidth = (width > paramWidth) ? width : paramWidth;
}
// Draw params
con_setAttrib(0x08);
con_printXY(_x + 1 + instWidth, _y + 2,
sChip->getInstrumentParamName(sSelectedInstrument, param, paramWidth));
for (size_t instRow = 0; instRow < instLen; instRow++) {
// Highlight param
for (int i = 0; i < paramWidth; ++i) {
if (instRow == sInstrumentY) {
u16 attrib = con_getAttribXY(_x + 1 + instWidth + i, _y + 3 + instRow - instOffset);
attrib = (attrib & 0xF0F) | 0x40;
con_setAttribXY(_x + 1 + instWidth + i, _y + 3 + instRow - instOffset, attrib);
}
}
if (instRow >= instOffset && instRow - instOffset < _height - 2) {
con_gotoXY(_x + 1 + instWidth, _y + 3 + instRow - instOffset);
int numData = sChip->getNumInstrumentData(sSelectedInstrument, param, instRow);
for (size_t dataColumn = 0; dataColumn < numData; dataColumn++) {
ChipDataType type = sChip->getInstrumentDataType(sSelectedInstrument, param, instRow, dataColumn);
u8 data = sChip->getInstrumentData(sSelectedInstrument, param, instRow, dataColumn);
if (instRow == sInstrumentY) {
if (dataColumn == sInstrumentX &&
param == sInstrumentParam &&
!sTEInstrumentName->isActive) {
if (sTrackerState == TRACKER_EDIT_INSTRUMENT) {
if (sbEditing) {
con_setAttrib(0x14F);
} else {
con_setAttrib(0xF4);
}
} else {
con_setAttrib(0x4F);
}
} else {
con_setAttrib(0x47);
}
} else {
con_setAttrib(0x07);
}
switch (type) {
case CDT_LABEL: con_putc(data);
break;
case CDT_NOTE: hit = addHit(rectRel(3), TRACKER_EDIT_ANY);
hit->trackerState = TRACKER_EDIT_INSTRUMENT;
hit->instrumentY = instRow;
hit->instrumentX = dataColumn;
hit->instrumentParam = param;
tracker_drawNote(data);
break;
case CDT_HEX: hit = addHit(rectRel(1), TRACKER_EDIT_ANY);
hit->trackerState = TRACKER_EDIT_INSTRUMENT;
hit->instrumentY = instRow;
hit->instrumentX = dataColumn;
hit->instrumentParam = param;
con_putc(data + ((data < 16) ? (data < 10 ? 48 : 55) : 0));
break;
case CDT_ASCII: hit = addHit(rectRel(1), TRACKER_EDIT_ANY);
hit->trackerState = TRACKER_EDIT_INSTRUMENT;
hit->instrumentY = instRow;
hit->instrumentX = dataColumn;