-
Notifications
You must be signed in to change notification settings - Fork 9
/
halo.lisp
267 lines (197 loc) · 6.72 KB
/
halo.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
;;; halo.lisp --- morphic-style object handles
;; Copyright (C) 2011, 2012 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/>.
;;; Commentary:
;;; Code:
(in-package :blocky)
(defparameter *handle-scale* 3.2)
(defparameter *handle-highlight-background-color* "gray50")
(defparameter *handle-highlight-foreground-color* "white")
(defparameter *indicator-positions*
'(:asterisk (0 1)
:bang (0 0)
:top-left-triangle (0 0)
:menu (1/2 0)
:move (2/3 1)
:drop (0 1)
:pick-up (1/3 1)
:resize (1 1)
:define (0 0)
:close (1 0)
:copy (0 1/2)
:cut (1 1/2)
:bottom-right-triangle (1 1)))
(define-block handle target indicator color foreground-color)
(define-method initialize handle (&optional target)
(initialize%super self)
(setf %target target))
(define-method can-pick handle () t)
(define-method pick handle () self)
(define-method can-escape handle () nil)
(define-method layout handle ())
(define-method toggle-halo handle () nil) ;; don't let halos have halos
(define-method highlight handle ()
(setf %color *handle-highlight-background-color*)
(setf %foreground-color *handle-highlight-foreground-color*))
(define-method alternate-tap handle (x y)
(tap self x y))
(define-method scroll-tap handle (x y)
(tap self x y))
(define-method layout handle ()
(with-fields (x y width height) %target
(destructuring-bind (px py) (getf *indicator-positions* %indicator)
(let* ((margin (* *handle-scale* (indicator-size)))
(x0 (- x margin))
(y0 (- y margin)))
(setf %x (+ x0
(* px (+ width margin))))
(setf %y (+ y0
(* py (+ height margin))))
(setf %width margin)
(setf %height margin)))))
(define-method draw handle ()
(draw-indicator %indicator %x %y
:color %foreground-color
:scale *handle-scale*
:background %color))
(define-method draw-hover handle ())
(defmacro define-handle (name indicator
&key (color "gray10")
(foreground-color "white")
fields)
(assert (symbolp name))
(assert (stringp color))
`(define-block (,name :super handle)
(indicator :initform ,indicator)
(color :initform ,color)
(foreground-color :initform ,foreground-color)
,@fields))
;;; Evaluation
(define-handle evaluate :define)
(define-method tap evaluate (x y)
(evaluate-here %target))
;;; Getting a context menu
(define-handle open-menu :menu)
(define-method tap open-menu (x y)
(let ((menu (context-menu %target)))
(add-block (current-buffer) menu)
(move-to menu x y)))
;;; Dropping things down into the object layer
(define-handle drop :drop)
(define-method tap drop (x0 y0)
(drop-selection (current-buffer)))
(define-method update drop ()
(when (%quadtree-node %target)
;; ghost/highlight when already in object layer
(highlight self))
(update%super self))
;;; Picking them up from the object layer
(define-handle pick-up :pick-up)
(define-method tap pick-up (x0 y0)
(unless (contains (current-buffer) %target)
(add-block (current-buffer) %target)))
(define-method update pick-up ()
(when (null (%quadtree-node %target))
;; ghost/highlight when not in object layer
(highlight self))
(update%super self))
;;; Moving objects or groups of them
(define-handle move :move
:fields (positions))
(define-method can-pick move () t)
(define-method pick move () self)
(define-method drag move (x0 y0)
(with-fields (positions) self
(when (null positions)
;; drag all selected objects
(dolist (thing (cons %target (get-selection (current-buffer))))
(with-fields (x y) thing
;; store initial offset from pointer
(push (list thing
(- x x0)
(- y y0))
positions))))
(dolist (entry positions)
(destructuring-bind (thing x y) entry
(move-to thing
(+ x x0)
(+ y y0))))))
;;; Resizing objects interactively
(define-handle resize :resize)
(define-method can-pick resize () t)
(define-method pick resize () self)
(define-method drag resize (x0 y0)
(with-fields (x y width height) %target
(resize %target
(- x0 x)
(- y0 y))))
;;; Definitions
(define-handle define :define)
(define-method tap define (x y)
(show-definition %target))
;;; Destroying objects
(define-handle destroy :close)
(define-method tap destroy (x y)
(assert %target)
(destroy %target)
;; get rid of halo
(when %parent
(destroy %parent)))
(define-handle collapse :collapse)
;;; Copy and cut
(define-handle copy :copy)
(define-method tap copy (x y)
(copy (current-buffer) (cons %target (get-selection (current-buffer)))))
(define-handle cut :cut)
(define-method tap cut (x y)
(cut (current-buffer) (cons %target (get-selection (current-buffer)))))
;;; The halo, which manages all the handles
(defparameter *halo-handles*
'(evaluate drop move pick-up resize cut copy destroy))
(define-block halo target)
(define-method initialize halo (target)
(assert (blockyp target))
(setf %target target)
(apply #'initialize%super self
(mapcar #'(lambda (handle)
(clone (make-prototype-id handle) target))
*halo-handles*)))
(defun halo-minimum-height () (* 5 *handle-scale* (indicator-size)))
(defun halo-minimum-width () (* 5 *handle-scale* (indicator-size)))
(define-method layout halo ()
(with-fields (x y width height) %target
(let ((size (* *handle-scale* (indicator-size))))
(setf %x (- x size))
(setf %y (- y size))
;; add twice the halo border to make sure we get clicks all the
;; way to the right of the halo
(setf %width (max (+ width (* 2 size)) (halo-minimum-width)))
(setf %height (max (+ height (* 2 size)) (halo-minimum-height)))
;; now lay out the individual items
(mapc #'layout %inputs))))
(define-method draw halo ()
(mapc #'draw %inputs))
(define-method can-pick halo ()
(can-pick %target))
(define-method pick halo ()
(pick %target))
(define-method scroll-tap halo (x y)
(toggle-halo %target))
(define-method alternate-tap halo (x y)
(destroy-halo %target))
(define-method draw-hover halo ())
(define-method draw-focus halo ())
(define-method draw-highlight halo ())
(define-method accept halo (other))
;;; halo.lisp ends here