-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtools.lisp
1682 lines (1569 loc) · 79.1 KB
/
tools.lisp
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
;; Copyright (c) 2024, April & May
(in-package charapainter)
(proclaim *optimization*)
(defclass board-tool ()
((board :initarg :board
:accessor tool-board)
(settings-layout :initform nil)
(default-message :initform ""
:initarg :default-message)))
(defgeneric set-tool (interface board name)
(:method (interface board name)
(with-slots (tools) interface
(let ((tool (find name tools :key #'class-name-of)))
(set-tool-internal interface board tool)))))
(defgeneric set-tool-internal (interface board tool)
(:method (itf board tool)
(with-slots (current-tool) board
(when current-tool (tool-cleanup current-tool))
(setf current-tool tool)
(set-message @tool.default-message itf))))
(defgeneric tool-cleanup (tool) (:method (tool)))
(defgeneric tool-press (tool x y) (:method (tool x y)))
(defgeneric tool-drag (tool x y) (:method (tool x y)))
(defgeneric tool-release (tool x y) (:method (tool x y)))
(defgeneric tool-motion (tool x y) (:method (tool x y)))
(defgeneric tool-shift-press (tool x y) (:method (tool x y)))
(defgeneric tool-shift-drag (tool x y) (:method (tool x y)))
(defgeneric tool-ctrl-space (tool x y key) (:method (tool x y key)))
(defgeneric tool-escape (tool x y key) (:method (tool x y key)))
(defgeneric tool-meta-arrow (tool x y key) (:method (tool x y key)))
(defgeneric tool-shift-arrow (tool x y key) (:method (tool x y key)))
(defgeneric tool-ctrl-shift-arrow (tool x y key) (:method (tool x y key)))
(defun board-pixel (board)
(declare (inline board-pixel))
(make-pixel :char @board.char :fg @board.fg :bg @board.bg
:bold-p @board.bold-p
:italic-p @board.italic-p
:underline-p @board.underline-p))
(defgeneric tool-return (tool x y key)
(:method ((tool board-tool) x y key)
(with-slots (board) tool
(with-slots (x-offset y-offset) board
(let* ((point (editor-pane-point board))
(x (+ x-offset (point-column point)))
(y (+ y-offset (point-linenum point)))
(origin (get-pixel x y board)))
(tagbody
(when (plusp (ring-length @board.current-layer.undo-ring))
(let ((undo (ring-ref @board.current-layer.undo-ring 0))
(maybe-prev-loc
(case @board.cursor-movement
(:left (cons (1+ x) y)) (:right (cons (1- x) y))
(:up (cons x (1+ y))) (:down (cons x (1- y))))))
(if (and (pixels-line-p undo)
(second (multiple-value-list (gethash maybe-prev-loc (pixels-table undo))))
(null (second (multiple-value-list (nfind-pixel x y undo)))))
(progn
(add-pixel x y origin undo)
(go end))
(go add-one))))
add-one
(let ((pixels (make-pixels)))
(add-pixel x y origin pixels)
(ring-push pixels @board.current-layer.undo-ring))
end)
(paint-pixel board x y (board-pixel board))
(case @board.cursor-movement
(:left (decf x)) (:right (incf x))
(:up (decf y)) (:down (incf y)))
(move-cursor board x y))))))
(defgeneric tool-backspace (tool x y key)
(:method ((tool board-tool) x y key)
(with-slots (board) tool
(with-slots (x-offset y-offset) board
(let* ((point (editor-pane-point board))
(x (+ x-offset (point-column point)))
(y (+ y-offset (point-linenum point)))
(prev-x x) (prev-y y))
(case @board.cursor-movement
(:left (incf x)) (:right (decf x))
(:up (incf y)) (:down (decf y)))
(let ((origin (get-pixel x y board)))
(tagbody
(when (plusp (ring-length @board.current-layer.undo-ring))
(let ((undo (ring-ref @board.current-layer.undo-ring 0)))
(if (and (pixels-line-p undo)
(second (multiple-value-list (nfind-pixel prev-x prev-y undo)))
(null (second (multiple-value-list (nfind-pixel x y undo)))))
(progn
(add-pixel x y origin undo)
(go end))
(go add-one))))
add-one
(let ((pixels (make-pixels)))
(add-pixel x y origin pixels)
(ring-push pixels @board.current-layer.undo-ring))
end))
(paint-pixel board x y nil)
(move-cursor board x y))))))
(defgeneric tool-arrow-key (tool x y key)
(:method ((tool board-tool) x y key)
(with-slots (board) tool
(let* ((point (editor-pane-point board))
(x (point-column point))
(y (point-linenum point)))
(case (sys:gesture-spec-data key)
((or :up 112)
(decf y))
((or :down 110)
(incf y))
((or :left 98)
(decf x))
((or :right 102)
(incf x)))
(setq x (clamp x 0 @board.width)
y (clamp y 0 @board.height))
(move-cursor-relative board x y)))))
(defgeneric tool-line-start (tool x y key)
(:method ((tool board-tool) x y key)
(with-slots (board) tool
(let* ((point (editor-pane-point board))
(y (point-linenum point)))
(move-cursor-relative board 0 y)))))
(defgeneric tool-line-end (tool x y key)
(:method ((tool board-tool) x y key)
(with-slots (board) tool
(let* ((point (editor-pane-point board))
(y (point-linenum point)))
(move-cursor-relative board @board.width y)))))
(defgeneric tool-key (tool x y key)
(:method ((tool board-tool) x y key)
(with-slots (board) tool
(with-slots (x-offset y-offset) board
(awhen (system:gesture-spec-to-character key :errorp nil)
(let* ((point (editor-pane-point board))
(x (+ x-offset (point-column point)))
(y (+ y-offset (point-linenum point)))
(origin (get-pixel x y board)))
(tagbody
(when (plusp (ring-length @board.current-layer.undo-ring))
(let ((undo (ring-ref @board.current-layer.undo-ring 0))
(maybe-prev-loc
(case @board.cursor-movement
(:left (cons (1+ x) y)) (:right (cons (1- x) y))
(:up (cons x (1+ y))) (:down (cons x (1- y))))))
(if (and (pixels-line-p undo)
(second (multiple-value-list (gethash maybe-prev-loc (pixels-table undo))))
(null (second (multiple-value-list (nfind-pixel x y undo)))))
(progn
(add-pixel x y origin undo)
(go end))
(go add-one))))
add-one
(let ((pixels (make-pixels)))
(add-pixel x y origin pixels)
(ring-push pixels @board.current-layer.undo-ring))
end)
(paint-pixel board x y (make-pixel :char it :fg @board.fg :bg @board.bg
:bold-p @board.bold-p
:italic-p @board.italic-p
:underline-p @board.underline-p))
(case @board.cursor-movement
(:left (decf x)) (:right (incf x))
(:up (decf y)) (:down (incf y)))
(move-cursor board x y)))))))
(defgeneric make-settings-layout (interface tool)
(:method (itf tool) (make 'capi:message-pane)))
;; Pan
(defclass pan (board-tool)
((drag-start-x-offset :initform nil)
(drag-start-y-offset :initform nil)
(drag-start-x :initform nil)
(drag-start-y :initform nil))
(:default-initargs
:default-message (editor::make-buffer-string
:%string "Use ⌘ ←↑↓→ to move the board anytime!"
:properties '((0 4 (face editor::default))
(4 12 (face hl-background))
(12 39 (face editor::default))))))
(defmethod set-tool :after (itf board (name (eql 'pan)))
(setf (capi:simple-pane-cursor board) :open-hand))
(defmethod tool-press ((tool pan) x y)
(with-slots (board drag-start-x drag-start-y drag-start-x-offset drag-start-y-offset) tool
(with-slots (x-offset y-offset) board
(setf drag-start-x-offset x-offset
drag-start-y-offset y-offset
drag-start-x x
drag-start-y y
(capi:simple-pane-cursor board) :closed-hand))))
(defmethod tool-drag ((tool pan) x y)
(with-slots (board drag-start-x drag-start-y drag-start-x-offset drag-start-y-offset) tool
(with-slots (x-offset y-offset) board
(let ((new-x-offset (+ drag-start-x-offset (floor (- drag-start-x x) @board.char-width)))
(new-y-offset (+ drag-start-y-offset (floor (- drag-start-y y) @board.char-height))))
(unless (and (= new-x-offset x-offset)
(= new-y-offset y-offset))
(setf x-offset new-x-offset
y-offset new-y-offset)
(refresh-board board))))))
(defmethod tool-release ((tool pan) x y)
(setf (capi:simple-pane-cursor @tool.board) :open-hand))
(defmethod tool-cleanup ((tool pan))
(setf (capi:simple-pane-cursor @tool.board) nil))
;; Painting Tools
;; Class Definition
(defclass paint-tool (board-tool)
((brush :initform nil)
(diameter :initform 1)
(old-pixels :initform nil
:documentation "The old PIXELS before last update.")
(new-pixels :initform (make-pixels)
:documentation "New pixels being added this time.
It stores the result of GENERATE-PIXELS, and will be merged into PIXELS.")
(pixels :initform (make-pixels)
:documentation "All pixels being drawn.")
(updated-pixels :initform nil
:documentation "Pixels that have been modified since last update.
New pixels added into PIXELS slot will be collected, and pixels that
have been removed from PIXELS slot will be collected with it's
original color value.")
(original-pixels :initform (make-pixels)
:documentation "Original pixels and colors of PIXELS.")
(track :initform nil)
(paint-option :initform '(:foreground :background :character)))
(:optimize-slot-access nil))
;; Generic Functions
(defgeneric generate-pixels (tool new-x new-y)
(:documentation "Generate new pixels.
This generic function will be called each time user make movement. It
should return new pixels generated.")
(:method ((tool paint-tool) x y)
(with-slots (board track) tool
(multiple-value-bind (x0 y0) (translate-position board x y)
(let ((pixels (make-pixels))
(option @tool.paint-option)
(blank-pixel (make-pixel :char #\Space)))
(flet ((modify-and-add (x y)
(let ((pixel (multiple-value-bind (pixel found)
(find-pixel x y @tool.original-pixels)
(or pixel (unless found (get-pixel x y board)) (make-pixel)))))
(when (member :background option)
(setf (pixel-bg pixel) @board.bg))
(when (member :foreground option)
(setf (pixel-fg pixel) @board.fg))
(when (member :character option)
(setf (pixel-char pixel) @board.char
(pixel-bold-p pixel) @board.bold-p
(pixel-italic-p pixel) @board.italic-p
(pixel-underline-p pixel) @board.underline-p))
(add-pixel x y pixel pixels))))
(if track
(multiple-value-bind (x1 y1) (apply #'translate-position board (car (last track)))
(let ((line (line-pixels blank-pixel x1 y1 x0 y0)))
(loop-pixels line
(modify-and-add %x %y))))
(modify-and-add x0 y0)))
pixels)))))
(defgeneric update-pixels (tool new-x new-y)
(:method ((tool paint-tool) new-x new-y)
(with-slots (board pixels old-pixels new-pixels updated-pixels original-pixels track) tool
(setf old-pixels (copy-pixels pixels)
new-pixels (or (generate-pixels tool new-x new-y) (make-pixels))
pixels (nunion-pixels pixels new-pixels)
updated-pixels (make-pixels))
(push-end (list new-x new-y) track)
(loop-pixels old-pixels
(unless (equalp (find-pixel %x %y pixels) %pixel)
(add-pixel %x %y (find-pixel %x %y original-pixels) updated-pixels)))
(loop-pixels pixels
(unless (equalp (find-pixel %x %y old-pixels) %pixel)
(add-pixel %x %y %pixel updated-pixels)
(unless (second (multiple-value-list (nfind-pixel %x %y original-pixels)))
(add-pixel %x %y (get-pixel %x %y board) original-pixels)))))))
(defgeneric end-stroke (tool)
(:method ((tool paint-tool))
(with-slots (old-pixels new-pixels pixels updated-pixels original-pixels track) tool
(setf old-pixels nil
new-pixels (make-pixels)
pixels (make-pixels)
updated-pixels nil
original-pixels (make-pixels)
track nil))))
(defgeneric tool-paint (tool)
(:method ((tool paint-tool))
(with-slots (board updated-pixels) tool
(when (and updated-pixels (pixels-not-empty-p updated-pixels))
(paint-pixels board updated-pixels)))))
(defmethod tool-press ((tool paint-tool) x y)
(update-pixels tool x y)
(tool-paint tool)
(move-cursor-pixelwise (tool-board tool) x y))
(defmethod tool-drag ((tool paint-tool) x y)
(update-pixels tool x y)
(tool-paint tool)
(move-cursor-pixelwise (tool-board tool) x y))
(defmethod tool-release ((tool paint-tool) x y)
(with-slots (board original-pixels) tool
(ring-push original-pixels @board.current-layer.undo-ring)
(end-stroke tool)
(set-message
@tool.default-message
(capi:element-interface @tool.board))))
(defmethod tool-motion ((tool paint-tool) x y)
(when (pixels-not-empty-p @tool.pixels)
(tool-release tool x y))
(move-cursor-pixelwise @tool.board x y))
(defmethod tool-cleanup ((tool paint-tool))
(with-slots (board original-pixels track) tool
(when track
(ring-push original-pixels @board.current-layer.undo-ring))
(end-stroke tool)))
(defmethod tool-arrow-key ((tool paint-tool) x y key)
(with-slots (board track) tool
(let* ((point (editor-pane-point board))
(x (* (point-column point) @board.char-width))
(y (* (point-linenum point) @board.char-height)))
(case (sys:gesture-spec-data key)
((or :up 112)
(decf y @board.char-height))
((or :down 110)
(incf y @board.char-height))
((or :left 98)
(decf x @board.char-width))
((or :right 102)
(incf x @board.char-width)))
(setq x (clamp x 0 (* @board.width @board.char-width))
y (clamp y 0 (* @board.height @board.char-height)))
(when track
(tool-release tool x y))
(tool-motion tool x y))))
(defmethod tool-meta-arrow ((tool paint-tool) x y key)
(with-slots (board track) tool
(let* ((point (editor-pane-point board))
(x (* (point-column point) @board.char-width))
(y (* (point-linenum point) @board.char-height)))
(unless track
(tool-press tool x y))
(case (sys:gesture-spec-data key)
((or :up 112)
(decf y @board.char-height))
((or :down 110)
(incf y @board.char-height))
((or :left 98)
(decf x @board.char-width))
((or :right 102)
(incf x @board.char-width)))
(setq x (clamp x 0 (* @board.width @board.char-width))
y (clamp y 0 (* @board.height @board.char-height)))
(tool-drag tool x y))))
(defmethod tool-line-start ((tool paint-tool) x y key)
(with-slots (board track) tool
(let* ((point (editor-pane-point board))
(y (* (point-linenum point) @board.char-height)))
(if track
(tool-drag tool 0 y)
(tool-motion tool 0 y)))))
(defmethod tool-line-end ((tool paint-tool) x y key)
(with-slots (board track) tool
(let* ((point (editor-pane-point board))
(x (* @board.width @board.char-width))
(y (* (point-linenum point) @board.char-height)))
(if track
(tool-drag tool x y)
(tool-motion tool x y)))))
(defmethod tool-escape ((tool paint-tool) x y key)
(with-slots (board track) tool
(when track
(let* ((point (editor-pane-point board))
(x (* (point-column point) @board.char-width))
(y (* (point-linenum point) @board.char-height)))
(tool-release tool x y)))))
;; Brush & Eraser
(defclass brush (paint-tool) ()
(:default-initargs
:default-message (editor::make-buffer-string
:%string "Write one: ⏎ Erase one: ⌫ Start: ⌃Space "
:properties '((0 11 (face editor::default))
(11 14 (face hl-background))
(14 27 (face editor::default))
(27 30 (face hl-background))
(30 39 (face editor::default))
(39 47 (face hl-background))))))
(defclass eraser (paint-tool) ()
(:default-initargs
:default-message (editor::make-buffer-string
:%string "Write one: ⏎ Erase one: ⌫ Start: ⌃Space "
:properties '((0 11 (face editor::default))
(11 14 (face hl-background))
(14 27 (face editor::default))
(27 30 (face hl-background))
(30 39 (face editor::default))
(39 47 (face hl-background))))))
(defmethod generate-pixels ((tool eraser) x y)
(with-slots (board track) tool
(multiple-value-bind (x0 y0) (translate-position board x y)
(let ((pixels (make-pixels))
(option @tool.paint-option)
(blank-pixel (make-pixel :char #\Space)))
(flet ((erase-and-add (x y)
(let ((pixel (find-pixel x y pixels)))
(if pixel
(progn
(when (member :background option)
(setf (pixel-bg pixel) nil))
(when (member :foreground option)
(setf (pixel-fg pixel) nil))
(when (member :character option)
(setf (pixel-char pixel) #\Space))
(add-pixel x y pixel pixels))
(add-pixel x y blank-pixel pixels)))))
(if track
(multiple-value-bind (x1 y1) (apply #'translate-position board (car (last track)))
(let ((line (line-pixels blank-pixel x1 y1 x0 y0)))
(loop-pixels line
(erase-and-add %x %y))))
(erase-and-add x0 y0)))
pixels))))
(defclass brush-preview (capi:pinboard-object need-invalidate-after-style-change)
((tool :initarg :tool)))
(defmethod capi:draw-pinboard-object (port (self brush-preview) &key)
(let* ((board @(capi:element-interface port).board)
(font (gp:find-best-font
port
(gp:make-font-description :family @board.project.font-family
:size *default-font-size*
:weight (if @board.bold-p :bold :normal)
:slant (if @board.italic-p :italic :roman))))
(width (gp:get-font-width port font))
(height (gp:get-font-height port font))
(ascent (gp:get-font-ascent port font))
(descent (gp:get-font-descent port font)))
(capi:set-hint-table self (list :visible-min-height (* height 6)
:visible-max-height t))
(capi:with-geometry self
(let* ((cx (+ capi:%x% (/ capi:%width% 2)))
(cy (+ capi:%y% (/ capi:%height% 2)))
(rows (loop for i from -1 to 4
collect (+ (- cy descent ascent descent)
(* height i))))
(eraser-p (typep @self.tool 'eraser))
(fg (if eraser-p
(if @board.fg (term-color-spec-with-alpha @board.fg) *default-foreground*)
(if (capi:top-level-interface-dark-mode-p (capi:element-interface port))
:gray40 :gray60)))
(bg (if eraser-p
(if @board.bg (term-color-spec-with-alpha @board.bg) *default-background*)
nil))
(char #\§))
(gp:draw-rectangle port (1+ capi:%x%) (1+ capi:%y%) (- capi:%width% 2) (- capi:%height% 2)
:foreground *default-background*
:filled t)
(dotimes (y 6)
(dorange (x -10 10)
(gp:draw-character port char (+ cx (* width x)) (nth y rows)
:foreground fg
:background bg
:font font
:block t)))
(gp:draw-rectangle port (1+ capi:%x%) (1+ capi:%y%) (- capi:%width% 2) (- capi:%height% 2)
:foreground (if (capi:top-level-interface-dark-mode-p (capi:element-interface port))
:white :black)
:thickness 2)
(when (member :foreground @self.tool.paint-option)
(if eraser-p
(setq fg *default-foreground*)
(setq fg (if @board.fg (term-color-spec-with-alpha @board.fg) *default-foreground*))))
(when (member :background @self.tool.paint-option)
(if eraser-p
(setq bg *default-background*)
(setq bg (if @board.bg (term-color-spec-with-alpha @board.bg) *default-background*))))
(when (member :character @self.tool.paint-option)
(if eraser-p
(setq char #\Space)
(setq char @board.char)))
(loop for x from -8 below 8
for y in '(3 3 2 2 2 2 2 3 4 5 5 5 5 5 4 4)
do (gp:draw-character port char (+ cx (* width x)) (nth (1- y) rows)
:foreground fg
:background bg
:font font
:block t))))))
(defmethod make-settings-layout (itf (tool brush))
(let ((brush-preview (make 'brush-preview :tool tool)))
(make 'capi:column-layout
:adjust :center
:description
(list (make 'capi:check-button-panel
:title "Paint:" :title-position :left
:layout-class 'capi:column-layout
:items '(:foreground :background :character)
:print-function #'string-capitalize
:callback-type :data
:selected-items @tool.paint-option
:selection-callback (op (pushnew _ @tool.paint-option)
(capi:redraw-pinboard-object brush-preview))
:retract-callback (op (setf @tool.paint-option (delete _ @tool.paint-option))
(capi:redraw-pinboard-object brush-preview)))
(make 'capi:simple-pinboard-layout
:visible-min-height '(character 12)
:description (list brush-preview))
(make #+lispworks8.1 'capi:dummy-pane
#-lispworks8.1 'capi::dumm-pane)))))
(defmethod make-settings-layout (itf (tool eraser))
(let ((brush-preview (make 'brush-preview :tool tool)))
(make 'capi:column-layout
:adjust :center
:description
(list (make 'capi:check-button-panel
:title "Erase:" :title-position :left
:layout-class 'capi:column-layout
:items '(:foreground :background :character)
:print-function #'string-capitalize
:callback-type :data
:selected-items @tool.paint-option
:selection-callback (op (pushnew _ @tool.paint-option)
(capi:redraw-pinboard-object brush-preview))
:retract-callback (op (setf @tool.paint-option (delete _ @tool.paint-option))
(capi:redraw-pinboard-object brush-preview)))
(make 'capi:simple-pinboard-layout
:visible-min-height '(character 12)
:description (list brush-preview))
(make #+lispworks8.1 'capi:dummy-pane
#-lispworks8.1 'capi::dumm-pane)))))
;; Stroke
(defclass stroke (paint-tool)
((charset :initform :ascii)
(arrow-p :initform nil)
(connect-surroundings :initform nil))
(:default-initargs
:default-message (editor::make-buffer-string
:%string "Start: ⌃Space Change direction: ⇧ ←↑↓→ Draw arrow: ⌃⇧ ←↑↓→"
:properties '((0 7 (face editor::default))
(7 15 (face hl-background))
(15 35 (face editor::default))
(35 43 (face hl-background))
(43 57 (face editor::default))
(57 65 (face hl-background))))))
(defclass selector-board (capi:pinboard-layout)
((tool :initarg :tool)
(hover :initform nil)
(refresh-timer :initform nil))
(:default-initargs
:description
(list (make 'capi:grid-layout
:columns 2
:description
(list (make 'stroke-tool-selector :charset :ascii :selected t)
(make 'stroke-tool-selector :charset :ascii :arrow-p t)
(make 'stroke-tool-selector :charset :box-light)
(make 'stroke-tool-selector :charset :box-light :arrow-p t)
(make 'stroke-tool-selector :charset :box-heavy)
(make 'stroke-tool-selector :charset :box-heavy :arrow-p t)
(make 'stroke-tool-selector :charset :box-rounded)
(make 'stroke-tool-selector :charset :box-rounded :arrow-p t)
(make 'stroke-tool-selector :charset :box-double)
(make 'stroke-tool-selector :charset :box-double :arrow-p t))))
:resize-callback
(lambda (self x y w h)
(setf (capi:static-layout-child-geometry
(first (capi:layout-description self)))
(values x y w h)))
:input-model `(((:button-1 :release)
,(lambda (pane x y)
(when-let (obj (capi:pinboard-object-at-position pane x y))
(let ((old-obj (find-if (lambda (obj)
(and (eql @obj.charset @pane.tool.charset)
(eql @obj.arrow-p @pane.tool.arrow-p)))
(capi:layout-description
(car (capi:layout-description pane))))))
(when old-obj
(setf @old-obj.selected nil)
(capi:redraw-pinboard-object old-obj nil))
(setf @pane.tool.charset @obj.charset
@pane.tool.arrow-p @obj.arrow-p
@obj.selected t)
(capi:redraw-pinboard-object obj)))))
(:motion ,(lambda (pane x y)
(when-let (obj (capi:pinboard-object-at-position pane x y))
(when @pane.hover
(capi:unhighlight-pinboard-object pane @pane.hover))
(capi:highlight-pinboard-object pane obj)
(setf @pane.hover obj)
(gp:invalidate-rectangle pane)
(mp:schedule-timer-relative @pane.refresh-timer 1 1)))))))
(defmethod initialize-instance :after ((self selector-board) &key)
(setf @self.refresh-timer
(mp:make-timer
(lambda ()
(unless (capi:pane-has-focus-p self)
(capi:apply-in-pane-process self #'capi:unhighlight-pinboard-object self @self.hover)
(setf @self.hover nil)
:stop)))))
(defclass stroke-tool-selector (capi:pinboard-object need-invalidate-after-style-change)
((charset :initform :ascii
:initarg :charset)
(selected :initform nil
:initarg :selected)
(arrow-p :initform nil
:initarg :arrow-p)))
(defmethod capi:draw-pinboard-object (port (self stroke-tool-selector) &key)
(capi:with-geometry self
(let* ((itf (capi:element-interface port))
(board @itf.board)
(fg (if (or @self.selected
(capi:pinboard-object-highlighted-p self))
(if (member :foreground @board.current-tool.paint-option)
(if @board.fg (term-color-spec-with-alpha @board.fg) *default-foreground*)
(if (capi:top-level-interface-dark-mode-p itf)
:gray60 :gray40))
(if (capi:top-level-interface-dark-mode-p itf)
:gray60 :gray40)))
(bg (if (member :background @board.current-tool.paint-option)
(if @board.bg (term-color-spec-with-alpha @board.bg) *default-background*)
nil))
(font (gp:find-best-font
port
(gp:make-font-description :family @board.project.font-family
:size *default-font-size*
:weight (if @board.bold-p :bold :normal)
:slant (if @board.italic-p :italic :roman)))))
(gp:draw-rectangle port capi:%x% capi:%y% capi:%width% capi:%height%
:foreground *default-background*
:filled t)
(gp:with-graphics-state (port :foreground fg :background bg :font font)
(let* ((h (charset-get @self.charset :h))
(lb (charset-get @self.charset :lb))
(rt (charset-get @self.charset :rt))
(end (if @self.arrow-p (charset-get @self.charset :arr-r) h))
(width (gp:get-font-width port font))
(ascent (gp:get-font-ascent port font))
(descent (gp:get-font-descent port font))
(cx (+ capi:%x% (/ capi:%width% 2)))
(cy (+ capi:%y% (/ capi:%height% 2)))
(line1-y (- cy descent))
(line2-y (+ cy ascent))
(line1-x (- cx (* width 7/2)))
(line2-x (- cx (/ width 2))))
(gp:draw-character port h line1-x line1-y :block t)
(gp:draw-character port h (+ line1-x width) line1-y :block t)
(gp:draw-character port h (+ line1-x (* width 2)) line1-y :block t)
(gp:draw-character port lb (+ line1-x (* width 3)) line1-y :block t)
(gp:draw-character port rt line2-x line2-y :block t)
(gp:draw-character port h (+ line2-x width) line2-y :block t)
(gp:draw-character port h (+ line2-x (* width 2)) line2-y :block t)
(gp:draw-character port end (+ line2-x (* width 3)) line2-y :block t)))
(gp:draw-rectangle port (1+ capi:%x%) (1+ capi:%y%) (- capi:%width% 2) (- capi:%height% 2)
:foreground (if (or @self.selected
(capi:pinboard-object-highlighted-p self))
(if (capi:top-level-interface-dark-mode-p itf)
:white :black)
(if (capi:top-level-interface-dark-mode-p itf)
:gray40 :gray60))
:thickness 2))))
(defmethod capi:draw-pinboard-object-highlighted (port (self stroke-tool-selector) &key)
(capi:draw-pinboard-object port self))
(defmethod make-settings-layout (itf (tool stroke))
(let ((selector-board (make 'selector-board :tool tool)))
(make 'capi:column-layout
:adjust :center
:description
(list (make 'capi:check-button-panel
:title "Paint:" :title-position :left
:layout-class 'capi:column-layout
:items '(:foreground :background)
:print-function #'string-capitalize
:callback-type :data
:selected-items @tool.paint-option
:selection-callback (op (pushnew _ @tool.paint-option)
(capi:with-geometry selector-board
(capi:redraw-pinboard-layout selector-board 0 0 capi:%width% capi:%height%)))
:retract-callback (op (setf @tool.paint-option (delete _ @tool.paint-option))
(capi:with-geometry selector-board
(capi:redraw-pinboard-layout selector-board 0 0 capi:%width% capi:%height%))))
(make 'capi:check-button
:selected @tool.connect-surroundings
:text "Connect with surrounding characters"
:callback-type :none
:selection-callback (op (setf @tool.connect-surroundings t))
:retract-callback (op (setf @tool.connect-surroundings nil)))
selector-board))))
(defmethod update-pixels ((tool stroke) new-x new-y)
(with-slots (board pixels old-pixels new-pixels updated-pixels original-pixels track charset) tool
(setf old-pixels (copy-pixels pixels)
new-pixels (or (generate-pixels tool new-x new-y) (make-pixels)))
(when @tool.connect-surroundings
(let ((new (make-pixels)))
(loop-pixels new-pixels
(loop for x in (list (1- %x) (1+ %x) %x %x)
for y in (list %y %y (1- %y) (1+ %y))
unless (or (nfind-pixel x y new-pixels)
(nfind-pixel x y old-pixels))
do (when-let (pixel (get-pixel x y board))
(add-pixel x y pixel new))))
(nunion-pixels new-pixels new)))
(setf pixels (join-pixels charset (nunion-pixels pixels new-pixels))
updated-pixels (make-pixels))
(when @tool.arrow-p
(multiple-value-bind (x y) (translate-position board new-x new-y)
(let ((u (union-pixels pixels new-pixels))
char)
(cond ((nfind-pixel x (1- y) u)
(setq char (charset-get charset :arr-b)))
((nfind-pixel x (1+ y) u)
(setq char (charset-get charset :arr-t)))
((or (nfind-pixel (1- x) (1- y) u)
(nfind-pixel (1- x) y u)
(nfind-pixel (1- x) (1+ y) u))
(setq char (charset-get charset :arr-r)))
((or (nfind-pixel (1+ x) (1- y) u)
(nfind-pixel (1+ x) y u)
(nfind-pixel (1+ x) (1+ y) u))
(setq char (charset-get charset :arr-l))))
(when char
(add-pixel x y (make-pixel :char char :fg @board.fg :bg @board.bg
:bold-p @board.bold-p :italic-p @board.italic-p :underline-p @board.underline-p)
pixels)))))
(push-end (list new-x new-y) track)
(loop-pixels old-pixels
(unless (nfind-pixel %x %y pixels)
(add-pixel %x %y (nfind-pixel %x %y original-pixels) updated-pixels)))
(loop-pixels pixels
(unless (equalp (find-pixel %x %y old-pixels) %pixel)
(add-pixel %x %y %pixel updated-pixels)
(unless (second (multiple-value-list (nfind-pixel %x %y original-pixels)))
(add-pixel %x %y (get-pixel %x %y board) original-pixels))))))
(defmethod tool-shift-arrow ((tool stroke) x y key)
(with-slots (board charset) tool
(with-slots (x-offset y-offset) board
(let* ((point (editor-pane-point board))
(x (+ x-offset (point-column point)))
(y (+ y-offset (point-linenum point)))
(adjacent-pixels (make-pixels))
(char (charset-get charset :cross)))
(add-pixel (1- x) y (get-pixel (1- x) y board) adjacent-pixels)
(add-pixel (1+ x) y (get-pixel (1+ x) y board) adjacent-pixels)
(add-pixel x (1- y) (get-pixel x (1- y) board) adjacent-pixels)
(add-pixel x (1+ y) (get-pixel x (1+ y) board) adjacent-pixels)
(add-pixel x y (make-pixel :fg @board.fg :bg @board.bg) adjacent-pixels)
(case (sys:gesture-spec-data key)
(:up (add-pixel x (1- y) (make-pixel :char char) adjacent-pixels))
(:down (add-pixel x (1+ y) (make-pixel :char char) adjacent-pixels))
(:left (add-pixel (1- x) y (make-pixel :char char) adjacent-pixels))
(:right (add-pixel (1+ x) y (make-pixel :char char) adjacent-pixels)))
(let ((original-pixels (make-pixels)))
(add-pixel x y (get-pixel x y board) original-pixels)
(ring-push original-pixels @board.current-layer.undo-ring))
(paint-pixel board x y (find-pixel x y (join-pixels charset adjacent-pixels)))
(move-cursor board x y)))))
(defmethod tool-ctrl-shift-arrow ((tool stroke) x y key)
(with-slots (board charset) tool
(with-slots (x-offset y-offset) board
(let* ((point (editor-pane-point board))
(x (+ x-offset (point-column point)))
(y (+ y-offset (point-linenum point))))
(let ((original-pixels (make-pixels)))
(add-pixel x y (get-pixel x y board) original-pixels)
(ring-push original-pixels @board.current-layer.undo-ring))
(paint-pixel board x y
(make-pixel :char (charset-get charset (case (sys:gesture-spec-data key)
(:up :arr-t)
(:down :arr-b)
(:left :arr-l)
(:right :arr-r)))
:fg @board.fg :bg @board.bg
:bold-p @board.bold-p :italic-p @board.italic-p :underline-p @board.underline-p))
(move-cursor board x y)))))
(defclass rectangle (paint-tool)
((border-style :initform :ascii)
(border-foreground :initform :fill)
(border-background :initform :fill)
(border-character :initform :fill)
(filling-foreground :initform :none)
(filling-background :initform :none)
(filling-character :initform :none))
(:default-initargs
:default-message (editor::make-buffer-string
:%string "Start: ⌃Space "
:properties '((0 7 (face editor::default))
(7 15 (face hl-background))))))
(defclass rectangle-preview (capi:pinboard-object need-invalidate-after-style-change)
((tool :initarg :tool))
(:default-initargs
:visible-min-height '(character 12)))
(defmethod capi:draw-pinboard-object (port (self rectangle-preview) &key)
(let* ((itf (capi:element-interface port))
(board @itf.board)
(fg (if @board.fg (term-color-spec-with-alpha @board.fg) *default-foreground*))
(alt-fg (if (capi:top-level-interface-dark-mode-p itf)
:gray40 :gray60))
(bg (if @board.bg (term-color-spec-with-alpha @board.bg) *default-background*))
(font (gp:find-best-font
board
(gp:make-font-description :family @board.project.font-family
:size *default-font-size*
:weight (if @board.bold-p :bold :normal)
:slant (if @board.italic-p :italic :roman))))
(width (gp:get-font-width port font))
(height (gp:get-font-height port font))
(descent (gp:get-font-descent port font)))
(capi:set-hint-table self (list :visible-min-height (* height 6)
:visible-max-height t))
(capi:with-geometry self
(gp:with-graphics-state (port :font font)
(gp:draw-rectangle port capi:%x% capi:%y% capi:%width% capi:%height%
:foreground *default-background*
:filled t)
(let* ((cx (+ capi:%x% (/ capi:%width% 2)))
(cy (+ capi:%y% (/ capi:%height% 2)))
(start-x (- cx (* 9 width)))
(start-y (- cy descent (* 2 height)))
v h lt lb rt rb stroke-fg stroke-bg
fill fill-fg fill-bg)
(case @self.tool.border-character
(:none (setq v #\§ h #\§ lt #\§ lb #\§ rt #\§ rb #\§))
(:clear (setq v #\Space h #\Space lt #\Space lb #\Space rt #\Space rb #\Space))
(:fill (case @self.tool.border-style
(:default (setq v @board.char h @board.char lt @board.char lb @board.char rt @board.char rb @board.char))
(t (let ((it @self.tool.border-style))
(setq v (charset-get it :v) h (charset-get it :h)
lt (charset-get it :lt) lb (charset-get it :lb)
rt (charset-get it :rt) rb (charset-get it :rb)))))))
(case @self.tool.border-foreground
(:none (setq stroke-fg alt-fg))
(:clear (setq stroke-fg *default-foreground*))
(:fill (setq stroke-fg fg)))
(case @self.tool.border-background
(:none (setq stroke-bg nil))
(:clear (setq stroke-bg *default-background*))
(:fill (setq stroke-bg bg)))
(case @self.tool.filling-character
(:none (setq fill #\§))
(:clear (setq fill #\Space))
(:fill (setq fill @board.char)))
(case @self.tool.filling-foreground
(:none (setq fill-fg alt-fg))
(:clear (setq fill-fg *default-foreground*))
(:fill (setq fill-fg fg)))
(case @self.tool.filling-background
(:none (setq fill-bg nil))
(:clear (setq fill-bg *default-background*))
(:fill (setq fill-bg bg)))
(dotimes (y 6)
(dotimes (x 18)
(if (and (< 1 y 4) (< 2 x 15))
(gp:draw-character port fill (+ start-x (* x width)) (+ start-y (* y height))
:foreground fill-fg :background fill-bg :block t)
(unless (and (<= 1 y 4) (<= 2 x 15))
(gp:draw-character port #\§ (+ start-x (* x width)) (+ start-y (* y height))
:foreground alt-fg :background nil :block t)))))
(gp:draw-rectangle port (1+ capi:%x%) (1+ capi:%y%) (- capi:%width% 2) (- capi:%height% 2)
:foreground (if (capi:top-level-interface-dark-mode-p itf)
:white :black)
:thickness 2)
(dorange (x 3 15)
(gp:draw-character port h (+ start-x (* x width)) (+ start-y height)
:foreground stroke-fg :background stroke-bg :block t)
(gp:draw-character port h (+ start-x (* x width)) (+ start-y (* 4 height))
:foreground stroke-fg :background stroke-bg :block t))
(dorange (y 2 4)
(gp:draw-character port v (+ start-x (* 2 width)) (+ start-y (* y height))
:foreground stroke-fg :background stroke-bg :block t)
(gp:draw-character port v (+ start-x (* 15 width)) (+ start-y (* y height))
:foreground stroke-fg :background stroke-bg :block t))
(gp:draw-character port rb (+ start-x (* 2 width)) (+ start-y height)
:foreground stroke-fg :background stroke-bg :block t)
(gp:draw-character port lb (+ start-x (* 15 width)) (+ start-y height)
:foreground stroke-fg :background stroke-bg :block t)
(gp:draw-character port rt (+ start-x (* 2 width)) (+ start-y (* 4 height))
:foreground stroke-fg :background stroke-bg :block t)
(gp:draw-character port lt (+ start-x (* 15 width)) (+ start-y (* 4 height))
:foreground stroke-fg :background stroke-bg :block t))))))
(defmethod make-settings-layout (itf (tool rectangle))
(let* ((preview (make 'rectangle-preview :tool tool)))
(make-instance
'capi:column-layout
:adjust :center
:description
(list (make 'capi:option-pane
:title "Border Style:"
:title-position :left
:items (cons :default (serapeum:plist-keys *charsets*))
:selected-item @tool.border-style
:print-function #'string-capitalize
:callback-type :data
:selection-callback (op (setf @tool.border-style _)
(capi:redraw-pinboard-object preview)))
(make 'capi:radio-button-panel
:title "Border Foreground:"
:title-adjust :center
:items '(:clear :fill :none)
:selected-item @tool.border-foreground
:print-function #'string-capitalize
:callback-type :data
:selection-callback (op (setf @tool.border-foreground _)
(capi:redraw-pinboard-object preview)))
(make 'capi:radio-button-panel
:title "Border Background:"
:title-adjust :center
:items '(:clear :fill :none)
:selected-item @tool.border-background
:print-function #'string-capitalize
:callback-type :data
:selection-callback (op (setf @tool.border-background _)
(capi:redraw-pinboard-object preview)))
(make 'capi:radio-button-panel
:title "Border Character:"
:title-adjust :center
:items '(:clear :fill :none)
:selected-item @tool.border-character
:print-function #'string-capitalize
:callback-type :data
:selection-callback (op (setf @tool.border-character _)
(capi:redraw-pinboard-object preview)))
(make 'capi:radio-button-panel
:title "Filling Foreground:"
:title-adjust :center
:items '(:clear :fill :none)
:selected-item @tool.filling-foreground
:print-function #'string-capitalize
:callback-type :data
:selection-callback (op (setf @tool.filling-foreground _)
(capi:redraw-pinboard-object preview)))
(make 'capi:radio-button-panel
:title "Filling Background:"
:title-adjust :center
:items '(:clear :fill :none)
:selected-item @tool.filling-background
:print-function #'string-capitalize
:callback-type :data
:selection-callback (op (setf @tool.filling-background _)