diff options
| author | s-ol <s+removethis@s-ol.nu> | 2021-11-13 17:13:20 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2025-03-02 14:24:49 +0000 |
| commit | 9ba8e90d93dced691fa870e98f534ff2cb454c32 (patch) | |
| tree | de3830e9d810f9b483908a5f7198ece7d6ff4500 | |
| parent | update examples (diff) | |
| download | alive-9ba8e90d93dced691fa870e98f534ff2cb454c32.tar.gz alive-9ba8e90d93dced691fa870e98f534ff2cb454c32.zip | |
lib: filter arguments for love event streams
| -rw-r--r-- | alv-lib/love.moon | 178 | ||||
| -rw-r--r-- | examples/love.alv | 25 |
2 files changed, 170 insertions, 33 deletions
diff --git a/alv-lib/love.moon b/alv-lib/love.moon index 00a03bd..8ad09af 100644 --- a/alv-lib/love.moon +++ b/alv-lib/love.moon @@ -251,14 +251,6 @@ shear = Constant.meta shape! love.graphics.pop! -wrap_stream = (stream) -> - class extends Op - setup: => - @out = stream - super Input.hot @out - - poll: => - mouse_pos = Constant.meta meta: name: 'mouse-pos' @@ -266,42 +258,86 @@ mouse_pos = Constant.meta examples: { '(love/mouse-pos)' } description: "vec2~ stream of mouse position." - value: wrap_stream COPILOT.mouse_pos + value: class extends Op + setup: => + @out = COPILOT.mouse_pos + super Input.hot @out + + poll: => mouse_delta = Constant.meta meta: name: 'mouse-delta' summary: "outputs mouse move events." examples: { '(love/mouse-delta)' } - summary: "vec2! stream of mouse movements." + description: "vec2! stream of mouse movements." - value: wrap_stream COPILOT.mouse_delta + value: class extends Op + setup: => + @out = COPILOT.mouse_delta + super Input.hot @out + poll: => + mouse_presses = Constant.meta meta: name: 'mouse-presses' summary: "outputs mouse press events." - examples: { '(love/mouse-presses)' } - description: "!-stream of all mouse presses. - -Each press event is a struct with the following keys: + examples: { '(love/mouse-presses)', '(love/mouse-presses button)' } + description: "With no arguments, outputs a !-stream of press events with the following keys: - `pos` (vec2): x/y position of mouse -- `button` (num): mouse button number" +- `button` (num): mouse button number + +If `button` is passed, outputs a vec2! stream." + + value: class extends Op + setup: (inputs) => + button = (-sig.num)\match inputs + event = Input.hot COPILOT.mouse_presses + + if button + super :event, button: Input.cold button + @out = vec2\mk_evt! + else + super :event + @out = COPILOT.mouse_presses - value: wrap_stream COPILOT.mouse_presses + poll: => + + tick: => + { :button, :event } = @unwrap_all! + if event and event.button == button + @out\set event.pos mouse_releases = Constant.meta meta: name: 'mouse-releases' summary: "outputs mouse release events." - examples: { '(love/mouse-releases)' } - description: "!-stream of all mouse releases. - -Each release event is a struct with the following keys: + examples: { '(love/mouse-releases)', '(love/mouse-releases button)' } + description: "With no arguments, outputs a !-stream of release events with the following keys: - `pos` (vec2): x/y position of mouse -- `button` (num): mouse button number" +- `button` (num): mouse button number + +If `button` is passed, outputs a vec2! stream." + + value: class extends Op + setup: (inputs) => + button = (-sig.num)\match inputs + event = Input.hot COPILOT.mouse_releases + + if button + super :event, button: Input.cold button + @out = vec2\mk_evt! + else + super :event + @out = COPILOT.mouse_releases - value: wrap_stream COPILOT.mouse_releases + poll: => + + tick: (setup) => + { :button, :event } = @unwrap_all! + if event and event.button == button + @out\set event.pos mouse_down = Constant.meta meta: @@ -346,25 +382,106 @@ wheel_delta = Constant.meta examples: { '(love/wheel-delta)' } description: "vec2! stream of mouse wheel movements." - value: wrap_stream COPILOT.wheel_delta + value: class extends Op + setup: (inputs) => + assert #inputs == 0, Error "argument", "no arguments expected" + @out = COPILOT.wheel_delta + super Input.hot @out + + poll: => key_presses = Constant.meta meta: name: 'key-presses' summary: "outputs key press events." - examples: { '(love/key-presses)' } - description: "str! stream of all key presses." + examples: { '(love/key-presses)', '(love/key-presses key)' } + description: "With no arguments, outputs a str! stream of key names. - value: wrap_stream COPILOT.key_presses +If `key` is passed, outputs a bang! stream." + + value: class extends Op + setup: (inputs) => + key = (-sig.str)\match inputs + event = Input.hot COPILOT.key_presses + + if key + super :event, key: Input.cold key + @out = T.bang\mk_evt! + else + super :event + @out = COPILOT.key_presses + + poll: => + + tick: (setup) => + { :key, :event } = @unwrap_all! + if event and event == key + @out\set true key_releases = Constant.meta meta: name: 'key-releases' summary: "outputs key release events." - examples: { '(love/key-releases)' } - description: "str! stream of all key releases." + examples: { '(love/key-releases)', '(love/key-releases key)' } + description: "With no arguments, outputs a str! stream of key names. + +If `key` is passed, outputs a bang! stream." + + value: class extends Op + setup: (inputs) => + key = (-sig.str)\match inputs + event = Input.hot COPILOT.key_releases + + if key + super :event, key: Input.cold key + @out = T.bang\mk_evt! + else + super :event + @out = COPILOT.key_releases + + poll: => + + tick: (setup) => + { :key, :event } = @unwrap_all! + if event and event == key + @out\set true + + +key_down = Constant.meta + meta: + name: 'key-down?' + summary: "checks whether a key is down." + examples: { '(love/key-down? key)' } + description: "checks whether `key` is down and returns a bool ~-stream. - value: wrap_stream COPILOT.key_releases +`key` should be a str~ stream." + + value: class extends Op + pattern = sig.str + setup: (inputs) => + key = pattern\match inputs + + super + key: Input.hot key + press: Input.hot COPILOT.key_presses + release: Input.hot COPILOT.key_releases + + @out or= T.bool\mk_sig false + + poll: => + + tick: => + { :key, :press, :release } = @unwrap_all! + if key and @inputs.key\dirty! + @state = false + + if press and (not key or press == key) + @state = true + + if release and (not key or release == key) + @state = false + + @out\set @state Constant.meta meta: @@ -415,3 +532,4 @@ macro [->>][]: 'wheel-data': wheel_delta 'key-presses': key_presses 'key-releases': key_releases + 'key-down?': key_down diff --git a/examples/love.alv b/examples/love.alv index 7158e60..ca71a84 100644 --- a/examples/love.alv +++ b/examples/love.alv @@ -1,7 +1,26 @@ +#( + This example draws a rotating rectangle that follows the mouse cursor. + The size changes when the left mouse button is held, and the color + changes when space is pressed. +) ([1]import* love math time) + +([32]trace ([33]mouse-releases)) + +#(cycle colors when space is prssed) +([20]def fill-color + ([21]switch ([22]key-presses "space") + ([23]array 0.3 0 0.9) + ([24]array 0 0.9 0.3) + ([27]array 0.9 0.3 0) + ([28]array 0.3 0.9 0) + ([30]array 0 0.3 0.9) + ([31]array 0.9 0 0.3))) + ([2]draw ([3]->> ([8]rectangle 'fill' 100 100) - ([14]color 1 0 0) + ([14]color fill-color) ([15]scale ([16]switch ([17]mouse-down? 1) 0.5 1)) - ([5]rotate ([9]ramp 2 tau)) - ([4]translate ([10]mouse-pos)))) + ([5]rotate ([9]ramp 2 tau)) #(rotate tau (2*PI) every 2 seconds) + ([4]translate ([10]mouse-pos) #(move to mouse cursor) +))) |
