~rabbits/nasu

nasu/src/main.tal -rw-r--r-- 33.8 KiB
606c4b2bDevine Lu Linvega Minor optimization 21 days ago
                                                                                
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
( 
	app/nasu : spritesheet editor

	arrows - move selection
	enter     - insert tile
	backspace - delete tile
	left-click  - add pixel, select tile
	right-click - remove pixel, clone tile
	1 2 3 - select color
	q w - select tool
	space - toggle zoom
	ctrl+n - clear
	ctrl+r - rename
	ctrl+o - load
	ctrl+s - save
	ctrl+c - copy
	ctrl+v - paste
 )

%+  { ADD }  %-  { SUB }
%<  { LTH }  %>  { GTH }  %=  { EQU }  %!  { NEQ }
%++ { ADD2 } %-- { SUB2 }
%<< { LTH2 } %>> { GTH2 } %== { EQU2 } %!! { NEQ2 }
%!~ { NEQk NIP }

%DEBUG  { ;print-hex JSR2 #0a .Console/write DEO }
%DEBUG2 { SWP ;print-hex JSR2 ;print-hex JSR2 #0a .Console/write DEO }

%2*  { #10 SFT } %2/  { #01 SFT }
%4*  { #20 SFT } %4/  { #02 SFT }
%8*  { #30 SFT } %8/  { #03 SFT }
%20* { #50 SFT } 
%40* { #60 SFT }
%80* { #70 SFT }

%2**  { #10 SFT2 } %2//  { #01 SFT2 }
%4//  { #02 SFT2 }
%8**  { #30 SFT2 } %8//  { #03 SFT2 }
%10** { #40 SFT2 } 
%20** { #50 SFT2 } 
%100** { #80 SFT2 }
%200** { #90 SFT2 }

%2INC2 { INC2 INC2 }
%2MOD { #01 AND }
%4MOD { #03 AND }
%8MOD { #07 AND }
%10MOD { #0f AND }
%20MOD { #1f AND }
%8MOD2 { #0007 AND2 }
%100MOD2 { #00ff AND2 }

%TOS  { #00 SWP }
%SWP2? { #01 JCN SWP2 }

%RTN { JMP2r }
%ROL { DUP #07 SFT SWP #10 SFT + }
%ROR { DUP #70 SFT SWP #01 SFT + }
%SFL { #40 SFT SFT }
%GTS2 { #8000 ++ SWP2 #8000 ++ << }
%STEP8 { #33 SFT2 }
%TOGGLE { LDZk #00 = SWP STZ }
%GET-ITER { OVR2 NIP OVR SWP }
%BRK? { #01 JCN BRK }
%SWP? { #01 JCN SWP }

%HOR    { #20 }    %VER    { #10 }
%WIDTH  { #0100 } 
%LENGTH { #2000 }

%DATA-CLIP { #6000 }
%BANK      { #2000 }
%BANK-TEMP { #4000 }

%GET-HEX-ADDR  { TOS 8** ;font-hex ++ .Screen/addr DEO2 }
%GET-COLOR     { .settings/color LDZ STHk 2/ + DEI #01 STHr #01 AND - 4* SFT #0f AND }
%GET-POSITION  { .settings/focus LDZ2 BANK -- DUP2 2// 100MOD2 SWP2 WIDTH 2** DIV2 8** }
%ADD-POSITIONS { ROT2 ++ ROT2 ROT2 ++ SWP2 }
%RELEASE-MOUSE { #00 .Mouse/state DEO }
%RESET-SELECTION { .selection/x1 LDZ2 .selection/x2 STZ2 }
%GET-ADDR { TOS 20** ROT TOS ++ 10** BANK ++ } ( x y -- addr* )

%SET-STATE { #01 .state/changed STZ ;draw-state JSR2 }
%RESET-STATE { #00 .state/changed STZ ;draw-state JSR2 }

%SIZE-TO-RECT {
	STH2 STH2 OVR2 STH2r ++ OVR2 STH2r ++
} ( x y w h -- x1 y1 x2 y2 )

%SET-RECT {
	DUP2 ROT2 SWP2 #0006 ++ STA2
	DUP2 ROT2 SWP2 #0004 ++ STA2
	DUP2 ROT2 SWP2 #0002 ++ STA2
	DUP2 ROT2 SWP2 STA2
	POP2
} ( x1 y1 x2 y2 addr -- )

( devices )

|00 @System     [ &vector $2 &pad     $6 &r      $2 &g     $2 &b     $2 ]
|10 @Console    [ &vector $2 &read    $1 &pad    $5 &write $1 ]
|20 @Screen     [ &vector $2 &width   $2 &height $2 &pad   $2 &x     $2 &y $2 &addr $2 &pixel $1 &sprite $1 ]
|80 @Controller [ &vector $2 &button  $1 &key    $1 ]
|90 @Mouse      [ &vector $2 &x       $2 &y      $2 &state $1 &chord $1 ]
|a0 @File       [ &vector $2 &success $2 &offset $2 &pad   $2 &name  $2 &length $2 &load $2 &save $2 ]

( variables )

|0000

@state
	&timer $1 &blink $1 &changed $1
@settings
	&depth $1 &color $1 &blending $1 &ratio $1 &tool $1 &focus $2 &zoom $1
@cursor
	&x $2 &y $2 &last $1
@selection 
	&x1 $1 &y1 $1 
	&x2 $1 &y2 $1 
@path 
	&length $1 &name $20
@frame 
	&x1 $2 &y1 $2 
	&x2 $2 &y2 $2 
	&width $2 &height $2

( interface ) 
@toolview  [ &x1 $2 &y1 $2 &x2 $2 &y2 $2 ]
@colorview [ &x1 $2 &y1 $2 &x2 $2 &y2 $2 ]
@blendview [ &x1 $2 &y1 $2 &x2 $2 &y2 $2 ]
@dataview  [ &x1 $2 &y1 $2 &x2 $2 &y2 $2 ]
@preview   [ &x1 $2 &y1 $2 &x2 $2 &y2 $2 ]
@zoomview  [ &x1 $2 &y1 $2 &x2 $2 &y2 $2 ]
@tileview  
	&x1 $2 &y1 $2 
	&x2 $2 &y2 $2 
	&width $2 &height $2
@tileview-frame  [ &x1 $2 &y1 $2 &x2 $2 &y2 $2 ]
@zoomview-frame  [ &x1 $2 &y1 $2 &x2 $2 &y2 $2 ]

( program )

|0100 ( -> )

	( theme ) 
	#0a6f .System/r DEO2 
	#05cf .System/g DEO2 
	#0caf .System/b DEO2

	( size )
	#0130 .Screen/width DEO2
	#00e0 .Screen/height DEO2

	( vectors )
	;on-console  .Console/vector DEO2
	;on-button   .Controller/vector DEO2
	;on-mouse    .Mouse/vector DEO2
	;on-transfer .File/vector DEO2
	;on-frame    .Screen/vector DEO2

	( set defaults )
	#0100 .frame/width STZ2
	#0080 #0040 ++ .frame/height STZ2
	#01 .settings/depth STZ
	#01 .settings/color STZ
	#01 .settings/blending STZ
	#32 .settings/ratio STZ
	#01 .settings/tool STZ
	BANK .settings/focus STZ2

	#0100 .tileview/width STZ2
	#0080 .tileview/height STZ2

	.Screen/width DEI2 #01 SFT2 .frame/width LDZ2 #01 SFT2 --
	.Screen/height DEI2 #01 SFT2 .frame/height LDZ2 #01 SFT2 --
	.frame/width LDZ2 .frame/height LDZ2
		SIZE-TO-RECT ;frame SET-RECT

	.frame/x1 LDZ2 .frame/y1 LDZ2 #0030 ++ #0100 #0080 SIZE-TO-RECT ;tileview SET-RECT
	.tileview/x1 LDZ2 .tileview/y2 LDZ2 #0008 ++ #0100 #0008 SIZE-TO-RECT ;toolview SET-RECT

	.frame/x1 LDZ2 .frame/y1 LDZ2 #0020 #0020 SIZE-TO-RECT ;blendview SET-RECT
	.frame/x1 LDZ2 #0030 ++ .frame/y1 LDZ2 #0020 #0020 SIZE-TO-RECT ;preview SET-RECT
	.frame/x1 LDZ2 #0060 ++ .frame/y1 LDZ2 #0020 #0020 SIZE-TO-RECT ;colorview SET-RECT
	.frame/x1 LDZ2 #0090 ++ .frame/y1 LDZ2 #0020 #0028 SIZE-TO-RECT ;zoomview SET-RECT
	.frame/x1 LDZ2 #00c0 ++ .frame/y1 LDZ2 #0048 #0020 SIZE-TO-RECT ;dataview SET-RECT

	.tileview/x1 LDZ2 #0002 -- .tileview-frame/x1 STZ2
	.tileview/y1 LDZ2 #0002 -- .tileview-frame/y1 STZ2
	.tileview/x2 LDZ2 INC2 .tileview-frame/x2 STZ2
	.tileview/y2 LDZ2 INC2 .tileview-frame/y2 STZ2

	.zoomview/x1 LDZ2 #0002 -- .zoomview-frame/x1 STZ2
	.zoomview/y1 LDZ2 #0002 -- .zoomview-frame/y1 STZ2
	.zoomview/x2 LDZ2 INC2 .zoomview-frame/x2 STZ2
	.zoomview/y2 LDZ2 #0007 -- .zoomview-frame/y2 STZ2

	;untitled-txt ;new-file JSR2
	;load-theme JSR2

BRK

@on-console ( -- )

	#00 ;draw-filepath JSR2
	.Console/read DEI DUP 
	#0a = ,&counter LDR #20 = #0000 == ,&no-end JCN
		( set length ) ,&counter LDR .path/length STZ
		( null char ) #00 .path ,&counter LDR INC + STZ
		( reset counter ) #00 ,&counter STR 
		( load ) ;load-file JSR2
		POP BRK
		&no-end
	,&counter LDR 
		( length ) DUP .path/length STZ
		INC 
			( inc counter ) DUP ,&counter STR 
			( write ) .path + STZ
	#01 ;draw-filepath JSR2

BRK
	&counter $1

@on-frame ( -> )
	( inactive )
BRK

@on-frame-trap ( -> )

	.state/timer LDZ INC 
	DUP 8MOD ,&no-blink JCN
		.state/blink LDZ #00 =
			DUP 8* INC ;draw-filepath JSR2
			.state/blink STZ
		&no-blink
	.state/timer STZ 

BRK

@on-transfer ( -> )
	
	;redraw JSR2
	
BRK

@on-button ( -> )
	
	.Controller/key DEI .Controller/button DEI ADD BRK?

	.Controller/button DEI #01 AND ;on-control JCN2

	.Controller/key DEI 
		DUP #31 ! ,&no-1 JCN
			( color ) #00 .settings/color STZ ;draw-toolview JSR2 &no-1
		DUP #32 ! ,&no-2 JCN
			( color ) #01 .settings/color STZ ;draw-toolview JSR2 &no-2
		DUP #33 ! ,&no-3 JCN
			( color ) #02 .settings/color STZ ;draw-toolview JSR2 &no-3
		DUP #34 ! ,&no-4 JCN
			( color ) #03 .settings/color STZ ;draw-toolview JSR2 &no-4
		DUP #08 ! ,&no-delete JCN
			( delete ) ;op-erase ;run-selection JSR2 SET-STATE ;redraw JSR2 &no-delete
		DUP #20 ! ,&no-zoom JCN
			( zoom ) .settings/zoom TOGGLE ;redraw JSR2 &no-zoom
		DUP #71 ! ,&no-tool0 JCN
			( tool ) #00 .settings/tool STZ ;redraw JSR2 &no-tool0
		DUP #77 ! ,&no-tool1 JCN
			( tool ) #01 .settings/tool STZ ;redraw JSR2 &no-tool1
		DUP #1b ! ,&no-escape JCN
			( escape ) RESET-SELECTION ;redraw JSR2 &no-escape
	POP

	( release ) #00 .Controller/key DEO

	.Controller/button DEI
	( default )
	[ #10 ] !~ ,&no-up JCN
		#00 #ff ;mod-selection JSR2 POP BRK &no-up
	[ #20 ] !~ ,&no-down JCN
		#00 #01 ;mod-selection JSR2 POP BRK &no-down
	[ #40 ] !~ ,&no-left JCN
		#ff #00 ;mod-selection JSR2 POP BRK &no-left
	[ #80 ] !~ ,&no-right JCN
		#01 #00  ;mod-selection JSR2 POP BRK &no-right
	( shift )
	[ #14 ] !~ ,&no-shift-up JCN
		#00 #ff ;scale-selection JSR2 POP BRK &no-shift-up
	[ #24 ] !~ ,&no-shift-down JCN
		#00 #01 ;scale-selection JSR2 POP BRK &no-shift-down
	[ #44 ] !~ ,&no-shift-left JCN
		#ff #00 ;scale-selection JSR2 POP BRK &no-shift-left
	[ #84 ] !~ ,&no-shift-right JCN
		#01 #00 ;scale-selection JSR2 POP BRK &no-shift-right
	POP

	;redraw JSR2 

BRK

@on-control ( -> )

	.Controller/key DEI
	DUP [ LIT 'a ] ! ,&no-expand JCN
		#00 .selection/x1 STZ
		#00 .selection/y1 STZ
		HOR #01 - .selection/x2 STZ
		VER #01 - .selection/y2 STZ
		;clamp-selection JSR2
		;redraw JSR2 POP BRK 
		&no-expand
	DUP [ LIT 'c ] ! ,&no-copy JCN
		;copy-snarf JSR2 
		;redraw JSR2 POP BRK 
		&no-copy
	DUP [ LIT 'v ] ! ,&no-paste JCN
		;paste-snarf JSR2 RESET-SELECTION
		;redraw JSR2 POP BRK 
		&no-paste
	DUP [ LIT 'x ] ! ,&no-cut JCN
		;copy-snarf JSR2 ;op-erase ;run-selection JSR2 RESET-SELECTION
		;redraw JSR2 POP BRK 
		&no-cut
	DUP [ LIT 'r ] ! ,&no-name JCN
		#00 ;draw-filepath JSR2 
		;on-frame-trap .Screen/vector DEO2
		;on-button-trap .Controller/vector DEO2
		POP BRK
		&no-name
	DUP [ LIT 'R ] ! ,&no-rename JCN
		#00 ;draw-filepath JSR2 
		#00 .Controller/key DEO
		#00 .path/length STZ
		#0000 .path/name STZ2
		;on-frame-trap .Screen/vector DEO2
		;on-button-trap .Controller/vector DEO2
		POP BRK
		&no-rename
	DUP [ LIT 'n ] ! ,&no-new JCN
		#25 ;draw-filepath JSR2
		;untitled-txt ;new-file JSR2
		;redraw JSR2 POP BRK &no-new
	DUP [ LIT 'o ] ! ,&no-open JCN
		;load-file JSR2
		POP BRK &no-open
	DUP [ LIT 's ] ! ,&no-save JCN
		;save-file JSR2 
		;save-theme JSR2
		;draw-state JSR2 POP BRK  &no-save
	POP 

BRK

@on-button-trap ( -> )

	.Controller/key DEI
	DUP ,&continue JCN
		POP BRK
		&continue
	DUP ( backspace ) #08 ! ,&no-edit-backspace JCN
		( clamp ) .path/length LDZ #00 = ,&edit-end JCN
		#00 ;draw-filepath JSR2
		.path/length LDZ #01 - STHk .path/length STZ
		#00 .path/name STHr + STZ
		#01 ;draw-filepath JSR2 POP BRK
		&no-edit-backspace
	DUP ( special ) #20 > ,&no-edit-enter JCN
		;on-frame .Screen/vector DEO2
		;on-button .Controller/vector DEO2
		#00 .state/blink STZ 
		#01 ;draw-filepath JSR2 POP BRK
		&no-edit-enter
	( clamp ) .path/length LDZ #1f = ,&edit-end JCN
	DUP .path/name .path/length LDZ STHk + STZ
	STHr INC STHk .path/length STZ
	#00 .path/name STHr + STZ
	#01 ;draw-filepath JSR2
	&edit-end
	POP

BRK

@on-mouse ( -> )
	
	;draw-cursor JSR2

	.Mouse/x DEI2 .Mouse/y DEI2 .tileview ;within-rect JSR2
		;on-touch-tileview JCN2

	.Mouse/x DEI2 .Mouse/y DEI2 .toolview ;within-rect JSR2
		;on-touch-toolview JCN2
	.Mouse/x DEI2 .Mouse/y DEI2 .blendview ;within-rect JSR2
		;on-touch-blendview JCN2
	.Mouse/x DEI2 .Mouse/y DEI2 .colorview ;within-rect JSR2
		;on-touch-colorview JCN2
	.Mouse/x DEI2 .Mouse/y DEI2 .preview ;within-rect JSR2
		;on-touch-preview JCN2
	.Mouse/x DEI2 .Mouse/y DEI2 .zoomview ;within-rect JSR2
		;on-touch-zoomview JCN2

	( release-record ) #0000 .Mouse/state DEO .cursor/last STZ

BRK

@on-touch-tileview ( -> )
	
	.settings/zoom LDZ #01 = ;&zoomed JCN2

	.settings/tool LDZ #01 = ;&select JCN2

	( skip ) .Mouse/state DEI #00 ! BRK?

	.Mouse/x DEI2 .tileview/x1 LDZ2 -- 
	.Mouse/y DEI2 .tileview/y1 LDZ2 -- 

	.settings/tool LDZ .Mouse/state DEI
	DUP2 #0001 !! ,&no-brush-mouse1 JCN ( brush default )
		POP2
		.settings/color LDZ ;put-pixel JSR2
		SET-STATE
		;redraw JSR2
		BRK &no-brush-mouse1
	DUP2 #0010 !! ,&no-brush-mouse2 JCN ( brush erase )
		POP2
		#00 ;put-pixel JSR2
		SET-STATE
		;redraw JSR2
		BRK &no-brush-mouse2
	DUP2 #0101 !! ,&no-select-mouse1 JCN ( select default )
		POP2
		;get-tile-addr JSR2 .settings/focus STZ2
		;redraw JSR2
		BRK &no-select-mouse1
	DUP2 #0110 !! ,&no-select-mouse2 JCN ( select copy )
		POP2
		;get-tile-addr JSR2 .settings/focus LDZ2 SWP2 ;copy-tile JSR2
		SET-STATE
		;redraw JSR2
		BRK &no-select-mouse2
	POP2

	POP2 POP2

	BRK

	&select ( -> )

		.Mouse/state DEI DUP .cursor/last LDZ 
			DUP2 #0000 == ,&end JCN
			DUP2 #0100 !! ,&no-down JCN
				.Mouse/x DEI2 .tileview/x1 LDZ2 -- 8// NIP 
					DUP .selection/x1 STZ .selection/x2 STZ
				.Mouse/y DEI2 .tileview/y1 LDZ2 -- 8// NIP 
					DUP .selection/y1 STZ .selection/y2 STZ
				;clamp-selection JSR2 
				.selection LDZ2 GET-ADDR .settings/focus STZ2
				;redraw JSR2
				,&end JMP 
				&no-down
			( release )
			.Mouse/x DEI2 .tileview/x1 LDZ2 -- 8// NIP .selection/x2 STZ
			.Mouse/y DEI2 .tileview/y1 LDZ2 -- 8// NIP .selection/y2 STZ
			;clamp-selection JSR2 ;redraw JSR2
		&end
		POP2
		.cursor/last STZ

	BRK

	&zoomed ( -> )

		( skip ) .Mouse/state DEI #00 ! BRK?

		.Mouse/x DEI2 .tileview/x1 LDZ2 -- 
		.Mouse/y DEI2 .tileview/y1 LDZ2 -- 
		8// SWP2 8// SWP2
		GET-POSITION ADD-POSITIONS
		#00 .Mouse/state DEI #10 = ,&do-erase-zoom JCN 
			POP .settings/color LDZ 
			&do-erase-zoom 
		;put-pixel JSR2
		SET-STATE
		;redraw JSR2

BRK

@on-touch-toolview ( -> )
	
	( skip ) .Mouse/state DEI #00 ! BRK?
	
	.Mouse/x DEI2 .toolview/x1 LDZ2 -- 8// NIP
	
	DUP HOR SWP - #01 ! ,&no-save JCN
		;save-file JSR2
		;draw-state JSR2
		&no-save
	DUP HOR SWP - #03 ! ,&no-load JCN
		;load-file JSR2
		&no-load
	DUP HOR SWP - #04 ! ,&no-name JCN
		#00 ;draw-filepath JSR2
		;untitled-txt ;new-file JSR2
		;redraw JSR2
		&no-name
	DUP #04 ! ,&no-brush JCN
		#00 .settings/tool STZ
		;draw-toolview JSR2 
		POP RELEASE-MOUSE BRK
		&no-brush
	DUP #05 ! ,&no-selector JCN
		#01 .settings/tool STZ
		;draw-toolview JSR2 
		POP RELEASE-MOUSE BRK
		&no-selector
	DUP #06 ! ,&no-zoom JCN
		.settings/zoom TOGGLE
		;draw-tileview JSR2
		;draw-toolview JSR2
		&no-zoom
	DUP #07 > OVR HOR SWP - #05 > #0101 !! ,&no-rename JCN
		#00 ;draw-filepath JSR2 
		;on-frame-trap .Screen/vector DEO2
		;on-button-trap .Controller/vector DEO2
		BRK
		&no-rename
	INCk .settings/color LDZ ! ,&no-eraser JCN
		#00 .settings/color STZ
		;draw-colorview JSR2
		;draw-toolview JSR2 
		POP RELEASE-MOUSE BRK
		&no-eraser
	DUP #02 > ,&no-color JCN
		INCk .settings/color STZ
		;draw-colorview JSR2
		;draw-toolview JSR2 
		POP RELEASE-MOUSE BRK
		&no-color
	POP

	RELEASE-MOUSE

BRK

@on-touch-zoomview ( -> )

	( skip ) .Mouse/state DEI #00 ! BRK?
	
	.Mouse/y DEI2 .zoomview/y1 LDZ2 -- 8// NIP #04 ! ;&no-mod JCN2
		.Mouse/x DEI2 .zoomview/x1 LDZ2 -- 8// NIP 
		DUP #00 = .Mouse/state DEI #01 = #0101 !! ,&no-shiftup JCN
			;op-shift-tile-up ;run-selection JSR2
			SET-STATE &no-shiftup
		DUP #00 = .Mouse/state DEI #10 = #0101 !! ,&no-shiftdown JCN
			;op-shift-tile-down ;run-selection JSR2
			SET-STATE &no-shiftdown
		DUP #01 = .Mouse/state DEI #01 = #0101 !! ,&no-shiftright JCN
			;op-shift-tile-right ;run-selection JSR2
			SET-STATE &no-shiftright
		DUP #01 = .Mouse/state DEI #10 = #0101 !! ,&no-shiftleft JCN
			;op-shift-tile-left ;run-selection JSR2
			SET-STATE &no-shiftleft
		POP
		RELEASE-MOUSE
		,&finish JMP
	&no-mod

	( paint )
	.Mouse/x DEI2 .zoomview/x1 LDZ2 -- 4//
	.Mouse/y DEI2 .zoomview/y1 LDZ2 -- 4// 
	.settings/focus LDZ2 ++
	( detect erase )
	#00 .Mouse/state DEI #10 = ,&do-erase JCN 
		POP .settings/color LDZ &do-erase 
	STH
	OVR2 OVR2
		STHkr #00 ;toggle-pixel JSR2
	#0008 ++ 
		STHr #01 ;toggle-pixel JSR2
	SET-STATE

	&finish

	;redraw JSR2

BRK

@on-touch-blendview ( -> )

	( skip ) .Mouse/state DEI #00 ! BRK?

	.Mouse/x DEI2 .blendview/x1 LDZ2 -- 8// NIP
	.Mouse/y DEI2 .blendview/y1 LDZ2 -- 8// NIP 4* +
	.settings/blending STZ

	;redraw JSR2

	RELEASE-MOUSE

BRK

@on-touch-colorview ( -> )

	( skip ) .Mouse/state DEI #00 ! BRK?
	
	.Mouse/y DEI2 .colorview/y1 LDZ2 -- 8// NIP

	DUP #00 ! ,&no-r JCN
		.System/r STHk ,&set-color JSR 
		STHr [ .settings/color LDZ 2/ + ] DEO
		&no-r
	DUP #01 ! ,&no-g JCN
		.System/g STHk ,&set-color JSR 
		STHr [ .settings/color LDZ 2/ + ] DEO
		&no-g
	DUP #02 ! ,&no-b JCN
		.System/b STHk ,&set-color JSR 
		STHr [ .settings/color LDZ 2/ + ] DEO
		&no-b
	POP

	;redraw JSR2 

	BRK

	&set-color
		.settings/color LDZ STHk 2/ + DEI 
		STHr 2MOD STHk 
		#0f SWP [ 40* SFT ] AND 
		STHr #00 = 
		.Mouse/x DEI2 .colorview/x1 LDZ2 -- 2// NIP 
		( clamp to #0f ) DUP #10 = - 
		SWP [ 40* SFT ] +
	RTN

BRK

@on-touch-preview ( -> )

	( skip ) .Mouse/state DEI #00 ! BRK?

	( y ) .Mouse/y DEI2 .preview/y1 LDZ2 -- 8// NIP
	( x ) .Mouse/x DEI2 .preview/x1 LDZ2 -- 8// NIP

	#40 SFT + #11 +
	.settings/ratio STZ

	;redraw JSR2

	RELEASE-MOUSE

BRK

( operations )

@mod-selection ( x y -- )

	DUP
	.selection/y2 LDZ + 10MOD .selection/y2 STZ
	.selection/y1 LDZ + 10MOD .selection/y1 STZ
	DUP
	.selection/x2 LDZ + 20MOD .selection/x2 STZ
	.selection/x1 LDZ + 20MOD .selection/x1 STZ
	( clamp )
	.selection/x2 LDZ .selection/x1 LDZ GTH ,&no-x JCN
		.selection/x1 LDZ .selection/x2 STZ
		&no-x
	.selection/y2 LDZ .selection/y1 LDZ GTH ,&no-y JCN
		.selection/y1 LDZ .selection/y2 STZ
		&no-y
	( focus )
	.selection LDZ2 GET-ADDR .settings/focus STZ2
	;redraw JSR2

RTN

@scale-selection ( x y -- )

	.selection/y2 LDZ + 10MOD .selection/y2 STZ
	.selection/x2 LDZ + 20MOD .selection/x2 STZ

	( clamp )
	.selection/x2 LDZ .selection/x1 LDZ GTH ,&no-x JCN
		.selection/x1 LDZ .selection/x2 STZ
		&no-x
	.selection/y2 LDZ .selection/y1 LDZ GTH ,&no-y JCN
		.selection/y1 LDZ .selection/y2 STZ
		&no-y
	( focus )
	.selection LDZ2 GET-ADDR .settings/focus STZ2
	;redraw JSR2

RTN

@run-selection ( op* -- )
	
	STH2
	.selection/y2 LDZ INC .selection/y1 LDZ
	&ver
		.selection/x2 LDZ INC .selection/x1 LDZ
		&hor
			( write char ) GET-ITER GET-ADDR STH2kr JSR2
			INC GTHk ,&hor JCN
		POP2
		INC GTHk ,&ver JCN
	POP2
	POP2r

RTN

@op-erase ( addr* -- )
	
	STH2
	#10 #00
	&loop
		( get )
		DUP TOS STH2kr ++ #00 ROT ROT STA
		INC GTHk ,&loop JCN
	POP2
	POP2r

RTN

@clamp-selection ( -- )
	
	.selection/x1 LDZ HOR #01 - STHk < ,&ok-limitx1 JCN
		STHkr .selection/x1 STZ &ok-limitx1 POPr
	.selection/y1 LDZ VER #01 - STHk < ,&ok-limity1 JCN
		STHkr .selection/y1 STZ &ok-limity1 POPr
	.selection/x2 LDZ HOR #01 - STHk < ,&ok-limitx2 JCN
		STHkr .selection/x2 STZ &ok-limitx2 POPr
	.selection/y2 LDZ VER #01 - STHk < ,&ok-limity2 JCN
		STHkr .selection/y2 STZ &ok-limity2 POPr
	.selection/x2 LDZ .selection/x1 LDZ STHk > ,&ok-flipx JCN
		STHkr .selection/x2 STZ &ok-flipx POPr
	.selection/y2 LDZ .selection/y1 LDZ STHk > ,&ok-flipy JCN
		STHkr .selection/y2 STZ &ok-flipy POPr

RTN

@op-shift-tile-up ( addr* -- )

	DUP2
	DUP2 #0007 ++ SWP2 LDAk STH
	&ch1
		INC2k LDA STH
		DUP2 STHr ROT ROT STA
		INC2 GTH2k ,&ch1 JCN
	POP2
	( cap ) STHr ROT ROT STA
	#0008 ++ DUP2 #0007 ++ SWP2 LDAk STH
	&ch2
		INC2k LDA STH
		DUP2 STHr ROT ROT STA
		INC2 GTH2k ,&ch2 JCN
	POP2
	( cap ) STHr ROT ROT STA
	
RTN

@op-shift-tile-down ( addr* -- )
		
	DUP2
	DUP2 #0007 ++ LDAk STH
	&ch1
		DUP2 #0001 -- LDA STH
		DUP2 STHr ROT ROT STA
		#0001 -- LTH2k ,&ch1 JCN
	POP2
	( cap ) STHr ROT ROT STA
	#0008 ++ DUP2 #0007 ++ LDAk STH
	&ch2
		DUP2 #0001 -- LDA STH
		DUP2 STHr ROT ROT STA
		#0001 -- LTH2k ,&ch2 JCN
	POP2
	( cap ) STHr ROT ROT STA

RTN

@op-shift-tile-left ( addr* -- )
	
	STH2
	#08 #00
	&loop
		( ch1 )
		DUP TOS STH2kr ++ DUP2
		LDA [ ROL ] ROT ROT STA
		( ch2 )
		DUP TOS STH2kr #0008 ++ ++ DUP2
		LDA [ ROL ] ROT ROT STA
		INC GTHk ,&loop JCN
	POP2
	POP2r

RTN

@op-shift-tile-right ( addr* -- )
	
	STH2
	#08 #00
	&loop
		( ch1 )
		DUP TOS STH2kr ++ DUP2
		LDA [ ROR ] ROT ROT STA
		( ch2 )
		DUP TOS STH2kr #0008 ++ ++ DUP2
		LDA [ ROR ] ROT ROT STA
		INC GTHk ,&loop JCN
	POP2
	POP2r

RTN

@clear ( addr* length* -- )
	
	OVR2 ++ SWP2
	&loop
		DUP2 #00 ROT ROT STA
		INC2 NEQ2k ,&loop JCN
	POP2 POP2

RTN

@redraw ( -- )
	
	;draw-toolview JSR2 
	;draw-blendview JSR2
	;draw-colorview JSR2
	;draw-dataview JSR2
	;draw-preview JSR2
	;draw-tileview JSR2
	;draw-zoomview JSR2

RTN

@is-selected ( x y -- flag )

	OVR .selection/x1 LDZ < ,&end JCN
	OVR .selection/x2 LDZ > ,&end JCN
	DUP .selection/y1 LDZ < ,&end JCN
	DUP .selection/y2 LDZ > ,&end JCN
		POP2 #01 RTN 
		&end
	POP2 #00

RTN

@draw-tileview ( -- )

	.settings/zoom LDZ ;draw-tileview-zoom JCN2

	VER #00
	&ver
		DUP TOS 8** .tileview/y1 LDZ2 ++ .Screen/y DEO2
		HOR #00
		&hor
			DUP TOS 8** .tileview/x1 LDZ2 ++ .Screen/x DEO2
			GET-ITER
				DUP2 GET-ADDR .Screen/addr DEO2
			#0d00 SWP2 ;is-selected JSR2 SWP? POP
			.settings/blending LDZ +
			.settings/depth LDZ 80* +
				#00 + .Screen/sprite DEO
			INC GTHk ,&hor JCN
		POP2
		INC GTHk ,&ver JCN
	POP2

	.tileview-frame #03 ;line-rect JSR2

RTN

@draw-tileview-zoom ( -- )

	( find focus ) GET-POSITION STH2 STH2
	;pixel-icn .Screen/addr DEO2
	VER #00
	&ver
		DUP 8* TOS .tileview/y1 LDZ2 ++ .Screen/y DEO2
		HOR #00
		&hor
			DUP 8* TOS .tileview/x1 LDZ2 ++ .Screen/x DEO2
			( get pixel offset ) GET-ITER SWP TOS ROT TOS
			( add focus ) STH2kr SWP2r STH2kr SWP2r ADD-POSITIONS
			;get-pixel JSR2 #00 + .Screen/sprite DEO
			INC GTHk ,&hor JCN
		POP2
		INC GTHk ,&ver JCN
	POP2
	POP2r POP2r

	( guides )
	VER 8/ #01 - #00
	&guides-hor
		STHk
		.tileview/x1 LDZ2 
			.tileview/x2 LDZ2 
			.tileview/y1 LDZ2 STHr INC 40* #01 - TOS ++ 
			#02 
			;line-hor JSR2
		INC GTHk ,&guides-hor JCN
	POP2

	HOR 8/ #01 - #00
	&guides-ver
		STHk
		.tileview/x1 LDZ2 STHr INC 40* #01 - TOS ++ 
			.tileview/y1 LDZ2 
			.tileview/y2 LDZ2 
			#02 
			;line-ver JSR2
		INC GTHk ,&guides-ver JCN
	POP2

RTN

@get-pixel ( x* y* -- color )
	
	( channel 1 )
	OVR2 SWP2 ,get-addr JSR [ STH2k ] LDA
	ROT ROT NIP 8MOD [ STHk ]
	#07 SWP - SFT #01 AND
	( channel 2 )
	[ STHr ] [ STH2r ] #0008 ++ LDA SWP 
	#07 SWP - SFT #01 AND
	2* 
	+

RTN

@get-addr ( x* y* -- addr* )
	
	( get row )
	DUP2 8MOD2 
	( get tile )
	SWP2 8// 200** ++
	SWP2 STEP8 2** ++ 
	BANK ++

RTN

@put-pixel ( x* y* color -- )

	STH
	OVR2 SWP2 ,get-addr JSR 
	( ch1 )
	OVR2 OVR2
		STHkr #00 ,toggle-pixel JSR
	( ch2 )
	#0008 ++ 
		STHr #01 ,toggle-pixel JSR

RTN

@toggle-pixel ( x* addr* color* -- )
	
	STH2
	LDAk
	STH SWP2 NIP
	STHr SWP 
	STH2r SFT #01 AND ,&do-set JCN
		( mask ) #01 #07 ROT 8MOD - SFL #ff EOR AND
		( save ) ROT ROT STA
		RTN
	&do-set
		( mask ) #01 #07 ROT 8MOD - SFL ORA
		( save ) ROT ROT STA

RTN

@get-tile-addr ( x* y* -- addr* )
	
	STEP8 100** 4// SWP2
	STEP8 2** ++
	BANK ++

RTN

@copy-tile ( src* dst* -- )

	STH2 STH2
	#10 #00
	&loop
		( get )
		DUP TOS DUP2 STH2kr ++ LDA 
		( put )
		ROT ROT SWP2r STH2kr SWP2r ++ STA
		INC GTHk ,&loop JCN
	POP2
	POP2r POP2r

RTN

@draw-toolview ( -- )
		
	.toolview/y1 LDZ2 .Screen/y DEO2
	( colors )
	.toolview/x1 LDZ2 STH2k .Screen/x DEO2
	;circle-icns [ .settings/color LDZ #01 = TOS 8** ] ++ .Screen/addr DEO2
	#01 .Screen/sprite DEO
	STH2kr #0008 ++ .Screen/x DEO2
	;circle-icns [ .settings/color LDZ #02 = TOS 8** ] ++ .Screen/addr DEO2
	#02 .Screen/sprite DEO
	STH2kr #0010 ++ .Screen/x DEO2
	;circle-icns [ .settings/color LDZ #03 = TOS 8** ] ++ .Screen/addr DEO2
	#03 .Screen/sprite DEO
	( tools )
	STH2kr #0020 ++ .Screen/x DEO2
	;tool-brush .Screen/addr DEO2
	#01 .settings/tool LDZ #00 = + .Screen/sprite DEO
	STH2kr #0029 ++ .Screen/x DEO2
	;tool-selector .Screen/addr DEO2
	#01 .settings/tool LDZ #01 = + .Screen/sprite DEO
	STH2r #0030 ++ .Screen/x DEO2
	;zoom-icns [ .settings/zoom LDZ TOS 8** ++ ] .Screen/addr DEO2
	#01 .settings/zoom LDZ + .Screen/sprite DEO
	( file i/o )
	;draw-state JSR2
	.toolview/x2 LDZ2 STH2k #0018 -- .Screen/x DEO2
	;load-icn .Screen/addr DEO2
	#01 .Screen/sprite DEO
	STH2r #0020 -- .Screen/x DEO2
	;make-icn .Screen/addr DEO2
	#01 .Screen/sprite DEO
	( filepath )
	#03 ;draw-filepath JSR2

RTN

@draw-blendview ( -- )
	
	( value )
	.blendview/x1 LDZ2 .Screen/x DEO2
	.blendview/y2 LDZ2 INC2 .Screen/y DEO2
	( get blending ) .settings/blending LDZ
	( get depth ) .settings/depth LDZ 80*
	( draw ) + #01 ;draw-byte JSR2

	.settings/focus LDZ2 .Screen/addr DEO2
	#10 #00
	&loop
		DUP 4MOD TOS 8** .blendview/x1 LDZ2 ++ .Screen/x DEO2
		DUP 4/ TOS 8** .blendview/y1 LDZ2 ++ .Screen/y DEO2
		#00 .Screen/sprite DEO
		DUP #00 + .settings/depth LDZ 80* + .Screen/sprite DEO
		INC GTHk ,&loop JCN
	POP2

RTN

@draw-preview ( -- )

	( value )
	.preview/x1 LDZ2 .Screen/x DEO2
	.preview/y2 LDZ2 INC2 .Screen/y DEO2
	.settings/ratio LDZ #02 ;draw-byte JSR2

	( stash address )
	.settings/focus LDZ2 STH2k .Screen/addr DEO2
	#04 #00
	&ver
		DUP 8* TOS .preview/y1 LDZ2 ++ .Screen/y DEO2
		#04 #00
		&hor
			DUP 8* TOS .preview/x1 LDZ2 ++ .Screen/x DEO2
			( get x,y )
			GET-ITER
			( check if within ratio )
			.settings/ratio LDZ #0f AND < STH
			.settings/ratio LDZ #04 SFT < STHr
			#0101 !! ,&outside JCN 
				( get tile ) STH2kr .Screen/addr DEO2
				( get blending ) .settings/blending LDZ
				( get depth ) .settings/depth LDZ 80*
				( draw ) + .Screen/sprite DEO
				( incr ) STH2r #0008 [ .settings/depth LDZ 8* TOS ++ ] ++ STH2
				,&resume JMP
			&outside
			;halftone-icn .Screen/addr DEO2
			#03 .Screen/sprite DEO
			&resume
			INC GTHk ,&hor JCN
		POP2
		INC GTHk ;&ver JCN2
	POP2
	POP2r

RTN

@draw-colorview ( -- )
	
	.colorview/y2 LDZ2 INC2 .Screen/y DEO2
	.colorview/x1 LDZ2 .Screen/x DEO2
	.System/r GET-COLOR GET-HEX-ADDR 
	#02 .Screen/sprite DEO
	.Screen/x DEI2 #0008 ++ .Screen/x DEO2
	.System/g GET-COLOR GET-HEX-ADDR 
	#02 .Screen/sprite DEO
	.Screen/x DEI2 #0008 ++ .Screen/x DEO2
	.System/b GET-COLOR GET-HEX-ADDR 
	#02 .Screen/sprite DEO

	.colorview/x1 LDZ2 .colorview/y1 LDZ2 
		[ .System/r GET-COLOR ] ,&slider JSR
	.colorview/x1 LDZ2 .colorview/y1 LDZ2 #0008 ++
		[ .System/g GET-COLOR ] ,&slider JSR
	.colorview/x1 LDZ2 .colorview/y1 LDZ2 #0010 ++
		[ .System/b GET-COLOR ] ,&slider JSR

	RTN

	&slider ( x* y* value -- )
		STH
		.Screen/y DEO2
		.Screen/x DEO2
		#10 #00
		&loop
			DUP STHkr > 8* TOS ;slider-icns ++ .Screen/addr DEO2
			#02 .Screen/sprite DEO
			.Screen/x DEI2 2INC2 .Screen/x DEO2
			INC GTHk ,&loop JCN
		POP2
		POPr

RTN

@draw-dataview ( -- )
	
	( position )
	.dataview/x1 LDZ2 .Screen/x DEO2
	.dataview/y2 LDZ2 INC2 .Screen/y DEO2
	.settings/focus LDZ2 BANK -- #02 ;draw-short JSR2

	( depth1 )
	#04 #00
	&loop
		.dataview/x1 LDZ2 .Screen/x DEO2
		DUP 8* TOS .dataview/y1 LDZ2 ++ .Screen/y DEO2
		DUP 2* TOS .settings/focus LDZ2 ++ LDA2 #01 ;draw-short JSR2
		.dataview/x1 LDZ2 #0020 ++ .Screen/x DEO2
		DUP 2* TOS .settings/focus LDZ2 ++ #0008 ++ LDA2 #03 ;draw-short JSR2
		INC GTHk ,&loop JCN
	POP2

RTN

@draw-zoomview ( -- )

	;bigpixel-icn .Screen/addr DEO2
	#08 #00
	&ver
		DUP 4* TOS .zoomview/y1 LDZ2 ++ .Screen/y DEO2
		#08 #00
		&hor
			DUP 4* TOS .zoomview/x1 LDZ2 ++ .Screen/x DEO2
			( get pixel )
			GET-ITER
			( ch 1 )
			TOS .settings/focus LDZ2 ++ [ STH2k ] LDA
			SWP [ STHk ] #07 SWP - SFT #01 AND
			( ch2 )
			[ STHr ] [ STH2r ] #0008 ++ LDA SWP 
			#07 SWP - SFT #01 AND
			2* +
			.Screen/sprite DEO
			INC GTHk ,&hor JCN
		POP2
		INC GTHk ,&ver JCN
	POP2

	.zoomview-frame #03 ;line-rect JSR2

	( effects )
	.zoomview/y1 LDZ2 #0021 ++ .Screen/y DEO2
	.zoomview/x1 LDZ2 .Screen/x DEO2
	;opshift-icn .Screen/addr DEO2
	#01 .Screen/sprite DEO

	.Screen/x DEI2 #0008 ++ .Screen/x DEO2
	;oproll-icn .Screen/addr DEO2
	#01 .Screen/sprite DEO

RTN

@draw-cursor ( -- )

	( clear last cursor )
	.cursor/x LDZ2 .Screen/x DEO2
	.cursor/y LDZ2 .Screen/y DEO2
	#40 .Screen/sprite DEO

	( record mouse positions )
	.Mouse/x DEI2 DUP2 .cursor/x STZ2 .Screen/x DEO2
	.Mouse/y DEI2 DUP2 .cursor/y STZ2 .Screen/y DEO2

	( draw new cursor )
	;tool-brush #00 .settings/tool LDZ 8* ++ .Screen/addr DEO2
	#42 .Mouse/state DEI #00 ! + .Screen/sprite DEO

RTN

@draw-filepath ( color -- )
	
	STH
	.toolview/x1 LDZ2 #0040 ++ .Screen/x DEO2
	.toolview/y1 LDZ2 .Screen/y DEO2
	;path/name
	&loop
		LDAk DUP #00 ! 20* - TOS 8** ;font ++ .Screen/addr DEO2 
		STHkr .Screen/sprite DEO
		.Screen/x DEI2 #0008 ++ .Screen/x DEO2
		INC2
		LDAk #00 ! ,&loop JCN
	POP2
	POPr
	( clear last ) 
	#00 .Screen/sprite DEO

RTN

@draw-state ( -- )
	
	.toolview/x2 LDZ2 #0008 -- .Screen/x DEO2
	.toolview/y1 LDZ2 .Screen/y DEO2
	;save-icn .Screen/addr DEO2
	#0f05 .state/changed LDZ SWP? POP .Screen/sprite DEO

RTN

@draw-short ( short* color -- )
	
	ROT OVR ,draw-byte JSR
	.Screen/x DEI2 #0008 ++ .Screen/x DEO2
	,draw-byte JSR

RTN

@draw-byte ( byte color -- )
	
	STH 
	DUP 
		#04 SFT GET-HEX-ADDR 
		( draw ) STHkr .Screen/sprite DEO
	.Screen/x DEI2 #0008 ++ .Screen/x DEO2
		#0f AND GET-HEX-ADDR 
		( draw ) STHr .Screen/sprite DEO

RTN

( file )

@new-file ( default* -- )
	
	BANK LENGTH ;clear JSR2
	STH2
	#0c #00
	&loop
		DUPk TOS STH2kr ++ LDA
		SWP .path/name + STZ
		INC GTHk ,&loop JCN
	( end ) #00 SWP .path/name + STZ 
	.path/length STZ 
	POP2r
	RESET-STATE

RTN

@load-file ( -- )

	( icn mode ) .Controller/button DEI #02 AND ,load-file-icn JCN

	BANK LENGTH ;clear JSR2
	;path/name .File/name DEO2 
	#2000 .File/length DEO2 
	BANK .File/load DEO2
	RESET-STATE
	;redraw JSR2

RTN

@load-file-icn ( -- )
	
	BANK LENGTH ;clear JSR2
	;path/name .File/name DEO2 
	#2000 .File/length DEO2 
	BANK-TEMP .File/load DEO2
	RESET-STATE

	#1000 #0000
	&loop
		DUP2 BANK-TEMP ++ LDA STH
		DUP2k [ 8// 8** ++ ] BANK ++ STHr ROT ROT STA
		INC2 GTH2k ,&loop JCN
	POP2 POP2
	;redraw JSR2

RTN

@save-file ( -- )

	;path/name .File/name DEO2 
	#2000 .File/length DEO2 
	BANK .File/save DEO2
	RESET-STATE

	;save-theme JSR2

RTN

( theme )

@load-theme ( -- )

	;&path .File/name DEO2 
	#0006 .File/length DEO2 
	#fffa .File/load DEO2
	.File/success DEI2 #0006 !! ,&ignore JCN
		#fffa LDA2 .System/r DEO2
		#fffc LDA2 .System/g DEO2
		#fffe LDA2 .System/b DEO2
		&ignore
	;redraw JSR2

RTN
	&path ".theme $1

@save-theme ( -- )

	.System/r DEI2 #fffa STA2
	.System/g DEI2 #fffc STA2
	.System/b DEI2 #fffe STA2
	;load-theme/path .File/name DEO2 
	#0006 .File/length DEO2 
	#fffa .File/save DEO2

RTN

( snarf )

@snarf-txt ".snarf $1

@copy-snarf ( -- )

	( open ) DATA-CLIP STH2
	.selection/y2 LDZ INC .selection/y1 LDZ
	&ver
		.selection/x2 LDZ INC .selection/x1 LDZ
		&hor
			( write char ) GET-ITER GET-ADDR STH2kr ;copy-tile JSR2
			( incr index ) LIT2r 0010 ADD2r
			INC GTHk ,&hor JCN
		POP2
		INC GTHk ,&ver JCN
	POP2
	( close ) #00 STH2kr STA

	;snarf-txt .File/name DEO2 
	STH2r DATA-CLIP -- .File/length DEO2 
	DATA-CLIP .File/save DEO2

RTN

@paste-snarf ( -- )

	;snarf-txt .File/name DEO2
	.selection/y2 LDZ INC .selection/y1 LDZ - TOS
	.selection/x2 LDZ INC .selection/x1 LDZ - TOS
	MUL2 10** .File/length DEO2
	DATA-CLIP .File/load DEO2

	( stash x,y ) .selection LDZ2 STH2
	.File/success DEI2 #0000
	&loop
		( limit at selection )
		OVRr STHr .selection/x2 LDZ INC LTH ,&linebreak JCN
			STH2r INC NIP .selection/x1 LDZ SWP STH2
			&linebreak
		DUP2 [ DATA-CLIP ++ ] [ STH2kr GET-ADDR ] ;copy-tile JSR2
		( move-x ) SWPr LITr 01 ADDr SWPr
		#0010 ++ GTH2k ,&loop JCN
	POP2 POP2
	( destroy x,y ) POP2r

RTN

( generics )

@within-rect ( x* y* rect -- flag )
	
	STH
	( y < rect.y1 ) DUP2 STHkr #02 ADD LDZ2 LTH2 ,&skip JCN
	( y > rect.y2 ) DUP2 STHkr #06 ADD LDZ2 GTH2 ,&skip JCN
	SWP2
	( x < rect.x1 ) DUP2 STHkr LDZ2 LTH2 ,&skip JCN
	( x > rect.x2 ) DUP2 STHkr #04 ADD LDZ2 GTH2 ,&skip JCN
	POP2 POP2 POPr
	#01 
RTN
	&skip
	POP2 POP2 POPr
	#00

RTN

@line-rect ( rect color -- )

	STH STH
	( y2 ) STHkr #06 + LDZ2
	( y1 ) STHkr #02 + LDZ2 #0001 -- ( flip sign ) GTH2k SWP2?
	&ver
		( save ) DUP2 .Screen/y DEO2
		( x1 ) STHkr LDZ2 #0001 -- .Screen/x DEO2 
		OVRr STHr .Screen/pixel DEO
		( x2 ) STHkr #04 + LDZ2 .Screen/x DEO2 
		OVRr STHr .Screen/pixel DEO
		( incr )
		INC2 GTH2k ,&ver JCN
	POP2
	( x2 ) STHkr #04 + LDZ2
	( x1 ) STHkr LDZ2 #0001 -- ( flip sign ) GTH2k SWP2?
	&hor
		( save ) DUP2 .Screen/x DEO2
		( y1 ) STHkr #02 + LDZ2 #0001 -- .Screen/y DEO2 
		OVRr STHr .Screen/pixel DEO
		( y2 ) STHkr #06 + LDZ2 .Screen/y DEO2 
		OVRr STHr .Screen/pixel DEO
		( incr )
		INC2 GTH2k ,&hor JCN
	POP2
	POPr 
	.Screen/x DEO2
	.Screen/y DEO2 
	STHr .Screen/pixel DEO

RTN

@line-hor ( x0* x1* y* color -- )
	
	STH .Screen/y DEO2 SWP2
	&loop
		( save ) DUP2 .Screen/x DEO2
		( draw ) STHkr .Screen/pixel DEO
		2INC2 GTH2k ,&loop JCN
	POP2 POP2 POPr

RTN

@line-ver ( x* y0* y1* color -- )
	
	STH ROT2 .Screen/x DEO2 SWP2
	&loop
		( save ) DUP2 .Screen/y DEO2
		( draw ) STHkr .Screen/pixel DEO
		2INC2 GTH2k ,&loop JCN
	POP2 POP2 POPr

RTN

@print-hex ( value -- )
	
	STHk #04 SFT ,&parse JSR .Console/write DEO
	STHr #0f AND ,&parse JSR .Console/write DEO
	RTN
	&parse ( value -- char )
		DUP #09 GTH ,&above JCN #30 ADD RTN &above #09 SUB #60 ADD RTN

RTN

@tool-brush    e0d0 8844 2212 0c00
@tool-selector 80c0 e0f0 f8e0 1000

@pixel-icn     7cfe fefe fefe 7c00
@bigpixel-icn  f0f0 f0f0 0000 0000
@halftone-icn  aa55 aa55 aa55 aa55

@opshift-icn   0010 387c fe10 1000
@oproll-icn    0008 0c0e 7f0e 0c08

@save-icn      1054 28c6 2854 1000 
@make-icn      fe82 8282 848a f400 
@load-icn      feaa d6aa d4aa f400 

@untitled-txt  "untitled.chr $1

@font-hex ( 0-F )
	007c 8282 8282 827c 0030 1010 1010 1010
	007c 8202 7c80 80fe 007c 8202 1c02 827c
	000c 1424 4484 fe04 00fe 8080 7c02 827c
	007c 8280 fc82 827c 007c 8202 1e02 0202
	007c 8282 7c82 827c 007c 8282 7e02 827c
	007c 8202 7e82 827e 00fc 8282 fc82 82fc
	007c 8280 8080 827c 00fc 8282 8282 82fc
	007c 8280 f080 827c 007c 8280 f080 8080

@slider-icns
	f0f0 f0f0 f0f0 f000 ( full )
	50a0 50a0 50a0 5000 ( empty )
@circle-icns
	3844 8282 8244 3800 ( full )
	387c fefe fe7c 3800 ( line )
@zoom-icns
	3048 8484 4834 0200 ( in )
	3245 8284 4834 0200 ( out )
@font ( specter8-frag font )
	0000 0000 0000 0000 0008 0808 0800 0800
	0014 1400 0000 0000 0024 7e24 247e 2400
	0008 1e28 1c0a 3c08 0000 2204 0810 2200
	0030 4832 4c44 3a00 0008 1000 0000 0000
	0004 0808 0808 0400 0020 1010 1010 2000
	0000 2214 0814 2200 0000 0808 3e08 0800
	0000 0000 0000 0810 0000 0000 3e00 0000
	0000 0000 0000 0800 0000 0204 0810 2000
	003c 464a 5262 3c00 0018 0808 0808 1c00
	003c 4202 3c40 7e00 003c 421c 0242 3c00
	000c 1424 447e 0400 007e 407c 0242 3c00
	003c 407c 4242 3c00 007e 0204 0810 1000
	003c 423c 4242 3c00 003c 4242 3e02 3c00
	0000 0010 0000 1000 0000 1000 0010 1020
	0000 0810 2010 0800 0000 003e 003e 0000
	0000 1008 0408 1000 003c 420c 1000 1000
	003c 4232 4a42 3c00 003c 4242 7e42 4200
	007c 427c 4242 7c00 003c 4240 4042 3c00
	007c 4242 4242 7c00 007e 4078 4040 7e00
	007e 4078 4040 4000 003c 4240 4642 3c00
	0042 427e 4242 4200 001c 0808 0808 1c00
	007e 0202 0242 3c00 0042 4478 4442 4200
	0040 4040 4040 7e00 0042 665a 4242 4200
	0042 6252 4a46 4200 003c 4242 4242 3c00
	007c 4242 7c40 4000 003c 4242 4244 3a00
	007c 4242 7c44 4200 003e 403c 0242 3c00
	007e 0808 0808 1000 0042 4242 4244 3a00
	0042 4242 4224 1800 0042 4242 5a66 4200
	0042 423c 4242 4200 0042 423e 0242 3c00
	007e 020c 3040 7e00 000c 0808 0808 0c00
	0040 2010 0804 0200 0030 1010 1010 3000
	0008 1400 0000 0000 0000 0000 0000 7e00
	0008 0400 0000 0000 0000 3c02 3e42 3a00
	0040 407c 4242 7c00 0000 3c42 4042 3c00
	0002 023e 4242 3e00 0000 3c42 7e40 3e00
	0000 3e40 7840 4000 0000 3c42 3e02 3c00
	0040 405c 6242 4200 0008 0018 0808 0400
	0008 0018 0808 4830 0040 4244 7844 4200
	0010 1010 1010 0c00 0000 6c52 5252 5200
	0000 5c62 4242 4200 0000 3c42 4242 3c00
	0000 7c42 427c 4040 0000 3e42 423e 0202
	0000 5c62 4040 4000 0000 3e40 3c02 7c00
	0008 7e08 0808 1000 0000 4242 4244 3a00
	0000 4242 4224 1800 0000 5252 5252 2e00
	0000 4224 1824 4200 0000 4242 3e02 7c00
	0000 7e02 3c40 7e00 000c 0810 1008 0c00
	0008 0808 0808 0800 0030 1008 0810 3000
	0000 0032 4c00 0000 3c42 99a1 a199 423c