~rabbits/noodle

noodle/src/main.tal -rw-r--r-- 34.2 KiB
ed2992feneauoire Fixed issue with filestate and oddnumber canvas size in zoom 9 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
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
(
	app/noodle : illustration program
	
	right-click - erase
	arrows - move zoom
	alt+arrows - resize canvas
	space - toogle zoom
	tab - invert image
	enter - next frame
	backspace - blank canvas
	1-8 - select brush size
	ctrl+n - clear
	ctrl+r - rename
	ctrl+o - load
	ctrl+s - save
)

%** { MUL2 } ( could be ;mul2_by_shifting JSR2 )

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

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

%2*   { #10 SFT }
%8*   { #30 SFT }
%20*  { #50 SFT } 
%40*  { #60 SFT }
%2**  { #10 SFT2 } %2// { #01 SFT2 }
%8**  { #30 SFT2 } %8// { #03 SFT2 }
%2++  { INC2 INC2 }
%TOS  { #00 SWP }  

%SWP? { #01 JCN SWP }
%SWP2? { #01 JCN SWP2 }
%BRK? { #01 JCN BRK }
%RTN? { #01 JCN RTN }
%DEC2 { #0001 -- }
%RTN  { JMP2r }
%MIN2 { GTH2k SWP2? POP2 }
%MAX2 { LTH2k SWP2? POP2 }
%SFL  { #40 SFT SFT }
%ABS2 { DUP2 #0f SFT2 = #05 JCN #0000 SWP2 -- }
%LTS2 { #8000 ++ SWP2 #8000 ++ >> }
%GTS2 { #8000 ++ SWP2 #8000 ++ << }
%8MOD2 { #0007 AND2 }
%8MOD { #07 AND }

%PAD { #0003 }

%GET-ITER { OVR2 NIP OVR SWP }
%ADD-POSITIONS { ROT2 ++ ROT2 ROT2 ++ SWP2 } 

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

%GET-FRAME-SIZE { .canvas/h LDZ2 .canvas/w LDZ2 MUL2 8** } ( -- length* )
%GET-ANIM-SIZE { GET-FRAME-SIZE .anim/limit LDZ INC TOS MUL2 } ( -- length* )
%GET-FRAME-ADDR { TOS GET-FRAME-SIZE ** ;data ++ } ( frame -- addr* )

%TOGGLE { LDZk #00 = SWP STZ }
%RELEASE-MOUSE { #00 .Mouse/state DEO }

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

( 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
	&presentation $1
	&changed $1
@path
	&length $1 
	&name $20
@brush 
	&tool $1 
	&size $1 
	&patt $1 
	&oper $2 
	&mode $1
	&drag $1
@cursor 
	&x1 $2 &y1 $2 
	&x2 $2 &y2 $2 
	&last $1 
@zoom 
	&active $1 
	&x $2 &y $2
@anim
	&frame $1
	&limit $1
	&onion $1
	&addr $2
@theme
	&r $1 &g $1 &b $1
@center
	&x $2 &y $2
@canvas
	&x1 $2 &y1 $2 &x2 $2 &y2 $2 &w $2 &h $2
@guide
	&x1 $2 &y1 $2 &x2 $2 &y2 $2

@toolpane
	&x1 $2 &y1 $2 &x2 $2 &y2 $2
@pattpane
	&x1 $2 &y1 $2 &x2 $2 &y2 $2
@sizepane
	&x1 $2 &y1 $2 &x2 $2 &y2 $2
@animpane
	&x1 $2 &y1 $2 &x2 $2 &y2 $2
@filepane
	&x1 $2 &y1 $2 &x2 $2 &y2 $2
@namepane
	&x1 $2 &y1 $2 &x2 $2 &y2 $2
@line
	&x1 $2 &y1 $2 &x2 $2 &y2 $2 &sx $2 &sy $2 &dx $2 &dy $2 &e1 $2 &e2 $2
@color $1 

( program )

|0100
	
	;untrap JSR2

	( colors )
	#70fe .System/r DEO2 
	#c0f3 .System/g DEO2 
	#90f3 .System/b DEO2

	( default brush )
	#04 .brush/size STZ 
	#00 .brush/patt STZ 
	#00 .brush/tool STZ

	.Screen/width DEI2 2// .center/x STZ2
	.Screen/height DEI2 2// .center/y STZ2

	( above panes )
	#0010 #000c #0030 PAD ++ #0008 PAD ++ SIZE-TO-RECT 
		.toolpane/y2 STZ2 .toolpane/x2 STZ2 .toolpane/y1 STZ2 .toolpane/x1 STZ2 
	#0048 #000c #0040 PAD ++ #0008 PAD ++ SIZE-TO-RECT
		.sizepane/y2 STZ2 .sizepane/x2 STZ2 .sizepane/y1 STZ2 .sizepane/x1 STZ2 
	.Screen/width DEI2 #0053 -- #000c #0040 PAD ++ #0008 PAD ++ SIZE-TO-RECT 
		.pattpane/y2 STZ2 .pattpane/x2 STZ2 .pattpane/y1 STZ2 .pattpane/x1 STZ2 
	
	( below panes )
	#0010 .Screen/height DEI2 #0010 -- #0020 #0008 SIZE-TO-RECT
		.animpane/y2 STZ2 .animpane/x2 STZ2 .animpane/y1 STZ2 .animpane/x1 STZ2 
	.Screen/width DEI2 #0031 -- .Screen/height DEI2 #0010 -- #0020 #0008 SIZE-TO-RECT
		.filepane/y2 STZ2 .filepane/x2 STZ2 .filepane/y1 STZ2 .filepane/x1 STZ2 
	#0038 .Screen/height DEI2 #0010 -- .Screen/width DEI2 #0070 -- #0008 SIZE-TO-RECT
		.namepane/y2 STZ2 .namepane/x2 STZ2 .namepane/y1 STZ2 .namepane/x1 STZ2 

	( starting frame )
	.anim/frame LDZ GET-FRAME-ADDR .anim/addr STZ2

	( default size )
	.Screen/width DEI2 8// #0004 -- 
	.Screen/height DEI2 8// #0006 -- 
		;set-size JSR2

	( ready. )
	;clear JSR2
	;draw-filepane/state JSR2

	( load default file )
	;untitled-txt ;new-file JSR2

	( theme support )
	;load-theme JSR2

BRK

@untrap ( -- )
	
	( vectors ) 
	;on-frame .Screen/vector DEO2
	;on-button .Controller/vector DEO2
	;on-mouse .Mouse/vector DEO2

	( filepath )
	#00 .state/blink STZ 
	#01 ;draw-filepath JSR2
	RELEASE-MOUSE

RTN

@trap ( -- )

	( vectors ) 
	;on-frame-trap .Screen/vector DEO2
	;on-button-trap .Controller/vector DEO2
	;on-mouse-trap .Mouse/vector DEO2

	( filepath )
	#01 .state/blink STZ 
	#01 ;draw-filepath JSR2
	RELEASE-MOUSE

	( clear brush )
	.cursor/x2 LDZ2 .Screen/x DEO2 
	.cursor/y2 LDZ2 .Screen/y DEO2 
	#40 .Screen/sprite DEO

RTN

@on-frame ( -> )

	( inactive )

BRK

@on-frame-trap ( -> )

	( incr ) .state/timer LDZ INC DUP .state/timer STZ 

	#08 ! ,&no-toggle JCN
		.state/blink TOGGLE
		#01 .state/blink LDZ 8* + ;draw-filepath JSR2
		#00 .state/timer STZ 
		&no-toggle
	
BRK

@on-mouse-trap ( -> )

	( release trap on touch )
	.Mouse/state DEI BRK?

	;untrap JSR2
	
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
		;untrap 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 

	( begin with canvas )
	OVR2 OVR2 .canvas ;within-rect JSR2
		;on-touch-canvas JCN2

	( only when click originates in pane )
	.Mouse/state DEI .cursor/last LDZ #0100 !! ,&end JCN

	( anim enabled in presentation )
	OVR2 OVR2 .animpane ;within-rect JSR2
		;on-touch-animpane JCN2

	( disable with presentation )
	.state/presentation LDZ ,&end-present JCN
		
	OVR2 OVR2 .toolpane ;within-rect JSR2
		;on-touch-toolpane JCN2
	OVR2 OVR2 .sizepane ;within-rect JSR2
		;on-touch-sizepane JCN2
	OVR2 OVR2 .pattpane ;within-rect JSR2
		;on-touch-pattpane JCN2
	OVR2 OVR2 .filepane ;within-rect JSR2
		;on-touch-filepane JCN2
	OVR2 OVR2 .namepane ;within-rect JSR2
		;on-touch-namepane JCN2

	&end-present

	( toggle present mode )
	;present JSR2

	&end
	POP2 POP2

BRK

@on-touch-canvas ( x* y* -> )

	( set erase from right-click/mode )
	[ ;remove-pixel ;add-pixel ] .Mouse/state DEI #10 = .brush/mode LDZ + SWP2? POP2 .brush/oper STZ2
	( get mouse state )
	.Mouse/state DEI #00 > #10 SFT .cursor/last LDZ #00 > + #00 SWP 2**
	( non hover states )
	DUP2 #0000 >> ,&continue JCN
		POP2 POP2 POP2 BRK
		&continue
	( get tool offset )
	.brush/tool LDZ #00 SWP 8**
	( record state )
	.Mouse/state DEI .cursor/last STZ
	( find fns )
	;tools ++ ++ LDA2 STH2 
	( canvas offset )
	.canvas/y1 LDZ2 -- SWP2
	.canvas/x1 LDZ2 -- SWP2
	( zoom offset )
	.zoom/active LDZ #01 ! ,&no-zoom JCN
		8// .zoom/y LDZ2 ++ SWP2
		8// .zoom/x LDZ2 ++ SWP2
		&no-zoom
	STH2r JMP2

BRK

@on-touch-toolpane ( x* y* -- )

	( unused y ) POP2

	.toolpane/x1 LDZ2 -- 8// NIP 

	DUP #06 < ,&valid JCN
		POP BRK
		&valid

	;select-tool JSR2
	RELEASE-MOUSE

BRK

@on-touch-sizepane ( x* y* -- )

	( unused y ) POP2

	.sizepane/x1 LDZ2 -- 8// NIP 

	DUP #08 < ,&valid JCN
		POP BRK
		&valid

	;select-size JSR2
	RELEASE-MOUSE

BRK

@on-touch-pattpane ( x* y* -- )

	( unused y ) POP2

	.pattpane/x1 LDZ2 -- 8// NIP 

	DUP #08 < ,&valid JCN
		POP BRK
		&valid

	;select-patt JSR2
	RELEASE-MOUSE 

BRK

@on-touch-animpane ( x* y* -- )

	( unused y ) POP2

	.animpane/x1 LDZ2 -- 8// NIP 

	[ #00 ] !~ ,&no-onion JCN
		.anim/onion TOGGLE
		;redraw JSR2
		RELEASE-MOUSE POP BRK
		&no-onion
	[ #02 ] !~ ,&no-next JCN
		#01 ;mod-frame JSR2
		RELEASE-MOUSE POP BRK
		&no-next
	[ #03 ] !~ ,&no-frame JCN
		.Mouse/state DEI ;mod-limit JSR2
		RELEASE-MOUSE POP BRK
		&no-frame
	POP

BRK

@on-touch-filepane ( x* y* -- )

	( unused y ) POP2

	.filepane/x1 LDZ2 -- 8// NIP 

	[ #00 ] !~ ,&no-blank JCN
		;untitled-txt ;new-file JSR2
		;draw-canvas JSR2
		RELEASE-MOUSE POP BRK 
		&no-blank
	[ #01 ] !~ ,&no-load JCN
		;load-file JSR2
		;draw-canvas JSR2
		RELEASE-MOUSE POP BRK 
		&no-load
	[ #03 ] !~ ,&no-save JCN
		;path/name ;save-file JSR2
		RELEASE-MOUSE POP BRK 
		&no-save
	POP

BRK

@on-touch-namepane ( x* y* -- )

	( unused x y ) POP2 POP2

	#00 ;draw-filepath JSR2 
	;trap JSR2

BRK

@on-button ( -> )

	.Controller/button DEI 
		DUP #01 AND ;on-control JCN2
		DUP #02 AND ;on-alt JCN2
	POP
	
	( control zoom )
	.zoom/active LDZ #00 = ,&skip-zoom JCN
		.Controller/button DEI #f0 AND
		DUP #04 SFT #01 AND #01 ! ,&no-up JCN
			( move ) .zoom/y LDZ2 #0001 -- .zoom/y STZ2 
			&no-up
		DUP #05 SFT #01 AND #01 ! ,&no-down JCN
			( move ) .zoom/y LDZ2 INC2 .zoom/y STZ2 
			&no-down
		DUP #06 SFT #01 AND #01 ! ,&no-left JCN
			( move ) .zoom/x LDZ2 #0001 -- .zoom/x STZ2 
			&no-left
		DUP #07 SFT #01 AND #01 ! ,&no-right JCN
			( move ) .zoom/x LDZ2 INC2 .zoom/x STZ2 
			&no-right
		#00 = #04 JCN ;draw-canvas JSR2
	&skip-zoom

	.Controller/key DEI
	DUP #00 ! ,&continue JCN
		POP BRK &continue
	DUP #1b ! ,&no-escape JCN
		;load-theme JSR2 POP BRK &no-escape
	DUP #08 ! ,&no-backspace JCN
		( erase ) POP ;clear JSR2 BRK &no-backspace
	DUP #0d ! ,&no-enter JCN
		#01 ;mod-frame JSR2 BRK &no-enter
	DUP #09 ! ,&no-invert JCN
		( invert ) POP ;invert JSR2 BRK &no-invert
	DUP #20 ! ,&no-space JCN
		( toggle zoom ) .zoom/active TOGGLE ;redraw JSR2 &no-space
	DUP #71 ! ,&no-qkey JCN
		( tool0 ) #00 ;select-tool JSR2 &no-qkey
	DUP #77 ! ,&no-wkey JCN
		( tool1 ) #01 ;select-tool JSR2 &no-wkey
	DUP #65 ! ,&no-ekey JCN
		( tool2 ) #02 ;select-tool JSR2 &no-ekey
	DUP #72 ! ,&no-rkey JCN
		( tool3 ) #03 ;select-tool JSR2 &no-rkey
	DUP #74 ! ,&no-tkey JCN
		( tool4 ) #04 ;select-tool JSR2 &no-tkey
	DUP
		DUP #30 > SWP #39 < #0101 !! ,&no-numkey JCN
		( size ) DUP #31 SUB ;select-size JSR2 &no-numkey
	POP

BRK

@on-control ( button -> )
	
	POP
	.Controller/key DEI
	DUP [ LIT 'r ] ! ,&no-name JCN
		#00 ;draw-filepath JSR2 
		;trap JSR2
		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
		;trap JSR2
		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
		;redraw JSR2 POP BRK &no-open
	DUP [ LIT 's ] ! ,&no-save JCN
		;path/name ;save-file JSR2 
		;draw-filepane/state JSR2 POP BRK  &no-save
	POP 

BRK

@on-alt ( button -> )

	#04 SFT
	[ #01 !~ ] ,&no-up JCN
		.canvas/w LDZ2 .canvas/h LDZ2 #0001 -- ;set-size JSR2
		&no-up
	[ #02 !~ ] ,&no-down JCN
		.canvas/w LDZ2 .canvas/h LDZ2 INC2 ;set-size JSR2
		&no-down
	[ #04 !~ ] ,&no-left JCN
		.canvas/w LDZ2 #0001 -- .canvas/h LDZ2 ;set-size JSR2
		&no-left
	[ #08 !~ ] ,&no-right JCN
		.canvas/w LDZ2 INC2 .canvas/h LDZ2 ;set-size JSR2
		&no-right
	POP

BRK

@select-tool ( tool -- )
	
	.brush/tool STZ 
	;draw-toolpane JSR2

RTN

@select-patt ( patt -- )

	.brush/patt STZ
	( select brush )
	.brush/tool LDZ ,&no-switch JCN
		#01 ;select-tool JSR2
		&no-switch
	;draw-pattpane JSR2

RTN

@select-size ( size -- )
	
	( toggle mode on reselection )
	DUP .brush/size LDZ ! ,&no-toggle JCN
		.brush/mode TOGGLE
		&no-toggle
	.brush/size STZ
	( select brush )
	.brush/tool LDZ ,&no-switch JCN
		#01 ;select-tool JSR2
		&no-switch
	;draw-sizepane JSR2

RTN

@get-last-frame ( -- addr* )

	.anim/frame LDZ 
	DUP #00 = ,&rewind JCN
	#01 - GET-FRAME-ADDR RTN
	&rewind
		POP .anim/limit LDZ GET-FRAME-ADDR

RTN

@clear ( -- )
	
	.anim/addr LDZ2 STH2k GET-FRAME-SIZE ++ STH2r
	&loop
		( save ) DUP2 #00 ROT ROT STA
		INC2 GTH2k ,&loop JCN
	POP2 POP2
	;redraw JSR2

RTN

@invert ( -- )
	
	.anim/addr LDZ2 STH2k GET-FRAME-SIZE ++ STH2r
	&loop
		( load ) DUP2 DUP2 LDA #ff EOR
		( save ) ROT ROT STA
		INC2 GTH2k ,&loop JCN
	POP2 POP2
	;redraw JSR2

RTN

@set-size ( w* h* -- )

		DUP2 .canvas/h LDZ2 !! ,&changed JCN
		OVR2 .canvas/w LDZ2 !! ,&changed JCN
		POP2 POP2 RTN
	&changed

	( undraw )
	.canvas #00 ;fill-rect JSR2

	[ #0002 ] MIN2 [ .Screen/height DEI2 8// #0006 -- ] MAX2
		STH2k .canvas/h STZ2 
	[ #0002 ] MIN2 [ .Screen/width DEI2 8// #0004 -- ] MAX2
		STH2k .canvas/w STZ2

	.center/x LDZ2 STH2kr 8** 2// -- .canvas/x1 STZ2
	STH2r 8** .canvas/x1 LDZ2 ++ .canvas/x2 STZ2
	.center/y LDZ2 STH2kr 8** 2// -- .canvas/y1 STZ2
	STH2r 8** .canvas/y1 LDZ2 ++ .canvas/y2 STZ2

	( draw ui )
	;draw-background JSR2

	( draw size )
	.Screen/width DEI2 #0078 -- .Screen/x DEO2
	#000d .Screen/y DEO2
	.canvas/w LDZ2 #80 SFT2 .canvas/h LDZ2 ++ #01 ;print/short JSR2

	( frame )
	.canvas #01 ;line-rect JSR2

	;draw-canvas JSR2

RTN

@present ( -- )
	
	.state/presentation TOGGLE

	.state/presentation LDZ ,&hide JCN
	,&show JMP

	&show
		( load theme )
		.theme/r LDZ .System/r DEO
		.theme/g LDZ .System/g DEO
		.theme/b LDZ .System/b DEO
		;draw-background JSR2
		;draw-canvas-outside JSR2
		RELEASE-MOUSE
		RTN

	&hide
		( save theme )
		.System/r DEI DUP .theme/r STZ #0f AND .System/r DEO
		.System/g DEI DUP .theme/g STZ #0f AND .System/g DEO
		.System/b DEI DUP .theme/b STZ #0f AND .System/b DEO
		( clear panes )
		.toolpane #00 ;fill-rect JSR2
		.sizepane #00 ;fill-rect JSR2
		.pattpane #00 ;fill-rect JSR2
		;draw-background JSR2
		;draw-canvas-outside JSR2
		RELEASE-MOUSE
		RTN

RTN

@mod-frame ( mod -- )

	DUP #01 = .anim LDZ2 = #0101 == ,&c JCN
	DUP #ff = #00 .anim/frame LDZ = #0101 == ,&cc JCN
	.anim/frame LDZ ADD ,&end JMP
	&c
		POP #00 ,&end JMP
	&cc
		.anim/limit LDZ 
	&end
		DUP .anim/frame STZ 
		GET-FRAME-ADDR .anim/addr STZ2
		;redraw JSR2 

RTN

@mod-limit ( mod -- )

	( reset ) #00 .anim/frame STZ
	.anim/limit LDZ + DUP .anim/limit STZ

	( get maximum frames that can fit in memory )
	#fff0 ;data -- GET-FRAME-SIZE DIV2 #0001 -- NIP
	GTH ,&clamp JCN
	;redraw JSR2 
	RTN
	&clamp
		#00 .anim/limit STZ
		;redraw JSR2 

RTN

@paint-line ( x1* y1* x2* y2* -- )
	
	( load ) .line/y1 STZ2 .line/x1 STZ2 .line/y2 STZ2 .line/x2 STZ2

	.line/x1 LDZ2 .line/x2 LDZ2 -- ABS2 .line/dx STZ2
	.line/y1 LDZ2 .line/y2 LDZ2 -- ABS2 #0000 SWP2 -- .line/dy STZ2
	#ffff #00 .line/x2 LDZ2 .line/x1 LDZ2 LTS2 2** ++ .line/sx STZ2
	#ffff #00 .line/y2 LDZ2 .line/y1 LDZ2 LTS2 2** ++ .line/sy STZ2
	.line/dx LDZ2 .line/dy LDZ2 ++ .line/e1 STZ2
	&loop
		( paint ) .line/x2 LDZ2 .line/y2 LDZ2 .brush/oper LDZ2 JSR2
		.line/x2 LDZ2 .line/y2 LDZ2 ;draw-tile JSR2
		.line/x2 LDZ2 .line/x1 LDZ2 == .line/y2 LDZ2 .line/y1 LDZ2 == #0101 == ,&end JCN
		.line/e1 LDZ2 2** .line/e2 STZ2
		.line/e2 LDZ2 .line/dy LDZ2 LTS2 ,&skipy JCN
			.line/e1 LDZ2 .line/dy LDZ2 ++ .line/e1 STZ2
			.line/x2 LDZ2 .line/sx LDZ2 ++ .line/x2 STZ2
			&skipy
		.line/e2 LDZ2 .line/dx LDZ2 GTS2 ,&skipx JCN
			.line/e1 LDZ2 .line/dx LDZ2 ++ .line/e1 STZ2
			.line/y2 LDZ2 .line/sy LDZ2 ++ .line/y2 STZ2
			&skipx
		;&loop JMP2
	&end

RTN

@fill-selection ( -- )

	.cursor/y2 LDZ2 .cursor/y1 LDZ2 ( flip sign ) GTH2k SWP2?
	&ver
		STH2k
		.cursor/x2 LDZ2 .cursor/x1 LDZ2 ( flip sign ) GTH2k SWP2?
		&hor
			DUP2 STH2kr ;patternize JSR2 #00 = ,&no-pixel JCN
				DUP2 STH2kr .brush/oper LDZ2 JSR2 
				&no-pixel
			INC2
			OVR2 OVR2 GTS2 ,&hor JCN
		POP2 POP2 POP2r
		INC2
		OVR2 OVR2 GTS2 ,&ver JCN
	POP2 POP2
	;draw-canvas JSR2
	
RTN

@paint-brush ( x* y* -- )

	STH2 STH2
	#08 #00
	&ver
		#08 #00
		&hor
			GET-ITER
			DUP2 STH STH ;size-icns #00 .brush/size LDZ 8** ++ #00 STHr ++ LDA #07 STHr SUB SFT #01 AND #00 = ,&skip JCN
			DUP2 [ TOS ROT TOS SWP2 ] STH2kr SWP2r STH2kr SWP2r ADD-POSITIONS OVR2 OVR2 ;patternize JSR2 #00 = ,&no-pixel JCN
					OVR2 OVR2 .brush/oper LDZ2 JSR2 
					;draw-tile JSR2
					,&skip JMP
					&no-pixel
					POP2 POP2
				&skip
				POP2
			INC GTHk ,&hor JCN
			POP2
		INC GTHk ,&ver JCN
	POP2
	POP2r POP2r

RTN

@patternize ( x* y* -- )
	
	STH2 STH2
	.brush/patt LDZ 
	DUP ,&noplain JCN
		POP POP2r POP2r
		#01 RTN &noplain
	DUP #01 ! ,&notone1 JCN
		POP OVR2r OVR2r
		STH2r STH2r ++ #0001 AND2 #0000 ==
		STH2r STH2r -- #0001 AND2 #0000 ==
		#0101 == RTN &notone1
	DUP #02 ! ,&notone2 JCN
		POP OVR2r OVR2r
		STH2r STH2r ++ #0003 AND2 #0000 ==
		STH2r STH2r -- #0003 AND2 #0000 ==
		#0101 == RTN &notone2
	DUP #03 ! ,&notone3 JCN
		POP OVR2r OVR2r
		STH2r STH2r ++ #0005 AND2 #0000 ==
		STH2r STH2r -- #0005 AND2 #0000 ==
		#0101 == RTN &notone3
	DUP #04 ! ,&notone4 JCN
		POP STH2r STH2r 
		++ #0003 AND2 #0000 == RTN &notone4
	DUP #05 ! ,&notone5 JCN
		POP STH2r STH2r 
		-- #0003 AND2 #0000 == RTN &notone5
	DUP #06 ! ,&notone6 JCN
		POP SWP2r POP2r STH2r
		#0001 AND2 NIP RTN &notone6
	DUP #07 ! ,&notone7 JCN
		POP POP2r STH2r 
		#0001 AND2 NIP RTN &notone7
	POP POP2r POP2r
	#00

RTN

@paint-magic ( x* y* -- )

	STH2 STH2
	.brush/size LDZ #00
	&ver
		.brush/size LDZ #00 
		&hor
			GET-ITER [ TOS ROT TOS ]
			STH2kr SWP2r STH2kr SWP2r ADD-POSITIONS

			( skip blanks ) 
			OVR2 OVR2 ;get-pixel JSR2 #00 = ,&blank JCN
				OVR2 OVR2 ,clean-pixel JSR
				OVR2 OVR2 ;draw-tile JSR2
				&blank
			POP2 POP2

			INC GTHk ,&hor JCN
			POP2
		INC GTHk ,&ver JCN
	POP2
	POP2r POP2r

RTN

@clean-pixel ( x* y* -- )

	( get neighbors )
	OVR2 OVR2 DEC2 ,get-pixel JSR STH
	OVR2 INC2 OVR2 ,get-pixel JSR #10 SFT STH ADDr
	OVR2 OVR2 INC2 ,get-pixel JSR #20 SFT STH ADDr
	OVR2 DEC2 OVR2 ,get-pixel JSR #30 SFT STH ADDr
	STHr
	DUP #03 = ,&remove JCN
	DUP #06 = ,&remove JCN
	DUP #09 = ,&remove JCN
	DUP #0c = ,&remove JCN
	POP POP2 POP2 RTN
	&remove
		POP ;remove-pixel JSR2 RTN 

RTN

@get-pixel ( x* y* -- b )

	,within-canvas JSR ,&continue JCN
		POP2 POP2 #01 RTN
		&continue

	( keep x* ) OVR2 NIP 8MOD STH
	( get byte ) ,get-row JSR LDA
	( flag ) #07 STHr SUB SFT #01 AND

RTN

@add-pixel ( x* y* -- )
	
	,within-canvas JSR ,&continue JCN
		POP2 POP2 RTN
		&continue

	( keep x* ) OVR2 NIP 8MOD STH
	( get byte ) ,get-row JSR LDAk
	( mask ) #01 #07 STHr SUB SFL ORA
	( save ) ROT ROT STA

RTN

@remove-pixel ( x* y* -- )

	,within-canvas JSR ,&continue JCN
		POP2 POP2 RTN
		&continue

	( keep x* ) OVR2 NIP 8MOD STH 
	( get byte ) ,get-row JSR LDAk
	( mask ) #01 #07 STHr SUB SFL #ff EOR AND
	( save ) ROT ROT STA

RTN

@within-canvas ( x* y* -- x* y* flag )

	( clamp ) DUP2 8// .canvas/h LDZ2 << #00 = ,&skip JCN
	( clamp ) OVR2 8// .canvas/w LDZ2 << #00 = ,&skip JCN
	#01 RTN
	&skip
		#00 RTN

RTN

@get-row ( x* y* -- row* )

	STH2k 
		8// SWP2 
		8// SWP2 
		.canvas/w LDZ2 ** ++ 8** 
	STH2r 
		8MOD2 ++ 
	.anim/addr LDZ2 ++

RTN

( file )

@new-file ( path* -- )

	#2c ;draw-filepath JSR2 

	STH2
	#0c #00
	&loop
		DUP DUP TOS STH2kr ++ LDA
		SWP .path/name + STZ
		INC GTHk ,&loop JCN
	( end ) #00 SWP .path/name + STZ 
	.path/length STZ 
	POP2r
	RESET-STATE
	;clear JSR2

RTN

@load-file ( path* -- )

	;path/name .File/name DEO2 
	GET-ANIM-SIZE .File/length DEO2 
	;data .File/load DEO2

	RESET-STATE

	;draw-canvas JSR2
	;draw-filepane/state JSR2

RTN

@save-file ( path* -- )

	.File/name DEO2 
	GET-ANIM-SIZE .File/length DEO2 
	;data .File/save DEO2

	RESET-STATE
	;draw-filepane/state JSR2

RTN

( theme )

@theme-txt ".theme $1

@load-theme ( -- )

	;theme-txt .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

( Drawing )

@redraw ( -- )
	
	;draw-background JSR2
	,draw-canvas JSR
		
RTN

@draw-canvas-outside ( -- )

	( frame )
	.canvas #01 ;line-rect JSR2

	,draw-canvas JSR

RTN

@draw-canvas ( -- )

	.zoom/active LDZ ,draw-canvas-zoom JCN
	.anim/limit LDZ #00 = ,&no-onion JCN
	.anim/onion LDZ ,&no-onion JCN
		( onionskin ) ;get-last-frame JSR2 #0b ,draw-canvas-body JSR
		( drawing ) .anim/addr LDZ2 #05 ,draw-canvas-body JSR
		RTN
		&no-onion
	( drawing ) .anim/addr LDZ2 #09 ,draw-canvas-body JSR

RTN

@draw-canvas-body ( addr* color -- )
	
	STH
	.Screen/addr DEO2
	.canvas/y2 LDZ2 .canvas/y1 LDZ2
	&ver
		DUP2 .Screen/y DEO2
		.canvas/x2 LDZ2 .canvas/x1 LDZ2
		&hor
			DUP2 .Screen/x DEO2
			STHkr .Screen/sprite DEO
			.Screen/addr DEI2 #0008 ++ .Screen/addr DEO2 
			#0008 ++ GTH2k ,&hor JCN
		POP2 POP2
		#0008 ++ GTH2k ,&ver JCN
	POP2 POP2
	POPr

	( resizer )
	.state/presentation LDZ ,&no-edge JCN
		;edge-icn .Screen/addr DEO2
		#05 .Screen/sprite DEO 
		&no-edge

RTN

@draw-canvas-zoom ( -- )

	.canvas/y1 LDZ2 .Screen/y DEO2
	.canvas/h LDZ2 #0000
	&ver
		( save ) STH2k
		.canvas/x1 LDZ2 .Screen/x DEO2
		.canvas/w LDZ2 #0000
		&hor
			DUP2 .zoom/x LDZ2 ++ 
			STH2kr .zoom/y LDZ2 ++ 
				;get-pixel JSR2 TOS 8** 
				;bigpixel-icns ++ .Screen/addr DEO2
			( draw ) #09 .Screen/sprite DEO
			.Screen/x DEI2 #0008 ++ .Screen/x DEO2
			INC2 GTH2k ,&hor JCN
		POP2 POP2
		POP2r
		.Screen/y DEI2 #0008 ++ .Screen/y DEO2
		INC2 GTH2k ,&ver JCN
	POP2 POP2

RTN

@draw-tile ( x* y* -- )

	;within-viewport JSR2 ,&continue JCN
		POP2 POP2 RTN
		&continue
	.zoom/active LDZ #00 = ,&no-zoom JCN
		OVR2 OVR2 ;get-pixel JSR2 #00 SWP 8** ;bigpixel-icns ++ .Screen/addr DEO2
		.zoom/y LDZ2 -- 8** .canvas/y1 LDZ2 ++ #22 SFT2 .Screen/y DEO2
		.zoom/x LDZ2 -- 8** .canvas/x1 LDZ2 ++ #22 SFT2 .Screen/x DEO2
		#09 .Screen/sprite DEO
		RTN
		&no-zoom
	( TODO: Draw onionskin )
	#33 SFT2 SWP2 #33 SFT2 SWP2
	OVR2 OVR2 ;get-row JSR2 .Screen/addr DEO2
	.canvas/y1 LDZ2 ++ .Screen/y DEO2
	.canvas/x1 LDZ2 ++ .Screen/x DEO2
	#09 .Screen/sprite DEO

RTN

@within-viewport ( x* y* -- x* y* flag )

	.zoom/active LDZ #00 = ,&no-zoom JCN
		OVR2 .zoom/x LDZ2 -- .canvas/w LDZ2 << ,&within-y-zoom JCN [ #00 RTN ] &within-y-zoom
		DUP2 .zoom/y LDZ2 -- .canvas/h LDZ2 << ,&within-x-zoom JCN [ #00 RTN ] &within-x-zoom
		#01 RTN
		&no-zoom
	DUP2 .canvas/h LDZ2 8** << ,&within-y JCN [ #00 RTN ] &within-y
	OVR2 .canvas/w LDZ2 8** << ,&within-x JCN [ #00 RTN ] &within-x
		#01 RTN

RTN

@draw-cursor ( -- )

	( clear brush size )
	.cursor/x2 LDZ2
	.cursor/y2 LDZ2
	( clear size cursor )
	OVR2 OVR2
	#0003 -- .Screen/y DEO2 
	#0003 -- .Screen/x DEO2 
	#40 .Screen/sprite DEO
	( clear last cursor )
	#40 ;draw-pointer JSR2

	( record cursor positions )
	.Mouse/x DEI2 .cursor/x2 STZ2 
	.Mouse/y DEI2 .cursor/y2 STZ2

	( draw size cursor )
	.brush/tool LDZ DUP #01 = SWP #03 = #0000 == ;&outside-canvas JCN2
	( outside canvas )
	.Mouse/x DEI2 .Mouse/y DEI2 .canvas ;within-rect JSR2 #00 = ;&outside-canvas JCN2
		.cursor/x2 LDZ2 #0003 -- .Screen/x DEO2 
		.cursor/y2 LDZ2 #0003 -- .Screen/y DEO2
		;brush-icns #00 .brush/size LDZ 8** ++ .Screen/addr DEO2
		.Mouse/state DEI DUP 2* #41 ADD .Screen/sprite DEO
		#00 = ,&outside-canvas JCN RTN
	&outside-canvas

	( draw new cursor )
	.cursor/x2 LDZ2 
	.cursor/y2 LDZ2 
	#4143 .Mouse/state DEI SWP? POP 
		,draw-pointer JSR

RTN

@draw-pointer ( x* y* color -- )
	
	STH 
	.Screen/y DEO2 
	.Screen/x DEO2 
	;pointers-icns .Screen/addr DEO2
	STHr .Screen/sprite DEO

RTN

@draw-toolpane ( -- )

	( frame ) .toolpane #01 ;line-rect JSR2

	.toolpane/x1 LDZ2 2++ .Screen/x DEO2 
	.toolpane/y1 LDZ2 2++ .Screen/y DEO2 
	;tool-icns .Screen/addr DEO2

	( stash selection ) .brush/tool LDZ STH
	#06 #00
	&loop
		DUP STHkr = INC .Screen/sprite DEO
		.Screen/x DEI2 #0008 ++ .Screen/x DEO2
		.Screen/addr DEI2 #0008 ++ .Screen/addr DEO2
		INC GTHk ,&loop JCN
	POP2 
	.zoom/active LDZ #01 ! ,&no-zoom JCN
		.Screen/x DEI2 #0008 -- .Screen/x DEO2
		;tool-icns #0030 ++ .Screen/addr DEO2
		#05 STHkr = INC .Screen/sprite DEO
		&no-zoom
	( pop selection ) POPr

RTN

@draw-sizepane ( -- )

	( frame )
	.sizepane #01 ;line-rect JSR2

	.sizepane/x1 LDZ2 2++ .Screen/x DEO2 
	.sizepane/y1 LDZ2 2++ .Screen/y DEO2 
	;size-icns .brush/mode LDZ 40* TOS ++ .Screen/addr DEO2

	( stash selection ) .brush/size LDZ STH
	#08 #00 
	&loop
		DUP STHkr = INC .Screen/sprite DEO
		.Screen/x DEI2 #0008 ++ .Screen/x DEO2
		.Screen/addr DEI2 #0008 ++ .Screen/addr DEO2
		INC GTHk ,&loop JCN
	POP2
	( pop selection ) POPr

RTN

@draw-pattpane ( -- )

	( frame ) 	
	.pattpane #01 ;line-rect JSR2

	.pattpane/x1 LDZ2 2++ .Screen/x DEO2 
	.pattpane/y1 LDZ2 2++ .Screen/y DEO2 
	;patt-icns .Screen/addr DEO2

	( stash selection ) .brush/patt LDZ STH
	#08 #00 
	&loop
		DUP STHkr = INC .Screen/sprite DEO
		.Screen/x DEI2 #0008 ++ .Screen/x DEO2
		.Screen/addr DEI2 #0008 ++ .Screen/addr DEO2
		INC GTHk ,&loop JCN
	POP2
	( pop selection ) POPr

RTN

@draw-animpane ( -- )

	.animpane/y1 LDZ2 .Screen/y DEO2 

	.animpane/x1 LDZ2 STH2k .Screen/x DEO2 
	;eyes-icns .anim/onion LDZ TOS 8** ++ .Screen/addr DEO2
	#01 .Screen/sprite DEO

	STH2kr #0010 ++ .Screen/x DEO2 
	.anim/frame LDZ ;print/parse JSR2 .Screen/addr DEO2
	#02 .Screen/sprite DEO

	STH2r #0018 ++ .Screen/x DEO2 
	.anim/limit LDZ ;print/parse JSR2 .Screen/addr DEO2
	#08 .Screen/sprite DEO

RTN

@draw-filepane ( -- )

	( file i/o )
	.filepane/x1 LDZ2 .Screen/x DEO2
	.filepane/y1 LDZ2 .Screen/y DEO2
	;make-icn .Screen/addr DEO2
	#01 .Screen/sprite DEO
	.Screen/x DEI2 #0008 ++ .Screen/x DEO2
	;load-icn .Screen/addr DEO2
	#01 .Screen/sprite DEO
	.Screen/x DEI2 #0010 ++ .Screen/x DEO2

	&state ( -- )
		.filepane/x1 LDZ2 #0018 ++ .Screen/x DEO2
		.filepane/y1 LDZ2 .Screen/y DEO2
		;save-icn .Screen/addr DEO2
		#0a05 .state/changed LDZ SWP? POP .Screen/sprite DEO

RTN

@draw-namepane ( -- )
	
	( bottom )
	.namepane/x1 LDZ2 .Screen/x DEO2
	.namepane/y1 LDZ2 .Screen/y DEO2
	#01 ;draw-filepath JSR2
	&clear
		#00 .Screen/sprite DEO
		.Screen/x DEI2 #0008 ++ DUP2 .Screen/x DEO2
		.namepane/x2 LDZ2 << ,&clear JCN

RTN

@draw-background ( -- )
	
	;draw-filepane/state JSR2
	;draw-animpane JSR2
	;draw-filepane JSR2

	.state/presentation LDZ #00 = #01 JCN [ RTN ]

	( draw hor line )
	#0000 
	.Screen/width DEI2 
	.center/y LDZ2 
	.state/presentation LDZ #00 = 
		;line-hor JSR2

	( draw ver line )
	.center/x LDZ2 
	#0000 
	.Screen/height DEI2 
	.state/presentation LDZ #00 = 
		;line-ver JSR2

	;draw-toolpane JSR2
	;draw-pattpane JSR2
	;draw-sizepane JSR2
	;draw-namepane JSR2

RTN

@draw-filepath ( color -- )
	
	STH
	#0038 .Screen/x DEO2
	.Screen/height DEI2 #0010 -- .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 ,&loop JCN
	POP2
	POPr
	( clear last ) 
	#00 .Screen/sprite DEO

RTN

( Generics )

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

	&byte ( byte color -- )
		STH DUP 
			#04 SFT ,&parse JSR .Screen/addr DEO2
			STHkr .Screen/sprite DEO
		.Screen/x DEI2 #0008 ++ .Screen/x DEO2
			#0f AND ,&parse JSR .Screen/addr DEO2
			STHr .Screen/sprite DEO
	RTN

	&parse ( value -- addr* )
		DUP #09 GTH ,&above JCN 
			#30 ADD #20 - TOS 8** ;font ++ RTN 
		&above 
			#09 SUB #40 ADD #20 - TOS 8** ;font ++ RTN

RTN

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

RTN

@line-hor ( x0* x1* y* color -- )
	
	STH .Screen/y DEO2 SWP2
	&loop
		( save ) DUP2 .Screen/x DEO2
		( draw ) STHkr .Screen/pixel DEO
		2++ 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
		2++ GTH2k ,&loop JCN
	POP2 POP2 POPr

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

@fill-rect ( rect color -- )
	
	STH STH
	( y2 ) STHkr #06 + LDZ2 2++
	( y1 ) STHkr #02 + LDZ2 #0001 --
	&ver
		( save ) DUP2 .Screen/y DEO2
		( x2 ) STHkr #04 + LDZ2 2++
		( x1 ) STHkr LDZ2 #0001 --
		&hor
			( save ) DUP2 .Screen/x DEO2
			( draw ) OVRr STHr .Screen/pixel DEO
			INC2 GTH2k ,&hor JCN
		POP2 POP2 
		INC2 GTH2k ,&ver JCN
	POP2 POP2 
	POPr POPr

RTN

@print-hex ( value* -- )
	
	&short ( value* -- )
		SWP ,&echo JSR 
	&byte ( value -- )
		,&echo JSR
	RTN

	&echo ( 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 + RTN &above #09 - #60 + RTN

RTN

( Tools )

@tools
	:pencil/hover  :pencil/up  :pencil/down  :pencil/drag
	:marker/hover  :marker/up  :marker/down  :marker/drag
	:stroke/hover  :stroke/up  :stroke/down  :stroke/drag
	:magic/hover   :magic/up   :magic/down   :magic/drag
	:box/hover     :box/up     :box/down     :box/drag
	:magnify/hover :magnify/up :magnify/down :magnify/drag

@pencil ( x* y* -> )

	&down
		OVR2 OVR2 .cursor/y1 STZ2 .cursor/x1 STZ2
		OVR2 OVR2 .brush/oper LDZ2 JSR2
	&up
		;draw-tile JSR2
		SET-STATE
		BRK
	&drag
		OVR2 OVR2 STH2 STH2
		.cursor/x1 LDZ2 .cursor/y1 LDZ2 STH2r STH2r 
		;paint-line JSR2
		.cursor/y1 STZ2 .cursor/x1 STZ2
		BRK
	&hover 
		
BRK

@marker ( x* y* -> )

	&up
		POP2 POP2 
		SET-STATE BRK
	&down
	&drag
		#0003 -- SWP2 #0003 -- SWP2 
		;paint-brush JSR2 
		BRK
	&hover 
		
BRK

@stroke ( x* y* -> )

	&down
		.cursor/y1 STZ2 .cursor/x1 STZ2
		BRK
	&up
		STH2 STH2
		.cursor/x1 LDZ2 .cursor/y1 LDZ2 STH2r STH2r 
		;paint-line JSR2
		SET-STATE
		BRK
	&drag
		POP2 POP2
		BRK
	&hover 
		
BRK

@magic ( x* y* -> )
	
	&up
		SET-STATE
	&down
	&drag
		#0003 -- SWP2 #0003 -- SWP2 
		;paint-magic JSR2 
		BRK
	&hover 
		
BRK

@box ( x* y* -> )
	
	&down
		( clear cursor ) .cursor/x2 LDZ2 .cursor/y2 LDZ2 #40 ;draw-pointer JSR2
		( erase guide ) .guide #40 ;line-rect JSR2
		.cursor/y1 STZ2 .cursor/x1 STZ2
		BRK
	&up
		( clear cursor ) .cursor/x2 LDZ2 .cursor/y2 LDZ2 #40 ;draw-pointer JSR2
		( erase guide ) .guide #40 ;line-rect JSR2
		.cursor/y2 STZ2 .cursor/x2 STZ2
		;fill-selection JSR2
		SET-STATE
		BRK
	&drag
		( clear cursor ) .cursor/x2 LDZ2 .cursor/y2 LDZ2 #40 ;draw-pointer JSR2
		( erase guide ) .guide #40 ;line-rect JSR2
		.cursor/y2 STZ2 .cursor/x2 STZ2
		( update guide )
		.cursor/x1 LDZ2 .cursor/y1 LDZ2 ,to-canvas JSR INC2 .guide/y1 STZ2 INC2 .guide/x1 STZ2
		.cursor/x2 LDZ2 .cursor/y2 LDZ2 ,to-canvas JSR .guide/y2 STZ2 .guide/x2 STZ2
		( draw guide ) .guide #43 ;line-rect JSR2
		BRK
	&hover 
		
BRK

@to-canvas ( x* y* -- x* y* )
	
	.zoom/active LDZ ,&on-zoom JCN
		.canvas/y1 LDZ2 ++ SWP2 
		.canvas/x1 LDZ2 ++ SWP2 
	RTN

	&on-zoom
		.zoom/y LDZ2 -- 8** .canvas/y1 LDZ2 ++ SWP2 
		.zoom/x LDZ2 -- 8** .canvas/x1 LDZ2 ++ SWP2

RTN

@magnify ( x* y* -> )

	&down
		.zoom/active TOGGLE
		.canvas/h LDZ2 2// -- .zoom/y STZ2 
		.canvas/w LDZ2 2// -- .zoom/x STZ2
		;redraw JSR2 
		BRK
	&up
	&drag
		POP2 POP2 
		BRK
	&hover 
		
BRK

@save-icn      1054 28c6 2854 1000 
@make-icn      fe82 8282 848a f400 
@load-icn      feaa d6aa d4aa f400 
@edge-icn      0000 0000 0103 070f
@drag-icn      1010 10ee 1010 1000
@untitled-txt  "untitled.bit $1

@bigpixel-icns
	5580 0080 0080 0080 
	55ff 7fff 7fff 7fff 
@size-icns
	0000 0010 0000 0000
	0000 1038 1000 0000
	0000 3838 3800 0000
	0010 387c 3810 0000
	0038 7c7c 7c38 0000
	1038 7cfe 7c38 1000
	387c fefe fe7c 3800
	7cfe fefe fefe 7c00
	0000 0000 0000 0000
	0000 0010 0000 0000
	0000 1028 1000 0000
	0010 2844 2810 0000
	0038 4444 4438 0000
	1028 4482 4428 1000
	3844 8282 8244 3800
	7c82 8282 8282 7c00
@anim-icns
	0000 1838 7838 1800
	0000 181c 1e1c 1800
@eyes-icns
	0038 4492 2810 0000
	0000 0082 4438 0000
@patt-icns
	fefe fefe fefe fe00
	fed6 aad6 aad6 fe00
	fe92 82d6 8292 fe00
	fe82 92aa 9282 fe00
	fea6 ca92 a6ca fe00
	feca a692 caa6 fe00
	feaa aaaa aaaa fe00	
	fe82 fe82 fe82 fe00
@tool-icns
	c0e0 5028 140a 0400
	e0d0 a844 2212 0c00
	c0b8 4848 7804 0200
	a850 8850 a804 0200
	fc84 8480 84ea 0400
	3048 8484 4834 0200 ( zoom )
	3245 8284 4834 0200 ( zoom out )
@brush-icns
	0000 0010 0000 0000
	0000 1028 1000 0000
	0000 3828 3800 0000
	0010 2844 2810 0000
	0038 4444 4438 0000
	1028 4482 4428 1000
	3844 8282 8244 3800
	7c82 8282 8282 7c00
	7cfe fefe fefe 7c00
@pointers-icns
	80c0 e0f0 f8e0 1000
	2020 20b8 7c7c 3838 
@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

@data