-
Notifications
You must be signed in to change notification settings - Fork 9
/
buffers.lisp
1397 lines (1211 loc) · 41.5 KB
/
buffers.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
;;; buffers.lisp --- squeakish lispspaces
;; Copyright (C) 2006-2013 David O'Toole
;; Author: David O'Toole [email protected]
;; Keywords:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see %http://www.gnu.org/licenses/
(in-package :blocky)
(define-block buffer
(name :initform nil)
(variables :initform nil
:documentation "Hash table mapping values to values, local to the current buffer.")
(cursor :initform nil)
(point :initform nil :documentation "The currently highlighted word.")
(modified-p :initform nil)
(followed-object :initform nil)
(background-image :initform nil)
(background-color :initform nil)
(redraw-cursor :initform t)
(category :initform :data)
(x :initform 0)
(y :initform 0)
(paused :initform nil)
(heading :initform 0.0)
(height :initform 256)
(width :initform 256)
(depth :initform *z-far*)
(field-of-view :initform *field-of-view*)
(was-key-repeat-p :initform nil)
;; objects and collisions
(objects :initform nil :documentation "A hash table with all the buffer's objects.")
(quadtree :initform nil)
(quadtree-depth :initform nil)
;; viewing window
(window-x :initform 0)
(window-y :initform 0)
(window-z :initform 0)
(window-x0 :initform nil)
(window-y0 :initform nil)
(window-z0 :initform nil)
(horizontal-scrolling-margin :initform 1/4)
(vertical-scrolling-margin :initform 1/4)
(window-scrolling-speed :initform 5)
(window-scale-x :initform 1)
(window-scale-y :initform 1)
(window-scale-z :initform 1)
(projection-mode :initform :orthographic)
(rewound-selection :initform nil)
(future :initform nil)
(future-steps :initform 32)
(future-step-interval :initform 8)
(default-events :initform
'(((:pause) :transport-toggle-play)
;; ((:e :alt) :edit-word)
;; ((:x :control) :exec)
;; ((:d :control) :delete-word)
;; ((:c :control) :copy-word)
((:x :alt) :command-prompt)
((:g :control) :cancel)
((:c :alt) :clear-stack)
((:s :alt) :show-stack)
((:m :alt) :show-messages)
; ((:p :control) :paste)
; ((:return) :enter)
((:escape) :cancel)
; ((:f1) :help)
((:h :control) :help)
((:x :control) :edit-cut)
((:c :control) :edit-copy)
((:v :control) :edit-paste)
((:v :control :shift) :paste-here)
((:f9) :toggle-minibuffer)
((:f12) :transport-toggle-play)
((:g :control) :escape)
((:d :control) :drop-selection)))
;; ((:f12) :toggle-other-windows)
;; ))
;; prototype control
(excluded-fields :initform
'(:events :quadtree :click-start :click-start-block :drag-origin :drag-start :drag-offset :focused-block :minibuffer :drag :hover :highlight
;; program objects are not saved:
:inputs)
:documentation "Don't serialize the menu bar.")
(field-collection-type :initform :hash)
;; rectangle-select
(region :initform nil)
(region-start :initform nil)
;; dragging info
(drag :initform nil
:documentation "Block being dragged, if any.")
(drag-button :initform nil)
(hover :initform nil
:documentation "Block being hovered over, if any.")
(highlight :initform nil
:documentation "Block being highlighted, if any.")
(ghost :initform nil
:documentation "Dummy block to hold original place of currently dragged block onscreen.")
(focused-block :initform nil
:documentation "Block having current input focus, if any.")
(last-focus :initform nil)
(click-start :initform nil
:documentation "A cons (X . Y) of widget location at moment of click.")
(click-start-block :initform nil
:documentation "The block indicated at the beginning of a drag.")
(drag-origin :initform nil
:documentation "The parent block originally holding the dragged block.")
(object-p :initform nil
:documentation "When non-nil, the dragged object is in the buffer.")
(drag-start :initform nil
:documentation "A cons (X . Y) of widget location at start of dragging.")
(drag-offset :initform nil
:documentation "A cons (X . Y) of relative mouse click location on dragged block."))
(defun uniquify-buffer-name (name)
(let ((n 1)
(name0 name))
(block naming
(loop while name0 do
(if (find-buffer name0 :noerror t)
(setf name0 (format nil "~A<~S>" name n)
n (1+ n))
(return-from naming name0))))))
(define-method begin-region buffer ()
(setf %region-start (list (window-pointer-x) (window-pointer-y))))
(define-method update-region buffer ()
(when %region-start
(let ((x (window-pointer-x))
(y (window-pointer-y)))
(destructuring-bind (x0 y0) %region-start
;; always normalize it
(setf %region
(list (min x x0)
(min y y0)
(abs (- x x0))
(abs (- y y0))))))))
(define-method end-region buffer ()
(setf %region-start nil))
(define-method draw-region buffer ()
(when (consp %region)
(destructuring-bind (x y width height) %region
(draw-box x y width height :color "gray60" :alpha (max 0.2 (+ 0.2 (sin (/ *updates* 2))))))))
(define-method clear-region buffer ()
(setf %region nil %region-start nil))
(defun make-buffer-name (name)
(uniquify-buffer-name (or name "*untitled*")))
(defmacro define-buffer (name &body body)
`(define-block (,name :super buffer)
,@body))
(defmacro with-buffer (buffer &rest body)
`(let* ((*buffer* (find-uuid ,buffer)))
,@body))
(define-method toggle-other-windows buffer ()
(glass-toggle))
(define-method set-modified-p buffer (&optional (value t))
(setf %modified-p value))
(defun buffer-modified-p (&optional (buffer (current-buffer)))
(%modified-p buffer))
(defun selection ()
(get-selection (current-buffer)))
(defun selected-object ()
(let ((sel (selection)))
(assert (consp sel))
(first sel)))
(defun clear-selection ()
(clear-halos (current-buffer))
(clear-deleted-program-objects (current-buffer)))
(defun select-all ()
(clear-halos (current-buffer))
(loop for thing being the hash-values in
(%objects (current-buffer))
do (make-halo thing)))
(define-method get-objects buffer ()
(loop for object being the hash-values in %objects
when (blockyp object) collect object))
(define-method has-object buffer (thing)
(gethash (find-uuid thing) %objects))
(define-method region-objects buffer ()
(when %region
(destructuring-bind (x y width height) %region
(loop for thing being the hash-values of %objects
when (colliding-with-rectangle thing y x width height)
collect thing))))
(define-method select-region buffer ()
(when %region
(clear-selection)
(dolist (each (region-objects self))
(make-halo each))
(clear-region self)))
(define-method destroy-region buffer ()
(when %region
(clear-selection)
(prog1 nil
(dolist (each (region-objects self))
(destroy each)))))
(define-method destroy-selection buffer ()
(prog1 nil (mapc #'destroy (selection))))
(define-method emptyp buffer ()
(or (null %objects)
(zerop (hash-table-count %objects))))
(define-method initialize buffer (&optional name)
(initialize%super self)
(setf %objects (make-hash-table :test 'equal))
(when name
(let ((buffer-name (make-buffer-name name)))
(setf %buffer-name buffer-name)
(add-buffer buffer-name self))))
(define-method rename buffer (name)
(assert (stringp name))
(when (find-buffer name :noerror t)
(kill-buffer name))
(add-buffer name self))
(defun point ()
(%point (current-buffer)))
(defun set-point (word)
(setf (%point (current-buffer)) word))
;; Defining and scrolling the screen viewing window
(defun window-y () (%window-y (current-buffer)))
(defun window-x () (%window-x (current-buffer)))
(define-method window-bounding-box buffer ()
(values %window-y
%window-x
(+ %window-x *gl-screen-width*)
(+ %window-y *gl-screen-height*)))
(define-method move-window-to buffer (x y &optional z)
(setf %window-x x
%window-y y)
(when z (setf %window-z z)))
(define-method move-window-to-object buffer (object)
(multiple-value-bind (top left right bottom)
(bounding-box object)
(declare (ignore right bottom))
(move-window-to
self
(max 0 (- left (/ *gl-screen-width* 2)))
(max 0 (- top (/ *gl-screen-width* 2))))))
(define-method move-window-to-cursor buffer ()
(when %cursor
(move-window-to-object self %cursor)))
(define-method snap-window-to-object buffer (object)
(multiple-value-bind (top left right bottom)
(bounding-box object)
(declare (ignore right bottom))
(move-window-to
self
(min (- %width (+ %window-x *gl-screen-width*))
(max 0 (- left (/ *gl-screen-width* 2))))
(min (- %height (+ %window-y *gl-screen-height*))
(max 0 (- top (/ *gl-screen-width* 2)))))))
(define-method snap-window-to-cursor buffer ()
(when %cursor
(snap-window-to-object self %cursor)))
(define-method move-window buffer (dx dy &optional dz)
(incf %window-x dx)
(incf %window-y dy)
(when dz (setf %window-dz dz)))
(define-method glide-window-to buffer (x y &optional z)
(setf %window-x0 x)
(setf %window-y0 y)
(when z (setf %window-z z)))
(define-method glide-window-to-object buffer (object)
(multiple-value-bind (top left right bottom)
(bounding-box object)
(declare (ignore right bottom))
(glide-window-to
self
(max 0 (- left (/ *gl-screen-width* 2)))
(max 0 (- top (/ *gl-screen-width* 2))))))
(define-method glide-window-to-cursor buffer ()
(when %cursor
(glide-window-to-object self %cursor)))
(define-method follow-with-camera buffer (thing)
(assert (or (null thing) (blockyp thing)))
(setf %followed-object thing)
(glide-window-to-object self %followed-object))
(define-method glide-follow buffer (object)
(with-fields (window-x window-y width height) self
(let ((margin-x (* %horizontal-scrolling-margin *gl-screen-width*))
(margin-y (* %vertical-scrolling-margin *gl-screen-height*))
(object-x (field-value :x object))
(object-y (field-value :y object)))
;; are we outside the "comfort zone"?
(if (or
;; too far left
(> (+ window-x margin-x)
object-x)
;; too far right
(> object-x
(- (+ window-x *gl-screen-width*)
margin-x))
;; too far up
(> (+ window-y margin-y)
object-y)
;; too far down
(> object-y
(- (+ window-y *gl-screen-height*)
margin-y)))
;; yes. recenter.
(glide-window-to self
(max 0
(min (- width *gl-screen-width*)
(- object-x
(truncate (/ *gl-screen-width* 2)))))
(max 0
(min (- height *gl-screen-height*)
(- object-y
(truncate (/ *gl-screen-height* 2))))))))))
(define-method update-window-glide buffer ()
(with-fields (window-x window-x0 window-y window-y0 window-scrolling-speed) self
(labels ((nearby (a b)
(> window-scrolling-speed (abs (- a b))))
(jump (a b)
(if (< a b) window-scrolling-speed (- window-scrolling-speed))))
(when (and window-x0 window-y0)
(if (nearby window-x window-x0)
(setf window-x0 nil)
(incf window-x (jump window-x window-x0)))
(if (nearby window-y window-y0)
(setf window-y0 nil)
(incf window-y (jump window-y window-y0)))))))
(define-method scale-window buffer (&optional (window-scale-x 1.0) (window-scale-y 1.0))
(setf %window-scale-x window-scale-x)
(setf %window-scale-y window-scale-y))
(define-method project-window buffer ()
(ecase %projection-mode
(:orthographic (project-orthographically))
(:perspective (project-with-perspective :field-of-view %field-of-view :depth %depth)))
(transform-window :x %window-x :y %window-y :z %window-z
:scale-x %window-scale-x
:scale-y %window-scale-y
:scale-z %window-scale-z))
;;; Transport control
(define-method transport-pause buffer ()
(setf %paused t)
(setf %rewound-selection
(mapcar #'duplicate
(get-selection self))))
(define-method transport-play buffer ()
(setf %paused nil)
(clear-future self)
(mapc #'destroy (get-selection self))
(dolist (each %rewound-selection)
(add-object (current-buffer) each))
(setf %rewound-selection nil))
(define-method transport-toggle-play buffer ()
(if %paused
(transport-play self)
(transport-pause self)))
(define-method show-future buffer ()
(prog1 nil
(let ((selection (get-selection self)))
(let (future)
(dolist (thing selection)
(remove-object self thing)
(let (trail)
(dotimes (i %future-steps)
(let ((ghost (duplicate thing)))
(with-buffer self
(with-quadtree %quadtree
(add-object self ghost)
(assert (%quadtree-node ghost))
(dotimes (j (* i %future-step-interval))
(update ghost)
(run-tasks ghost)
(quadtree-collide ghost))))
(remove-object self ghost)
(push ghost trail)))
(push trail future))
(add-object self thing)
(make-halo thing))
(setf %future future)))))
(define-method clear-future buffer ()
(setf %future nil))
(define-method update-future buffer ()
(when %future (show-future self)))
;;; The object layer holds the contents of the buffer.
(defvar *object-placement-capture-hook*)
(define-method add-object buffer (object &optional x y (z 0))
(with-buffer self
(with-quadtree %quadtree
;; (remove-thing-maybe self object)
;; (assert (not (contains-object self object)))
(setf (gethash (find-uuid object)
%objects)
(find-uuid object))
(when (and (numberp x) (numberp y))
(setf (%x object) x
(%y object) y))
(when (numberp z)
(setf (%z object) z))
(clear-saved-location object)
(quadtree-insert-maybe object)
(after-place-hook object))))
(define-method remove-object buffer (object)
(destroy-halo object)
(when (%quadtree-node object)
(quadtree-delete-maybe object))
(remhash (find-uuid object) %objects))
(define-method remove-thing-maybe buffer (object)
(with-buffer self
(when (gethash (find-uuid object) %objects)
(remove-object self object))
(when (%parent object)
(unplug-from-parent object))))
(define-method add-block buffer (object &optional x y prepend)
(remove-thing-maybe self object)
(add-block%super self object x y))
(define-method drop-block buffer (object x y)
(add-object self object)
(move-to object x y))
(define-method drop-object buffer (object &optional x y)
(add-object self object)
(when (and (numberp x) (numberp y))
(move-to object x y))
(after-drop-hook object))
(define-method drop-selection buffer ()
(dolist (each (get-selection self))
(drop-object self each)))
(define-method add-at-pointer buffer (object)
(layout object)
(add-block self object
(window-pointer-x)
(- (window-pointer-y) (%height object))
:prepend)
(focus-on self object))
(define-method contains-object buffer (object)
(gethash (find-uuid object)
%objects))
(define-method destroy-block buffer (object)
(remhash (find-uuid object) %objects))
;;; Buffer-local variables
(define-method initialize-variables-maybe buffer ()
(when (null %variables)
(setf %variables (make-hash-table :test 'equal))
(setf (gethash "BUFFER" %variables) self)))
(define-method set-variable buffer (var value)
(initialize-variables-maybe self)
(setf (gethash var %variables) value))
(define-method get-variable buffer (var)
(initialize-variables-maybe self)
(gethash var %variables))
(defun buffer-variable (var-name)
(get-variable (current-buffer) var-name))
(defun set-buffer-variable (var-name value)
(set-variable (current-buffer) var-name value))
(defsetf buffer-variable set-buffer-variable)
(defmacro with-buffer-variables (vars &rest body)
(labels ((make-clause (sym)
`(,sym (buffer-variable ,(make-keyword sym)))))
(let* ((symbols (mapcar #'make-non-keyword vars))
(clauses (mapcar #'make-clause symbols)))
`(symbol-macrolet ,clauses ,@body))))
;;; About the cursor. deprecated.
(define-method get-cursor buffer ()
%cursor)
(defun cursor ()
(get-cursor (current-buffer)))
(defun cursorp (thing)
(object-eq thing (cursor)))
(define-method set-cursor buffer (cursor)
(setf %cursor (find-uuid cursor)))
;; (unless (contains-object self cursor)
;; (add-object self cursor)))
;;; Configuring the buffer's space and its quadtree indexing
(defparameter *buffer-bounding-box-scale* 1.01
"Actual size of bounding box used for quadtree. The buffer is bordered
around on all sides by a thin margin designed to prevent objects near
the edge of the universe piling up into the top quadrant and causing
slowdown. See also quadtree.lisp")
(define-method install-quadtree buffer ()
;; make a box with a one-percent margin on all sides.
;; this margin helps edge objects not pile up in quadrants
(let ((box (multiple-value-list
(scale-bounding-box
(multiple-value-list (bounding-box self))
*buffer-bounding-box-scale*))))
(with-fields (quadtree) self
(setf quadtree (build-quadtree
box
(or %quadtree-depth
*default-quadtree-depth*)))
(assert quadtree)
(let ((objects (get-objects self)))
(when objects
(quadtree-fill objects quadtree))))))
(define-method resize buffer (new-height new-width)
(assert (and (plusp new-height)
(plusp new-width)))
(with-fields (height width quadtree objects) self
(setf height new-height)
(setf width new-width)
(when quadtree
(install-quadtree self))))
(define-method reset buffer ())
(define-method trim buffer ()
(prog1 self
(let ((objects (get-objects self)))
(when objects
(with-fields (quadtree height width) self
;; adjust bounding box so that all objects have positive coordinates
(multiple-value-bind (top left right bottom)
(find-bounding-box objects)
;; resize the buffer so that everything just fits
(setf %x 0 %y 0)
(resize self (- bottom top) (- right left))
;; move all the objects
(dolist (object objects)
(with-fields (x y) object
(with-quadtree quadtree
(move-to object (- x left) (- y top)))))))))))
;;; Cut and paste
(define-method get-selection buffer ()
(let ((all (append (get-objects self) %inputs)))
(remove-if-not #'%halo all)))
(defun copy (&optional (self (current-buffer)) objects0)
(let ((objects (or objects0 (get-selection self))))
(clear-halos self)
(when objects
(setf *clipboard* (new 'buffer))
(dolist (object objects)
(let ((duplicate (duplicate object)))
;; don't keep references to anything in the (current-buffer)
(clear-buffer-data duplicate)
(add-object *clipboard* duplicate))))))
(defun cut (&optional (self (current-buffer)) objects0)
(with-buffer self
(let ((objects (or objects0 (get-selection self))))
(when objects
(clear-halos self)
(setf *clipboard* (new 'buffer))
(dolist (object objects)
(with-quadtree (%quadtree self)
(remove-thing-maybe self object))
(add-object *clipboard* object))))))
(defun paste-from (self source &optional (dx 0) (dy 0))
(dolist (object (mapcar #'duplicate (get-objects source)))
(with-fields (x y) object
(clear-buffer-data object)
(with-buffer self
(with-quadtree (%quadtree self)
(add-object self object)
(move-to object (+ x dx) (+ y dy)))))))
(defun paste (&optional (self (current-buffer)) (dx 0) (dy 0))
(paste-from self *clipboard* dx dy))
(defun paste-at-pointer (&optional (self (current-buffer)))
(let ((temp (new 'buffer)))
(paste-from temp *clipboard*)
(send :trim temp)
(paste-from self temp
(window-pointer-x)
(window-pointer-y))))
(define-method paste-here buffer ()
(paste-at-pointer self))
(define-method edit-cut buffer ()
(cut))
(define-method edit-paste buffer ()
(paste))
(define-method edit-copy buffer ()
(copy))
(defun paste-as-new-buffer ()
(let ((temp (new 'buffer "*new-buffer*")))
(paste-from temp *clipboard*)
(trim temp)
(switch-to-buffer temp)))
;; (define-method paste-cut
;;; Algebraic operations on buffers and their contents
(defvar *buffer-prototype* "BLOCKY:BUFFER")
(defmacro with-buffer-prototype (buffer &rest body)
`(let ((*buffer-prototype* (find-super ,buffer)))
,@body))
(define-method adjust-bounding-box-maybe buffer ()
(if (emptyp self)
self
(let ((objects-bounding-box
(multiple-value-list
(find-bounding-box (get-objects self)))))
(destructuring-bind (top left right bottom)
objects-bounding-box
;; are all the objects inside the existing box?
(prog1 self
(unless (bounding-box-contains
(multiple-value-list (bounding-box self))
objects-bounding-box)
(resize self bottom right)))))))
(defmacro with-new-buffer (&body body)
`(with-buffer (clone *buffer-prototype*)
,@body
(adjust-bounding-box-maybe (current-buffer))))
(defun translate (buffer dx dy)
(when buffer
(assert (and (numberp dx) (numberp dy)))
(with-new-buffer
(paste-from (current-buffer) buffer dx dy))))
(defun combine (buffer1 buffer2)
(with-new-buffer
(when (and buffer1 buffer2)
(dolist (object (nconc (get-objects buffer1)
(get-objects buffer2)))
(add-object (current-buffer) object)))))
(define-method scale buffer (sx &optional sy)
(let ((objects (get-objects self)))
(dolist (object objects)
(with-fields (x y width height) object
(move-to object (* x sx) (* y (or sy sx)))
(resize object (* width sx) (* height (or sy sx))))))
(trim self))
;(define-method destroy-region buffer (bounding-box))
(defun vertical-extent (buffer)
(if (or (null buffer)
(emptyp buffer))
0
(multiple-value-bind (top left right bottom)
(bounding-box buffer)
(declare (ignore left right))
(- bottom top))))
(defun horizontal-extent (buffer)
(if (or (null buffer)
(emptyp buffer))
0
(multiple-value-bind (top left right bottom)
(bounding-box buffer)
(declare (ignore top bottom))
(- right left))))
(defun arrange-below (&optional buffer1 buffer2)
(when (and buffer1 buffer2)
(combine buffer1
(translate buffer2
0
(field-value :height buffer1)))))
(defun arrange-beside (&optional buffer1 buffer2)
(when (and buffer1 buffer2)
(combine buffer1
(translate buffer2
(field-value :width buffer1)
0))))
(defun stack-vertically (&rest buffers)
(reduce #'arrange-below buffers :initial-value (with-new-buffer)))
(defun stack-horizontally (&rest buffers)
(reduce #'arrange-beside buffers :initial-value (with-new-buffer)))
(define-method flip-horizontally buffer ()
(let ((objects (get-objects self)))
(dolist (object objects)
(with-fields (x y) object
(move-to object (- x) y))))
;; get rid of negative coordinates
(trim self))
(define-method flip-vertically buffer ()
(let ((objects (get-objects self)))
(dolist (object objects)
(with-fields (x y) object
(move-to object x (- y)))))
(trim self))
(define-method mirror-horizontally buffer ()
(stack-horizontally
self
(flip-horizontally (duplicate self))))
(define-method mirror-vertically buffer ()
(stack-vertically
self
(flip-vertically (duplicate self))))
(defun with-border (border buffer)
(with-fields (height width) buffer
(with-new-buffer
(paste-from (current-buffer) buffer border border)
(resize (current-buffer)
(+ height (* border 2))
(+ width (* border 2))))))
;;; The Program is an optional layer of objects on top of the buffer
(define-method add-minibuffer-maybe buffer (&optional force)
(when (or force (null *minibuffer*))
(setf *minibuffer*
(new 'minibuffer))))
(define-method enter-minibuffer buffer ()
(when (not *minibuffer-open-p*)
(add-minibuffer-maybe self)
(setf %last-focus %focused-block)
(focus-on self (minibuffer-prompt) :clear-selection nil)
(when (null *minibuffer-open-p*) (setf %was-key-repeat-p (key-repeat-p)))
(setf *minibuffer-open-p* t)
(enable-key-repeat)))
(define-method command-prompt buffer ()
(enter-minibuffer self)
(focus-on self (minibuffer-prompt) :clear-selection nil))
(define-method exit-minibuffer buffer ()
(when *minibuffer-open-p*
;; (add-minibuffer-maybe self)
(setf *minibuffer-open-p* nil)
(focus-on self %last-focus :clear-selection nil)
(setf %last-focus nil)
(unless %was-key-repeat-p
(disable-key-repeat))
(setf %was-key-repeat-p nil)))
(define-method toggle-minibuffer buffer ()
(if *minibuffer-open-p*
(exit-minibuffer self)
(enter-minibuffer self)))
(define-method grab-focus buffer ())
(define-method layout-program-objects buffer ()
(mapc #'layout %inputs))
(define-method update-program-objects buffer ()
(mapc #'update %inputs)
(update *minibuffer*))
(define-method draw-program-objects buffer ()
(with-buffer self
(with-fields (drag-start drag focused-block
highlight inputs hover point
ghost prompt) self
;; now start drawing the program objects
(mapc #'draw inputs)
;; draw any future
(when %future
(let ((*image-opacity* 0.2))
(dolist (trail %future)
(mapc #'draw trail))))
;; during dragging we draw the dragged block.
(when drag
(layout drag)
(when (field-value :parent drag)
(draw-ghost ghost))
;; also draw any hover-over highlights
;; on objects you might drop stuff onto
(when hover
(draw-hover hover))
(draw drag))
(when *minibuffer*
(draw *minibuffer*))
(when (blockyp %cursor)
(draw-cursor %cursor))
;; draw focus
(when focused-block
(when (blockyp focused-block))
(draw-focus focused-block)))))
;;
;; ;; (when *minibuffer*
;; ;; (draw-focus (minibuffer-prompt)))
;; ;; (when highlight
;; ;; (draw-highlight highlight))
;; (when (and point (read-only-p point))
;; (draw-point point)))))
(define-method draw-programs buffer ())
(define-method draw buffer ()
(with-buffer self
(with-field-values (objects width focused-block height
background-image background-color) self
(unless %parent
(project-window self))
;; (when %parent
;; (gl:push-matrix)
;; (gl:translate %x %y 0))
;; draw background
(if background-image
(draw-image background-image 0 0)
(when background-color
(draw-box 0 0 width height
:color background-color)))
;; now draw the object layer
(let ((box (multiple-value-list (window-bounding-box self))))
(loop for object being the hash-values in objects do
;; only draw onscreen objects
(when (colliding-with-bounding-box object box)
(draw object))))
;; possibly redraw cursor to ensure visibility.
(when (and (blockyp %cursor) %redraw-cursor)
(draw %cursor))
;; draw region if needed
(when %region (draw-region self))
;; draw any overlays
(if *minibuffer-open-p*
(draw-program-objects self)
(draw-programs self)))))
;; ;; draw focus
;; (when focused-block
;; (assert (blockyp focused-block))
;; (draw-focus focused-block))
;; )))
;; (if %parent
;; (gl:pop-matrix)
;; possibly draw minibuffer
;; (if *minibuffer-open-p*
;; (draw-minibuffer-objects self)
;; (draw-minibuffers self)))))
;;; Simulation update
(define-method clear-deleted-program-objects buffer ()
;; clean up any deleted objects
(when (not (blockyp %cursor)) (setf %cursor nil))
(when (not (blockyp %drag)) (setf %drag nil))
(when (not (blockyp %point)) (setf %point nil))
(when (not (blockyp %drag-origin)) (setf %drag-origin nil))
(when (not (blockyp %hover)) (setf %hover nil))
(when (not (blockyp %focused-block)) (setf %focused-block nil))
(when (not (blockyp %last-focus)) (setf %last-focus nil)))
(define-method clear-deleted-objects buffer ()
(loop for object being the hash-keys of %objects
do (unless (blockyp object) (remhash object %objects))))
(define-method update buffer ()
(with-field-values (objects drag cursor) self
;; build quadtree if needed
(when (null %quadtree)
(install-quadtree self))
(assert %quadtree)
(unless %paused
(with-buffer self
;; enable quadtree for collision detection
(with-quadtree %quadtree
;; possibly run the objects
(loop for object being the hash-values in %objects do
(if (blockyp object)
(progn
(update object)
;; might have been destroyed during update.
(when (blockyp object)
(run-tasks object)))
(remhash object %objects)))
;; detect collisions
(loop for object being the hash-values in objects do
(unless (eq :passive (field-value :collision-type object))
(quadtree-collide object)))
;; update window movement
(let ((thing (or
%followed-object
(when (holding-shift) drag)
cursor)))
(when (blockyp thing)
(glide-follow self thing)
(update-window-glide self))))))
;; now outside the quadtree,
;; possibly update the program layer
(with-buffer self
(when *minibuffer-open-p*
(with-quadtree nil
(layout self)
(layout-program-objects self)
(update-program-objects self)
(when *minibuffer* (update *minibuffer*))
(clear-deleted-program-objects self))))))
(define-method evaluate buffer ()
(prog1 self
(with-buffer self
(mapc #'evaluate %inputs))))
(define-method layout buffer ()
;; take over the entire GL window
(with-buffer self
;; (setf %x 0 %y 0)
;; %width *gl-screen-width*
;; %height *gl-screen-height*)
(mapc #'layout %inputs)
(when *minibuffer*
(layout *minibuffer*))))
(define-method handle-event buffer (event)
(clear-deleted-program-objects self)
(with-field-values (cursor quadtree focused-block) self
(with-buffer self
(or (block%handle-event self event)
(let ((thing