OpenComputerScripts/libevent.lua

203 lines
5.6 KiB
Lua
Raw Normal View History

2017-10-23 18:59:47 +08:00
require("checkarg")
2017-10-23 18:38:37 +08:00
local event=require("event")
2018-01-04 13:38:11 +08:00
local internal_evtb={}
local function canEventTranslate(name)
for k,v in pairs(internal_evtb) do
if(k==name) then
return true
end
end
return false
end
local function doEventCustomTranslate(raw_event,t)
local name=raw_event[1]
local call=internal_evtb[name]
call(raw_event,t)
end
local function doEventTranslate(raw_event)
local t={}
local nop=0
local name=raw_event[1]
local a=raw_event[2]
local b=raw_event[3]
local c=raw_event[4]
local d=raw_event[5]
local e=raw_event[6]
local f=raw_event[7]
local g=raw_event[8]
t["event"]=name
if(name=="component_added" or name=="component_removed") then
t["address"]=a
t["componentType"]=b
elseif(name=="component_available" or name=="component_unavailable") then
t["componentType"]=a
elseif(name=="term_available" or name=="term_unavailable") then
nop=nop+1
elseif(name=="screen_resized") then
t["screenAddress"]=a
t["newWidth"]=b
t["newHeight"]=c
elseif(name=="touch" or name=="drag" or name=="drop") then
t["screenAddress"]=a
t["x"]=b
t["y"]=c
t["button"]=d
t["playerName"]=e
elseif(name=="scroll") then
t["screenAddress"]=a
t["x"]=b
t["y"]=c
t["direction"]=d
t["playerName"]=e
elseif(name=="walk") then
t["screenAddress"]=a
t["x"]=b
t["y"]=c
t["playerName"]=d
elseif(name=="key_down" or name=="key_up") then
t["keyboardAddress"]=a
t["char"]=b
t["code"]=c
t["playerName"]=d
elseif(name=="clipboard") then
t["keyboardAddress"]=a
t["value"]=b
t["playerName"]=c
elseif(name=="redstone_changed") then
t["address"]=a
t["side"]=b
t["oldValue"]=c
t["newValue"]=d
elseif(name=="motion") then
t["address"]=a
t["relativeX"]=b
t["relativeY"]=c
t["relativeZ"]=d
t["entityName"]=e
elseif(name=="modem_message") then --- Special
t["receiverAddress"]=a
t["senderAddress"]=b
t["port"]=c
t["distance"]=d
local dtb={}
for i=6,raw_event.n,1 do
table.insert(dtb,raw_event[i])
end
t["data"]=dtb
elseif(name=="inventory_changed") then
t["slot"]=a
elseif(name=="bus_message") then
t["protocolId"]=a
t["senderAddress"]=b
t["targetAddress"]=c
t["data"]=d
t["metadata"]=e
2018-01-04 14:16:23 +08:00
elseif(name=="interrupted") then
t["uptime"]=a
2018-01-04 13:44:40 +08:00
elseif(name=="minecart") then
2018-01-04 13:38:11 +08:00
t["detectorAddress"]=a
t["minecartType"]=b
t["minecartName"]=c
t["primaryColor"]=d
t["secondaryColor"]=e
t["destination"]=f
t["ownerName"]=g
elseif(name=="aspect_changed") then
t["address"]=a
t["signalName"]=b
t["signalValue"]=c
else -- Unknown Event
if(canEventTranslate(name)) then -- Try Translate
doEventCustomTranslate(raw_event,t)
else -- Cannot Translate
return table.unpack(raw_event)
end
end
return t
end
function SetEventTranslator(event_name,callback)
checkstring(event_name)
if(callback~=nil) then
checkfunction(callback)
end
internal_evtb[event_name]=callback
end
2017-10-23 18:59:47 +08:00
function AddEventListener(EventName,CallbackFunction)
checkstring(EventName)
checkfunction(CallbackFunction)
2018-01-04 13:38:11 +08:00
return event.listen(EventName,
function(...)
local raw_event=table.pack(...)
local rt=table.pack(doEventTranslate(raw_event))
if(type(rt[1])=="table") then
return CallbackFunction(rt[1])
else
return CallbackFunction(table.unpack(rt))
end
end)
2017-10-23 18:38:37 +08:00
end
function RemoveEventListener(ListenerID)
2017-10-23 18:59:47 +08:00
checknumber(ListenerID)
2017-10-23 18:38:37 +08:00
return event.ignore(event.handlers[ListenerID].key,event.handlers[ListenerID].callback)
2017-10-23 18:59:47 +08:00
end
2018-01-04 14:10:47 +08:00
function WaitEvent(...)
local tb=table.pack(...)
if(tb.n==0) then -- WaitEvent(),event.pull()
2018-01-04 13:44:40 +08:00
return doEventTranslate(table.pack(event.pull()))
2018-01-04 14:10:47 +08:00
elseif(type(tb[1])=="string") then
if(tb[2]==nil) then -- WaitEvent("key_up"),event.pull("key_up")
2018-01-04 14:10:47 +08:00
return doEventTranslate(table.pack(event.pull(tb[1])))
else -- WaitEvent("key_up",1),event.pull(1,"key_up")
checknumber(tb[2])
return doEventTranslate(table.pack(event.pull(tb[2],tb[1])))
end
elseif(type(tb[1])=="number") then -- WaitEvent(1),event.pull(1)
return doEventTranslate(table.pack(event.pull(tb[1])))
else
2018-01-04 14:35:10 +08:00
error("Syntax error. Usage: WaitEvent([EventName],[TimeOutSecond])")
2018-01-04 13:44:40 +08:00
end
2017-10-23 18:59:47 +08:00
end
2018-01-04 14:35:10 +08:00
function WaitMultipleEvent(...)
local tb=table.pack(...)
for i=1,tb.n,1 do
if(type(tb[i])~="string") then
error("Syntax error. Usage: WaitMultipleEvent(EventName1,[EventName2]...)")
end
end
return doEventTranslate(table.pack(event.pullMultiple(...)))
end
2017-10-23 19:42:40 +08:00
function PushEvent(EventName,...)
checkstring(EventName)
return event.push(EventName,...)
end
2017-10-23 19:09:15 +08:00
function AddTimer(Interval,CallbackFunction,Times)
checknumber(Interval)
checkfunction(CallbackFunction)
2017-10-25 22:18:52 +08:00
checknumber(Times)
if(Times<1) then -- Timer will infinitly run (when times <0)
return event.timer(Interval,CallbackFunction,math.huge)
else -- Timer will run [Times] times.
2017-10-23 19:09:15 +08:00
return event.timer(Interval,CallbackFunction,Times)
end
end
function RemoveTimer(TimerID)
checknumber(TimerID)
return event.cancel(TimerID)
end