-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathplotter-legends.lisp
More file actions
executable file
·274 lines (244 loc) · 10.9 KB
/
plotter-legends.lisp
File metadata and controls
executable file
·274 lines (244 loc) · 10.9 KB
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
(in-package :plotter)
;; ----------------------------------------------------------------
(defun internal-draw-existing-legend (port plt)
(let* ((legend (plotter-legend plt))
(items (has-content legend)))
(when items
(with-plotview-coords (port plt)
(let* ((font1 (find-best-font port
:size $tiny-times-font-size))
(font (find-best-font port
:size $tiny-times-font-size
;; :size $tiny-times-font-size
))
(nitems (length items)))
(multiple-value-bind (txtwd txtht)
(let ((maxwd 0)
(maxht 0))
(dolist (item items)
(multiple-value-bind (lf tp rt bt)
(gp:get-string-extent port (legend item) font)
(setf maxwd (max maxwd (- rt lf))
maxht (max maxht (- bt tp))
)))
(values maxwd maxht))
(let* ((totwd (+ txtwd 40))
(totht (+ 2 (* nitems txtht)))
(effwd totwd)
(effht totht)
(effht1 txtht)
(box (plotter-box plt))
(x (or (preferred-x plt)
(let ((x (get-x-location plt (plotter-legend-x plt))))
(ecase (plotter-legend-anchor plt)
(:auto (if (> x (/ (box-width box) 2))
(- x effwd)
x))
((:nw :w :sw) (- x effwd))
((:ne :e :se) x)
((:n :ctr :s) (- x (/ effwd 2)))
))))
(y (or (preferred-y plt)
(let ((y (get-y-location plt (plotter-legend-y plt))))
(case (plotter-legend-anchor plt)
(:auto (if (> y (/ (box-height box) 2))
(- y effht)
y))
((:nw :n :ne) y)
((:sw :s :sw) (- y effht))
((:w :ctr :e) (- y (/ effht 2)))
)))))
(setf (x legend) x
(y legend) y
(width legend) totwd
(height legend) totht)
(gp:draw-rectangle port x y effwd effht
:filled t
:foreground (adjust-color port
(background-color port)
0.75))
(gp:draw-rectangle port x y effwd effht
:foreground (if (highlighted legend)
:magenta
:black))
(loop for item in items
for y from (+ y effht1 1) by effht1
do
(let* ((line-style (line-style item))
(symbol-style (symbol-style item)))
;; ---------------------------------------------
(labels ((draw-line (&optional thickness)
(gp:with-graphics-state
(port
:thickness (or thickness
(line-thick line-style))
:dashed (and line-style
(line-dashing line-style))
:dash (and line-style
(line-dashing line-style))
:foreground (if line-style
(adjust-color port
(line-color line-style)
(line-alpha line-style))
(adjust-color port
(fill-color symbol-style)
(fill-alpha symbol-style))))
(let ((y (floor (- y (/ effht1 2)))))
(gp:draw-line port
(+ x 3) y
(+ x 33) y)
))))
;; ---------------------------------------------
(cond (symbol-style
(case (plot-symbol symbol-style)
((:vbars :hbars) (draw-line 5))
(otherwise
(when line-style
(draw-line))
(funcall (get-symbol-plotfn port symbol-style)
(+ x 18) (- y (/ effht1 2))
))
))
(line-style (draw-line))
))
;; ---------------------------------------------
(gp:draw-string port (legend item) (+ x 36) (- y 3)
:font font1)
))
))
))
)))
(defun draw-accumulated-legend (plt)
(let ((items (all-legends plt))
(legend (plotter-legend plt)))
(cond ((null items)
(setf (has-content legend) nil))
((activep legend)
(setf (has-content legend) items)
(internal-draw-existing-legend (display-pane-of plt) plt))
)))
(defun refresh-view (plt x y w h)
;; Code to convert a plotter region rectangle to an absolute,
;; scaled, rectangle in the parent graphport.
;;
;; CAPI:REDISPLAY-ELEMENT is great, but it doesn't know anything
;; about graphics transforms.
(let ((xform (gp:make-transform))
(sf (plotter-sf plt))
(box (plotter-box plt)))
(gp:apply-translation xform (box-left box) (box-top box))
(gp:apply-scale xform sf sf)
(multiple-value-bind (xp yp)
(gp:transform-point xform x y)
(multiple-value-bind (rp bp)
(gp:transform-point xform (+ x w) (+ y h))
(capi:redisplay-element plt xp yp (- rp xp) (- bp yp))
))
))
(defun draw-existing-legend (plt legend)
(let* ((extra 2)
(extra*2 (* 2 extra))
(w (ceiling (+ extra*2 (width legend))))
(h (ceiling (+ extra*2 (height legend))))
(x (floor (- (or (preferred-x plt)
(x legend))
extra)))
(y (floor (- (or (preferred-y plt)
(y legend))
extra))))
(refresh-view plt x y w h)
))
(defun restore-legend-background (plt legend)
(let* ((extra 2)
(extra*2 (* 2 extra))
(w (ceiling (+ extra*2 (width legend))))
(h (ceiling (+ extra*2 (height legend))))
(x-old (floor (- (x legend) extra)))
(y-old (floor (- (y legend) extra))))
(refresh-view plt x-old y-old w h)
))
(defun highlight-legend (plt)
(let ((legend (plotter-legend plt)))
(when (activep legend)
(unless (highlighted legend)
(setf (highlighted legend) t)
(restore-legend-background plt legend)
(draw-existing-legend plt legend)
))))
(defun unhighlight-legend (plt)
(let ((legend (plotter-legend plt)))
(when (and (activep legend)
(highlighted legend))
(setf (highlighted legend) nil)
(restore-legend-background plt legend)
(draw-existing-legend plt legend)
)))
(defun window-to-plotview-coords (plt x y)
;; Convert reported screen coords to nominal plotting region coords.
;; Effectively, this just makes the system resize-scaling agnostic.
(multiple-value-bind (xd yd)
;; back to data coords
(gp:transform-point (plotter-inv-xform plt) x y)
;; then to nominal frame coords
(gp:transform-point (plotter-xform plt) xd yd)
))
(defun on-legend (plt x y)
(let ((legend (plotter-legend plt)))
(and (activep legend)
(has-content legend)
(multiple-value-bind (xp yp)
(window-to-plotview-coords plt x y)
(with-accessors ((xl x)
(yl y)
(wd width)
(ht height)) legend
(and (<= xl xp (+ xl wd))
(<= yl yp (+ yl ht)))
))
)))
(defun start-drag-legend (plt x y)
(let ((legend (plotter-legend plt)))
(when (and (activep legend)
(has-content legend))
(multiple-value-bind (xp yp)
(window-to-plotview-coords plt x y)
(setf (dragging legend) t
(dx legend) (- (x legend) xp)
(dy legend) (- (y legend) yp))
))))
(defun undrag-legend (plt x y)
(declare (ignore x y))
(let ((legend (plotter-legend plt)))
(when (dragging legend)
(setf (dragging legend) nil)
)))
(defun drag-legend (plt x y)
(let ((legend (plotter-legend plt)))
(when (dragging legend)
(restore-legend-background plt legend)
(multiple-value-bind (xp yp)
(window-to-plotview-coords plt x y)
(setf (preferred-x plt) (+ xp (dx legend))
(preferred-y plt) (+ yp (dy legend)))
(draw-existing-legend plt legend)
))))
#|
(setf plt (plt:wset 'plt))
(plt:fplot plt '(-10 10) (lambda (x) (/ (sin x) x))
:thick 2
:legend "Sinc(x)"
:clear t)
(defun draw-test-pane (gp pane
x y
width height)
(defclass test-pane (capi:drawn-pinboard-object)
()
(:default-initargs
:display-callback 'draw-test-pane
:visible-min-width 50
:visible-min-height 50))
(capi:contain
(make-instance 'test-pane
:best-width 400
:best-height 300))
|#