Skip to content

Commit 219f7eb

Browse files
author
Björn Ritzl
committed
Added flow.until_input_x
1 parent 69bfc69 commit 219f7eb

File tree

2 files changed

+65
-21
lines changed

2 files changed

+65
-21
lines changed

examples/flow/flow.script

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,23 @@ local flow = require "ludobits.m.flow"
33
function init(self)
44
msg.post(".", "acquire_input_focus")
55
flow(function()
6-
print("1 Flow has started")
6+
print("Flow has started")
77

88
-- wait until 0.5 seconds have elapsed
99
flow.delay(0.5)
10-
print("2 flow.delay() half a second has elapsed")
10+
print("flow.delay() half a second has elapsed")
1111

1212
-- wait until 10 updates/frames have been registered for this flow
1313
flow.frames(10)
14-
print("3 flow.frames() 10 frames have elapsed")
14+
print("flow.frames() 10 frames have elapsed")
15+
16+
print("Waiting for input")
17+
local action_id, action = flow.until_input_released(hash("touch"))
18+
print("flow.until_input_released()", action_id)
1519

1620
-- load a collection proxy and wait for it to become loaded
1721
flow.load("#a_proxy")
18-
print("4 flow.load() a_proxy loaded")
22+
print("flow.load() a_proxy loaded")
1923

2024
-- the running flow will wait for this flow to finish
2125
flow(function()
@@ -25,66 +29,66 @@ function init(self)
2529
flow.until_true(function()
2630
return go.get_position().x == 500
2731
end)
28-
print("5 flow.until_true() go.animate() done")
32+
print("flow.until_true() go.animate() done")
2933

3034
-- flows can be nested to any depth
3135
flow(function()
3236
flow.delay(1)
3337

3438
flow.load("#b_proxy")
35-
print("6 flow.load() b_proxy loaded")
39+
print("flow.load() b_proxy loaded")
3640
end)
3741

3842
flow.go_animate(".", "position.y", go.PLAYBACK_ONCE_PINGPONG, 500, go.EASING_INOUTCUBIC, 1, 0)
39-
print("7 flow.go_animate() done")
43+
print("flow.go_animate() done")
4044
end)
4145

4246
-- unload a collection proxy and wait until it is unloaded
4347
flow.unload("#a_proxy")
44-
print("8 flow.unload() a_proxy unloaded")
48+
print("flow.unload() a_proxy unloaded")
4549

4650
-- the running flow will not wait for this flow to finish
4751
flow(function()
48-
print("10 Parallel flow starting")
52+
print("Parallel flow starting")
4953
flow.delay(2)
50-
print("11 The outer flow did not wait for me")
54+
print("The outer flow did not wait for me")
5155
msg.post("#", "fooo")
5256
flow.delay(0.2)
5357
msg.post("#", "abc")
5458
flow.delay(0.2)
5559
msg.post("#", "booo")
5660
end, { parallel = true })
5761

58-
print("9 This flow will continue to run without waiting for the parallel flow to finish")
62+
print("This flow will continue to run without waiting for the parallel flow to finish")
5963

6064
-- wait for any message (the message will be posted by the parallel flow created above)
6165
local message_id, message, sender = flow.until_any_message()
62-
print("12 flow.until_any_message()", message_id, message, sender)
66+
print("flow.until_any_message()", message_id, message, sender)
6367

6468
-- wait for a specific message (the message will be posted by the parallel flow created above)
6569
local message_id, message, sender = flow.until_message(hash("booo"))
66-
print("13 flow.until_message()", message_id, message, sender)
70+
print("flow.until_message()", message_id, message, sender)
6771

6872
-- wait until a callback is invoked
6973
-- in this case we make a http.request call and wait for the callback
7074
local self, id, response = flow.until_callback(function(callback)
7175
http.request("http://www.google.com", "GET", callback)
7276
end)
73-
print("14 flow.until_callback() http.request() with callback done", response.status)
77+
print("flow.until_callback() http.request() with callback done", response.status)
7478

7579
-- waiting for a callback must also work when the callback is
7680
-- invoked immediately (as opposed to the result of some async operation)
7781
local foo, bar = flow.until_callback(function(callback)
7882
callback("foo", "bar")
7983
end)
80-
print("15 flow.until_callback() immediate callback", foo, bar)
84+
print("flow.until_callback() immediate callback", foo, bar)
8185

8286
flow.load("#a_proxy")
83-
print("16 flow.load() a_proxy loaded")
87+
print("flow.load() a_proxy loaded")
8488

8589
-- play a sprite animation and wait until it is done
8690
flow.play_animation("#sprite", "green_walk_once")
87-
print("17 DONE")
91+
print("DONE")
8892
end)
8993

9094
flow(function()
@@ -110,8 +114,5 @@ function on_message(self, message_id, message, sender)
110114
end
111115

112116
function on_input(self, action_id, action)
113-
if action.released then
114-
print("stopping")
115-
flow.stop(msg.url())
116-
end
117+
flow.on_input(action_id, action)
117118
end

ludobits/m/flow.lua

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,40 @@ function M.until_message(...)
226226
end
227227

228228

229+
--- Wait until input action with pressed state
230+
-- @param action_id Optional action to wait for (nil for any action)
231+
-- @return action_id
232+
-- @return action
233+
function M.until_input_pressed(action_id)
234+
local instance = create_or_get(coroutine.running())
235+
instance.state = WAITING
236+
instance.on_input = function(action_id, action)
237+
if (action_id and action_id == action_id) and action.pressed then
238+
instance.result = table_pack(action_id, action)
239+
instance.on_input = nil
240+
instance.state = READY
241+
end
242+
end
243+
return coroutine.yield()
244+
end
245+
246+
--- Wait until input action with released state
247+
-- @param action_id Optional action to wait for (nil for any action)
248+
-- @return action_id
249+
-- @return action
250+
function M.until_input_released(action_id)
251+
local instance = create_or_get(coroutine.running())
252+
instance.state = WAITING
253+
instance.on_input = function(action_id, action)
254+
if (action_id and action_id == action_id) and action.released then
255+
instance.result = table_pack(action_id, action)
256+
instance.on_input = nil
257+
instance.state = READY
258+
end
259+
end
260+
return coroutine.yield()
261+
end
262+
229263
--- Wait until a callback function is invoked
230264
-- @param fn The function to call. The function must take a callback function as its first argument
231265
-- @param arg1 Additional argument to pass to fn
@@ -433,6 +467,15 @@ function M.on_message(message_id, message, sender)
433467
end
434468
end
435469

470+
function M.on_input(action_id, action)
471+
local url = msg.url()
472+
for _,instance in pairs(instances) do
473+
if instance.on_input and instance.url == url then
474+
instance.on_input(action_id, action)
475+
end
476+
end
477+
end
478+
436479
return setmetatable(M, {
437480
__call = function(self, ...)
438481
return M.start(...)

0 commit comments

Comments
 (0)