Skip to content

Instantly share code, notes, and snippets.

@clofresh
Created September 5, 2016 01:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clofresh/d677873cd28e16c3b23889a8168c08a4 to your computer and use it in GitHub Desktop.
Save clofresh/d677873cd28e16c3b23889a8168c08a4 to your computer and use it in GitHub Desktop.
local V = require('vector')
local state = {idle = {}, pressed = {}}
local current = {
state = state.idle,
}
local lines = {}
local M = {
state = state,
current = current,
lines = lines,
tap = nil,
}
function M.load()
love.mousepressed = current.state.mousepressed
love.mousereleased = current.state.mousereleased
love.touchpressed = current.state.touchpressed
love.touchreleased = current.state.touchreleased
end
function M.update(dt)
for _, line in pairs(lines) do
line[1] = line[1] + dt
end
end
function M.getSwipe()
local line = table.remove(lines)
if line ~= nil then
local l = line[2]
return {x = l[3] - l[1], y = l[4] - l[2]}
else
return nil
end
end
function state.idle.mousepressed(x, y, button)
if button == 1 then
current.state = state.pressed
current.start = {x, y}
love.mousepressed = nil
love.mousereleased = current.state.mousereleased
love.touchpressed = nil
love.touchreleased = nil
end
end
function state.pressed.mousereleased(x2, y2, button)
if button == 1 then
local x1 = current.start[1]
local y1 = current.start[2]
if V.sqMag({x = x2 - x1, y = y2 - y1}) > 25 then
table.insert(lines, {0, {x1, y1, x2, y2}})
else
M.tap = {x=x2, y=y2}
end
current.state = state.idle
current.start = nil
love.mousepressed = current.state.mousepressed
love.mousereleased = nil
love.touchpressed = current.state.touchpressed
love.touchreleased = nil
end
end
function state.idle.touchpressed(_, x, y)
current.state = state.pressed
current.start = {x, y}
love.mousepressed = nil
love.mousereleased = nil
love.touchpressed = nil
love.touchreleased = current.state.touchreleased
end
function state.pressed.touchreleased(_, x2, y2)
local x1 = current.start[1]
local y1 = current.start[2]
if V.sqMag({x = x2 - x1, y = y2 - y1}) > 25 then
table.insert(lines, {0, {x1, y1, x2, y2}})
else
M.tap = {x=x2, y=y2}
end
current.state = state.idle
current.start = nil
love.mousepressed = current.state.mousepressed
love.mousereleased = nil
love.touchpressed = current.state.touchpressed
love.touchreleased = nil
end
return M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment