git.s-ol.nu alive / a922223
lib: filter arguments for love event streams s-ol 5 months ago
2 changed file(s) with 176 addition(s) and 39 deletion(s). Raw diff Collapse all Expand all
250250 shape!
251251 love.graphics.pop!
252252
253 wrap_stream = (stream) ->
254 class extends Op
255 setup: =>
256 @out = stream
257 super Input.hot @out
258
259 poll: =>
260
261253 mouse_pos = Constant.meta
262254 meta:
263255 name: 'mouse-pos'
265257 examples: { '(love/mouse-pos)' }
266258 description: "vec2~ stream of mouse position."
267259
268 value: wrap_stream COPILOT.mouse_pos
260 value: class extends Op
261 setup: =>
262 @out = COPILOT.mouse_pos
263 super Input.hot @out
264
265 poll: =>
269266
270267 mouse_delta = Constant.meta
271268 meta:
272269 name: 'mouse-delta'
273270 summary: "outputs mouse move events."
274271 examples: { '(love/mouse-delta)' }
275 summary: "vec2! stream of mouse movements."
276
277 value: wrap_stream COPILOT.mouse_delta
278
272 description: "vec2! stream of mouse movements."
273
274 value: class extends Op
275 setup: =>
276 @out = COPILOT.mouse_delta
277 super Input.hot @out
278
279 poll: =>
280
279281 mouse_presses = Constant.meta
280282 meta:
281283 name: 'mouse-presses'
282284 summary: "outputs mouse press events."
283 examples: { '(love/mouse-presses)' }
284 description: "!-stream of all mouse presses.
285
286 Each press event is a struct with the following keys:
285 examples: { '(love/mouse-presses)', '(love/mouse-presses button)' }
286 description: "With no arguments, outputs a !-stream of press events with the following keys:
287287 - `pos` (vec2): x/y position of mouse
288 - `button` (num): mouse button number"
289
290 value: wrap_stream COPILOT.mouse_presses
288 - `button` (num): mouse button number
289
290 If `button` is passed, outputs a vec2! stream."
291
292 value: class extends Op
293 setup: (inputs) =>
294 button = (-sig.num)\match inputs
295 event = Input.hot COPILOT.mouse_presses
296
297 if button
298 super :event, button: Input.cold button
299 @out = vec2\mk_evt!
300 else
301 super :event
302 @out = COPILOT.mouse_presses
303
304 poll: =>
305
306 tick: =>
307 { :button, :event } = @unwrap_all!
308 if event and event.button == button
309 @out\set event.pos
291310
292311 mouse_releases = Constant.meta
293312 meta:
294313 name: 'mouse-releases'
295314 summary: "outputs mouse release events."
296 examples: { '(love/mouse-releases)' }
297 description: "!-stream of all mouse releases.
298
299 Each release event is a struct with the following keys:
315 examples: { '(love/mouse-releases)', '(love/mouse-releases button)' }
316 description: "With no arguments, outputs a !-stream of release events with the following keys:
300317 - `pos` (vec2): x/y position of mouse
301 - `button` (num): mouse button number"
302
303 value: wrap_stream COPILOT.mouse_releases
318 - `button` (num): mouse button number
319
320 If `button` is passed, outputs a vec2! stream."
321
322 value: class extends Op
323 setup: (inputs) =>
324 button = (-sig.num)\match inputs
325 event = Input.hot COPILOT.mouse_releases
326
327 if button
328 super :event, button: Input.cold button
329 @out = vec2\mk_evt!
330 else
331 super :event
332 @out = COPILOT.mouse_releases
333
334 poll: =>
335
336 tick: (setup) =>
337 { :button, :event } = @unwrap_all!
338 if event and event.button == button
339 @out\set event.pos
304340
305341 mouse_down = Constant.meta
306342 meta:
345381 examples: { '(love/wheel-delta)' }
346382 description: "vec2! stream of mouse wheel movements."
347383
348 value: wrap_stream COPILOT.wheel_delta
384 value: class extends Op
385 setup: (inputs) =>
386 assert #inputs == 0, Error "argument", "no arguments expected"
387 @out = COPILOT.wheel_delta
388 super Input.hot @out
389
390 poll: =>
349391
350392 key_presses = Constant.meta
351393 meta:
352394 name: 'key-presses'
353395 summary: "outputs key press events."
354 examples: { '(love/key-presses)' }
355 description: "str! stream of all key presses."
356
357 value: wrap_stream COPILOT.key_presses
396 examples: { '(love/key-presses)', '(love/key-presses key)' }
397 description: "With no arguments, outputs a str! stream of key names.
398
399 If `key` is passed, outputs a bang! stream."
400
401 value: class extends Op
402 setup: (inputs) =>
403 key = (-sig.str)\match inputs
404 event = Input.hot COPILOT.key_presses
405
406 if key
407 super :event, key: Input.cold key
408 @out = T.bang\mk_evt!
409 else
410 super :event
411 @out = COPILOT.key_presses
412
413 poll: =>
414
415 tick: (setup) =>
416 { :key, :event } = @unwrap_all!
417 if event and event == key
418 @out\set true
358419
359420 key_releases = Constant.meta
360421 meta:
361422 name: 'key-releases'
362423 summary: "outputs key release events."
363 examples: { '(love/key-releases)' }
364 description: "str! stream of all key releases."
365
366 value: wrap_stream COPILOT.key_releases
424 examples: { '(love/key-releases)', '(love/key-releases key)' }
425 description: "With no arguments, outputs a str! stream of key names.
426
427 If `key` is passed, outputs a bang! stream."
428
429 value: class extends Op
430 setup: (inputs) =>
431 key = (-sig.str)\match inputs
432 event = Input.hot COPILOT.key_releases
433
434 if key
435 super :event, key: Input.cold key
436 @out = T.bang\mk_evt!
437 else
438 super :event
439 @out = COPILOT.key_releases
440
441 poll: =>
442
443 tick: (setup) =>
444 { :key, :event } = @unwrap_all!
445 if event and event == key
446 @out\set true
447
448
449 key_down = Constant.meta
450 meta:
451 name: 'key-down?'
452 summary: "checks whether a key is down."
453 examples: { '(love/key-down? key)' }
454 description: "checks whether `key` is down and returns a bool ~-stream.
455
456 `key` should be a str~ stream."
457
458 value: class extends Op
459 pattern = sig.str
460 setup: (inputs) =>
461 key = pattern\match inputs
462
463 super
464 key: Input.hot key
465 press: Input.hot COPILOT.key_presses
466 release: Input.hot COPILOT.key_releases
467
468 @out or= T.bool\mk_sig false
469
470 poll: =>
471
472 tick: =>
473 { :key, :press, :release } = @unwrap_all!
474 if key and @inputs.key\dirty!
475 @state = false
476
477 if press and (not key or press == key)
478 @state = true
479
480 if release and (not key or release == key)
481 @state = false
482
483 @out\set @state
367484
368485 Constant.meta
369486 meta:
414531 'wheel-data': wheel_delta
415532 'key-presses': key_presses
416533 'key-releases': key_releases
534 'key-down?': key_down
0 #(
1 This example draws a rotating rectangle that follows the mouse cursor.
2 The size changes when the left mouse button is held, and the color
3 changes when space is pressed.
4 )
05 ([1]import* love math time)
6
7 ([32]trace ([33]mouse-releases))
8
9 #(cycle colors when space is prssed)
10 ([20]def fill-color
11 ([21]switch ([22]key-presses "space")
12 ([23]array 0.3 0 0.9)
13 ([24]array 0 0.9 0.3)
14 ([27]array 0.9 0.3 0)
15 ([28]array 0.3 0.9 0)
16 ([30]array 0 0.3 0.9)
17 ([31]array 0.9 0 0.3)))
18
119 ([2]draw ([3]->>
220 ([8]rectangle 'fill' 100 100)
3 ([14]color 1 0 0)
21 ([14]color fill-color)
422 ([15]scale ([16]switch ([17]mouse-down? 1) 0.5 1))
5 ([5]rotate ([9]ramp 2 tau))
6 ([4]translate ([10]mouse-pos))))
23 ([5]rotate ([9]ramp 2 tau)) #(rotate tau (2*PI) every 2 seconds)
24 ([4]translate ([10]mouse-pos) #(move to mouse cursor)
25 )))