git.s-ol.nu alive / 408a236
Fix love event input streams s-ol 5 months ago
3 changed file(s) with 131 addition(s) and 25 deletion(s). Raw diff Collapse all Expand all
22 --
33 -- @classmod LoveCopilot
44 import CLICopilot from require 'alv.copilot.cli'
5 import T, Array from require 'alv.base'
5 import T, Struct, Array from require 'alv.base'
66
77 export COPILOT
88
99 vec2 = Array 2, T.num
10 mouse_evt = Struct pos: vec2, button: T.num
1011
1112 class LoveCopilot extends CLICopilot
1213 new: (arg) =>
1617 @drawlist = {}
1718 @mouse_pos = vec2\mk_sig { love.mouse.getPosition! }
1819 @mouse_delta = vec2\mk_evt!
19 @mouse_presses = T['love/_click']\mk_evt!
20 @mouse_releases = T['love/_click']\mk_evt!
20 @mouse_presses = mouse_evt\mk_evt!
21 @mouse_releases = mouse_evt\mk_evt!
2122 @wheel_delta = vec2\mk_evt!
2223 @key_presses = T.str\mk_evt!
2324 @key_releases = T.str\mk_evt!
6565 --
6666 -- Marks this stream as dirty for the remainder of the current tick.
6767 -- If `event` is nil, does nothing.
68 set: (event) =>
68 set: (event, force=false) =>
6969 return if event == nil
70 assert not @dirty!, "#{@} is already dirty!"
70 assert force or not @dirty!, "#{@} is already dirty!"
7171 @updated = COPILOT.T
7272 @value = event
7373
0 import Constant, Op, PureOp, Input, Error, T, Array, any from require 'alv.base'
0 import Constant, Op, PureOp, Input, Error, T, Array, any, sig from require 'alv.base'
1 import RTNode from require 'alv'
12
23 unpack or= table.unpack
34
1112 draw = Constant.meta
1213 meta:
1314 name: 'draw'
14 summary: "draw a love/shape shape."
15 examples: { '(love/draw shape)' }
15 summary: "draw one or more love/shape shapes."
16 examples: { '(love/draw shape1 …)', '(love/draw shapes)' }
1617
1718 value: class extends Op
1819 new: (...) =>
2122
2223 pattern = any['love/shape']*0
2324 setup: (inputs, scope) =>
25 if #inputs == 1
26 only = inputs[1]
27 type = only\type!
28 if type.__class == Array and type.type == T['love/shape']
29 super Input.hot only
30 return
31
2432 shapes = pattern\match inputs
2533 inputs = [Input.hot shape for shape in *shapes]
2634 inputs.num = Input.cold Constant.num #shapes
2836
2937 tick: =>
3038 shapes = @unwrap_all!
31 for i=1, shapes.num do shapes[i] or= ->
39 num = shapes.num or #shapes
40 for i=1, num do shapes[i] or= ->
3241 COPILOT.drawlist[@state] = shapes
3342
3443 destroy: =>
241250 shape!
242251 love.graphics.pop!
243252
253 wrap_stream = (stream) ->
254 class extends Op
255 setup: =>
256 @out = stream
257 super Input.hot @out
258
259 poll: =>
260
244261 mouse_pos = Constant.meta
245262 meta:
246263 name: 'mouse-pos'
247264 summary: "outputs current mouse position."
248265 examples: { '(love/mouse-pos)' }
266 description: "vec2~ stream of mouse position."
267
268 value: wrap_stream COPILOT.mouse_pos
269
270 mouse_delta = Constant.meta
271 meta:
272 name: 'mouse-delta'
273 summary: "outputs mouse move events."
274 examples: { '(love/mouse-delta)' }
275 summary: "vec2! stream of mouse movements."
276
277 value: wrap_stream COPILOT.mouse_delta
278
279 mouse_presses = Constant.meta
280 meta:
281 name: 'mouse-presses'
282 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:
287 - `pos` (vec2): x/y position of mouse
288 - `button` (num): mouse button number"
289
290 value: wrap_stream COPILOT.mouse_presses
291
292 mouse_releases = Constant.meta
293 meta:
294 name: 'mouse-releases'
295 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:
300 - `pos` (vec2): x/y position of mouse
301 - `button` (num): mouse button number"
302
303 value: wrap_stream COPILOT.mouse_releases
304
305 mouse_down = Constant.meta
306 meta:
307 name: 'mouse-down?'
308 summary: "checks whether a mouse button is down."
309 examples: { '(love/mouse-down? [button])' }
310 description: "checks whether `button` is down and returns a bool ~-stream.
311
312 `button` should be a num~ stream and defaults to `1` (the left mouse button)."
249313
250314 value: class extends Op
251 new: (...) =>
252 super ...
253 @out or= vec2\mk_evt!
254 @state = {}
255
256 setup: =>
257 super io: Input.hot T.bang\mk_evt!
315 pattern = -sig.num
316 setup: (inputs) =>
317 button = pattern\match inputs
318
319 super
320 button: Input.hot button or T.num\mk_const 1
321 press: Input.hot COPILOT.mouse_presses
322 release: Input.hot COPILOT.mouse_releases
323
324 @out or= T.bool\mk_sig false
258325
259326 poll: =>
260 x, y = love.mouse.getPosition!
261 if x != @state.x or y != @state.y
262 @state.x, @state.y = x, y
263 if not @inputs.io\dirty!
264 @inputs.io.result\set true
265 true
266
267 tick: =>
268 @out\set { @state.x, @state.y }
327
328 tick: =>
329 { :button, :press, :release } = @unwrap_all!
330 if button and @inputs.button\dirty!
331 @state = false
332
333 if press and (not button or press.button == button)
334 @state = true
335
336 if release and (not button or release.button == button)
337 @state = false
338
339 @out\set @state
340
341 wheel_delta = Constant.meta
342 meta:
343 name: 'wheel-delta'
344 summary: "outputs mouse wheel move events."
345 examples: { '(love/wheel-delta)' }
346 description: "vec2! stream of mouse wheel movements."
347
348 value: wrap_stream COPILOT.wheel_delta
349
350 key_presses = Constant.meta
351 meta:
352 name: 'key-presses'
353 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
358
359 key_releases = Constant.meta
360 meta:
361 name: 'key-releases'
362 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
269367
270368 Constant.meta
271369 meta:
309407 :color, 'line-width': line_width
310408
311409 'mouse-pos': mouse_pos
410 'mouse-delta': mouse_delta
411 'mouse-presses': mouse_presses
412 'mouse-releases': mouse_releases
413 'mouse-down?': mouse_down
414 'wheel-data': wheel_delta
415 'key-presses': key_presses
416 'key-releases': key_releases