Posted 14 April 2013 - 10:41 AM
Hey guys, I needed to make my flexibleUI API compatible (sorta) with basic computers with no mouse support, so here is the result: a silly API that allows you to use mouse click (left click only for now).
There is no need for screens as of yet.
The use is [ui name.]pullEvent([filter]) and uses arrow key and enter for clicking.
Note this is a WIP API, and I will gladly improve it if it is used, suggestions are welcome ^^
Enjoy :)/>
Small script to test with :
There is no need for screens as of yet.
The use is [ui name.]pullEvent([filter]) and uses arrow key and enter for clicking.
Note this is a WIP API, and I will gladly improve it if it is used, suggestions are welcome ^^
Enjoy :)/>
function pullEvent(filter)
local xx, yy = term.getCursorPos()
local event = {}
local w, h = term.getSize()
local nKeys = {200, 203, 205, 208, 28}
local sKeys = {'UP', 'LEFT', 'RIGHT', 'DOWN', 'LCLICK'}
local x, y = math.floor(w/2), math.floor(h/2)
filter = filter or 'none'
term.setCursorBlink(true)
local function inTable(tbl, item)
for key, value in pairs(tbl) do
if value == item then
return key
end
end
return 0
end
while true do
term.setCursorPos(x, y)
local ev, p1, p2, p3 = os.pullEvent()
local it = inTable(nKeys, tonumber(p1))
if ev == 'key' and it ~= 0 then
local k = sKeys[it]
if k == 'UP' and y > 1 then
y = y - 1
elseif k == 'DOWN' and y < h then
y = y + 1
elseif k == 'LEFT' and x > 1 then
x = x - 1
elseif k == 'RIGHT' and x < w then
x = x + 1
elseif k == 'LCLICK' then
event = {'mouse_click', 1, x, y}
break
end
elseif ev == filter then
event = {ev, p1, p2, p3}
break
end
end
term.setCursorBlink(false)
term.setCursorPos(xx, yy)
return unpack(event)
end
Small script to test with :
term.clear()
term.setCursorPos(1, 1)
while true do
_, _, x, y = pullEvent()
print(x, ':', y)
end