-
-
Notifications
You must be signed in to change notification settings - Fork 290
/
camera2d.jl
363 lines (328 loc) · 11.6 KB
/
camera2d.jl
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
struct Camera2D <: AbstractCamera
area::Observable{Rect2f}
zoomspeed::Observable{Float32}
zoombutton::Observable{IsPressedInputType}
panbutton::Observable{IsPressedInputType}
padding::Observable{Float32}
last_area::Observable{Vec{2, Int}}
update_limits::Observable{Bool}
end
"""
cam2d!(scene::SceneLike, kwargs...)
Creates a 2D camera for the given `scene`. The camera implements zooming by
scrolling and translation using mouse drag. It also implements rectangle
selections.
## Keyword Arguments
- `zoomspeed = 0.1f0` sets the zoom speed.
- `zoombutton = true` sets a button (combination) which needs to be pressed to enable zooming. By default no button needs to be pressed.
- `panbutton = Mouse.right` sets the button used to translate the camera. This must include a mouse button.
- `selectionbutton = (Keyboard.space, Mouse.left)` sets the button used for rectangle selection. This must include a mouse button.
"""
function cam2d!(scene::SceneLike; kw_args...)
cam_attributes = merged_get!(:cam2d, scene, Attributes(kw_args)) do
Attributes(
area = Observable(Rectf(0, 0, 1, 1)),
zoomspeed = 0.10f0,
zoombutton = true,
panbutton = Mouse.right,
selectionbutton = (Keyboard.space, Mouse.left),
padding = 0.001,
last_area = Vec(size(scene)),
update_limits = false,
)
end
cam = from_dict(Camera2D, cam_attributes)
# remove previously connected camera
disconnect!(camera(scene))
add_zoom!(scene, cam)
add_pan!(scene, cam)
correct_ratio!(scene, cam)
selection_rect!(scene, cam, cam_attributes.selectionbutton)
cameracontrols!(scene, cam)
cam
end
wscale(screenrect, viewrect) = widths(viewrect) ./ widths(screenrect)
"""
update_cam!(scene::SceneLike, area)
Updates the camera for the given `scene` to cover the given `area` in 2d.
"""
update_cam!(scene::SceneLike, area) = update_cam!(scene, cameracontrols(scene), area)
"""
update_cam!(scene::SceneLike)
Updates the camera for the given `scene` to cover the limits of the `Scene`.
Useful when using the `Observable` pipeline.
"""
update_cam!(scene::SceneLike) = update_cam!(scene, cameracontrols(scene), data_limits(scene))
function update_cam!(scene::Scene, cam::Camera2D, area3d::Rect)
area = Rect2f(area3d)
area = positive_widths(area)
# ignore rects with width almost 0
any(x-> x ≈ 0.0, widths(area)) && return
pa = pixelarea(scene)[]
px_wh = normalize(widths(pa))
wh = normalize(widths(area))
ratio = px_wh ./ wh
if ratio ≈ Vec(1.0, 1.0)
cam.area[] = area
else
# we only want to make the area bigger, to at least show what was selected
# so we make the minimum 1.0, and grow in the other dimension
s = ratio ./ minimum(ratio)
newwh = s .* widths(area)
cam.area[] = Rectf(minimum(area), newwh)
end
update_cam!(scene, cam)
end
function update_cam!(scene::SceneLike, cam::Camera2D)
x, y = minimum(cam.area[])
w, h = widths(cam.area[]) ./ 2f0
# These observables should be final, no one should do map(cam.projection),
# so we don't push! and just update the value in place
view = translationmatrix(Vec3f(-x - w, -y - h, 0))
projection = orthographicprojection(-w, w, -h, h, -10_000f0, 10_000f0)
set_proj_view!(camera(scene), projection, view)
cam.last_area[] = Vec(size(scene))
return
end
function correct_ratio!(scene, cam)
on(camera(scene), pixelarea(scene)) do area
neww = widths(area)
change = neww .- cam.last_area[]
if !(change ≈ Vec(0.0, 0.0))
s = 1.0 .+ (change ./ cam.last_area[])
camrect = Rectf(minimum(cam.area[]), widths(cam.area[]) .* s)
cam.area[] = camrect
update_cam!(scene, cam)
end
return
end
end
function add_pan!(scene::SceneLike, cam::Camera2D)
startpos = RefValue((0.0, 0.0))
drag_active = RefValue(false)
e = events(scene)
on(
camera(scene),
Observable.((scene, cam, startpos, drag_active))...,
e.mousebutton
) do scene, cam, startpos, active, event
mp = e.mouseposition[]
if ispressed(scene, cam.panbutton[])
if event.action == Mouse.press && is_mouseinside(scene) && !active[]
startpos[] = mp
active[] = true
return Consume(true)
end
elseif event.action == Mouse.release && active[]
diff = startpos[] .- mp
startpos[] = mp
area = cam.area[]
diff = Vec(diff) .* wscale(pixelarea(scene)[], area)
cam.area[] = Rectf(minimum(area) .+ diff, widths(area))
update_cam!(scene, cam)
active[] = false
return Consume(true)
end
return Consume(false)
end
on(
camera(scene),
Observable.((scene, cam, startpos, drag_active))...,
e.mouseposition
) do scene, cam, startpos, active, pos
if active[] && ispressed(scene, cam.panbutton[])
diff = startpos[] .- pos
startpos[] = pos
area = cam.area[]
diff = Vec(diff) .* wscale(pixelarea(scene)[], area)
cam.area[] = Rectf(minimum(area) .+ diff, widths(area))
update_cam!(scene, cam)
return Consume(true)
end
return Consume(false)
end
end
function add_zoom!(scene::SceneLike, cam::Camera2D)
e = events(scene)
on(camera(scene), e.scroll) do x
@extractvalue cam (zoomspeed, zoombutton, area)
zoom = Float32(x[2])
if zoom != 0 && ispressed(scene, zoombutton) && is_mouseinside(scene)
pa = pixelarea(scene)[]
z = (1f0 - zoomspeed)^zoom
mp = Vec2f(e.mouseposition[]) - minimum(pa)
mp = (mp .* wscale(pa, area)) + minimum(area)
p1, p2 = minimum(area), maximum(area)
p1, p2 = p1 - mp, p2 - mp # translate to mouse position
p1, p2 = z * p1, z * p2
p1, p2 = p1 + mp, p2 + mp
cam.area[] = Rectf(p1, p2 - p1)
update_cam!(scene, cam)
return Consume(true)
end
return Consume(false)
end
end
function camspace(scene::SceneLike, cam::Camera2D, point)
point = Vec(point) .* wscale(pixelarea(scene)[], cam.area[])
return Vec(point) .+ Vec(minimum(cam.area[]))
end
function absrect(rect)
xy, wh = minimum(rect), widths(rect)
xy = ntuple(Val(2)) do i
wh[i] < 0 ? xy[i] + wh[i] : xy[i]
end
return Rectf(Vec2f(xy), Vec2f(abs.(wh)))
end
function selection_rect!(scene, cam, key)
rect = RefValue(Rectf(NaN, NaN, NaN, NaN))
lw = 2f0
scene_unscaled = Scene(
scene, transformation = Transformation(),
cam = copy(camera(scene)), clear = false
)
scene_unscaled.clear = false
rect_vis = lines!(
scene_unscaled,
rect[],
linestyle = :dot,
linewidth = 2f0,
color = (:black, 0.4),
visible = false
)
waspressed = RefValue(false)
on(camera(scene), events(scene).mousebutton, key) do event, key
if ispressed(scene, key) && is_mouseinside(scene)
mp = camspace(scene, cam, events(scene).mouseposition[])
if event.action == Mouse.press && !waspressed[]
waspressed[] = true
rect_vis[:visible] = true # start displaying
rect[] = Rectf(mp, 0, 0)
rect_vis[1] = rect[]
return Consume(true)
end
else
if event.action == Mouse.release && waspressed[]
waspressed[] = false
r = absrect(rect[])
w, h = widths(r)
if w > 0.0 && h > 0.0
update_cam!(scene, cam, r)
end
rect[] = Rectf(NaN, NaN, NaN, NaN)
rect_vis[1] = rect[]
return Consume(true)
end
# always hide if not the right key is pressed
rect_vis[:visible] = false # hide
return
end
return Consume(false)
end
on(camera(scene), events(scene).mouseposition, key) do mp, key
# this is only true after a mousebutton update
if ispressed(scene, key) && is_mouseinside(scene)
mp = camspace(scene, cam, mp)
mini = minimum(rect[])
rect[] = Rectf(mini, mp - mini)
rect_vis[1] = rect[]
return Consume(true)
end
return Consume(false)
end
# TODO: this needs explicit cleanup?
# why?
return rect_vis, rect
end
function reset!(cam, boundingbox, preserveratio = true)
w1 = widths(boundingbox)
if preserveratio
w2 = widths(cam[Screen][Area])
ratio = w2 ./ w1
w1 = if ratio[1] > ratio[2]
s = w2[1] ./ w2[2]
Vec2f(s * w1[2], w1[2])
else
s = w2[2] ./ w2[1]
Vec2f(w1[1], s * w1[1])
end
end
p = minimum(w1) .* 0.001 # 2mm padding
update_cam!(cam, Rectf(-p, -p, w1 .+ 2p))
return
end
function add_restriction!(cam, window, rarea::Rect2, minwidths::Vec)
area_ref = Base.RefValue(cam[Area])
restrict_action = paused_action(1.0) do t
o = lerp(origin(area_ref[]), origin(cam[Area]), t)
wh = lerp(widths(area_ref[]), widths(cam[Area]), t)
update_cam!(cam, Rectf(o, wh))
end
on(window, Mouse.Drag) do drag
if drag == Mouse.up && !isplaying(restrict_action)
area = cam[Area]
o = origin(area)
maxi = maximum(area)
newo = max.(o, origin(rarea))
newmax = min.(maxi, maximum(rarea))
maxi = maxi - newmax
newo = newo - maxi
newwh = newmax - newo
scale = 1f0
for (w1, w2) in zip(minwidths, newwh)
stmp = w1 > w2 ? w1 / w2 : 1f0
scale = max(scale, stmp)
end
newwh = newwh * scale
area_ref[] = Rectf(newo, newwh)
if area_ref[] != cam[Area]
play!(restrict_action)
end
end
return
end
restrict_action
end
struct PixelCamera <: AbstractCamera end
struct UpdatePixelCam
camera::Camera
near::Float32
far::Float32
end
function (cam::UpdatePixelCam)(window_size)
w, h = Float32.(widths(window_size))
projection = orthographicprojection(0f0, w, 0f0, h, cam.near, cam.far)
set_proj_view!(cam.camera, projection, Mat4f(I))
end
"""
campixel!(scene; nearclip=-1000f0, farclip=1000f0)
Creates a pixel camera for the given `scene`. This means that the positional
data of a plot will be interpreted in pixel units. This camera does not feature
controls.
"""
function campixel!(scene::Scene; nearclip=-10_000f0, farclip=10_000f0)
disconnect!(camera(scene))
update_once = Observable(false)
closure = UpdatePixelCam(camera(scene), nearclip, farclip)
on(closure, camera(scene), pixelarea(scene))
cam = PixelCamera()
# update once
closure(pixelarea(scene)[])
cameracontrols!(scene, cam)
update_once[] = true
return cam
end
struct RelativeCamera <: AbstractCamera end
"""
cam_relative!(scene)
Creates a camera for the given `scene` which maps the scene area to a 0..1 by
0..1 range. This camera does not feature controls.
"""
function cam_relative!(scene::Scene; nearclip=-10_000f0, farclip=10_000f0)
projection = orthographicprojection(0f0, 1f0, 0f0, 1f0, nearclip, farclip)
set_proj_view!(camera(scene), projection, Mat4f(I))
cam = RelativeCamera()
cameracontrols!(scene, cam)
cam
end
# disconnect!(::Makie.PixelCamera) = nothing