-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterface.lisp
1194 lines (1114 loc) · 52.3 KB
/
interface.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*)
;; Color picker
(defun 4-bit-board-display-callback (pane x y w h)
(declare (ignore x y w h))
(capi:with-geometry pane
(let* ((gw (/ capi:%width% 8))
black-lines white-lines)
(dotimes (y 2)
(dotimes (x 8)
(let ((sx (* x gw)) (sy (* y gw)))
(nconcf black-lines
(list sx sy (+ sx gw) sy)
(list sx sy sx (+ sy gw)))
(nconcf white-lines
(list (+ sx gw) sy (+ sx gw) (+ sy gw))
(list sx (+ sy gw) (+ sx gw) (+ sy gw)))
(gp:draw-rectangle pane sx sy gw gw
:foreground (term-color-spec
(aref *4-bit-colors* (+ (* y 8) x)))
:filled t))))
(gp:draw-lines pane (mapcar #'1- white-lines) :foreground :white)
(gp:draw-lines pane black-lines :foreground :black))))
(defun 8-bit-grayscale-display-callback (pane x y w h)
(declare (ignore x y w h))
(capi:with-geometry pane
(capi:set-hint-table pane (list :visible-min-height (/ capi:%width% 24)
:visible-max-height t)))
(capi:with-geometry pane
(let* ((gw (/ capi:%width% 24))
black-lines white-lines)
(dotimes (x 24)
(let ((sx (* x gw)) (sy 0))
(nconcf black-lines
(list sx sy (+ sx gw) sy)
(list sx sy sx (+ sy gw)))
(nconcf white-lines
(list (+ sx gw) sy (+ sx gw) (+ sy gw))
(list sx (+ sy gw) (+ sx gw) (+ sy gw)))
(gp:draw-rectangle pane sx sy gw gw
:foreground (term-color-spec
(aref *8-bit-colors* (+ 232 x)))
:filled t)))
(gp:draw-lines pane (mapcar #'1- white-lines) :foreground :white)
(gp:draw-lines pane black-lines :foreground :black))))
(defun spectrum-callback (pane x y w h)
(declare (ignore x y w h))
(capi:with-geometry pane
(dorange$fixnum (y 0 capi:%height%)
(dorange$fixnum (x 0 capi:%width%)
(let* ((w/2 (/ capi:%width% 2))
(hue (* (/ y capi:%height%) gp:2pi))
(saturation (if (< x w/2) (/ x w/2) 1))
(value (if (>= x w/2) (- 1 (- (/ x w/2) 1)) 1)))
(gp:draw-point pane x y :foreground (color:make-hsv hue saturation value)))))))
(defun 8-bit-spectrum-callback (pane x y w h)
(declare (ignore x y w h))
(capi:with-geometry pane
(dorange$fixnum (y 0 capi:%height%)
(dorange$fixnum (x 0 capi:%width%)
(let* ((w/2 (/ capi:%width% 2))
(hue (* (/ y capi:%height%) gp:2pi))
(saturation (if (< x w/2) (/ x w/2) 1))
(value (if (>= x w/2) (- 1 (- (/ x w/2) 1)) 1)))
(gp:draw-point pane x y
:foreground (term-color-spec
(coerce-color-to 8 (color:make-hsv hue saturation value)))))))))
(defun spectrum-translate-position (pane x y)
(capi:with-geometry pane
(let* ((w/2 (/ capi:%width% 2))
(hue (* (/ y capi:%height%) gp:2pi))
(saturation (if (< x w/2) (/ x w/2) 1))
(value (if (>= x w/2) (- 1 (- (/ x w/2) 1)) 1)))
(color:make-hsv hue saturation value))))
(defun 4-bit-board-translate-position (pane x y)
(capi:with-geometry pane
(let ((gw (/ capi:%width% 8)))
(aref *4-bit-colors*
(+ (* (floor y gw) 8) (floor x gw))))))
(defun 8-bit-grayscale-translate-position (pane x y)
(declare (ignore y))
(capi:with-geometry pane
(let ((gw (/ capi:%width% 24)))
(aref *8-bit-colors* (+ (floor x gw) 232)))))
;; Refresh functions for setting variable
(defun refresh-font (pane &optional preserve-location)
(let* ((itf (ensure-element-interface pane))
(board @itf.board)
(fdesc (gp:make-font-description :family @board.project.font-family
:size @board.project.font-size))
(font (gp:find-best-font board fdesc))
(char-w (gp:get-font-width board font))
(char-h (gp:get-font-height board font))
(point (editor-pane-point board))
(point-x (point-column point))
(point-y (point-linenum point)))
(capi:with-geometry board
(setf (capi:simple-pane-font board) font
@board.char-width char-w
@board.char-height char-h
@board.width (floor capi:%width% char-w)
@board.height (floor capi:%height% char-h)
(capi:text-input-range-value @itf.font-size-input) @board.project.font-size))
(capi:map-pane-descendant-children
itf
(lambda (obj)
(when (typep obj 'need-invalidate-after-style-change)
(if (typep obj 'capi:pinboard-object)
(capi:redraw-pinboard-object obj)
(capi:apply-in-pane-process obj #'gp:invalidate-rectangle obj))))
:do-pinboard-objects t
:leaf-only t)
(gp:invalidate-rectangle @itf.current)
(refresh-board @itf.board)
(when preserve-location
(move-cursor-relative board point-x point-y))))
(defun refresh-style (pane)
(let ((itf (ensure-element-interface pane)))
(capi:map-pane-descendant-children
itf
(lambda (obj)
(when (typep obj 'need-invalidate-after-style-change)
(if (typep obj 'capi:pinboard-object)
(capi:redraw-pinboard-object obj)
(capi:apply-in-pane-process obj #'gp:invalidate-rectangle obj))))
:do-pinboard-objects t
:leaf-only t)
(gp:invalidate-rectangle @itf.current)
(setf (capi:choice-selected-items @itf.font-style-panel)
(loop for i across (capi:collection-items @itf.font-style-panel)
when (slot-value @itf.board (capi:item-data i)) collect i))))
(defun set-fg (color pane)
(let ((itf (ensure-element-interface pane)))
(setf @itf.board.fg color)
(if (term-color-p color)
(let ((spec (term-color-spec color))
(alpha (term-color-alpha color)))
(setf (capi:text-input-pane-text @itf.24-bit-fg-input)
(spec-to-hex (color:color-with-alpha spec alpha)))
(unless (= alpha @itf.fg-alpha)
(setf @itf.fg-alpha alpha
(capi:text-input-range-value @itf.fg-alpha-value) (round (* alpha 100))
(capi:range-slug-start @itf.fg-alpha-slider) (round (* alpha 100)))))
(progn
(setf (capi:text-input-pane-text @itf.24-bit-fg-input) "")
(setf @itf.fg-alpha 1.0
(capi:text-input-range-value @itf.fg-alpha-value) 100
(capi:range-slug-start @itf.fg-alpha-slider) 100)))
(refresh-style itf)))
(defun set-bg (color pane)
(let ((itf (ensure-element-interface pane)))
(setf @itf.board.bg color)
(if (term-color-p color)
(let ((spec (term-color-spec color))
(alpha (term-color-alpha color)))
(setf (capi:text-input-pane-text @itf.24-bit-bg-input)
(spec-to-hex (color:color-with-alpha spec alpha)))
(unless (= alpha @itf.bg-alpha)
(setf @itf.bg-alpha alpha
(capi:text-input-range-value @itf.bg-alpha-value) (round (* alpha 100))
(capi:range-slug-start @itf.bg-alpha-slider) (round (* alpha 100)))))
(progn
(setf (capi:text-input-pane-text @itf.24-bit-bg-input) "")
(setf @itf.bg-alpha 1.0
(capi:text-input-range-value @itf.bg-alpha-value) 100
(capi:range-slug-start @itf.bg-alpha-slider) 100)))
(refresh-style itf)))
(defun set-char (char pane)
(setf @(ensure-element-interface pane).board.char char)
(refresh-style pane))
;; Current state displayer
(defclass current-pane (capi:output-pane need-invalidate-after-style-change) ()
(:default-initargs
:title "Current"
:name 'current
:visible-min-width (list 'string (make-string 3 :initial-element #\Ideographic-Space))
:visible-max-width t
:display-callback (lambda (pane x y w h) (declare (ignore x y))
(let ((board @(capi:element-interface pane).board))
(capi:with-geometry pane
(let* ((font (gp:find-best-font pane (gp:make-font-description
:family @board.project.font-family
:size @board.project.font-size
:weight (if @board.bold-p :bold :normal)
:slant (if @board.italic-p :italic :roman))))
(ascent (gp:get-font-ascent pane font))
(char-width (gp:get-font-width pane font))
(char-height (gp:get-font-height pane font)))
(gp:draw-rectangle pane 0 0 (or capi:%width% w) (or capi:%height% h)
:foreground (if @board.bg (compose-two-colors
*default-background*
(color:color-to-premultiplied
(term-color-spec-with-alpha @board.bg)))
*default-background*)
:filled t)
(gp:draw-character pane @board.char
(max 0 (/ (- (or capi:%width% w) char-width) 2))
(+ (/ (- (or capi:%height% h) char-height) 2) ascent)
:foreground (when @board.fg (term-color-spec @board.fg))
:font font)))))
:input-model `(((:key :press) ,(lambda (pane x y key)
(declare (ignore x y))
(awhen (gesture-spec-char key) (set-char it pane)))))))
(defmethod capi:pane-interface-paste-p ((current current-pane) itf)
(capi:clipboard current))
(defmethod capi:pane-interface-paste-object ((current current-pane) itf)
(set-char (char (capi:clipboard current) 0) itf))
;; Characters board
(defclass char-board (capi:output-pane need-invalidate-after-style-change)
((hover :initform nil)
(refresh-timer :initform nil)
(pad-start :initform 0))
(:default-initargs
:vertical-scroll :without-bar
:display-callback #'char-board-display-callback
:input-model `(((:button-1 :release)
,(lambda (pane x y)
(ignore-errors
(let ((w (gp:get-font-width pane))
(h (gp:get-font-height pane)))
(set-char (char *characters*
(+ (* (floor y (+ h 2)) *char-board-columns*)
(floor (- x @pane.pad-start) (+ w 2))))
pane)
(gp:invalidate-rectangle pane)))))
(:motion ,(lambda (pane x y)
(ignore-errors
(let ((w (gp:get-font-width pane))
(h (gp:get-font-height pane)))
(setf @pane.hover (char *characters*
(+ (* (floor y (+ h 2)) *char-board-columns*)
(floor (- x @pane.pad-start) (+ w 2)))))
(gp:invalidate-rectangle pane)
(mp:schedule-timer-relative @pane.refresh-timer 1 1))))))))
(defmethod initialize-instance :after ((self char-board) &key)
(setf @self.refresh-timer
(mp:make-timer
(lambda ()
(unless (capi:pane-has-focus-p self)
(setf @self.hover nil)
(capi:apply-in-pane-process self #'gp:invalidate-rectangle self)
:stop)))))
(defun char-board-display-callback (pane x y w h)
(capi:with-geometry pane
(let* ((itf (capi:element-interface pane))
(board @itf.board)
(dark-mode-p (capi:top-level-interface-dark-mode-p itf))
(cols *char-board-columns*)
(rows (floor (length *characters*) *char-board-columns*))
(size (floor (* (/ capi:%width% cols) 4/3)))
(font (gp:find-best-font
pane
(gp:make-font-description :family @board.project.font-family :size size)))
(char-w (gp:get-font-width pane font))
(pad-start (round (- capi:%width% (* (+ char-w 2) cols)) 2))
(fg (if @board.fg (color:color-to-premultiplied
(term-color-spec-with-alpha @board.fg))
*default-foreground*))
(bg (if @board.bg (color:color-to-premultiplied
(term-color-spec-with-alpha @board.bg))
*default-background*))
(default-fg (if dark-mode-p :gray60 :gray40))
(default-bg (if dark-mode-p :black :white))
(border-fg (if dark-mode-p :gray30 :gray70))
(gw (+ char-w 2))
(gh (+ (gp:get-font-height pane font) 2))
selected borders)
(setf @pane.pad-start pad-start
(capi:simple-pane-font pane) font)
(gp:draw-rectangle pane x y w h :foreground default-bg :filled t)
(dorange$fixnum (y (floor y gh) (min (ceiling (+ y h) gh) rows))
(dorange$fixnum (x (floor x gw) (min (ceiling (+ x w) gw) cols))
(let* ((sx (+ (* x gw) pad-start))
(sy (* y gh))
(char (char *characters* (+ (* y 16) x)))
(ascent (gp:get-char-ascent pane char font)))
(gp:draw-character pane char (+ sx 1) (+ sy 1 ascent)
:foreground (if (eql char @board.char) fg default-fg)
:background (if (eql char @board.char) bg default-bg)
:block t)
(if (member char (list @board.char @pane.hover))
(setq selected (nconc selected (list sx sy gw gh)))
(setq borders (nconc borders (list sx sy gw gh)))))))
(gp:draw-rectangles pane borders :foreground border-fg)
(gp:draw-rectangles pane selected
:foreground (if dark-mode-p :white :black)
:thickness 2))))
;; Messager - "Echo Area"
(defclass messager (capi:editor-pane) ()
(:default-initargs
:buffer :temp
:visible-min-height '(character 1)
:visible-max-height t
:vertical-scroll nil
:horizontal-scroll nil
:font (gp:make-font-description :size *default-font-size*)
:enabled nil))
(defun set-message (string itf)
(let* ((pane @itf.messager)
(window (capi:editor-window pane))
(buffer (capi:editor-pane-buffer pane))
(point (buffer-point buffer)))
(let ((func (lambda ()
(clear-buffer buffer)
(editor::insert-buffer-string point string)
(buffer-start (buffer-point buffer)))))
(if window
(let ((callback (capi:output-pane-display-callback pane)))
(capi:output-pane-cache-display pane)
(setf (capi:output-pane-display-callback pane) #'true)
(process-character
(lambda (p) (declare (ignore p))
(funcall func)
(capi:apply-in-pane-process
pane
(lambda ()
(setf (capi:output-pane-display-callback pane) callback)
(capi:output-pane-free-cached-display pane)
(gp:invalidate-rectangle pane))))
window))
(funcall func)))))
;; Layer selector
(defclass layer-selector (capi:multi-column-list-panel) ()
(:default-initargs
:title "Layers"
:title-position :top
:title-adjust :center
:columns '((:title "Name" :adjust :center :visible-min-width (string "Default Layer "))
(:title "Visible" :adjust :right :visible-min-width :text-width)
(:title "Opacity" :adjust :right :visible-min-width :text-width))
:column-function (lambda (layer)
(list (layer-name layer)
(if (layer-visible layer) "✅" "❌")
(round (* (layer-alpha layer) 100))))
:print-function #'layer-name
:selection-callback #'set-layer
:action-callback (lambda (layer itf)
(set-layer layer itf)
(toggle-layer-visible itf))
:editing-callback
(lambda (self index layer action data)
(case action
(:editp (member index '(0 2)))
(:validp (case index
(0 (plusp (length data)))
(2 (every #'digit-char-p data))))
(:set (let ((itf (capi:element-interface self)))
(case index
(0 (setf (layer-name layer) data)
(capi:redisplay-collection-item self layer))
(2 (let ((data (parse-integer data :junk-allowed t)))
(setf @itf.board.current-layer.alpha (/ data 100)
(capi:range-slug-start @itf.layer-alpha-slider) data
(capi:text-input-range-value @itf.layer-alpha-value) data)
(capi:redisplay-collection-item @itf.layer-selector @itf.board.current-layer)
(refresh-board @itf.board))))))))))
(defmethod capi:make-pane-popup-menu ((pane layer-selector) itf &key)
@itf.layers-menu)
;; Refresh functions
(defun set-layer (layer pane)
(let ((itf (ensure-element-interface pane)))
(unless (eq @itf.board.current-layer layer)
(setf @itf.board.current-layer layer))
(unless (eq (capi:choice-selected-item @itf.layer-selector) layer)
(setf (capi:choice-selected-item @itf.layer-selector) layer))))
(defun set-project (project pane)
(let ((itf (ensure-element-interface pane)))
(setf @itf.board.saved t
@itf.board.project project
(capi:collection-items @itf.layer-selector) (project-layers project))
(set-layer (first (project-layers project)) pane)
(refresh-font itf)
(refresh-board @itf.board)))
(defun toggle-layer-visible (pane)
(let* ((itf (ensure-element-interface pane))
(layer @itf.board.current-layer)
(menu @itf.layer-visible-menu))
(if (layer-visible layer)
(setf (layer-visible layer) nil
(capi:choice-selection menu) nil
(capi:choice-selection @itf.layer-visible-component) nil)
(setf (layer-visible layer) t
(capi:choice-selection menu) 0
(capi:choice-selection @itf.layer-visible-component) 0))
(capi:redisplay-collection-item @itf.layer-selector layer)
(refresh-board @itf.board)))
;; Main interface
#|(capi:define-layout expanded-column-layout (capi:column-layout) ())
(defmethod capi:calculate-layout :after ((layout expanded-column-layout) x y w h)
(let (widths)
(capi:map-pane-children
layout
(lambda (child)
(capi:with-geometry child
(push capi:%width% widths)))
:do-pinboard-objects t)
(let ((width (min w (reduce #'max widths))))
(block nil
(capi:map-pane-children
layout
(lambda (child)
(capi:with-geometry child
(setf capi:%width% width)
(incf y capi:%min-height%)))
:reverse t
:do-pinboard-objects t)))))|#
(capi:define-interface main-interface ()
((tools :initform nil)
(file :initform nil)
(app-interface :initform nil
:initarg :app-interface)
(board :initform nil)
(fg-alpha :initform 1.0)
(bg-alpha :initform 1.0)
(copy-option :initform :text))
(:panes
(current current-pane)
(char-board char-board)
(messager messager)
(layer-selector layer-selector)
(cursor-movement-option
capi:option-pane
:name 'cursor-movement-option
:title "Cursor Movement"
:items '(:left :right :up :down)
:selected-item :right
:visible-min-width '(string "Right")
:visible-max-width t
:print-function #'string-capitalize
:selection-callback (lambda (data itf) (setf @itf.board.cursor-movement data)))
(settings-button
capi:toolbar-button
:text "Settings"
:name 'settings
:image 'settings
:callback-type :interface
:callback (lambda (itf)
(capi:popup-confirmer
(make 'settings-interface :parent itf)
"Settings"
:cancel-button nil)))
;; Colors
(4-bit-grid
capi:output-pane
:display-callback #'4-bit-board-display-callback
:resize-callback (lambda (pane x y w h)
(declare (ignore x y h))
(capi:set-geometric-hint pane :visible-min-height (/ w 4))
(capi:set-geometric-hint pane :visible-max-height t))
:input-model `(((:button-1 :press)
,(lambda (pane x y)
(let ((itf (capi:element-interface pane)))
(set-fg (coerce-color-to 4 (4-bit-board-translate-position pane x y) @itf.fg-alpha) pane)
(capi:set-pane-focus @itf.board))))
((:button-3 :press)
,(lambda (pane x y)
(let ((itf (capi:element-interface pane)))
(set-bg (coerce-color-to 4 (4-bit-board-translate-position pane x y) @itf.bg-alpha) pane)
(capi:set-pane-focus @itf.board))))))
(8-bit-spectrum
capi:output-pane
:display-callback #'8-bit-spectrum-callback
:resize-callback (lambda (pane x y w h)
(declare (ignore x y h))
(capi:set-geometric-hint pane :visible-min-height w)
(capi:set-geometric-hint pane :visible-max-height t))
:input-model `(((:button-1 :press)
,(lambda (pane x y)
(let ((itf (capi:element-interface pane)))
(set-fg (coerce-color-to 8 (spectrum-translate-position pane x y) @itf.fg-alpha) pane)
(capi:set-pane-focus @itf.board))))
((:button-3 :press)
,(lambda (pane x y)
(let ((itf (capi:element-interface pane)))
(set-bg (coerce-color-to 8 (spectrum-translate-position pane x y) @itf.bg-alpha) pane)
(capi:set-pane-focus @itf.board))))))
(8-bit-grayscale
capi:output-pane
:visible-min-height '(character 1)
:display-callback #'8-bit-grayscale-display-callback
:input-model `(((:button-1 :press)
,(lambda (pane x y)
(let ((itf (capi:element-interface pane)))
(set-fg (coerce-color-to 8 (8-bit-grayscale-translate-position pane x y) @itf.fg-alpha) pane)
(capi:set-pane-focus @itf.board))))
((:button-3 :press)
,(lambda (pane x y)
(let ((itf (capi:element-interface pane)))
(set-bg (coerce-color-to 8 (8-bit-grayscale-translate-position pane x y) @itf.bg-alpha) pane)
(capi:set-pane-focus @itf.board))))))
(24-bit-spectrum
capi:output-pane
:display-callback #'spectrum-callback
:resize-callback (lambda (pane x y w h)
(declare (ignore x y h))
(capi:set-geometric-hint pane :visible-min-height w)
(capi:set-geometric-hint pane :visible-max-height t))
:input-model `(((:button-1 :press)
,(lambda (pane x y)
(let ((itf (capi:element-interface pane)))
(set-fg (coerce-color-to 24 (spectrum-translate-position pane x y) @itf.fg-alpha) pane)
(capi:set-pane-focus @itf.board))))
((:button-3 :press)
,(lambda (pane x y)
(let ((itf (capi:element-interface pane)))
(set-bg (coerce-color-to 24 (spectrum-translate-position pane x y) @itf.bg-alpha) pane)
(capi:set-pane-focus @itf.board))))))
(24-bit-fg-input
capi:text-input-pane
:title "F:" :title-position :left
:change-callback-type :data-interface
:text-change-callback (lambda (data itf)
(if (> (length data) 0)
(awhen (hex-to-spec data)
(set-fg (coerce-color-to 24 it @itf.fg-alpha) itf))
(set-fg nil itf))))
(24-bit-bg-input
capi:text-input-pane
:title "B:" :title-position :left
:change-callback-type :data-interface
:text-change-callback (lambda (data itf)
(if (> (length data) 0)
(awhen (hex-to-spec data)
(set-bg (coerce-color-to 24 it @itf.bg-alpha) itf))
(set-bg nil itf))))
(color-picker-prompt-1
capi:title-pane :text "Left click to set foreground")
(color-picker-prompt-2
capi:title-pane :text "Right click to set Background")
(restore-default-colors
capi:push-button
:text "Restore default colors"
:callback-type :interface
:callback (op (set-fg nil _1) (set-bg nil _1)))
;; Alpha
(fg-alpha-value
capi:text-input-range
:start 0 :end 100 :value 100
:callback-type :data-interface
:callback (lambda (data itf)
(setf @itf.fg-alpha (/ data 100)
(capi:range-slug-start @itf.fg-alpha-slider) data)
(let ((new (copy-term-color @itf.board.fg)))
(setf (term-color-alpha new) @itf.fg-alpha)
(set-fg new itf))))
(fg-alpha-slider
capi:slider
:title "F:" :title-position :left
:start 0 :end 100 :slug-start 100
:callback (lambda (self how where)
(declare (ignore how where))
(let ((itf (capi:element-interface self))
(data (capi:range-slug-start self)))
(setf @itf.fg-alpha (/ data 100)
(capi:text-input-range-value @itf.fg-alpha-value) data)
(let ((new (copy-term-color @itf.board.fg)))
(setf (term-color-alpha new) @itf.fg-alpha)
(set-fg new itf)))))
(bg-alpha-value
capi:text-input-range
:start 0 :end 100 :value 100
:callback-type :data-interface
:callback (lambda (data itf)
(setf @itf.bg-alpha (/ data 100)
(capi:range-slug-start @itf.bg-alpha-slider) data)
(let ((new (copy-term-color @itf.board.bg)))
(setf (term-color-alpha new) @itf.bg-alpha)
(set-bg new itf))))
(bg-alpha-slider
capi:slider
:title "B:" :title-position :left
:start 0 :end 100 :slug-start 100
:callback (lambda (self how where)
(declare (ignore how where))
(let ((itf (capi:element-interface self))
(data (capi:range-slug-start self)))
(setf @itf.bg-alpha (/ data 100)
(capi:text-input-range-value @itf.bg-alpha-value) data)
(let ((new (copy-term-color @itf.board.bg)))
(setf (term-color-alpha new) @itf.bg-alpha)
(set-bg new itf)))))
;; Offset
(x-offset-input
capi:text-input-pane
:title "Offset:" :title-position :left
:text "0"
:visible-min-width '(character 4)
:visible-max-width t
:change-callback-type '(:data :element :interface)
:text-change-callback (lambda (data self itf)
(let* ((str (remove-if-not (lambda (c) (or (digit-char-p c) (eql c #\-))) data))
(num (parse-integer str :junk-allowed t)))
(when num
(setf (capi:text-input-pane-text self) (princ-to-string num)
@itf.board.x-offset num)
(refresh-board @itf.board)))))
(y-offset-input
capi:text-input-pane
:title "×" :title-position :left
:text "0"
:visible-min-width '(character 4)
:visible-max-width t
:change-callback-type '(:data :element :interface)
:text-change-callback (lambda (data self itf)
(let* ((str (remove-if-not (lambda (c) (or (digit-char-p c) (eql c #\-))) data))
(num (parse-integer str :junk-allowed t)))
(when num
(setf (capi:text-input-pane-text self) (princ-to-string num)
@itf.board.y-offset num)
(refresh-board @itf.board)))))
;; Font
(font-size-input
capi:text-input-range
:name 'font-size-input
:title "Font Size"
:start 5 :end 999 :value *default-font-size*
:visible-min-width '(character 4)
:visible-max-width t
:change-callback (lambda (data pane itf caret)
(declare (ignore pane caret))
(let* ((str (remove-if-not (lambda (c) (or (digit-char-p c) (eql c #\-))) data))
(num (parse-integer str :junk-allowed t)))
(when num
(setf @itf.board.project.font-size (max num 5))
(refresh-font itf t))))
:callback-type :data-interface
:callback (lambda (data itf)
(setf @itf.board.project.font-size (max data 5))
(refresh-font itf t)))
(font-style-panel
capi:check-button-panel
:layout-class 'capi:row-layout
:items (list (make 'capi:item :data 'bold-p :text "Bold")
(make 'capi:item :data 'italic-p :text "Italic")
(make 'capi:item :data 'underline-p :text "Underline"))
:selection-callback (lambda (data itf)
(setf (slot-value @itf.board data) t)
(refresh-style itf))
:retract-callback (lambda (data itf)
(setf (slot-value @itf.board data) nil)
(refresh-style itf)))
;; Layers
(layer-alpha-slider
capi:slider
:title "Opacity:" :title-position :left
:start 0 :end 100 :slug-start 100
:callback (lambda (self how where)
(declare (ignore how where))
(let ((itf (capi:element-interface self))
(data (capi:range-slug-start self)))
(setf @itf.board.current-layer.alpha (/ data 100)
(capi:text-input-range-value @itf.layer-alpha-value) data)
(capi:redisplay-collection-item @itf.layer-selector @itf.board.current-layer)
(refresh-board @itf.board))))
(layer-alpha-value
capi:text-input-range
:start 0 :end 100 :value 100
:callback-type :data-interface
:callback (lambda (data itf)
(setf @itf.board.current-layer.alpha (/ data 100)
(capi:range-slug-start @itf.layer-alpha-slider) data)
(capi:redisplay-collection-item @itf.layer-selector @itf.board.current-layer)
(refresh-board @itf.board)))
(move-layer-up-button
capi:toolbar-button
:image 'up
:remapped 'move-layer-up)
(move-layer-down-button
capi:toolbar-button
:image 'down
:remapped 'move-layer-down)
(add-layer-button
capi:toolbar-button
:image 'add
:remapped 'add-layer)
(delete-layer-button
capi:toolbar-button
:image 'remove
:remapped 'delete-layer)
(layer-visible-button
capi:toolbar-button
:selected nil
:image 'visible
:selected-image 'invisible
:remapped 'layer-invisible)
(layer-visible-component
capi:toolbar-component
:items '(layer-visible-button)
:selection nil
:interaction :single-selection)
(layout-toolbar
capi:toolbar
:flatp t
:image-width 20 :image-height 20
:button-width 32 :button-height 32))
(:layouts
(main-layout capi:row-layout
'(left-column :divider center-column :divider right-column)
:ratios '(1 nil 3 nil 1))
(center-column capi:column-layout
'(container messager))
(container board-container nil)
(left-column capi:column-layout
'(offset-row
:separator
tool-settings-container
:divider
layer-selector
layout-alpha-row
layout-toolbar)
:adjust :right)
(offset-row capi:row-layout
'(x-offset-input
y-offset-input)
:adjust :right)
(tool-settings-container capi:simple-layout
'()
:vertical-scroll t)
(layout-alpha-row capi:row-layout
'(layer-alpha-slider
layer-alpha-value))
(right-column capi:column-layout
'(colors-tab
alpha-column
restore-default-colors
:separator
font-style-panel
char-board)
:adjust :center)
(colors-tab capi:tab-layout
()
:items '(4-bit 8-bit 24-bit)
:print-function #'string-downcase
:visible-child-function #'identity)
(4-bit capi:column-layout
'(color-picker-prompt-1 color-picker-prompt-2 4-bit-grid)
:adjust :center)
(8-bit capi:column-layout
'(8-bit-spectrum 8-bit-grayscale)
:adjust :center)
(24-bit capi:column-layout
'(24-bit-spectrum 24-bit-input-row)
:adjust :center)
(24-bit-input-row capi:row-layout
'(24-bit-fg-input 24-bit-bg-input))
(alpha-column capi:column-layout
'(fg-alpha-row bg-alpha-row)
:title "Opacity"
:title-position :top
:title-adjust :center
:title-gap 0
:gap 0)
(fg-alpha-row capi:row-layout
'(fg-alpha-slider fg-alpha-value))
(bg-alpha-row capi:row-layout
'(bg-alpha-slider bg-alpha-value)))
(:menus
(file-menu
"File"
((:component
(("New" :name 'file-new :callback #'file-new)
("Open..." :name 'file-open :callback #'file-open)
("Save" :name 'file-save :callback #'file-save)
("Save As..." :name 'file-save-as :callback #'file-save-as)
("Export" :name 'file-export :callback #'file-export))
:callback-type :interface)))
(edit-menu
"Edit"
((:component
(("Undo" :callback #'(lambda (itf)
(if (capi:pane-has-focus-p @itf.board)
(board-undo itf)
(capi:active-pane-undo itf)))
:enabled-function #'capi:active-pane-undo-p)))
(:component
(("Cut" :callback #'(lambda (itf)
(if (capi:pane-has-focus-p @itf.board)
(board-cut itf)
(capi:active-pane-cut itf)))
:enabled-function #'capi:active-pane-cut-p)
("Copy" :callback #'(lambda (itf)
(if (capi:pane-has-focus-p @itf.board)
(board-copy itf)
(capi:active-pane-copy itf)))
:enabled-function #'capi:active-pane-copy-p)
("Paste" :callback #'(lambda (itf)
(if (capi:pane-has-focus-p @itf.board)
(board-paste itf)
(capi:active-pane-paste itf)))
:enabled-function #'capi:active-pane-paste-p)))
(:component
(("Copy to plain text" :callback (op (setf @_.copy-option :text)))
("Copy to HTML" :callback (op (setf @_.copy-option :html)))
("Copy to ANSI Escaped sequence" :callback (op (setf @_.copy-option :ansi))))
:interaction :single-selection)
(:component
(("Move Left after insert" :data :left :callback #'change-cursor-movement :accelerator "accelerator-shift-left")
("Move Right after insert" :data :right :callback #'change-cursor-movement :accelerator "accelerator-shift-right")
("Move Up after insert" :data :up :callback #'change-cursor-movement :accelerator "accelerator-shift-up")
("Move Down after insert" :data :down :callback #'change-cursor-movement :accelerator "accelerator-shift-down"))
:selected-item :right
:interaction :single-selection
:callback-type :data-interface))
:callback-type :interface)
(layer-visible-menu
:component
(("Visible" :name 'layer-invisible))
:callback #'toggle-layer-visible
:callback-type :interface
:interaction :single-selection)
(layers-menu
"Layers"
((:component
(("Add Layer" :name 'add-layer
:callback (lambda (itf)
(let ((new (make-layer :name "New Layer")))
(push new @itf.board.project.layers)
(setf (capi:collection-items @itf.layer-selector) @itf.board.project.layers)
(set-layer new itf)
(capi:collection-item-edit @itf.layer-selector new)
(capi:collection-item-set-editing-string @itf.layer-selector "New Layer"))))
("Delete Layer" :name 'delete-layer
:callback (lambda (itf)
(let ((layer (capi:choice-selected-item @itf.layer-selector))
(layers @itf.board.project.layers))
(if (= (length layers) 1)
(capi:prompt-with-message "Cannot delete the only layer!")
(progn
(setf layers (delete layer layers)
@itf.board.project.layers layers
(capi:collection-items @itf.layer-selector) layers)
(set-layer (first layers) itf)
(refresh-board @itf.board))))))
layer-visible-menu
("Move Current Layer Up" :name 'move-layer-up
:callback (lambda (itf)
(let ((index (capi:choice-selection @itf.layer-selector)))
(when (plusp index)
(rotatef (nth (1- index) @itf.board.project.layers)
(nth index @itf.board.project.layers))
(setf (capi:collection-items @itf.layer-selector) @itf.board.project.layers
(capi:choice-selection @itf.layer-selector) (1- index))
(refresh-board @itf.board)))))
("Move Current Layer Down" :name 'move-layer-down
:callback (lambda (itf)
(let ((index (capi:choice-selection @itf.layer-selector)))
(when (< index (1- (length @itf.board.project.layers)))
(rotatef (nth index @itf.board.project.layers)
(nth (1+ index) @itf.board.project.layers))
(setf (capi:collection-items @itf.layer-selector) @itf.board.project.layers
(capi:choice-selection @itf.layer-selector) (1+ index))
(refresh-board @itf.board))))))))
:callback-type :interface)
(tools-menu
"Tools"
((:component nil
:items-function
(lambda (itf) (declare (ignore itf))
(with-collector (c)
(dolist* (i name *all-tools-names*)
(let ((name name))
(c (make 'capi:menu-item
:name name
:text (string-capitalize name)
:accelerator (char (princ-to-string i) 0)
:callback (lambda (itf) (set-tool itf @itf.board name))))))))
:callback-type :interface)))
(window-menu
"Window"
((:component
(("Close Window" :callback #'capi:quit-interface :accelerator #\w)
("Minimize" :callback (op (setf (capi:top-level-interface-display-state _) :iconic))
:accelerator #\m))))
:callback-type :interface)
(help-menu
"Help"
((:component
(("Contact developer" :callback (op (sys:open-url "http://apr.sdf.org/contact.html")))
("Privacy Policy" :callback (op (capi:display (make 'privacy-interface)))))))
:callback-type :none))
(:menu-bar file-menu edit-menu layers-menu tools-menu window-menu help-menu)
(:default-initargs
:title "Charapainter"
:confirm-destroy-callback #'check-saved
:destroy-callback (op (awhen @_.app-interface
(when (null (capi:collect-interfaces 'main-interface))
(capi:destroy it))))
:best-width '(* :screen-width 0.8)
:best-height '(* :screen-height 0.65)
:initial-focus 'container))
(defmethod capi:interface-keys-style ((self main-interface)) :emacs)
(defmethod initialize-instance :after ((self main-interface) &key)
(setf @self.board @self.container.board
@self.tools (mapcar (op (make _ :board @self.board)) *all-tools-names*))
(setf (capi:interface-toolbar-items self)
(list @self.current
(make 'capi:toolbar-component
:items (mapcar (lambda (name)
(make 'capi:toolbar-button
:name name :data name :image name :remapped name
:print-function #'string-capitalize))
*all-tools-names*))
@self.font-size-input
@self.cursor-movement-option
@self.settings-button)
(capi:interface-toolbar-state self :items)
(append '(:flexible-space current :space)
*all-tools-names*
'(font-size-input cursor-movement-option :space settings :flexible-space))
(capi:collection-items @self.layer-selector) (project-layers @self.board.project)
(capi:collection-items @self.layer-visible-component)
(list @self.layer-visible-button)
(capi:collection-items @self.layout-toolbar)
(list @self.layer-visible-component
@self.move-layer-up-button
@self.move-layer-down-button
@self.add-layer-button
@self.delete-layer-button))
(set-tool self @self.board 'brush))
(defmethod set-tool-internal :after ((itf main-interface) board tool)
(when (eq @itf.board.current-tool tool)
(unless @tool.settings-layout
(setf @tool.settings-layout (make-settings-layout itf tool)))
(setf (capi:layout-description @itf.tool-settings-container) (list @tool.settings-layout))))
;; Settings & other interfaces
(capi:define-interface settings-interface ()
((parent :initarg :parent))
(:panes
(font-option
capi:push-button
:title "" :title-position :left
:text "Choose Font"