control improvements
This commit is contained in:
+57
-39
@@ -18,10 +18,13 @@ function Aux.control.on_update()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Aux.control.event_listener(event, action)
|
|
||||||
local listener = { event=event, action=action }
|
|
||||||
|
function Aux.control.event_listener(event, action) -- async!
|
||||||
local self = {}
|
local self = {}
|
||||||
|
|
||||||
|
local listener = { event=event, action=action }
|
||||||
|
|
||||||
function self:set_action(self, action)
|
function self:set_action(self, action)
|
||||||
listener.action = action
|
listener.action = action
|
||||||
end
|
end
|
||||||
@@ -46,9 +49,9 @@ function Aux.control.event_listener(event, action)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Aux.control.update_listener(action)
|
function Aux.control.update_listener(action)
|
||||||
local listener = { action=action }
|
|
||||||
|
|
||||||
local self = {}
|
local self = {}
|
||||||
|
|
||||||
|
local listener = { action=action }
|
||||||
|
|
||||||
function self:set_action(self, action)
|
function self:set_action(self, action)
|
||||||
listener.action = action
|
listener.action = action
|
||||||
@@ -67,52 +70,67 @@ function Aux.control.update_listener(action)
|
|||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
function Aux.control.on_next_event(event, action)
|
|
||||||
local listener = Aux.control.event_listener(event)
|
|
||||||
listener.set_action(function()
|
|
||||||
listener.stop()
|
|
||||||
action()
|
|
||||||
end
|
|
||||||
listener.start()
|
|
||||||
end
|
|
||||||
|
|
||||||
function Aux.control.on_next_update(action)
|
|
||||||
|
function Aux.control.on_next_update(callback)
|
||||||
local listener = Aux.control.update_listener()
|
local listener = Aux.control.update_listener()
|
||||||
listener.set_action(function()
|
|
||||||
listener.stop()
|
listener:set_action(function()
|
||||||
action()
|
listener:stop()
|
||||||
|
callback()
|
||||||
end
|
end
|
||||||
listener.start()
|
|
||||||
|
listener:start()
|
||||||
end
|
end
|
||||||
|
|
||||||
function Aux.control.controller()
|
function Aux.control.as_soon_as(p, callback)
|
||||||
local state
|
local listener = Aux.control.update_listener()
|
||||||
|
|
||||||
|
listener:set_action(function()
|
||||||
|
if p() then
|
||||||
|
callback()
|
||||||
|
listener:stop()
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
listener:start()
|
||||||
|
end
|
||||||
|
|
||||||
|
function Aux.control.on_next_event(event, callback)
|
||||||
|
local ok
|
||||||
|
|
||||||
|
local listener = Aux.control.event_listener(event)
|
||||||
|
|
||||||
|
listener:set_action(function()
|
||||||
|
listener:stop()
|
||||||
|
ok = true
|
||||||
|
end
|
||||||
|
|
||||||
|
listener:start()
|
||||||
|
|
||||||
|
Aux.control.as_soon_as(function() return ok end, callback)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function Aux.control.timer()
|
||||||
local self = {}
|
local self = {}
|
||||||
|
|
||||||
local listener = Aux.control.update_listener(function()
|
local t_0
|
||||||
if state then
|
|
||||||
local continue, continuation = state.continue, state.continuation
|
|
||||||
if continue() then
|
|
||||||
state = nil
|
|
||||||
continuation()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end)()
|
|
||||||
|
|
||||||
function self:cleanup(self)
|
function self:start(self)
|
||||||
listener.stop()
|
t_0 = GetTime()
|
||||||
end
|
end
|
||||||
|
|
||||||
function self:wait(self, p, k)
|
function self:after(self, t, callback)
|
||||||
state = {
|
Aux.util.as_soon_as(function() return t_0 and GetTime() - t_0 > t end, callback)
|
||||||
continuation = k
|
|
||||||
continue = p
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
function self:wait_for_event(self, event, k)
|
|
||||||
self:wait(function()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Aux.control.after_time(t, callback)
|
||||||
|
local timer = Aux.control.timer()
|
||||||
|
timer:start()
|
||||||
|
timer:after(t, callback)
|
||||||
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user