-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.aspl
1837 lines (1797 loc) · 126 KB
/
main.aspl
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 graphics
import math
import math.geometry
import os
import time
import io
import libellus.utils
import libellus.dialogs
import json
import time
var int UNDO_REDO_LIMIT = 30
AppState:init()
var Window window = new Window("Libellus", 500, 500)
if(!io.exists_directory(Booklet:getBookletsPath())){
io.create_directory(Booklet:getBookletsPath())
}
var bookletName = "Libellus"
if(!io.exists_file(io.join_path([Booklet:getBookletsPath(), "current.txt"]))){
io.write_file(io.join_path([Booklet:getBookletsPath(), "current.txt"]), bookletName)
}else{
bookletName = io.read_file(io.join_path([Booklet:getBookletsPath(), "current.txt"]))
}
var Booklet booklet = new Booklet("Notes", Color:fromARGB(255, 249, 186, 5), new LazyChunkedCanvas(window.width - get_booklet_list_width(window), window.height - get_toolbar_height(window), io.join_path([Booklet:getBookletsPath(), "Notes", "chunks"])), [], 0, 0)
var hasAnySelected = false
var bookletFolders = utils.sort_alphabetically(io.directories(Booklet:getBookletsPath()))
{
var int i = 0
foreach(bookletFolders as folder){
if(i > 0 && folder == "Libellus"){
bookletFolders.removeAt(i)
bookletFolders.insert(0, folder)
}
i++
}
}
foreach(bookletFolders as folder){
if(!io.exists_directory(folder) && io.exists_directory(io.join_path([Booklet:getBookletsPath(), folder]))){
folder = io.join_path([Booklet:getBookletsPath(), folder])
}
if(io.exists_directory(folder)){
if(!io.exists_file(io.join_path([folder, "booklet.json"]))){
continue
}
if(!io.exists_directory(io.join_path([folder, "chunks"]))){
io.create_directory(io.join_path([folder, "chunks"]))
}
var data = json.decode(io.read_file(io.join_path([folder, "booklet.json"])))
var c = new LazyChunkedCanvas(window.width - get_booklet_list_width(window), window.height - get_toolbar_height(window), io.join_path([folder, "chunks"]))
var list<string> sheetFiles = []
if(map<string, any>(data).containsKey("sheets")){
foreach(list<any>(map<string, any>(data)["sheets"]) as sheet){
sheetFiles.add(string(sheet))
}
}
var scrollX = 0
var scrollY = 0
if(map<string, any>(data).containsKey("scroll")){
scrollX = int(map<string, any>(map<string, any>(data)["scroll"])["x"])
scrollY = int(map<string, any>(map<string, any>(data)["scroll"])["y"])
}
var Booklet b = new Booklet(io.directory_name(folder), Color:fromARGB(255, byte(map<string, any>(map<string, any>(data)["color"])["r"]), byte(map<string, any>(map<string, any>(data)["color"])["g"]), byte(map<string, any>(map<string, any>(data)["color"])["b"])), c, sheetFiles, scrollX, scrollY)
if(b.name == bookletName){
booklet = b
booklet.prepare()
hasAnySelected = true
AppState:scrollX = scrollX
AppState:scrollY = scrollY
}
AppState:booklets.add(b)
}
}
if(AppState:booklets.length == 0 || !hasAnySelected){
AppState:booklets.add(booklet)
if(!io.exists_directory(io.join_path([Booklet:getBookletsPath(), booklet.name, "chunks"]))){
if(!io.exists_directory(io.join_path([Booklet:getBookletsPath(), booklet.name]))){
io.create_directory(io.join_path([Booklet:getBookletsPath(), booklet.name]))
}
io.create_directory(io.join_path([Booklet:getBookletsPath(), booklet.name, "chunks"]))
}
if(!io.exists_directory(io.join_path([Booklet:getBookletsPath(), booklet.name, "sheets"]))){
io.create_directory(io.join_path([Booklet:getBookletsPath(), booklet.name, "sheets"]))
}
booklet.save() // create an .lci file
booklet.prepare()
io.write_file(io.join_path([Booklet:getBookletsPath(), "current.txt"]), booklet.name)
}
if(io.exists_file(io.join_path([Booklet:getBookletsPath(), "pens.json"]))){
AppState:pens = Pen:jsonDecodePens(io.read_file(io.join_path([Booklet:getBookletsPath(), "pens.json"])))
}
AppState:pen = AppState:pens[0]
var circularEraser = new CircularEraser(30)
var pathEraser = new PathEraser()
var list<Canvas> penIcons = []
var penIconDataLight = $embed("icons/pen_light.png")
var penIconDataDark = $embed("icons/pen_dark.png")
var circularEraserIcon = new Canvas(1, 1)
var pathEraserIcon = new Canvas(1, 1)
var themeSwitchIcon = new Canvas(1, 1)
var gridToggleIcon = new Canvas(1, 1)
var undoIconOriginal = Canvas:fromFileData($embed("icons/undo.png"))
var redoIconOriginal = Canvas:fromFileData($embed("icons/redo.png"))
var undoIcon = new Canvas(1, 1)
var redoIcon = new Canvas(1, 1)
var imageImporterIcon = new Canvas(1, 1)
var folderIcon = new Canvas(1, 1)
var imageFileIcon = new Canvas(1, 1)
var bookletIcon = new Canvas(1, 1)
var newBookletIcon = new Canvas(1, 1)
var newSheetIconOriginal = Canvas:fromFileData($embed("icons/new_sheet.png"))
var sheetCollectionHeight = 0
var list<Rectangle> sheetCollectionBounds = []
var sheetImporterHeight = 0
var hasInitializedSheetCollectionScroll = false
// TODO: The below lists should be of type ImageDiff (or LazyChunkedCanvas|Canvas with the $legacyUndoRedo flag), but due to a bug in ASPL, this is currently not possible
var list<any> undoList = []
var list<any> redoList = []
if(io.exists_file(io.join_path([Booklet:getBookletsPath(), "theme.txt"]))){
var t = io.read_file(io.join_path([Booklet:getBookletsPath(), "theme.txt"]))
if(t == "light"){
AppState:theme = Theme.Light
}elseif(t == "dark"){
AppState:theme = Theme.Dark
}
}else{
io.write_file(io.join_path([Booklet:getBookletsPath(), "theme.txt"]), "light")
}
if(io.exists_file(io.join_path([Booklet:getBookletsPath(), "grid.txt"]))){
AppState:gridVisible = io.read_file(io.join_path([Booklet:getBookletsPath(), "grid.txt"])) == "true"
}else{
io.write_file(io.join_path([Booklet:getBookletsPath(), "grid.txt"]), "false")
}
var hasChanged = false
var isMouseDown = false
var sheetImporterPath = ""
var hasPushedUndoDiff = false
var long? pointerDownForDrawingId = null
var cumulativePointerMovementAfterDownX = 0f
var cumulativePointerMovementAfterDownY = 0f
window.onMouseDown = callback(Point position, MouseButton button) {
if(position.x > get_booklet_list_width(window) && position.y > get_toolbar_height(window)){
if (button == MouseButton.Left){
isMouseDown = true
if(AppState:dialog != null){
if(AppState:dialog?!.containsPoint(position)){
AppState:dialog?!.onPointerDown(position)
}
}elseif(AppState:zone == Zone.Canvas || AppState:zone == Zone.Sheet){
if(undoList.length == 0){
CacheUtils:hasUndoAvailabilityChanged = true
}
if(AppState:zone == Zone.Canvas){
$if legacyUndoRedo{
if(booklet.canvas.width * booklet.canvas.height > 5000000){
// TODO: Remove this workaround when the undo/redo system doesn't store the entire canvas in memory
undoList.clear()
}
undoList.add(booklet.canvas.copy())
}$else{
if(AppState:pen == pathEraser){
var pixel = booklet.canvas.getPixel(int((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY))
if(!(pixel.a == 0b && pixel.r == 0b && pixel.g == 0b && pixel.b == 0b)){
undoList.add(new ImageDiff())
hasPushedUndoDiff = true
}else{
hasPushedUndoDiff = false
}
}else{
undoList.add(new ImageDiff())
hasPushedUndoDiff = true
}
}
}elseif(AppState:zone == Zone.Sheet){
$if legacyUndoRedo{
undoList.add(Canvas(Sheet(booklet.currentSheet).canvas).copy())
}$else{
if(AppState:pen == pathEraser){
var pixel = Canvas(Sheet(booklet.currentSheet).canvas).getPixel(int((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY))
if(!(pixel.a == 0b && pixel.r == 0b && pixel.g == 0b && pixel.b == 0b)){
undoList.add(new ImageDiff())
hasPushedUndoDiff = true
}else{
hasPushedUndoDiff = false
}
}else{
undoList.add(new ImageDiff())
hasPushedUndoDiff = true
}
}
}
if(undoList.length > UNDO_REDO_LIMIT){
undoList.removeAt(0)
}
if(hasPushedUndoDiff && redoList.length > 0){
redoList.clear()
CacheUtils:hasRedoAvailabilityChanged = true
}
if(AppState:pen == circularEraser){
if(AppState:zone == Zone.Canvas){
$if !legacyUndoRedo{
ImageDiff(undoList[undoList.length - 1]).trackFillCircleRectangular(new Ellipse(new Point((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX , (position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), new Size(float(CircularEraser(AppState:pen).thickness / 2), float(CircularEraser(AppState:pen).thickness / 2))), booklet.canvas)
}
booklet.canvas.fillCircle(new Ellipse(new Point((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX , (position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), new Size(float(CircularEraser(AppState:pen).thickness / 2), float(CircularEraser(AppState:pen).thickness / 2))), Color:fromARGB(0, 0, 0, 0), false)
}elseif(AppState:zone == Zone.Sheet){
$if !legacyUndoRedo{
ImageDiff(undoList[undoList.length - 1]).trackFillCircleRectangular(new Ellipse(new Point((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX , (position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), new Size(float(CircularEraser(AppState:pen).thickness / 2), float(CircularEraser(AppState:pen).thickness / 2))), Canvas(Sheet(booklet.currentSheet).canvas))
}
Canvas(Sheet(booklet.currentSheet).canvas).fillCircle(new Ellipse(new Point((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX , (position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), new Size(float(CircularEraser(AppState:pen).thickness / 2), float(CircularEraser(AppState:pen).thickness / 2))), Color:fromARGB(0, 0, 0, 0), false)
}
}elseif(AppState:pen == pathEraser){
if(AppState:zone == Zone.Canvas){
var pixel = booklet.canvas.getPixel(int((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY))
if(!(pixel.a == 0b && pixel.r == 0b && pixel.g == 0b && pixel.b == 0b)){
utils.clear_path(booklet.canvas, int((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), ImageDiff(undoList[undoList.length - 1]))
}
}elseif(AppState:zone == Zone.Sheet){
var pixel = Canvas(Sheet(booklet.currentSheet).canvas).getPixel(int((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY))
if(!(pixel.a == 0b && pixel.r == 0b && pixel.g == 0b && pixel.b == 0b)){
utils.clear_path(Canvas(Sheet(booklet.currentSheet).canvas), int((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), ImageDiff(undoList[undoList.length - 1]))
}
}
}else{
if(AppState:zone == Zone.Canvas){
pointerDownForDrawingId = 0
// automatically resize canvas when drawing near the edge
if((booklet.canvas.width < ((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX + Pen(AppState:pen).thickness + 300)) || (booklet.canvas.height < ((position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY + Pen(AppState:pen).thickness + 300))){
booklet.canvas.extendTo(int(math.max(double(booklet.canvas.width), double((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX + Pen(AppState:pen).thickness + 300))), int(math.max(double(booklet.canvas.height), double((position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY + Pen(AppState:pen).thickness + 300))))
}
}elseif(AppState:zone == Zone.Sheet){
pointerDownForDrawingId = 0
}
}
CacheUtils:hasChanged = true
CacheUtils:hasChangedForOnPaint = true
if(booklet.currentSheet != null){
Sheet(booklet.currentSheet).hasChanged = true
}
}
}
}
Dialog:pointerDownPosition = position
Dialog:pointerDownTimestamp = time.now()
}
var currentMousePosition = new Point(-1, -1)
window.onMouseMove = callback(Point position, float deltaX, float deltaY) {
if(Dialog:isInDownAction){
return
}
currentMousePosition = position
if(position.x > get_booklet_list_width(window) && position.y > get_toolbar_height(window)){
if (isMouseDown) {
if(AppState:dialog != null){
if(AppState:dialog?!.containsPoint(position) && AppState:dialog?!.containsPoint(new Point(position.x - deltaX, position.y - deltaY))){
AppState:dialog?!.onPointerMove(position, new Point(position.x - deltaX, position.y - deltaY))
}
}elseif(AppState:zone == Zone.Canvas || AppState:zone == Zone.Sheet){
if(AppState:pen == circularEraser){
if(AppState:zone == Zone.Canvas){
$if !legacyUndoRedo{
ImageDiff(undoList[undoList.length - 1]).trackFillCircleRectangular(new Ellipse(new Point((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX , (position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), new Size(float(CircularEraser(AppState:pen).thickness / 2), float(CircularEraser(AppState:pen).thickness / 2))), booklet.canvas)
}
booklet.canvas.fillCircle(new Ellipse(new Point((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX, (position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), new Size(float(CircularEraser(AppState:pen).thickness / 2), float(CircularEraser(AppState:pen).thickness / 2))), Color:fromARGB(0, 0, 0, 0), false)
}elseif(AppState:zone == Zone.Sheet){
$if !legacyUndoRedo{
ImageDiff(undoList[undoList.length - 1]).trackFillCircleRectangular(new Ellipse(new Point((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX , (position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), new Size(float(CircularEraser(AppState:pen).thickness / 2), float(CircularEraser(AppState:pen).thickness / 2))), Canvas(Sheet(booklet.currentSheet).canvas))
}
Canvas(Sheet(booklet.currentSheet).canvas).fillCircle(new Ellipse(new Point((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX, (position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), new Size(float(CircularEraser(AppState:pen).thickness / 2), float(CircularEraser(AppState:pen).thickness / 2))), Color:fromARGB(0, 0, 0, 0), false)
}
}elseif(AppState:pen == pathEraser){
if(AppState:zone == Zone.Canvas){
var pixel = booklet.canvas.getPixel(int((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY))
if(!(pixel.a == 0b && pixel.r == 0b && pixel.g == 0b && pixel.b == 0b)){
$if !legacyUndoRedo{
if(!hasPushedUndoDiff){
undoList.add(new ImageDiff())
hasPushedUndoDiff = true
CacheUtils:hasUndoAvailabilityChanged = true
}
}
utils.clear_path(booklet.canvas, int((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), ImageDiff(undoList[undoList.length - 1]))
}
}elseif(AppState:zone == Zone.Sheet){
var pixel = Canvas(Sheet(booklet.currentSheet).canvas).getPixel(int((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY))
if(!(pixel.a == 0b && pixel.r == 0b && pixel.g == 0b && pixel.b == 0b)){
$if !legacyUndoRedo{
if(!hasPushedUndoDiff){
undoList.add(new ImageDiff())
hasPushedUndoDiff = true
CacheUtils:hasUndoAvailabilityChanged = true
}
}
utils.clear_path(Canvas(Sheet(booklet.currentSheet).canvas), int((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), ImageDiff(undoList[undoList.length - 1]))
}
}
}elseif(AppState:pen oftype Pen){
if(!LinePerfectionUtils:hasPerfected){
if(!LinePerfectionUtils:isPointerDown){
LinePerfectionUtils:pointerDownPosition = new Point(position.x - deltaX, position.y - deltaY)
LinePerfectionUtils:isPointerDown = true
LinePerfectionUtils:cumulativeMovementX = 0
LinePerfectionUtils:cumulativeMovementY = 0
}
LinePerfectionUtils:lastMovementTimestamp = time.now()
LinePerfectionUtils:lastMovementPosition = position
LinePerfectionUtils:cumulativeMovementX += deltaX
LinePerfectionUtils:cumulativeMovementY += deltaY
if(LinePerfectionUtils:cumulativeMovementX >= 3 || LinePerfectionUtils:cumulativeMovementY >= 3){
LinePerfectionUtils:cumulativeMovementX = 0
LinePerfectionUtils:cumulativeMovementY = 0
LinePerfectionUtils:lastMovementTimestamp = time.now()
}
cumulativePointerMovementAfterDownX += float(math.abs(double(deltaX)))
cumulativePointerMovementAfterDownY += float(math.abs(double(deltaY)))
if(AppState:zone == Zone.Canvas){
$if !legacyUndoRedo{
ImageDiff(undoList[undoList.length - 1]).trackDrawLine(int((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX - deltaX / AppState:zoom), int((position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY - deltaY / AppState:zoom), int((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), Pen(AppState:pen).thickness, booklet.canvas)
}
booklet.canvas.drawLine(int((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX - deltaX / AppState:zoom), int((position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY - deltaY / AppState:zoom), int((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), Pen(AppState:pen).color, Pen(AppState:pen).thickness)
// automatically resize canvas when drawing near the edge
if((booklet.canvas.width < ((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX + Pen(AppState:pen).thickness + 300)) || (booklet.canvas.height < ((position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY + Pen(AppState:pen).thickness + 300))){
booklet.canvas.extendTo(int(math.max(double(booklet.canvas.width), double((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX + Pen(AppState:pen).thickness + 300))), int(math.max(double(booklet.canvas.height), double((position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY + Pen(AppState:pen).thickness + 300))))
}
}elseif(AppState:zone == Zone.Sheet){
$if !legacyUndoRedo{
ImageDiff(undoList[undoList.length - 1]).trackDrawLine(int((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX - deltaX / AppState:zoom), int((position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY - deltaY / AppState:zoom), int((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), Pen(AppState:pen).thickness, Canvas(Sheet(booklet.currentSheet).canvas))
}
Canvas(Sheet(booklet.currentSheet).canvas).drawLine(int((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX - deltaX / AppState:zoom), int((position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY - deltaY / AppState:zoom), int((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), Pen(AppState:pen).color, Pen(AppState:pen).thickness)
}
}
}
CacheUtils:hasChanged = true
CacheUtils:hasChangedForOnPaint = true
if(booklet.currentSheet != null){
Sheet(booklet.currentSheet).hasChanged = true
}
}
}
}
}
window.onMouseUp = callback(Point position, MouseButton button) {
if(position.x > get_booklet_list_width(window) && position.y > get_toolbar_height(window)){
if (button == MouseButton.Left) {
isMouseDown = false
if(pointerDownForDrawingId == 0l){
if(cumulativePointerMovementAfterDownX < 5 && cumulativePointerMovementAfterDownY < 5){
if(AppState:zone == Zone.Canvas){
ImageDiff(undoList[undoList.length - 1]).apply(booklet.canvas)
$if !legacyUndoRedo{
ImageDiff(undoList[undoList.length - 1]).trackFillCircleRectangular(new Ellipse(new Point((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX, (position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), new Size(float(Pen(AppState:pen).thickness), float(Pen(AppState:pen).thickness))), booklet.canvas)
}
booklet.canvas.fillCircle(new Ellipse(new Point((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX, (position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), new Size(float(Pen(AppState:pen).thickness), float(Pen(AppState:pen).thickness))), Pen(AppState:pen).color)
}elseif(AppState:zone == Zone.Sheet){
ImageDiff(undoList[undoList.length - 1]).apply(Canvas(Sheet(booklet.currentSheet).canvas))
$if !legacyUndoRedo{
ImageDiff(undoList[undoList.length - 1]).trackFillCircleRectangular(new Ellipse(new Point((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX, (position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), new Size(float(Pen(AppState:pen).thickness), float(Pen(AppState:pen).thickness))), Canvas(Sheet(booklet.currentSheet).canvas))
}
Canvas(Sheet(booklet.currentSheet).canvas).fillCircle(new Ellipse(new Point((position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX, (position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), new Size(float(Pen(AppState:pen).thickness), float(Pen(AppState:pen).thickness))), Pen(AppState:pen).color)
}
}
cumulativePointerMovementAfterDownX = 0
cumulativePointerMovementAfterDownY = 0
pointerDownForDrawingId = null
}
LinePerfectionUtils:isPointerDown = false
LinePerfectionUtils:hasPerfected = false
LinePerfectionUtils:hasPerfectedTwo = false
}
}
if(AppState:dialog != null){
if(!Dialog:isInDownAction && AppState:dialog?!.containsPoint(position)){
AppState:dialog?!.onPointerUp(position)
}
}
Dialog:pointerDownPosition = null
Dialog:pointerDownTimestamp = null
}
window.onMouseClick = callback(Point position, MouseButton button) {
if(AppState:dialog != null){
if(Dialog:isInDownAction){
Dialog:isInDownAction = false
}elseif(!AppState:dialog?!.containsPoint(position)){
AppState:dialog = null
}
return
}elseif(Dialog:isInUpAction){
Dialog:isInUpAction = false
return
}
if (button == MouseButton.Left) {
if(position.y < get_toolbar_height(window)){
var i = 0
foreach(AppState:pens as p){
var penIcon = penIcons[i]
if(position.x > (window.width / float(get_toolbar_items_count()) * (i + 1) + get_booklet_list_width(window) - penIcon.width / 2) && position.x < (window.width / float(get_toolbar_items_count()) * (i + 1) + get_booklet_list_width(window) - penIcon.width / 2) + penIcon.width && position.y > get_toolbar_height(window) / 2f - penIcon.height / 2 && position.y < get_toolbar_height(window) / 2f - penIcon.height / 2 + penIcon.height){
AppState:pen = p
return
}
i++
}
if(position.x > (window.width / float(get_toolbar_items_count()) * (i + 1) + get_booklet_list_width(window) - circularEraserIcon.width / 2) && position.x < (window.width / float(get_toolbar_items_count()) * (i + 1) + get_booklet_list_width(window) - circularEraserIcon.width / 2) + circularEraserIcon.width && position.y > get_toolbar_height(window) / 2f - circularEraserIcon.height / 2 && position.y < get_toolbar_height(window) / 2f - circularEraserIcon.height / 2 + circularEraserIcon.height){
AppState:pen = circularEraser
return
}
i++
if(position.x > (window.width / float(get_toolbar_items_count()) * (i + 1) + get_booklet_list_width(window) - pathEraserIcon.width / 2) && position.x < (window.width / float(get_toolbar_items_count()) * (i + 1) + get_booklet_list_width(window) - pathEraserIcon.width / 2) + pathEraserIcon.width && position.y > get_toolbar_height(window) / 2f - pathEraserIcon.height / 2 && position.y < get_toolbar_height(window) / 2f - pathEraserIcon.height / 2 + pathEraserIcon.height){
AppState:pen = pathEraser
return
}
i++
if(position.x > (window.width / float(get_toolbar_items_count()) * (i + 1) + get_booklet_list_width(window) - undoIcon.width / 2) && position.x < (window.width / float(get_toolbar_items_count()) * (i + 1) + get_booklet_list_width(window) - undoIcon.width / 2) + undoIcon.width && position.y > get_toolbar_height(window) / 2f - undoIcon.height / 2 && position.y < get_toolbar_height(window) / 2f - undoIcon.height / 2 + undoIcon.height){
if(AppState:zone == Zone.Canvas || AppState:zone == Zone.Sheet){
if(undoList.length > 0){
if(redoList.length == 0){
CacheUtils:hasRedoAvailabilityChanged = true
}
if(AppState:zone == Zone.Canvas){
$if legacyUndoRedo{
redoList.add(booklet.canvas.copy())
booklet.canvas = LazyChunkedCanvas(undoList[undoList.length - 1])
}$else{
redoList.add(ImageDiff(undoList[undoList.length - 1]).apply(booklet.canvas))
}
}elseif(AppState:zone == Zone.Sheet){
$if legacyUndoRedo{
redoList.add(Canvas(Sheet(booklet.currentSheet).canvas).copy())
Sheet(booklet.currentSheet).canvas = Canvas(undoList[undoList.length - 1])
}$else{
redoList.add(ImageDiff(undoList[undoList.length - 1]).apply(Canvas(Sheet(booklet.currentSheet).canvas)))
}
}
undoList.removeAt(undoList.length - 1)
if(undoList.length == 0){
CacheUtils:hasUndoAvailabilityChanged = true
}
CacheUtils:hasChanged = true
CacheUtils:hasChangedForOnPaint = true
if(booklet.currentSheet != null){
Sheet(booklet.currentSheet).hasChanged = true
}
}
}
return
}
i++
if(position.x > (window.width / float(get_toolbar_items_count()) * (i + 1) + get_booklet_list_width(window) - redoIcon.width / 2) && position.x < (window.width / float(get_toolbar_items_count()) * (i + 1) + get_booklet_list_width(window) - redoIcon.width / 2) + redoIcon.width && position.y > get_toolbar_height(window) / 2f - redoIcon.height / 2 && position.y < get_toolbar_height(window) / 2f - redoIcon.height / 2 + redoIcon.height){
if(AppState:zone == Zone.Canvas || AppState:zone == Zone.Sheet){
if(redoList.length > 0){
if(undoList.length == 0){
CacheUtils:hasUndoAvailabilityChanged = true
}
if(AppState:zone == Zone.Canvas){
$if legacyUndoRedo{
undoList.add(booklet.canvas.copy())
booklet.canvas = LazyChunkedCanvas(redoList[redoList.length - 1])
}$else{
undoList.add(ImageDiff(redoList[redoList.length - 1]).apply(booklet.canvas))
}
}elseif(AppState:zone == Zone.Sheet){
$if legacyUndoRedo{
undoList.add(Canvas(Sheet(booklet.currentSheet).canvas).copy())
Sheet(booklet.currentSheet).canvas = Canvas(redoList[redoList.length - 1])
}$else{
undoList.add(ImageDiff(redoList[redoList.length - 1]).apply(Canvas(Sheet(booklet.currentSheet).canvas)))
}
}
redoList.removeAt(redoList.length - 1)
if(redoList.length == 0){
CacheUtils:hasRedoAvailabilityChanged = true
}
CacheUtils:hasChanged = true
CacheUtils:hasChangedForOnPaint = true
if(booklet.currentSheet != null){
Sheet(booklet.currentSheet).hasChanged = true
}
}
}
return
}
i++
if(position.x > (window.width / float(get_toolbar_items_count()) * (i + 1) + get_booklet_list_width(window) - imageImporterIcon.width / 2) && position.x < (window.width / float(get_toolbar_items_count()) * (i + 1) + get_booklet_list_width(window) - imageImporterIcon.width / 2) + imageImporterIcon.width && position.y > get_toolbar_height(window) / 2f - imageImporterIcon.height / 2 && position.y < get_toolbar_height(window) / 2f - imageImporterIcon.height / 2 + imageImporterIcon.height){
if(AppState:zone == Zone.SheetCollection){
AppState:zone = Zone.Canvas
AppState:dialog = null
foreach(booklet.sheets as sheet){
if(Sheet(sheet).loaded){
Sheet(sheet).unload(false)
}
}
}else{
AppState:zone = Zone.SheetCollection
AppState:dialog = null
hasInitializedSheetCollectionScroll = false
if(booklet.currentSheet != null){
Sheet(booklet.currentSheet).unload(true)
}
}
if(AppState:zone == Zone.Canvas){
AppState:scrollX = booklet.scrollX
AppState:scrollY = booklet.scrollY
}else{
AppState:scrollX = 0
AppState:scrollY = 0
}
AppState:zoom = 1
if(undoList.length > 0){
undoList.clear()
CacheUtils:hasUndoAvailabilityChanged = true
}
if(redoList.length > 0){
redoList.clear()
CacheUtils:hasRedoAvailabilityChanged = true
}
CacheUtils:hasChangedForOnPaint = true
return
}
i++
if(position.x > (window.width / float(get_toolbar_items_count()) * (i + 1) + get_booklet_list_width(window) - themeSwitchIcon.width / 2) && position.x < (window.width / float(get_toolbar_items_count()) * (i + 1) + get_booklet_list_width(window) - themeSwitchIcon.width / 2) + themeSwitchIcon.width && position.y > get_toolbar_height(window) / 2f - themeSwitchIcon.height / 2 && position.y < get_toolbar_height(window) / 2f - themeSwitchIcon.height / 2 + themeSwitchIcon.height){
if(AppState:theme == Theme.Light){
AppState:theme = Theme.Dark
CacheUtils:hasThemeChanged = true
CacheUtils:hasThemeChangedForOnPaint = true
}else{
AppState:theme = Theme.Light
CacheUtils:hasThemeChanged = true
CacheUtils:hasThemeChangedForOnPaint = true
}
return
}
i++
if(AppState:zone == Zone.Canvas){
if(position.x > (window.width / float(get_toolbar_items_count()) * (i + 1) + get_booklet_list_width(window) - gridToggleIcon.width / 2) && position.x < (window.width / float(get_toolbar_items_count()) * (i + 1) + get_booklet_list_width(window) - gridToggleIcon.width / 2) + gridToggleIcon.width && position.y > get_toolbar_height(window) / 2f - gridToggleIcon.height / 2 && position.y < get_toolbar_height(window) / 2f - gridToggleIcon.height / 2 + gridToggleIcon.height){
if(AppState:zone == Zone.Canvas){
AppState:gridVisible = !AppState:gridVisible
CacheUtils:hasGridVisibilityChanged = true
CacheUtils:hasGridVisibilityChangedForOnPaint = true
}
return
}
}
}elseif(position.x <= get_booklet_list_width(window)){
var font = Font:default.withSize(int(math.max(int(math.sqrt(double(window.width * window.height))) / 30d, 10d))).asUnderlined()
var y = int(graphics.measure_text_size("Booklets", font).height * 3) + get_toolbar_height(window)
font = font.withSize(int(math.max(int(math.sqrt(double(window.width * window.height))) / 70d, 7d)))
var int i = 0
foreach(AppState:booklets as b){
var name = b.name
var bookletFont = font
if(b == booklet){
bookletFont = font.withSize(int(font.size * 1.15))
}
var size = graphics.measure_text_size(name, font) // font is used here instead of bookletFont because the space between all the booklets should be the same
var height = int(size.height)
if(position.y >= y - height * 1.5 && position.y <= y){
booklet.unprepare()
booklet = b
booklet.prepare()
if(undoList.length > 0){
undoList.clear()
CacheUtils:hasUndoAvailabilityChanged = true
}
if(redoList.length > 0){
redoList.clear()
CacheUtils:hasRedoAvailabilityChanged = true
}
AppState:scrollX = b.scrollX
AppState:scrollY = b.scrollY
AppState:zoom = 1
AppState:zone = Zone.Canvas
AppState:dialog = null
CacheUtils:hasChanged = true
CacheUtils:hasChangedForOnPaint = true
CacheUtils:hasSizeChanged = true
CacheUtils:hasSizeChangedForOnPaint = true
}
y += int(bookletIcon.height * 1.2)
i++
}
var size = graphics.measure_text_size("/", font)
if(position.y >= y - size.height * 1.5 && position.y <= y + size.height * 1.5){
AppState:dialog = new BookletCreationDialog(window)
}
}else{
if(AppState:zone == Zone.SheetCollection){
var int i = 0
foreach(sheetCollectionBounds as b){
if(position.x >= b.position.x && position.x <= b.position.x + b.size.width && position.y >= b.position.y && position.y <= b.position.y + b.size.height){
if(i == sheetCollectionBounds.length - 1){
AppState:zone = Zone.SheetImporter
AppState:dialog = null
sheetImporterPath = io.get_home_directory()
booklet.currentSheet = null
AppState:scrollX = 0
AppState:scrollY = 0
AppState:zoom = 1
CacheUtils:hasChanged = true
CacheUtils:hasChangedForOnPaint = true
CacheUtils:hasSizeChanged = true
CacheUtils:hasSizeChangedForOnPaint = true
}else{
AppState:zone = Zone.Sheet
AppState:dialog = null
booklet.currentSheet = booklet.sheets[i]
AppState:scrollX = 0
AppState:scrollY = 0
AppState:zoom = 1
CacheUtils:hasChanged = true
CacheUtils:hasChangedForOnPaint = true
CacheUtils:hasSizeChanged = true
CacheUtils:hasSizeChangedForOnPaint = true
}
}
i++
}
}elseif(AppState:zone == Zone.SheetImporter){
var font = Font:default.withSize(int(math.max(int(math.sqrt(double(window.width * window.height))) / 50d, 7d)))
var color = Color:fromARGB(255, 0, 0, 0)
if(AppState:theme == Theme.Dark){
color = Color:fromARGB(255, 255, 255, 255)
}
var ls = utils.ls(sheetImporterPath, [".png", ".jpg", ".jpeg", ".bmp", ".tga", ".hdr"])
var i = 0
var factor = 20 // TODO: Do not hardcode this
$if android{
factor = 40
}
var y = factor + get_toolbar_height(window) - AppState:scrollY
foreach(ls as s){
var size = graphics.measure_text_size(s, font)
var width = int(size.width)
var height = int(size.height)
if(position.x >= window.width / 2 - width / 2 && position.x <= window.width / 2 + width / 2 && position.y >= y && position.y <= y + height){
if(io.exists_directory(io.join_path([sheetImporterPath, s]))){
if(s == ".."){
sheetImporterPath = sheetImporterPath.reverse().split("/", 2)[1].reverse()
}else{
sheetImporterPath = io.join_path([sheetImporterPath, s])
}
AppState:scrollY = 0
}else{
io.write_file_bytes(Booklet:getBookletsPath() + "/" + booklet.name + "/" + "sheets/" + s, io.read_file_bytes(io.join_path([sheetImporterPath, s])))
var Sheet sheet = Sheet:fromFile(Booklet:getBookletsPath() + "/" + booklet.name + "/" + "sheets/" + s)
sheet.load()
booklet.sheets.add(sheet)
booklet.sheetFiles.add(s)
booklet.currentSheet = sheet
AppState:zone = Zone.Sheet
AppState:dialog = null
AppState:scrollX = 0
AppState:scrollY = 0
AppState:zoom = 1
CacheUtils:hasChanged = true
CacheUtils:hasChangedForOnPaint = true
CacheUtils:hasSizeChanged = true
CacheUtils:hasSizeChangedForOnPaint = true
sheet.hasChanged = true
}
}
y += factor
i++
}
}
}
}
}
window.onMouseWheel = callback(Point position, float deltaX, float deltaY) {
if(position.x > get_booklet_list_width(window) && position.y > get_toolbar_height(window)){
if(window.isKeyDown(KeyCode.left_control) || window.isKeyDown(KeyCode.right_control)){
if(AppState:zone == Zone.Canvas || AppState:zone == Zone.Sheet){
if(deltaY > 0){
AppState:zoom += 0.1
}else{
AppState:zoom -= 0.1
}
if(AppState:zoom < 0.2){
AppState:zoom = 0.2
}
if(AppState:zoom > 5){
AppState:zoom = 5
}
}
}else{
if(window.isKeyDown(KeyCode.left_shift) || window.isKeyDown(KeyCode.right_shift)){
if(deltaY > 0){
if(AppState:zone == Zone.Canvas || AppState:zone == Zone.Sheet){
AppState:scrollX = int(math.max(AppState:scrollX - double(deltaY * 5 / AppState:zoom), 0))
if(AppState:zone == Zone.Canvas){
booklet.scrollX = AppState:scrollX
CacheUtils:hasScrollChanged = true
}
}
}elseif(deltaY < 0){
if(AppState:zone == Zone.Canvas){
AppState:scrollX = int(math.max(math.min(AppState:scrollX - double(deltaY * 5 / AppState:zoom), double(booklet.canvas.width - window.width / AppState:zoom + get_booklet_list_width(window))), 0))
booklet.scrollX = AppState:scrollX
CacheUtils:hasScrollChanged = true
}elseif(AppState:zone == Zone.Sheet){
AppState:scrollX = int(math.max(math.min(AppState:scrollX - double(deltaY * 5 / AppState:zoom), double(Canvas(Sheet(booklet.currentSheet).canvas).width - window.width / AppState:zoom + get_booklet_list_width(window))), 0))
}
}
}else{
if(deltaY > 0){
if(AppState:zone == Zone.Canvas || AppState:zone == Zone.SheetCollection || AppState:zone == Zone.Sheet || AppState:zone == Zone.SheetImporter){
AppState:scrollY = int(math.max(AppState:scrollY - double(deltaY * 5 / AppState:zoom), 0))
if(AppState:zone == Zone.Canvas){
booklet.scrollY = AppState:scrollY
CacheUtils:hasScrollChanged = true
}
}
}elseif(deltaY < 0){
if(AppState:zone == Zone.Canvas || AppState:zone == Zone.SheetCollection || AppState:zone == Zone.Sheet || AppState:zone == Zone.SheetImporter){
var boundHeight = booklet.canvas.height
if(AppState:zone == Zone.SheetCollection){
boundHeight = sheetCollectionHeight
}elseif(AppState:zone == Zone.Sheet){
boundHeight = Canvas(Sheet(booklet.currentSheet).canvas).height
}elseif(AppState:zone == Zone.SheetImporter){
boundHeight = sheetImporterHeight
}
AppState:scrollY = int(math.max(math.min(AppState:scrollY - double(deltaY * 5 / AppState:zoom), double(boundHeight - window.height / AppState:zoom + get_toolbar_height(window))), 0))
if(AppState:zone == Zone.Canvas){
booklet.scrollY = AppState:scrollY
CacheUtils:hasScrollChanged = true
}
}
}
}
}
}
}
window.onTouchDown = callback(list<TouchPoint> points){
foreach(points as point){
if(point.changed){
if(AppState:dialog != null && AppState:dialog?!.containsPoint(point.position)){
AppState:dialog?!.onPointerDown(point.position)
continue
}
if(point.position.x > get_booklet_list_width(window) && point.position.y > get_toolbar_height(window)){
if(point.toolType == TouchToolType.Stylus){
if(AppState:zone == Zone.Canvas || AppState:zone == Zone.Sheet){
if(undoList.length == 0){
CacheUtils:hasUndoAvailabilityChanged = true
}
if(AppState:zone == Zone.Canvas){
$if legacyUndoRedo{
if(booklet.canvas.width * booklet.canvas.height > 5000000){
// TODO: Remove this workaround when the undo/redo system doesn't store the entire canvas in memory
undoList.clear()
}
undoList.add(booklet.canvas.copy())
}$else{
if(AppState:pen == pathEraser){
var pixel = booklet.canvas.getPixel(int((point.position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((point.position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY))
if(!(pixel.a == 0b && pixel.r == 0b && pixel.g == 0b && pixel.b == 0b)){
undoList.add(new ImageDiff())
hasPushedUndoDiff = true
}else{
hasPushedUndoDiff = false
}
}else{
undoList.add(new ImageDiff())
hasPushedUndoDiff = true
}
}
}elseif(AppState:zone == Zone.Sheet){
$if legacyUndoRedo{
undoList.add(Canvas(Sheet(booklet.currentSheet).canvas).copy())
}$else{
if(AppState:pen == pathEraser){
var pixel = Canvas(Sheet(booklet.currentSheet).canvas).getPixel(int((point.position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((point.position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY))
if(!(pixel.a == 0b && pixel.r == 0b && pixel.g == 0b && pixel.b == 0b)){
undoList.add(new ImageDiff())
hasPushedUndoDiff = true
}else{
hasPushedUndoDiff = false
}
}else{
undoList.add(new ImageDiff())
hasPushedUndoDiff = true
}
}
}
if(undoList.length > UNDO_REDO_LIMIT){
undoList.removeAt(0)
}
if(hasPushedUndoDiff && redoList.length > 0){
redoList.clear()
CacheUtils:hasRedoAvailabilityChanged = true
}
if(AppState:pen == circularEraser){
if(AppState:zone == Zone.Canvas){
$if !legacyUndoRedo{
ImageDiff(undoList[undoList.length - 1]).trackFillCircleRectangular(new Ellipse(new Point((point.position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX, (point.position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), new Size(float(CircularEraser(AppState:pen).thickness / 2), float(CircularEraser(AppState:pen).thickness / 2))), booklet.canvas)
}
booklet.canvas.fillCircle(new Ellipse(new Point((point.position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX, (point.position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), new Size(float(CircularEraser(AppState:pen).thickness / 2), float(CircularEraser(AppState:pen).thickness / 2))), Color:fromARGB(0, 0, 0, 0), false)
}elseif(AppState:zone == Zone.Sheet){
$if !legacyUndoRedo{
ImageDiff(undoList[undoList.length - 1]).trackFillCircleRectangular(new Ellipse(new Point((point.position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX, (point.position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), new Size(float(CircularEraser(AppState:pen).thickness / 2), float(CircularEraser(AppState:pen).thickness / 2))), Canvas(Sheet(booklet.currentSheet).canvas))
}
Canvas(Sheet(booklet.currentSheet).canvas).fillCircle(new Ellipse(new Point((point.position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX, (point.position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), new Size(float(CircularEraser(AppState:pen).thickness / 2), float(CircularEraser(AppState:pen).thickness / 2))), Color:fromARGB(0, 0, 0, 0), false)
}
}elseif(AppState:pen == pathEraser){
if(AppState:zone == Zone.Canvas){
var pixel = booklet.canvas.getPixel(int((point.position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((point.position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY))
if(!(pixel.a == 0b && pixel.r == 0b && pixel.g == 0b && pixel.b == 0b)){
utils.clear_path(booklet.canvas, int((point.position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((point.position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), ImageDiff(undoList[undoList.length - 1]))
}
}elseif(AppState:zone == Zone.Sheet){
var pixel = Canvas(Sheet(booklet.currentSheet).canvas).getPixel(int((point.position.x - get_booklet_list_width(window) + AppState:scrollX) / AppState:zoom), int((point.position.y - get_toolbar_height(window) + AppState:scrollY) / AppState:zoom))
if(!(pixel.a == 0b && pixel.r == 0b && pixel.g == 0b && pixel.b == 0b)){
utils.clear_path(Canvas(Sheet(booklet.currentSheet).canvas), int((point.position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((point.position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), ImageDiff(undoList[undoList.length - 1]))
}
}
}else{
if(AppState:zone == Zone.Canvas){
pointerDownForDrawingId = point.identifier
// automatically resize canvas when drawing near the edge
if((booklet.canvas.width < ((point.position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX + Pen(AppState:pen).thickness + 300)) || (booklet.canvas.height < ((point.position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY + Pen(AppState:pen).thickness + 300))){
booklet.canvas.extendTo(int(math.max(double(booklet.canvas.width), double((point.position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX + Pen(AppState:pen).thickness + 300))), int(math.max(double(booklet.canvas.height), double((point.position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY + Pen(AppState:pen).thickness + 300))))
}
}elseif(AppState:zone == Zone.Sheet){
pointerDownForDrawingId = point.identifier
}
}
CacheUtils:hasChanged = true
CacheUtils:hasChangedForOnPaint = true
if(booklet.currentSheet != null){
Sheet(booklet.currentSheet).hasChanged = true
}
}
}
}
Dialog:pointerDownPosition = point.position
Dialog:pointerDownTimestamp = time.now()
}
}
}
var map<long, Point> touchPoints = {}
var map<long, TouchToolType> touchPointToolTypes = {}
window.onTouchMove = callback(list<TouchPoint> points){
if(Dialog:isInDownAction){
return
}
var newScrollX = -1
var newScrollY = -1
var canBeZoomPoint = false
var TouchPoint? previousZoomPoint = null
var oldTouchPoints = touchPoints.cloneShallow()
foreach(points as point){
if(point.changed){
if(AppState:dialog != null && AppState:dialog?!.containsPoint(point.position)){
if(oldTouchPoints.containsKey(point.identifier)){
AppState:dialog?!.onPointerMove(oldTouchPoints[point.identifier], point.position)
}
continue
}
if(point.position.x > get_booklet_list_width(window) && point.position.y > get_toolbar_height(window)){
if(touchPoints.containsKey(point.identifier) && touchPointToolTypes[point.identifier] == point.toolType){
if(AppState:zone == Zone.Canvas || AppState:zone == Zone.Sheet){
if(point.toolType == TouchToolType.Stylus){
if(AppState:pen == circularEraser){
if(AppState:zone == Zone.Canvas){
$if !legacyUndoRedo{
ImageDiff(undoList[undoList.length - 1]).trackFillCircleRectangular(new Ellipse(new Point((point.position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX, (point.position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), new Size(float(CircularEraser(AppState:pen).thickness / 2), float(CircularEraser(AppState:pen).thickness / 2))), booklet.canvas)
}
booklet.canvas.fillCircle(new Ellipse(new Point((point.position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX, (point.position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), new Size(float(CircularEraser(AppState:pen).thickness / 2), float(CircularEraser(AppState:pen).thickness / 2))), Color:fromARGB(0, 0, 0, 0), false)
}elseif(AppState:zone == Zone.Sheet){
$if !legacyUndoRedo{
ImageDiff(undoList[undoList.length - 1]).trackFillCircleRectangular(new Ellipse(new Point((point.position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX, (point.position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), new Size(float(CircularEraser(AppState:pen).thickness / 2), float(CircularEraser(AppState:pen).thickness / 2))), Canvas(Sheet(booklet.currentSheet).canvas))
}
Canvas(Sheet(booklet.currentSheet).canvas).fillCircle(new Ellipse(new Point((point.position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX, (point.position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), new Size(float(CircularEraser(AppState:pen).thickness / 2), float(CircularEraser(AppState:pen).thickness / 2))), Color:fromARGB(0, 0, 0, 0), false)
}
}elseif(AppState:pen == pathEraser){
if(AppState:zone == Zone.Canvas){
var pixel = booklet.canvas.getPixel(int((point.position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((point.position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY))
if(!(pixel.a == 0b && pixel.r == 0b && pixel.g == 0b && pixel.b == 0b)){
$if !legacyUndoRedo{
if(!hasPushedUndoDiff){
undoList.add(new ImageDiff())
hasPushedUndoDiff = true
CacheUtils:hasUndoAvailabilityChanged = true
}
}
utils.clear_path(booklet.canvas, int((point.position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((point.position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), ImageDiff(undoList[undoList.length - 1]))
}
}elseif(AppState:zone == Zone.Sheet){
var pixel = Canvas(Sheet(booklet.currentSheet).canvas).getPixel(int((point.position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((point.position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY))
if(!(pixel.a == 0b && pixel.r == 0b && pixel.g == 0b && pixel.b == 0b)){
$if !legacyUndoRedo{
if(!hasPushedUndoDiff){
undoList.add(new ImageDiff())
hasPushedUndoDiff = true
CacheUtils:hasUndoAvailabilityChanged = true
}
}
utils.clear_path(Canvas(Sheet(booklet.currentSheet).canvas), int((point.position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((point.position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), ImageDiff(undoList[undoList.length - 1]))
}
}
}elseif(AppState:pen oftype Pen){
if(!LinePerfectionUtils:hasPerfected){
if(!LinePerfectionUtils:isPointerDown){
LinePerfectionUtils:pointerDownPosition = oldTouchPoints[point.identifier]
LinePerfectionUtils:isPointerDown = true
LinePerfectionUtils:cumulativeMovementX = 0
LinePerfectionUtils:cumulativeMovementY = 0
}
if(LinePerfectionUtils:lastMovementTimestamp == null){
LinePerfectionUtils:lastMovementTimestamp = time.now()
}
LinePerfectionUtils:lastMovementPosition = point.position
LinePerfectionUtils:pointerId = point.identifier
LinePerfectionUtils:cumulativeMovementX += float(math.abs(double(point.position.x - touchPoints[point.identifier].x)))
LinePerfectionUtils:cumulativeMovementY += float(math.abs(double(point.position.y - touchPoints[point.identifier].y)))
if(LinePerfectionUtils:cumulativeMovementX >= 3 || LinePerfectionUtils:cumulativeMovementY >= 3){
LinePerfectionUtils:cumulativeMovementX = 0
LinePerfectionUtils:cumulativeMovementY = 0
LinePerfectionUtils:lastMovementTimestamp = time.now()
}
cumulativePointerMovementAfterDownX += float(math.abs(double(point.position.x - touchPoints[point.identifier].x)))
cumulativePointerMovementAfterDownY += float(math.abs(double(point.position.y - touchPoints[point.identifier].y)))
if(AppState:zone == Zone.Canvas){
$if !legacyUndoRedo{
ImageDiff(undoList[undoList.length - 1]).trackDrawLine(int((touchPoints[point.identifier].x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((touchPoints[point.identifier].y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), int((point.position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((point.position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), Pen(AppState:pen).thickness, booklet.canvas)
}
booklet.canvas.drawLine(int((touchPoints[point.identifier].x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((touchPoints[point.identifier].y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), int((point.position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((point.position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), Pen(AppState:pen).color, Pen(AppState:pen).thickness)
// automatically resize canvas when drawing near the edge
if((booklet.canvas.width < ((point.position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX + Pen(AppState:pen).thickness + 300)) || (booklet.canvas.height < ((point.position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY + Pen(AppState:pen).thickness + 300))){
booklet.canvas.extendTo(int(math.max(double(booklet.canvas.width), double((point.position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX + Pen(AppState:pen).thickness + 300))), int(math.max(double(booklet.canvas.height), double((point.position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY + Pen(AppState:pen).thickness + 300))))
}
}elseif(AppState:zone == Zone.Sheet){
$if !legacyUndoRedo{
ImageDiff(undoList[undoList.length - 1]).trackDrawLine(int((touchPoints[point.identifier].x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((touchPoints[point.identifier].y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), int((point.position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((point.position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), Pen(AppState:pen).thickness, Canvas(Sheet(booklet.currentSheet).canvas))
}
Canvas(Sheet(booklet.currentSheet).canvas).drawLine(int((touchPoints[point.identifier].x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((touchPoints[point.identifier].y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), int((point.position.x - get_booklet_list_width(window)) / AppState:zoom + AppState:scrollX), int((point.position.y - get_toolbar_height(window)) / AppState:zoom + AppState:scrollY), Pen(AppState:pen).color, Pen(AppState:pen).thickness)
}
}
}
CacheUtils:hasChanged = true
CacheUtils:hasChangedForOnPaint = true
if(booklet.currentSheet != null){
Sheet(booklet.currentSheet).hasChanged = true
}
}elseif(point.toolType == TouchToolType.Finger){
if(canBeZoomPoint){
if(math.sqrt(math.pow(double(point.position.x - TouchPoint(previousZoomPoint).position.x), 2) + math.pow(double(point.position.y - TouchPoint(previousZoomPoint).position.y), 2)) > math.sqrt(math.pow(double(oldTouchPoints[point.identifier].x - oldTouchPoints[TouchPoint(previousZoomPoint).identifier].x), 2) + math.pow(double(oldTouchPoints[point.identifier].y - oldTouchPoints[TouchPoint(previousZoomPoint).identifier].y), 2))){
AppState:zoom += 0.01
}else{
AppState:zoom -= 0.01
}
if(AppState:zoom < 0.2){
AppState:zoom = 0.2
}
if(AppState:zoom > 5){
AppState:zoom = 5
}
}
if(AppState:zone == Zone.Canvas){
newScrollX = int(math.max(math.min(AppState:scrollX + double(oldTouchPoints[point.identifier].x - point.position.x) / AppState:zoom, double(booklet.canvas.width - window.width / AppState:zoom + get_booklet_list_width(window))), 0))
newScrollY = int(math.max(math.min(AppState:scrollY + double(oldTouchPoints[point.identifier].y - point.position.y) / AppState:zoom, double(booklet.canvas.height - window.height / AppState:zoom + get_toolbar_height(window))), 0))
}elseif(AppState:zone == Zone.Sheet){
newScrollX = int(math.max(math.min(AppState:scrollX + double(oldTouchPoints[point.identifier].x - point.position.x) / AppState:zoom, double(Canvas(Sheet(booklet.currentSheet).canvas).width - window.width / AppState:zoom + get_booklet_list_width(window))), 0))
var sheetHeight = Canvas(Sheet(booklet.currentSheet).canvas).height
$if android{
sheetHeight += window.height / 5 // Add a bit of padding at the bottom (otherwise one may accidentally click on the home button)
}
newScrollY = int(math.max(math.min(AppState:scrollY + double(oldTouchPoints[point.identifier].y - point.position.y) / AppState:zoom, double(sheetHeight - window.height / AppState:zoom + get_toolbar_height(window))), 0))
}
canBeZoomPoint = true
previousZoomPoint = point
}
}elseif(AppState:zone == Zone.SheetCollection){
if(point.toolType != TouchToolType.Stylus){
newScrollY = int(math.max(math.min(AppState:scrollY + double(touchPoints[point.identifier].y - point.position.y) / AppState:zoom, double(sheetCollectionHeight - window.height + get_toolbar_height(window))), 0))
}
}elseif(AppState:zone == Zone.SheetImporter){
if(point.toolType != TouchToolType.Stylus){
newScrollY = int(math.max(math.min(AppState:scrollY + double(touchPoints[point.identifier].y - point.position.y) / AppState:zoom, double(sheetImporterHeight - window.height + get_toolbar_height(window))), 0))
}
}
}
touchPoints[point.identifier] = point.position
touchPointToolTypes[point.identifier] = point.toolType
}
}
}
if(newScrollX != -1){
AppState:scrollX = newScrollX
if(AppState:zone == Zone.Canvas){