-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouse.lua
More file actions
69 lines (61 loc) · 1.98 KB
/
mouse.lua
File metadata and controls
69 lines (61 loc) · 1.98 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
local mouse = {}
e_mouse_state = { pressed = 1, released = 2, held = 3, idle = 4 }
mouse = {
is_idle = true,
position = { x = 0, y = 0 },
left = { prev = false, curr = false, state = 4, start = { x = 0, y = 0 }, delta = { x = 0, y = 0 } },
right = { prev = false, curr = false, state = 4, start = { x = 0, y = 0 }, delta = { x = 0, y = 0 } },
middle = { prev = false, curr = false, state = 4, start = { x = 0, y = 0 }, delta = { x = 0, y = 0 } }
}
function mouse.update()
local x, y = lovr.system.getMousePosition()
if x == mouse.position.x and y == mouse.position.y then
mouse.is_idle = true
else
mouse.is_idle = false
end
mouse.position.x, mouse.position.y = x, y
local button = { "left", "right", "middle" }
local buttons_idle = { false, false, false }
for i = 1, 3 do
if lovr.system.isMouseDown( i ) then
if mouse[ button[ i ] ].prev == false then
mouse[ button[ i ] ].prev = true
mouse[ button[ i ] ].curr = true
mouse[ button[ i ] ].state = e_mouse_state.pressed
mouse[ button[ i ] ].start = { x = mouse.position.x, y = mouse.position.y }
else
mouse[ button[ i ] ].prev = true
mouse[ button[ i ] ].curr = false
mouse[ button[ i ] ].state = e_mouse_state.held
mouse[ button[ i ] ].delta = {
x = (mouse[ button[ i ] ].start.x - mouse.position.x),
y = (mouse[ button[ i ] ].start.y - mouse.position.y)
}
end
buttons_idle[ i ] = false
else
if mouse[ button[ i ] ].prev == true and mouse[ button[ i ] ].curr == false then
mouse[ button[ i ] ].prev = false
mouse[ button[ i ] ].state = e_mouse_state.released
mouse[ button[ i ] ].delta = { x = 0, y = 0 }
else
mouse[ button[ i ] ].prev = false
mouse[ button[ i ] ].curr = false
mouse[ button[ i ] ].state = e_mouse_state.idle
buttons_idle[ i ] = true
end
end
end
if mouse.is_idle then
for i = 1, 3 do
if buttons_idle[ i ] == false then
mouse.is_idle = false
break
else
mouse.is_idle = true
end
end
end
end
return mouse