-
Notifications
You must be signed in to change notification settings - Fork 3
/
Tracker3.java
2629 lines (2495 loc) · 72 KB
/
Tracker3.java
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
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
public class Tracker3 extends Canvas implements KeyListener, MouseListener, MouseMotionListener, WindowListener
{
public static final String VERSION = "Tracker3 (c)2020 [email protected]";
private static final long[] TOPAZ_8 = new long[]
{
0x0000000000000000L,
0x1818181818001800L,
0x6C6C000000000000L,
0x6C6CFE6CFE6C6C00L,
0x183E603C067C1800L,
0x0066ACD8366ACC00L,
0x386C6876DCCE7B00L,
0x1818300000000000L,
0x0C18303030180C00L,
0x30180C0C0C183000L,
0x00663CFF3C660000L,
0x0018187E18180000L,
0x0000000000181830L,
0x0000007E00000000L,
0x0000000000181800L,
0x03060C183060C000L,
0x3C666E7E76663C00L,
0x1838781818181800L,
0x3C66060C18307E00L,
0x3C66061C06663C00L,
0x1C3C6CCCFE0C0C00L,
0x7E607C0606663C00L,
0x1C30607C66663C00L,
0x7E06060C18181800L,
0x3C66663C66663C00L,
0x3C66663E060C3800L,
0x0018180000181800L,
0x0018180000181830L,
0x0006186018060000L,
0x00007E007E000000L,
0x0060180618600000L,
0x3C66060C18001800L,
0x7CC6DED6DEC07800L,
0x3C66667E66666600L,
0x7C66667C66667C00L,
0x1E30606060301E00L,
0x786C6666666C7800L,
0x7E60607860607E00L,
0x7E60607860606000L,
0x3C66606E66663E00L,
0x6666667E66666600L,
0x3C18181818183C00L,
0x0606060606663C00L,
0xC6CCD8F0D8CCC600L,
0x6060606060607E00L,
0xC6EEFED6C6C6C600L,
0xC6E6F6DECEC6C600L,
0x3C66666666663C00L,
0x7C66667C60606000L,
0x78CCCCCCCCDC7E00L,
0x7C66667C6C666600L,
0x3C66703C0E663C00L,
0x7E18181818181800L,
0x6666666666663C00L,
0x666666663C3C1800L,
0xC6C6C6D6FEEEC600L,
0xC3663C183C66C300L,
0xC3663C1818181800L,
0xFE0C183060C0FE00L,
0x3C30303030303C00L,
0xC06030180C060300L,
0x3C0C0C0C0C0C3C00L,
0x10386CC600000000L,
0x00000000000000FEL,
0x18180C0000000000L,
0x00003C063E663E00L,
0x60607C6666667C00L,
0x00003C6060603C00L,
0x06063E6666663E00L,
0x00003C667E603C00L,
0x1C307C3030303000L,
0x00003E66663E063CL,
0x60607C6666666600L,
0x1800181818180C00L,
0x0C000C0C0C0C0C78L,
0x6060666C786C6600L,
0x1818181818180C00L,
0x0000ECFED6C6C600L,
0x00007C6666666600L,
0x00003C6666663C00L,
0x00007C66667C6060L,
0x00003E66663E0606L,
0x00007C6660606000L,
0x00003C603C067C00L,
0x30307C3030301C00L,
0x0000666666663E00L,
0x00006666663C1800L,
0x0000C6C6D6FE6C00L,
0x0000C66C386CC600L,
0x00006666663C1830L,
0x00007E0C18307E00L,
0x0E18187018180E00L,
0x1818181818181800L,
0x7018180E18187000L,
0x729C000000000000L
};
private static final Color SHADOW = toColor( 0x000 );
private static final Color HIGHLIGHT = toColor( 0xFFF );
private static final Color BACKGROUND = toColor( 0xAAA );
private static final Color SELECTED = toColor( 0x68B );
private static final int TEXT_SHADOW_BACKGROUND = 0;
private static final int TEXT_HIGHLIGHT_BACKGROUND = 1;
private static final int TEXT_SHADOW_SELECTED = 2;
private static final int TEXT_HIGHLIGHT_SELECTED = 3;
private static final int TEXT_BLUE = 4;
private static final int TEXT_GREEN = 5;
private static final int TEXT_CYAN = 6;
private static final int TEXT_RED = 7;
private static final int TEXT_MAGENTA = 8;
private static final int TEXT_YELLOW = 9;
private static final int TEXT_WHITE = 10;
private static final int TEXT_LIME = 11;
private static final int[] FX_COLOURS = new int[]
{
/* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F */
TEXT_GREEN, TEXT_GREEN, TEXT_GREEN, TEXT_GREEN,
TEXT_GREEN, TEXT_LIME, TEXT_LIME, TEXT_YELLOW, TEXT_YELLOW,
TEXT_MAGENTA, 0, 0, 0, 0, 0, 0, 0, TEXT_YELLOW, TEXT_WHITE,
TEXT_YELLOW, TEXT_WHITE, 0, TEXT_WHITE
};
private static final int[] EX_COLOURS = new int[]
{
/* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F */
TEXT_BLUE, TEXT_GREEN, TEXT_GREEN, TEXT_GREEN, TEXT_GREEN,
TEXT_GREEN, TEXT_WHITE, TEXT_YELLOW, TEXT_BLUE, TEXT_MAGENTA,
0, 0, 0, 0, 0, 0, 0, TEXT_YELLOW, TEXT_YELLOW, TEXT_MAGENTA,
TEXT_MAGENTA, TEXT_MAGENTA, TEXT_MAGENTA
};
private static final int[] KEY_MAP = new int[]
{
KeyEvent.VK_SPACE,
KeyEvent.VK_Z, KeyEvent.VK_S, KeyEvent.VK_X, KeyEvent.VK_D,
KeyEvent.VK_C, KeyEvent.VK_V, KeyEvent.VK_G, KeyEvent.VK_B,
KeyEvent.VK_H, KeyEvent.VK_N, KeyEvent.VK_J, KeyEvent.VK_M,
KeyEvent.VK_Q, KeyEvent.VK_2, KeyEvent.VK_W, KeyEvent.VK_3,
KeyEvent.VK_E, KeyEvent.VK_R, KeyEvent.VK_5, KeyEvent.VK_T,
KeyEvent.VK_6, KeyEvent.VK_Y, KeyEvent.VK_7, KeyEvent.VK_U,
KeyEvent.VK_I, KeyEvent.VK_9, KeyEvent.VK_O, KeyEvent.VK_0,
KeyEvent.VK_P
};
private static final int[] HEX_MAP = new int[]
{
KeyEvent.VK_0, KeyEvent.VK_1, KeyEvent.VK_2, KeyEvent.VK_3,
KeyEvent.VK_4, KeyEvent.VK_5, KeyEvent.VK_6, KeyEvent.VK_7,
KeyEvent.VK_8, KeyEvent.VK_9, KeyEvent.VK_A, KeyEvent.VK_B,
KeyEvent.VK_C, KeyEvent.VK_D, KeyEvent.VK_E, KeyEvent.VK_F
};
private static final String KEY_TO_STR = "A-A#B-C-C#D-D#E-F-F#G-G#";
private static final String HEX_TO_STR = "0123456789ABCDEF";
private static final int GAD_COUNT = 40;
private static final int GAD_TYPE_LABEL = 1;
private static final int GAD_TYPE_BUTTON = 2;
private static final int GAD_TYPE_TEXTBOX = 3;
private static final int GAD_TYPE_SLIDER = 4;
private static final int GAD_TYPE_LISTBOX = 5;
private static final int GAD_TYPE_PATTERN = 6;
private static final int KEY_ESCAPE = KeyEvent.VK_ESCAPE;
private static final int KEY_BACKSPACE = KeyEvent.VK_BACK_SPACE;
private static final int KEY_DELETE = KeyEvent.VK_DELETE;
private static final int KEY_HOME = KeyEvent.VK_HOME;
private static final int KEY_END = KeyEvent.VK_END;
private static final int KEY_PAGE_UP = KeyEvent.VK_PAGE_UP;
private static final int KEY_PAGE_DOWN = KeyEvent.VK_PAGE_DOWN;
private static final int KEY_UP = KeyEvent.VK_UP;
private static final int KEY_DOWN = KeyEvent.VK_DOWN;
private static final int KEY_LEFT = KeyEvent.VK_LEFT;
private static final int KEY_RIGHT = KeyEvent.VK_RIGHT;
private static final int GADNUM_PATTERN = 1;
private static final int GADNUM_PATTERN_SLIDER = 2;
private static final int GADNUM_DIR_TEXTBOX = 3;
private static final int GADNUM_DIR_BUTTON = 4;
private static final int GADNUM_DIR_LISTBOX = 5;
private static final int GADNUM_DIR_SLIDER = 6;
private static final int GADNUM_TITLE_LABEL = 7;
private static final int GADNUM_TITLE_TEXTBOX = 8;
private static final int GADNUM_INST_LABEL = 9;
private static final int GADNUM_INST_TEXTBOX = 10;
private static final int GADNUM_INST_DEC_BUTTON = 11;
private static final int GADNUM_INST_INC_BUTTON = 12;
private static final int GADNUM_INST_NAME_LABEL = 14;
private static final int GADNUM_INST_NAME_TEXTBOX = 15;
private static final int GADNUM_INST_REP_LABEL = 16;
private static final int GADNUM_INST_REP_TEXTBOX = 17;
private static final int GADNUM_INST_VOL_LABEL = 18;
private static final int GADNUM_INST_VOL_TEXTBOX = 19;
private static final int GADNUM_INST_LEN_LABEL = 20;
private static final int GADNUM_INST_LEN_TEXTBOX = 21;
private static final int GADNUM_INST_FINE_LABEL = 22;
private static final int GADNUM_INST_FINE_TEXTBOX = 23;
private static final int GADNUM_SEQ_TEXTBOX = 24;
private static final int GADNUM_SEQ_INS_BUTTON = 25;
private static final int GADNUM_SEQ_DEL_BUTTON = 26;
private static final int GADNUM_SEQ_LISTBOX = 27;
private static final int GADNUM_SEQ_SLIDER = 28;
private static final int GADNUM_LOAD_BUTTON = 29;
private static final int GADNUM_SAVE_BUTTON = 30;
private static final int GADNUM_VER_LABEL = 31;
private static final int GADNUM_PLAY_BUTTON = 32;
private static final int MAX_CHANNELS = 8;
private static final int SAMPLING_RATE = 48000;
private int width, height, clickX, clickY, focus;
private int[] gadType = new int[ GAD_COUNT ];
private int[] gadX = new int[ GAD_COUNT ];
private int[] gadY = new int[ GAD_COUNT ];
private int[] gadWidth = new int[ GAD_COUNT ];
private int[] gadHeight = new int[ GAD_COUNT ];
private boolean[] gadRedraw = new boolean[ GAD_COUNT ];
private String[][] gadText = new String[ GAD_COUNT ][];
private int[][] gadValues = new int[ GAD_COUNT ][];
private boolean[] gadSelected = new boolean[ GAD_COUNT ];
private int[] gadValue = new int[ GAD_COUNT ];
private int[] gadRange = new int[ GAD_COUNT ];
private int[] gadMax = new int[ GAD_COUNT ];
private int[] gadItem = new int[ GAD_COUNT ];
private int[] gadLink = new int[ GAD_COUNT ];
private Image charset, image;
private ModPlay3 modPlay3 = new ModPlay3( MAX_CHANNELS );
private int instrument, octave = 2, selectedFile, triggerChannel;
private byte[] copyBuf = new byte[ 0 ];
private boolean reverb;
private String error;
private static Color toColor( int rgb12 )
{
return new Color( ( ( rgb12 >> 8 ) & 0xF ) * 17, ( ( rgb12 >> 4 ) & 0xF ) * 17, ( rgb12 & 0xF ) * 17 );
}
private static int toRgb12( Color clr )
{
return ( clr.getRed() / 17 << 8 ) | ( clr.getGreen() / 17 << 4 ) | ( clr.getBlue() / 17 );
}
private static int toRgb24( int rgb12 )
{
int r = ( ( rgb12 & 0xF00 ) >> 8 ) * 17;
int g = ( ( rgb12 & 0xF0 ) >> 4 ) * 17;
int b = ( rgb12 & 0xF ) * 17;
return ( r << 16 ) | ( g << 8 ) | b;
}
private void createDiskGadgets( int x, int y )
{
int rows = 7, cols = 32;
createTextbox( GADNUM_DIR_TEXTBOX, x, y, ( cols - 1 ) * 8, 28, "" );
createButton( GADNUM_DIR_BUTTON, x + ( cols - 1 ) * 8 + 4, y + 2, 44, 24, "Dir" );
createListbox( GADNUM_DIR_LISTBOX, x, y + 32, ( cols + 2 ) * 8, rows * 16 + 12, GADNUM_DIR_SLIDER );
createSlider( GADNUM_DIR_SLIDER, x + ( cols + 2 ) * 8 + 4, y + 32, 20, rows * 16 + 12, 1, 1 );
createButton( GADNUM_LOAD_BUTTON, x, y + rows * 16 + 48, 64, 24, "Load" );
createButton( GADNUM_SAVE_BUTTON, x + 64 + 4, y + rows * 16 + 48, 64, 24, "Save" );
}
private void createInstGadgets( int x, int y )
{
createLabel( GADNUM_INST_LABEL, x, y + 6, "Instrument", TEXT_SHADOW_SELECTED );
createTextbox( GADNUM_INST_TEXTBOX, x + 10 * 8 + 4, y, 4 * 8, 28, "00" );
createButton( GADNUM_INST_DEC_BUTTON, x + 15 * 8, y + 2, 24, 24, "<" );
createButton( GADNUM_INST_INC_BUTTON, x + 15 * 8 + 28, y + 2, 24, 24, ">" );
createLabel( GADNUM_INST_NAME_LABEL, x, y + 32 + 6, "Name", TEXT_SHADOW_SELECTED );
createTextbox( GADNUM_INST_NAME_TEXTBOX, x + 4 * 8 + 4, y + 32, 24 * 8, 28, "[001 C-2 C#D#EF#G#A#B]" );
createLabel( GADNUM_INST_REP_LABEL, x, y + 32 * 2 + 6, "Repeat", TEXT_SHADOW_SELECTED );
createTextbox( GADNUM_INST_REP_TEXTBOX, x + 6 * 8 + 4, y + 32 * 2, 8 * 8, 28, "999999" );
createLabel( GADNUM_INST_VOL_LABEL, x + 16 * 8, y + 32 * 2 + 6, "Volume", TEXT_SHADOW_SELECTED );
createTextbox( GADNUM_INST_VOL_TEXTBOX, x + 24 * 8 + 4, y + 32 * 2, 4 * 8, 28, "64" );
createLabel( GADNUM_INST_LEN_LABEL, x, y + 32 * 3 + 6, "Length", TEXT_SHADOW_SELECTED );
createTextbox( GADNUM_INST_LEN_TEXTBOX, x + 6 * 8 + 4, y + 32 * 3, 8 * 8, 28, "999999" );
createLabel( GADNUM_INST_FINE_LABEL, x + 16 * 8, y + 32 * 3 + 6, "Finetune", TEXT_SHADOW_SELECTED );
createTextbox( GADNUM_INST_FINE_TEXTBOX, x + 24 * 8 + 4, y + 32 * 3, 4 * 8, 28, "-8" );
}
private void createSequenceGadgets( int x, int y )
{
int rows = 7;
createTextbox( GADNUM_SEQ_TEXTBOX, x, y, 5 * 8, 28, "0" );
createButton( GADNUM_SEQ_INS_BUTTON, x + 5 * 8 + 4, y + 2, 3 * 8, 24, "+" );
createButton( GADNUM_SEQ_DEL_BUTTON, x + 9 * 8, y + 2, 3 * 8, 24, "-" );
createListbox( GADNUM_SEQ_LISTBOX, x, y + 32, 9 * 8, rows * 16 + 12, GADNUM_SEQ_SLIDER );
gadText[ GADNUM_SEQ_LISTBOX ] = new String[] { "000 0" };
createSlider( GADNUM_SEQ_SLIDER, x + 9 * 8 + 4, y + 32, 20, rows * 16 + 12, 1, 1 );
}
public Tracker3( int width, int height )
{
this.width = width;
this.height = height;
addKeyListener( this );
addMouseListener( this );
addMouseMotionListener( this );
createPattern( GADNUM_PATTERN, 4, 192, GADNUM_PATTERN_SLIDER );
createSlider( GADNUM_PATTERN_SLIDER, 616, 192, 20, 256, 15, 78 );
createDiskGadgets( 4, 4 );
createLabel( GADNUM_TITLE_LABEL, 306, 10, "Title", TEXT_SHADOW_SELECTED );
createTextbox( GADNUM_TITLE_TEXTBOX, 306 + 5 * 8 + 4, 4, 23 * 8, 28, "" );
createInstGadgets( 306, 36 );
createSequenceGadgets( 540, 4 );
createLabel( GADNUM_VER_LABEL, 200, 6 + 7 * 16 + 50, VERSION, TEXT_HIGHLIGHT_SELECTED );
createButton( GADNUM_PLAY_BUTTON, 540, 6 + 7 * 16 + 46, 96, 24, "Play" );
modPlay3.setPatternData( new byte[ MAX_CHANNELS * 4 * 64 * 128 ], MAX_CHANNELS );
modPlay3.setSequencer( false );
setInstrument( 1 );
listDir( getDir() );
gadRedraw[ 0 ] = true;
}
public synchronized void keyPressed( KeyEvent e )
{
try
{
switch( e.getKeyCode() )
{
case KeyEvent.VK_F1:
octave = 1;
break;
case KeyEvent.VK_F2:
octave = 2;
break;
case KeyEvent.VK_F3:
octave = 3;
break;
case KeyEvent.VK_F4:
octave = 4;
break;
case KeyEvent.VK_F5:
copy();
break;
case KeyEvent.VK_F6:
paste( 0 );
break;
case KeyEvent.VK_F7:
reverb = !reverb;
break;
case KeyEvent.VK_F8:
setNumChannels( modPlay3.getNumChannels() < MAX_CHANNELS ? MAX_CHANNELS : 4 );
break;
case KeyEvent.VK_F9:
if( e.isShiftDown() )
{
cropInstrument();
}
else
{
saveInstrument();
}
break;
case KeyEvent.VK_F10:
if( e.isShiftDown() )
{
optimize();
}
break;
default:
switch( gadType[ focus ] )
{
case GAD_TYPE_TEXTBOX:
keyTextbox( focus, e.getKeyChar(), e.getKeyCode(), e.isShiftDown() );
break;
case GAD_TYPE_LISTBOX:
keyListbox( focus, e.getKeyChar(), e.getKeyCode(), e.isShiftDown() );
trigger( -1, mapEventKey( KEY_MAP, e.getKeyCode() ) );
break;
case GAD_TYPE_PATTERN:
keyPattern( focus, e.getKeyChar(), e.getKeyCode(), e.isShiftDown() );
break;
default:
trigger( -1, mapEventKey( KEY_MAP, e.getKeyCode() ) );
break;
}
break;
}
}
catch( Exception x )
{
x.printStackTrace();
setError( x.getMessage() );
}
repaint();
}
public synchronized void keyReleased( KeyEvent e )
{
}
public synchronized void keyTyped( KeyEvent e )
{
}
public synchronized void mouseClicked( MouseEvent e )
{
}
public synchronized void mouseEntered( MouseEvent e )
{
}
public synchronized void mouseExited( MouseEvent e )
{
}
public synchronized void mousePressed( MouseEvent e )
{
clickX = e.getX();
clickY = e.getY();
int clicked = findGadget( clickX, clickY );
if( focus > 0 && focus != clicked )
{
escape( focus );
gadRedraw[ focus ] = true;
}
switch( gadType[ clicked ] )
{
case GAD_TYPE_BUTTON:
gadSelected[ clicked ] = true;
gadRedraw[ clicked ] = true;
break;
case GAD_TYPE_TEXTBOX:
clickTextbox( clicked );
break;
case GAD_TYPE_SLIDER:
clickSlider( clicked, e.isShiftDown() );
break;
case GAD_TYPE_LISTBOX:
clickListbox( clicked, e.isShiftDown() );
break;
case GAD_TYPE_PATTERN:
clickPattern( clicked, e.isShiftDown() );
break;
default:
if( clicked > 0 )
{
action( clicked, e.isShiftDown() );
}
}
focus = clicked;
repaint();
}
public synchronized void mouseReleased( MouseEvent e )
{
if( focus > 0 )
{
switch( gadType[ focus ] )
{
case GAD_TYPE_BUTTON:
if( findGadget( e.getX(), e.getY() ) == focus )
{
gadSelected[ focus ] = false;
gadRedraw[ focus ] = true;
action( focus, e.isShiftDown() );
focus = 0;
repaint();
}
break;
case GAD_TYPE_SLIDER:
action( focus, e.isShiftDown() );
focus = 0;
repaint();
break;
}
}
}
public synchronized void mouseDragged( MouseEvent e )
{
if( focus > 0 )
{
switch( gadType[ focus ] )
{
case GAD_TYPE_BUTTON:
boolean selected = findGadget( e.getX(), e.getY() ) == focus;
if( gadSelected[ focus ] != selected )
{
gadSelected[ focus ] = selected;
gadRedraw[ focus ] = true;
repaint();
}
break;
case GAD_TYPE_SLIDER:
dragSlider( focus, e.getY() );
break;
}
}
}
public synchronized void mouseMoved( MouseEvent e )
{
}
public synchronized void windowActivated( WindowEvent e )
{
}
public synchronized void windowClosed( WindowEvent e )
{
}
public synchronized void windowClosing( WindowEvent e )
{
e.getWindow().dispose();
}
public synchronized void windowDeactivated( WindowEvent e )
{
}
public synchronized void windowDeiconified( WindowEvent e )
{
}
public synchronized void windowIconified( WindowEvent e )
{
}
public synchronized void windowOpened( WindowEvent e )
{
}
public synchronized void paint( Graphics g ) {
if( charset == null ) {
charset = createImage( charsetImage( TOPAZ_8 ).getSource() );
}
if( image == null ) {
image = createImage( width, height );
}
boolean redraw = gadRedraw[ 0 ];
for( int idx = 1; idx < GAD_COUNT && !redraw; idx++ )
{
redraw = gadRedraw[ idx ];
}
if( redraw )
{
Graphics imageGraphics = image.getGraphics();
try
{
if( gadRedraw[ 0 ] )
{
imageGraphics.setColor( SELECTED );
imageGraphics.fillRect( 0, 0, width, height );
}
for( int idx = 1; idx < GAD_COUNT; idx++ )
{
if( gadRedraw[ idx ] || gadRedraw[ 0 ] )
{
switch( gadType[ idx ] )
{
case GAD_TYPE_LABEL:
drawLabel( imageGraphics, idx );
break;
case GAD_TYPE_BUTTON:
drawButton( imageGraphics, idx );
break;
case GAD_TYPE_TEXTBOX:
drawTextbox( imageGraphics, idx );
break;
case GAD_TYPE_SLIDER:
if( gadLink[ idx ] <= 0 )
{
drawSlider( imageGraphics, idx );
}
break;
case GAD_TYPE_LISTBOX:
drawListbox( imageGraphics, idx );
break;
case GAD_TYPE_PATTERN:
drawPattern( imageGraphics, idx );
break;
}
gadRedraw[ idx ] = false;
}
}
gadRedraw[ 0 ] = false;
}
finally
{
imageGraphics.dispose();
}
}
g.drawImage( image, 0, 0, null );
}
public synchronized void update( Graphics g ) {
paint( g );
}
public synchronized Dimension getPreferredSize()
{
return new Dimension( width, height );
}
private static int readIntBe( java.io.InputStream inputStream, int length ) throws IOException
{
int value = 0;
for( int idx = 0; idx < length; idx++ )
{
value = ( value << 8 ) | inputStream.read();
}
return value;
}
private static int readIntLe( java.io.InputStream inputStream, int length ) throws IOException
{
int value = 0;
for( int idx = 0; idx < length; idx++ )
{
value = value | ( inputStream.read() << idx * 8 );
}
return value;
}
private static void drawChar( long[] source, int chr, int x, int y, int bg, int fg, int[] dest, int stride )
{
int destIdx = y * stride + x;
for( int cy = 0; cy < 8; cy++ ) {
for( int cx = 0; cx < 8; cx++ ) {
int pixel = ( ( source[ chr ] >> 63 - cy * 8 - cx ) & 1 ) == 0 ? bg : fg;
dest[ destIdx + cx ] = dest[ destIdx + cx + stride ] = pixel;
}
destIdx += stride * 2;
}
}
private static Image iconImage()
{
BufferedImage image = new BufferedImage( 24, 24, BufferedImage.TYPE_INT_RGB );
int[] pixels = new int[ 24 * 24 ];
drawChar( TOPAZ_8, 'T' - 32, 4, 5, 0, toRgb24( 0x07F ), pixels, 24 );
drawChar( TOPAZ_8, '3' - 32, 12, 5, 0, toRgb24( 0xF70 ), pixels, 24 );
image.setRGB( 0, 0, 24, 24, pixels, 0, 24 );
return image;
}
private static Image charsetImage( long[] source )
{
int[] pal = new int[]
{
( toRgb12( BACKGROUND ) << 12 ) | toRgb12( SHADOW ),
( toRgb12( BACKGROUND ) << 12 ) | toRgb12( HIGHLIGHT ),
( toRgb12( SELECTED ) << 12 ) | toRgb12( SHADOW ),
( toRgb12( SELECTED ) << 12 ) | toRgb12( HIGHLIGHT ),
/* Blue Green Cyan Red Magenta Yellow White Lime */
0x00C, 0x080, 0x088, 0x800, 0x808, 0x860, 0x888, 0x680,
0x06F, 0x0F0, 0x0FF, 0xF00, 0xF0F, 0xFC0, 0xFFF, 0xCF0
};
int w = 8 * source.length;
int h = 16 * pal.length;
int[] pixels = new int[ w * h ];
for( int clr = 0; clr < pal.length; clr++ ) {
int bg = toRgb24( pal[ clr ] >> 12 );
int fg = toRgb24( pal[ clr ] & 0xFFF );
for( int chr = 0; chr < source.length; chr++ ) {
drawChar( source, chr, chr * 8, clr * 16, bg, fg, pixels, w );
}
}
BufferedImage image = new BufferedImage( w, h, BufferedImage.TYPE_INT_RGB );
image.setRGB( 0, 0, w, h, pixels, 0, w );
return image;
}
private void setError( String message )
{
error = message;
gadRedraw[ GADNUM_PATTERN ] = true;
}
private void drawText( Graphics g, int x, int y, String text, int colour )
{
for( int idx = 0, len = text.length(); idx < len; idx++ )
{
int chr = text.charAt( idx );
if( chr < 32 || ( chr > 126 && chr < 192 ) || chr > 255 )
{
chr = 32;
}
else if( chr >= 192 )
{
chr = "AAAAAAECEEEEIIIIDNOOOOO*0UUUUYPSaaaaaaeceeeeiiiidnooooo/0uuuuypy".charAt( chr - 192 );
}
g.setClip( x, y, 8, 16 );
g.drawImage( charset, x - ( chr - 32 ) * 8, y - colour * 16, null );
x += 8;
}
g.setClip( null );
}
private void drawInt( Graphics g, int x, int y, int value, int len, int colour )
{
char[] chars = new char[ len ];
while( len > 0 ) {
len = len - 1;
chars[ len ] = ( char ) ( '0' + value % 10 );
value = value / 10;
}
drawText( g, x, y, new String( chars ), colour );
}
private void raiseBox( Graphics g, int x, int y, int w, int h )
{
g.setColor( SHADOW );
g.fillRect( x + w - 2, y, 2, h );
g.setColor( HIGHLIGHT );
g.fillRect( x, y, 2, h );
g.setColor( SHADOW );
g.fillRect( x + 1, y + h - 2, w - 1, 2 );
g.setColor( HIGHLIGHT );
g.fillRect( x, y, w - 1, 2 );
}
private void lowerBox( Graphics g, int x, int y, int w, int h )
{
g.setColor( HIGHLIGHT );
g.fillRect( x + w - 2, y, 2, h );
g.setColor( SHADOW );
g.fillRect( x, y, 2, h );
g.setColor( HIGHLIGHT );
g.fillRect( x + 1, y + h - 2, w - 1, 2 );
g.setColor( SHADOW );
g.fillRect( x, y, w - 1, 2 );
}
private void bevelBox( Graphics g, int x, int y, int w, int h )
{
raiseBox( g, x, y, w, h );
lowerBox( g, x + 2, y + 2, w - 4, h - 4 );
}
private void drawLabel( Graphics g, int gadnum )
{
drawText( g, gadX[ gadnum ], gadY[ gadnum ], gadText[ gadnum ][ 0 ], gadValue[ gadnum ] );
}
private void drawButton( Graphics g, int gadnum )
{
int x = gadX[ gadnum ];
int y = gadY[ gadnum ];
int w = gadWidth[ gadnum ];
int h = gadHeight[ gadnum ];
int textColour;
if( gadSelected[ gadnum ] )
{
g.setColor( SELECTED );
g.fillRect( x, y, w, h );
lowerBox( g, x, y, w, h );
textColour = TEXT_HIGHLIGHT_SELECTED;
}
else
{
g.setColor( BACKGROUND );
g.fillRect( x, y, w, h );
raiseBox( g, x, y, w, h );
textColour = TEXT_SHADOW_BACKGROUND;
}
String text = gadText[ gadnum ][ 0 ];
drawText( g, x + ( w - text.length() * 8 ) / 2,
y + ( h - 14 ) / 2, text, textColour );
}
private void drawTextbox( Graphics g, int gadnum )
{
int x = gadX[ gadnum ];
int y = gadY[ gadnum ];
int w = gadWidth[ gadnum ];
int h = gadHeight[ gadnum ];
String text = gadText[ gadnum ][ 0 ];
int cursor = gadItem[ gadnum ];
int columns = ( w - 16 ) / 8;
int offset = focus == gadnum ? gadValue[ gadnum ] : text.length() - columns;
if( offset < 0 || offset > text.length() )
{
offset = 0;
}
if( offset + columns > text.length() )
{
columns = text.length() - offset;
}
g.setColor( BACKGROUND );
g.fillRect( x, y, w, h );
drawText( g, x + 8, y + 6, text.substring( offset, offset + columns ), TEXT_SHADOW_BACKGROUND );
if( focus == gadnum && cursor >= offset && cursor <= offset + columns )
{
String chr = cursor < offset + columns ? String.valueOf( text.charAt( cursor ) ) : " ";
drawText( g, x + ( cursor - offset + 1 ) * 8, y + 6, chr, TEXT_SHADOW_SELECTED );
}
bevelBox( g, x, y, w, h );
}
private void clickTextbox( int gadnum )
{
int columns = ( gadWidth[ gadnum ] - 16 ) / 8;
String text = gadText[ gadnum ][ 0 ];
int offset = focus == gadnum ? gadValue[ gadnum ] : text.length() - columns;
if( offset < 0 || offset > text.length() )
{
offset = 0;
}
if( offset + columns > text.length() )
{
columns = text.length() - offset;
}
int cursor = offset + ( clickX - gadX[ gadnum ] ) / 8 - 1;
if( cursor > text.length() )
{
cursor = text.length();
}
if( cursor < 0 )
{
cursor = 0;
}
gadValue[ gadnum ] = offset;
gadItem[ gadnum ] = cursor;
gadRedraw[ gadnum ] = true;
}
private void keyTextbox( int gadnum, char chr, int key, boolean shift )
{
int columns = ( gadWidth[ gadnum ] - 16 ) / 8;
String text = gadText[ gadnum ][ 0 ];
int offset = gadValue[ gadnum ];
if( offset < 0 || offset > text.length() )
{
offset = 0;
}
int cursor = gadItem[ gadnum ];
if( cursor > text.length() )
{
cursor = text.length();
}
switch( key )
{
case KEY_BACKSPACE:
if( cursor > 0 )
{
text = text.substring( 0, cursor - 1 ) + text.substring( cursor );
cursor--;
if( cursor < offset )
{
offset = cursor;
}
}
break;
case KEY_DELETE:
if( cursor < text.length() )
{
text = text.substring( 0, cursor ) + text.substring( cursor + 1 );
}
break;
case KEY_END:
cursor = text.length();
if( cursor - offset >= columns )
{
offset = cursor - columns;
}
break;
case KEY_ESCAPE:
escape( gadnum );
text = gadText[ gadnum ][ 0 ];
focus = 0;
break;
case KEY_HOME:
offset = cursor = 0;
break;
case KEY_LEFT:
if( cursor > 0 )
{
cursor--;
if( cursor < offset )
{
offset = cursor;
}
}
break;
case KEY_RIGHT:
if( cursor < text.length() )
{
cursor++;
if( cursor - offset >= columns )
{
offset = cursor - columns + 1;
}
}
break;
default:
if( chr == 10 )
{
action( gadnum, shift );
text = gadText[ gadnum ][ 0 ];
focus = 0;
}
else if( chr >= 32 && chr < 127 )
{
text = text.substring( 0, cursor )
+ String.valueOf( chr ) + text.substring( cursor );
cursor++;
if( cursor - offset > columns )
{
offset = cursor - columns;
}
}
break;
}
gadText[ gadnum ][ 0 ] = text;
gadValue[ gadnum ] = offset;
gadItem[ gadnum ] = cursor;
gadRedraw[ gadnum ] = true;
}
private void drawSlider( Graphics g, int gadnum )
{
int x = gadX[ gadnum ];
int y = gadY[ gadnum ];
int w = gadWidth[ gadnum ];
int h = gadHeight[ gadnum ];
int s = ( h - 12 ) * gadRange[ gadnum ] / gadMax[ gadnum ] + 8;
int d = ( h - 12 ) * gadValue[ gadnum ] / gadMax[ gadnum ];
g.setColor( SELECTED );
g.fillRect( x, y, w, h );
lowerBox( g, x, y, w, h );
g.setColor( BACKGROUND );
g.fillRect( x + 2, y + d + 2, w - 4, s );
raiseBox( g, x + 2, y + d + 2, w - 4, s );
}
private void clickSlider( int gadnum, boolean shift )
{
int ss = ( gadHeight[ gadnum ] - 12 ) * gadRange[ gadnum ] / gadMax[ gadnum ] + 8;
int so = ( gadHeight[ gadnum ] - 12 ) * gadValue[ gadnum ] / gadMax[ gadnum ];
int sy = gadY[ gadnum ] + so + 2;
if( clickY < sy )
{
int sp = gadValue[ gadnum ] - gadRange[ gadnum ];
if( sp < 0 )
{
sp = 0;
}
gadValue[ gadnum ] = sp;
gadRedraw[ gadLink[ gadnum ] > 0 ? gadLink[ gadnum ] : gadnum ] = true;
action( gadnum, shift );
}
else if( clickY > ( sy + ss ) )
{
int sp = gadValue[ gadnum ] + gadRange[ gadnum ];
if( sp > ( gadMax[ gadnum ] - gadRange[ gadnum ] ) )
{
sp = gadMax[ gadnum ] - gadRange[ gadnum ];
}
gadValue[ gadnum ] = sp;
gadRedraw[ gadLink[ gadnum ] > 0 ? gadLink[ gadnum ] : gadnum ] = true;
action( gadnum, shift );
}
}
private void dragSlider( int gadnum, int y )
{
int ss = ( gadHeight[ gadnum ] - 12 ) * gadRange[ gadnum ] / gadMax[ gadnum ] + 8;
int so = ( gadHeight[ gadnum ] - 12 ) * gadValue[ gadnum ] / gadMax[ gadnum ];
int sg = gadHeight[ gadnum ] - 4 - ss;
int sy = gadY[ gadnum ] + so + 2;
if( clickY > sy && clickY < ( sy + ss ) )