forked from MakieOrg/Makie.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.jl
444 lines (377 loc) · 18.3 KB
/
events.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
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
using Makie: MouseButtonEvent, KeyEvent, Figure, Textbox
using Makie: Not, And, Or
using InteractiveUtils
# rudimentary equality for tests
Base.:(==)(l::Exclusively, r::Exclusively) = l.x == r.x
Base.:(==)(l::Not, r::Not) = l.x == r.x
Base.:(==)(l::And, r::And) = l.left == r.left && l.right == r.right
Base.:(==)(l::Or, r::Or) = l.left == r.left && l.right == r.right
@testset "Events" begin
@testset "Mouse and Keyboard state" begin
events = Makie.Events()
@test isempty(events.mousebuttonstate)
@test isempty(events.keyboardstate)
events.mousebutton[] = MouseButtonEvent(Mouse.left, Mouse.press)
events.keyboardbutton[] = KeyEvent(Keyboard.a, Keyboard.press)
@test events.mousebuttonstate == Set([Mouse.left])
@test events.keyboardstate == Set([Keyboard.a])
events.mousebutton[] = MouseButtonEvent(Mouse.right, Mouse.press)
events.keyboardbutton[] = KeyEvent(Keyboard.b, Keyboard.press)
@test events.mousebuttonstate == Set([Mouse.left, Mouse.right])
@test events.keyboardstate == Set([Keyboard.a, Keyboard.b])
events.mousebutton[] = MouseButtonEvent(Mouse.left, Mouse.release)
events.keyboardbutton[] = KeyEvent(Keyboard.a, Keyboard.release)
@test events.mousebuttonstate == Set([Mouse.right])
@test events.keyboardstate == Set([Keyboard.b])
events.mousebutton[] = MouseButtonEvent(Mouse.right, Mouse.release)
events.keyboardbutton[] = KeyEvent(Keyboard.b, Keyboard.release)
@test isempty(events.mousebuttonstate)
@test isempty(events.keyboardstate)
end
@testset "ispressed" begin
events = Makie.Events()
@test isempty(events.mousebuttonstate)
@test isempty(events.keyboardstate)
events.mousebutton[] = MouseButtonEvent(Mouse.left, Mouse.press)
events.keyboardbutton[] = KeyEvent(Keyboard.a, Keyboard.press)
# Buttons
@test ispressed(events, Keyboard.a)
@test !ispressed(events, Keyboard.b)
@test ispressed(events, Mouse.left)
@test !ispressed(events, Mouse.right)
# Collections
@test ispressed(events, (Keyboard.a, ))
@test ispressed(events, [Keyboard.a, ])
@test ispressed(events, Set((Keyboard.a, )))
@test !ispressed(events, (Keyboard.a, Keyboard.b))
@test !ispressed(events, [Keyboard.a, Keyboard.b])
@test !ispressed(events, Set((Keyboard.a, Keyboard.b)))
@test ispressed(events, (Keyboard.a, Mouse.left))
@test ispressed(events, [Keyboard.a, Mouse.left])
@test ispressed(events, Set((Keyboard.a, Mouse.left)))
# Boolean
@test ispressed(events, Keyboard.a & Mouse.left)
@test !ispressed(events, Keyboard.a & Mouse.right)
@test ispressed(events, Keyboard.a & !Mouse.right)
@test !ispressed(events, !Keyboard.a & Mouse.left)
@test ispressed(events, Keyboard.a | Mouse.left)
@test ispressed(events, Keyboard.a | Mouse.right)
@test !ispressed(events, Keyboard.b | Mouse.right)
@test ispressed(events, Keyboard.b | !Mouse.right)
# Exclusively
@test !ispressed(events, Exclusively(Keyboard.a))
@test !ispressed(events, Exclusively(Mouse.left))
@test ispressed(events, Exclusively((Keyboard.a, Mouse.left)))
@test ispressed(events, Exclusively([Keyboard.a, Mouse.left]))
@test ispressed(events, Exclusively(Set((Keyboard.a, Mouse.left))))
@test Exclusively(Keyboard.a & Mouse.left) == Exclusively((Keyboard.a, Mouse.left))
@test ispressed(events, Exclusively(Keyboard.a & Mouse.left))
@test Exclusively(Keyboard.a | Mouse.left) == Makie.Or(Exclusively(Keyboard.a), Exclusively(Mouse.left))
@test !ispressed(events, Exclusively(Keyboard.a | Mouse.left))
expr = Mouse.left & (Keyboard.a | (Keyboard.a & Keyboard.b))
lowered = Or(
Exclusively((Mouse.left, Keyboard.a)),
Exclusively((Mouse.left, Keyboard.a, Keyboard.b))
)
@test ispressed(events, expr)
@test Exclusively(expr) == lowered
@test ispressed(events, Exclusively(expr))
events.keyboardbutton[] = KeyEvent(Keyboard.b, Keyboard.press)
@test ispressed(events, expr)
@test ispressed(events, Exclusively(expr))
events.keyboardbutton[] = KeyEvent(Keyboard.c, Keyboard.press)
@test ispressed(events, expr)
@test !ispressed(events, Exclusively(expr))
events.keyboardbutton[] = KeyEvent(Keyboard.a, Keyboard.release)
@test !ispressed(events, expr)
@test !ispressed(events, Exclusively(expr))
# Bools
@test ispressed(events, true)
@test !ispressed(events, false)
@test Exclusively(true) == true
@test Exclusively(false) == false
for x in (Keyboard.a, Mouse.left)
@test true & x == x
@test x & true == x
@test false & x == false
@test x & false == false
@test true | x == true
@test x | true == true
@test false | x == x
@test x | false == x
end
end
# Okay, this is hacky,
# but we're not going to install a whole linux desktop environment on the CI just to test the clipboard
# (what the hell xclip, y u need all that)
@eval InteractiveUtils begin
const CLIP = Ref{String}()
clipboard(str::String) = (CLIP[] = str)
clipboard() = CLIP[]
end
@testset "copy_paste" begin
f = Figure(resolution=(640,480))
tb = Textbox(f[1,1], placeholder="Copy/paste into me")
e = events(f.scene)
# Initial state
@test !tb.focused[]
@test tb.stored_string[] === nothing
# Select textbox
e.mouseposition[] = (320, 240)
e.mousebutton[] = MouseButtonEvent(Mouse.left, Mouse.press)
e.mousebutton[] = MouseButtonEvent(Mouse.left, Mouse.release)
@test tb.focused[]
# Fill clipboard with a string
clipboard("test string")
# Trigger left ctrl+v
e.keyboardbutton[] = KeyEvent(Keyboard.left_control, Keyboard.press)
e.keyboardbutton[] = KeyEvent(Keyboard.v, Keyboard.press)
e.keyboardbutton[] = KeyEvent(Keyboard.v, Keyboard.release)
e.keyboardbutton[] = KeyEvent(Keyboard.left_control, Keyboard.release)
e.keyboardbutton[] = KeyEvent(Keyboard.enter, Keyboard.press)
e.keyboardbutton[] = KeyEvent(Keyboard.enter, Keyboard.release)
@test tb.stored_string[] == "test string"
# Refresh figure to test right control + v combination
empty!(f)
f = Figure(resolution=(640,480))
tb = Textbox(f[1,1], placeholder="Copy/paste into me")
e = events(f.scene)
# Initial state
@test !tb.focused[]
@test tb.stored_string[] === nothing
# Re-select textbox
e.mouseposition[] = (320, 240)
e.mousebutton[] = MouseButtonEvent(Mouse.left, Mouse.press)
e.mousebutton[] = MouseButtonEvent(Mouse.left, Mouse.release)
@test tb.focused[]
clipboard("test string2")
# Trigger right ctrl+v
e.keyboardbutton[] = KeyEvent(Keyboard.right_control, Keyboard.press)
e.keyboardbutton[] = KeyEvent(Keyboard.v, Keyboard.press)
e.keyboardbutton[] = KeyEvent(Keyboard.v, Keyboard.release)
e.keyboardbutton[] = KeyEvent(Keyboard.right_control, Keyboard.release)
e.keyboardbutton[] = KeyEvent(Keyboard.enter, Keyboard.press)
e.keyboardbutton[] = KeyEvent(Keyboard.enter, Keyboard.release)
@test tb.stored_string[] == "test string2"
end
# This testset is based on the results the current camera system has. If
# cam3d! is updated this is likely to break.
@testset "cam3d!" begin
scene = Scene(resolution=(800, 600));
e = events(scene)
cam3d!(scene, fixed_axis=true, cad=false, zoom_shift_lookat=false)
cc = cameracontrols(scene)
# Verify initial camera state
@test cc.lookat[] == Vec3f(0)
@test cc.eyeposition[] == Vec3f(3)
@test cc.upvector[] == Vec3f(0, 0, 1)
# Rotation
# 1) In scene, in drag
e.mouseposition[] = (400, 250)
e.mousebutton[] = MouseButtonEvent(Mouse.left, Mouse.press)
e.mouseposition[] = (600, 250)
@test cc.lookat[] ≈ Vec3f(0)
@test cc.eyeposition[] ≈ Vec3f(4.14532, -0.9035063, 3.0)
@test cc.upvector[] ≈ Vec3f(0, 0, 1)
# 2) Outside scene, in drag
e.mouseposition[] = (1000, 450)
@test cc.lookat[] ≈ Vec3f(0)
@test cc.eyeposition[] ≈ Vec3f(-2.8912058, -3.8524969, -1.9491522)
@test cc.upvector[] ≈ Vec3f(-0.5050875, -0.6730229, 0.5403024)
# 3) not in drag
e.mousebutton[] = MouseButtonEvent(Mouse.left, Mouse.release)
e.mouseposition[] = (400, 250)
@test cc.lookat[] ≈ Vec3f(0)
@test cc.eyeposition[] ≈ Vec3f(-2.8912058, -3.8524969, -1.9491522)
@test cc.upvector[] ≈ Vec3f(-0.5050875, -0.6730229, 0.5403024)
# Reset state so this is indepentent from the last checks
scene = Scene(resolution=(800, 600));
e = events(scene)
cam3d!(scene, fixed_axis=true, cad=false, zoom_shift_lookat=false)
cc = cameracontrols(scene)
# Verify initial camera state
@test cc.lookat[] == Vec3f(0)
@test cc.eyeposition[] == Vec3f(3)
@test cc.upvector[] == Vec3f(0, 0, 1)
# translation
# 1) In scene, in drag
e.mousebutton[] = MouseButtonEvent(Mouse.right, Mouse.press)
e.mouseposition[] = (600, 250)
@test cc.lookat[] ≈ Vec3f(5.4697413, -3.3484206, -2.1213205)
@test cc.eyeposition[] ≈ Vec3f(8.469742, -0.34842062, 0.8786795)
@test cc.upvector[] ≈ Vec3f(0.0, 0.0, 1.0)
# 2) Outside scene, in drag
e.mouseposition[] = (1000, 450)
@test cc.lookat[] ≈ Vec3f(9.257657, -5.4392805, -3.818377)
@test cc.eyeposition[] ≈ Vec3f(12.257658, -2.4392805, -0.81837714)
@test cc.upvector[] ≈ Vec3f(0.0, 0.0, 1.0)
# 3) not in drag
e.mousebutton[] = MouseButtonEvent(Mouse.right, Mouse.release)
e.mouseposition[] = (400, 250)
@test cc.lookat[] ≈ Vec3f(9.257657, -5.4392805, -3.818377)
@test cc.eyeposition[] ≈ Vec3f(12.257658, -2.4392805, -0.81837714)
@test cc.upvector[] ≈ Vec3f(0.0, 0.0, 1.0)
# Reset state
scene = Scene(resolution=(800, 600));
e = events(scene)
cam3d!(scene, fixed_axis=true, cad=false, zoom_shift_lookat=false)
cc = cameracontrols(scene)
# Verify initial camera state
@test cc.lookat[] == Vec3f(0)
@test cc.eyeposition[] == Vec3f(3)
@test cc.upvector[] == Vec3f(0, 0, 1)
@test cc.zoom_mult[] == 1f0
# Zoom
e.scroll[] = (0.0, 4.0)
@test cc.lookat[] ≈ Vec3f(0)
@test cc.eyeposition[] ≈ Vec3f(3)
@test cc.upvector[] ≈ Vec3f(0.0, 0.0, 1.0)
@test cc.zoom_mult[] ≈ 0.6830134f0
# should not work outside the scene
e.mouseposition[] = (1000, 450)
e.scroll[] = (0.0, 4.0)
@test cc.lookat[] ≈ Vec3f(0)
@test cc.eyeposition[] ≈ Vec3f(3)
@test cc.upvector[] ≈ Vec3f(0.0, 0.0, 1.0)
@test cc.zoom_mult[] ≈ 0.6830134f0
end
@testset "mouse state machine" begin
scene = Scene(resolution=(800, 600));
e = events(scene)
bbox = Observable(Rect2(200, 200, 400, 300))
msm = addmouseevents!(scene, bbox, priority=typemax(Int))
eventlog = MouseEvent[]
on(x -> begin push!(eventlog, x); false end, msm.obs)
e.mouseposition[] = (0, 200)
@test isempty(eventlog)
# move inside
e.mouseposition[] = (300, 200)
@test length(eventlog) == 1
@test eventlog[1].type == MouseEventTypes.enter
@test eventlog[1].px == Point2f(300, 200)
@test eventlog[1].prev_px == Point2f(0, 200)
empty!(eventlog)
# over
e.mouseposition[] = (300, 300)
@test length(eventlog) == 1
@test eventlog[1].type == MouseEventTypes.over
@test eventlog[1].px == Point2f(300, 300)
@test eventlog[1].prev_px == Point2f(300, 200)
empty!(eventlog)
for button in (:left, :middle, :right)
# click
e.mousebutton[] = MouseButtonEvent(getfield(Mouse, button), Mouse.press)
e.mousebutton[] = MouseButtonEvent(getfield(Mouse, button), Mouse.release)
@test length(eventlog) == 3
for (i, t) in enumerate((
getfield(MouseEventTypes, Symbol(button, :down)),
getfield(MouseEventTypes, Symbol(button, :click)),
getfield(MouseEventTypes, Symbol(button, :up))
))
@test eventlog[i].type == t
@test eventlog[i].px == Point2f(300, 300)
@test eventlog[i].prev_px == Point2f(300, 300)
end
empty!(eventlog)
# doubleclick
e.mousebutton[] = MouseButtonEvent(getfield(Mouse, button), Mouse.press)
e.mousebutton[] = MouseButtonEvent(getfield(Mouse, button), Mouse.release)
@test length(eventlog) == 3
for (i, t) in enumerate((
getfield(MouseEventTypes, Symbol(button, :down)),
getfield(MouseEventTypes, Symbol(button, :doubleclick)),
getfield(MouseEventTypes, Symbol(button, :up))
))
@test eventlog[i].type == t
@test eventlog[i].px == Point2f(300, 300)
@test eventlog[i].prev_px == Point2f(300, 300)
end
empty!(eventlog)
# triple click = click
e.mousebutton[] = MouseButtonEvent(getfield(Mouse, button), Mouse.press)
e.mousebutton[] = MouseButtonEvent(getfield(Mouse, button), Mouse.release)
@test length(eventlog) == 3
for (i, t) in enumerate((
getfield(MouseEventTypes, Symbol(button, :down)),
getfield(MouseEventTypes, Symbol(button, :click)),
getfield(MouseEventTypes, Symbol(button, :up))
))
@test eventlog[i].type == t
@test eventlog[i].px == Point2f(300, 300)
@test eventlog[i].prev_px == Point2f(300, 300)
end
empty!(eventlog)
# drag
e.mousebutton[] = MouseButtonEvent(getfield(Mouse, button), Mouse.press)
e.mouseposition[] = (500, 300)
e.mouseposition[] = (700, 200)
e.mousebutton[] = MouseButtonEvent(getfield(Mouse, button), Mouse.release)
@test length(eventlog) == 6
prev_px = Point2f[(300, 300), (300, 300), (300, 300), (500, 300), (700, 200), (700, 200)]
px = Point2f[(300, 300), (500, 300), (500, 300), (700, 200), (700, 200), (700, 200)]
for (i, t) in enumerate((
getfield(MouseEventTypes, Symbol(button, :down)),
getfield(MouseEventTypes, Symbol(button, :dragstart)),
getfield(MouseEventTypes, Symbol(button, :drag)),
getfield(MouseEventTypes, Symbol(button, :drag)),
getfield(MouseEventTypes, Symbol(button, :dragstop)),
getfield(MouseEventTypes, :out),
# TODO this is kinda missing an "up outside"
))
@test eventlog[i].type == t
@test eventlog[i].px == px[i]
@test eventlog[i].prev_px == prev_px[i]
end
e.mouseposition[] = (300, 300)
empty!(eventlog)
end
# TODO: This should probably produce:
# left down > right down > right click > right up > left up
e.mousebutton[] = MouseButtonEvent(Mouse.left, Mouse.press)
e.mousebutton[] = MouseButtonEvent(Mouse.right, Mouse.press)
e.mousebutton[] = MouseButtonEvent(Mouse.right, Mouse.release)
e.mousebutton[] = MouseButtonEvent(Mouse.left, Mouse.release)
@test length(eventlog) == 3
@test eventlog[1].type == MouseEventTypes.leftdown
@test eventlog[2].type == MouseEventTypes.leftclick
@test eventlog[3].type == MouseEventTypes.leftup
empty!(eventlog)
# double left up? :(
e.mousebutton[] = MouseButtonEvent(Mouse.left, Mouse.press)
e.mousebutton[] = MouseButtonEvent(Mouse.right, Mouse.press)
e.mousebutton[] = MouseButtonEvent(Mouse.left, Mouse.release)
e.mousebutton[] = MouseButtonEvent(Mouse.right, Mouse.release)
@test length(eventlog) == 4
@test eventlog[1].type == MouseEventTypes.leftdown
@test eventlog[2].type == MouseEventTypes.leftdoubleclick
@test eventlog[3].type == MouseEventTypes.leftup
@test eventlog[4].type == MouseEventTypes.leftup
empty!(eventlog)
# This should probably produce a leftdragstop on right down instead of left up
e.mouseposition[] = (300, 300)
empty!(eventlog)
e.mousebutton[] = MouseButtonEvent(Mouse.left, Mouse.press)
e.mouseposition[] = (350, 350)
e.mousebutton[] = MouseButtonEvent(Mouse.right, Mouse.press)
e.mouseposition[] = (350, 400)
e.mousebutton[] = MouseButtonEvent(Mouse.right, Mouse.release)
e.mouseposition[] = (400, 400)
e.mousebutton[] = MouseButtonEvent(Mouse.left, Mouse.release)
@test length(eventlog) == 7
@test eventlog[1].type == MouseEventTypes.leftdown
@test eventlog[2].type == MouseEventTypes.leftdragstart
@test eventlog[3].type == MouseEventTypes.leftdrag
@test eventlog[4].type == MouseEventTypes.leftdrag
@test eventlog[5].type == MouseEventTypes.over
@test eventlog[6].type == MouseEventTypes.leftdragstop
@test eventlog[7].type == MouseEventTypes.leftup
@test eventlog[1].px == Point2f(300, 300)
@test eventlog[2].px == Point2f(350, 350)
@test eventlog[3].px == Point2f(350, 350)
@test eventlog[4].px == Point2f(350, 400)
@test eventlog[5].px == Point2f(400, 400)
@test eventlog[6].px == Point2f(400, 400)
@test eventlog[7].px == Point2f(400, 400)
empty!(eventlog)
end
end