-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinput.lua
53 lines (45 loc) · 1.77 KB
/
input.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
input = {
cursor = {
x = 0,
dx = 0,
y = 0,
dy = 0,
held = false, -- (held this frame)
press = false, -- (held this frame) and (not held last frame)
release = false, -- (not held this frame) and (held last frame)
},
obj = {}
}
input.obj.held = function(obj) -- Detects whether a given object is being held
return input.cursor.held and (obj.x <= input.cursor.x and obj.x + obj.w >= input.cursor.x) and (obj.y <= input.cursor.y and obj.y + obj.h >= input.cursor.y)
end
input.obj.press = function(obj) -- Dectects whether a given object is being pressed
return input.cursor.press and (obj.x <= input.cursor.x and obj.x + obj.w >= input.cursor.x) and (obj.y <= input.cursor.y and obj.y + obj.h >= input.cursor.y)
end
input.obj.release = function(obj) -- Detects whether a given object is being released
return input.cursor.release and (obj.x <= input.cursor.x and obj.x + obj.w >= input.cursor.x) and (obj.y <= input.cursor.y and obj.y + obj.h >= input.cursor.y)
end
input.update = function()
local now = love.mouse.isDown(1)
local last = input.cursor.held
input.cursor.held = now
input.cursor.press = now and (not last)
input.cursor.release = (not now) and last
if now then
local lastX = input.cursor.x
local lastY = input.cursor.y
input.cursor.x = math.min(math.floor(love.mouse.getX() / 3), 320)
input.cursor.y = math.min(math.floor(love.mouse.getY() / 3), 240)
if last then
input.cursor.dx = input.cursor.x - lastX
input.cursor.dy = input.cursor.y - lastY
else
input.cursor.dx = 0
input.cursor.dy = 0
end
else
input.cursor.dx = 0
input.cursor.dy = 0
end
end
return input