love-loader/lib/keyb.lua

103 lines
2.4 KiB
Lua

return function(ll)
local mx, my, mb, mpb
local dir, sc1, sc2, sclm1, sclm2
-- d - direction (h, v, x, y, *)
-- c1 - coordinate before card (mouse) (be left or top)
-- c2 - coordinate after card (mouse) (be right or bottom)
-- clim1 - other coordinate limit before (mouse) (set -1 to disable)
-- clim2 - other coordinate limit after (mouse) (set -1 to disable)
function ll.kbInit(d, c1, c2, clim1, clim2)
if d == 'h' or d == 'v' or d == '*'
then dir = d
elseif d == 'y'
then dir = 'v'
elseif d == 'x'
then dir = 'h'
else error 'Direction must be *, h (x) or v (y)'
end
ll.kbSetCC(c1, c2)
ll.kbSetOC(clim1, clim2)
end
-- set card coordinate
-- c1 - coordinate before card (mouse) (be left or top)
-- c2 - coordinate after card (mouse) (be right or bottom)
function ll.kbSetCC(c1, c2)
c1, c2 =
tonumber(c1) or 0,
tonumber(c2) or 0
sc1, sc2 =
math.min(c1, c2),
math.max(c1, c2)
end
-- set other coordinate limit
-- clim1 - other coordinate limit before (mouse) (set -1 to disable)
-- clim2 - other coordinate limit after (mouse) (set -1 to disable)
function ll.kbSetOC(clim1, clim2)
sclm1, sclm2 =
tonumber(clim1) or -1,
tonumber(clim2) or -1
end
-- returns: <, >, o, m, nil
-- ^ and v if dir is *
function ll.kbGet()
assert(dir, 'Call ll.kbInit() before')
assert(love, 'Use it with LÖVE engine')
mx, my = love.mouse.getPosition()
mpb = mb
if love.mouse.isDown(1) then mb = 1
else mb = 0
end
if love.keyboard.isScancodeDown('up', 'w')
then return dir == '*' and '^' or '<'
elseif love.keyboard.isScancodeDown('left', 'a')
then return '<'
elseif love.keyboard.isScancodeDown('down', 's')
then return dir == '*' and 'v' or '>'
elseif love.keyboard.isScancodeDown('right', 'd')
then return '>'
elseif love.keyboard.isScancodeDown('return', 'space')
then return 'o'
elseif love.keyboard.isDown 'menu'
then return 'm'
elseif mb == 0 and mpb == 1 then -- mouse unpressed
if dir == 'h' then
if (sclm1 < 0 or my > sclm1)
and (sclm2 < 0 or my <= sclm2) then
if mx <= sc1
then return '<'
elseif mx >= sc2
then return '>'
else return 'o'
end
end
else
if (sclm1 < 0 or mx > sclm1)
and (sclm2 < 0 or mx <= sclm2) then
if my <= sc1
then return '<'
elseif my >= sc2
then return '>'
else return 'o'
end
end
end
end
end
end