-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmouse.lua
More file actions
148 lines (130 loc) · 3.02 KB
/
Copy pathmouse.lua
File metadata and controls
148 lines (130 loc) · 3.02 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
--mouse handling
local mouse = class({
name = "mouse",
})
mouse.buttons = {
1, 2, 3
}
function mouse:new()
self.button_data = {}
self.oldpos = vec2()
self.pos = vec2()
self.moved = false
self.delta = vec2()
self.scroll = vec2()
self.next_scroll = vec2()
self:clear()
end
function mouse:update(dt)
for _, v in ipairs(mouse.buttons) do
local d = self.button_data[v]
if #d.events > 0 then
local e = table.remove(d.events, 1)
if e == "pressed" then
d.time = 1
elseif e == "released" then
d.time = -1
end
else
d.time = d.time + dt * math.sign(d.time)
end
end
self.pos:sset(love.mouse.getPosition())
--check moved
self.moved = not self.pos:equals(self.oldpos)
self.delta:vset(self.pos):vsubi(self.oldpos)
self.oldpos:vset(self.pos)
--read buffer
self.scroll:vector_set(self.next_scroll)
self.next_scroll:scalar_set(0)
end
--callbacks
function mouse:mousepressed(x, y, button)
self.pos:sset(x, y)
local d = self.button_data[button]
if d == nil then --unknown buttons
return
end
table.insert(d.events, "pressed")
end
function mouse:mousereleased(x, y, button)
self.pos:sset(x, y)
local d = self.button_data[button]
if d == nil then --unknown buttons
return
end
table.insert(d.events, "released")
end
function mouse:wheelmoved(x, y)
self.next_scroll:scalar_add_inplace(x, y)
end
--clear the button states to all-released (handy on state transition)
function mouse:clear()
for _, v in ipairs(mouse.buttons) do
self.button_data[v] = {
time = 0,
events = {},
}
end
self.scroll:scalar_set(0)
self.next_scroll:scalar_set(0)
self.pos:sset(love.mouse.getPosition())
self.oldpos:vset(self.pos)
self.moved = false
end
function mouse:_raw_time(button)
local d = self.button_data[button]
return d and d.time or 0
end
--get the time a button has been pressed for
--or -1 if the button is not pressed
function mouse:pressed_time(button)
local t = self:_raw_time(button)
if t == nil or t < 0 then
return -1
end
return t - 1
end
--get the time a button has been released for
--or -1 if the button is not released
function mouse:released_time(button)
local t = self:_raw_time(button)
if t == nil or t > 0 then
return -1
end
return (t * -1) - 1
end
--return true if a button is currently pressed
function mouse:pressed(button)
return self:pressed_time(button) >= 0
end
--return true if a button is currently released
function mouse:released(button)
return self:released_time(button) >= 0
end
--return true if a button was just pressed (this frame)
function mouse:just_pressed(button)
return self:pressed_time(button) == 0
end
--return true if a button was just released (this frame)
function mouse:just_released(button)
return self:released_time(button) == 0
end
--"any" button
function mouse:any_pressed()
for _, button in ipairs(mouse.buttons) do
if self:pressed(button) then
return true
end
end
return false
end
function mouse:any_just_pressed()
for _, button in ipairs(mouse.buttons) do
if self:just_pressed(button) then
return true
end
end
return false
end
return mouse