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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
-- Class: Mouse
-- This tracks the state of the mouse, i.e. its coordinates onscreen
-- and if a button was just pressed or released this frame.
--
-- See http://love2d.org/wiki/MouseConstant for a list of mouse button names.
--
-- Extends:
-- <Sprite>
Mouse = Sprite:extend{
visible = false,
-- private property: _thisFrame
-- what buttons are pressed this frame
-- if you are interested in this, use allPressed() instead
_thisFrame = {},
-- private property: lastFrame
-- what mouse buttons were pressed last frame
_lastFrame = {},
new = function (self, obj)
obj = self:extend(obj)
the.mouse = obj
love.mousepressed = function (x, y, button) obj:mousePressed(button) end
love.mousereleased = function (x, y, button) obj:mouseReleased(button) end
if obj.onNew then obj:onNew() end
return obj
end,
-- Method: pressed
-- Are *any* of the buttons passed held down this frame?
--
-- Arguments:
-- string button descriptions passed as individual arguments;
-- if none are passed, the left mouse button is assumed
--
-- Returns:
-- boolean
pressed = function (self, ...)
local buttons = {...}
if #buttons == 0 then buttons[1] = 'l' end
for _, value in pairs(buttons) do
if STRICT then
assert(type(value) == 'string', 'all mouse buttons are strings; asked to check a ' .. type(value))
end
if self._thisFrame[value] then
return true
end
end
return false
end,
-- Method: justPressed
-- Are *any* of the buttons passed pressed for the first time this frame?
--
-- Arguments:
-- string button descriptions passed as individual arguments;
-- if none are passed, the left mouse button is assumed
--
-- Returns:
-- boolean
justPressed = function (self, ...)
local buttons = {...}
if #buttons == 0 then buttons[1] = 'l' end
for _, value in pairs(buttons) do
if STRICT then
assert(type(value) == 'string', 'all mouse buttons are strings; asked to check a ' .. type(value))
end
if self._thisFrame[value] and not self._lastFrame[value] then
return true
end
end
return false
end,
-- Method: released
-- Are *all* of the buttons passed not held down this frame?
--
-- Arguments:
-- string button descriptions passed as individual arguments;
-- if none are passed, the left mouse button is assumed
--
-- Returns:
-- boolean
released = function (self, ...)
local buttons = {...}
if #buttons == 0 then buttons[1] = 'l' end
for _, value in pairs(buttons) do
if STRICT then
assert(type(value) == 'string', 'all mouse buttons are strings; asked to check a ' .. type(value))
end
if self._thisFrame[value] then
return false
end
end
return true
end,
-- Method: justReleased
-- Are *any* of the buttons passed released after being held last frame?
--
-- Arguments:
-- string button descriptions passed as individual arguments;
-- if none are passed, the left mouse button is assumed
--
-- Returns:
-- boolean
justReleased = function (self, ...)
local buttons = {...}
if #buttons == 0 then buttons[1] = 'l' end
for _, value in pairs(buttons) do
if STRICT then
assert(type(value) == 'string', 'all mouse buttons are strings; asked to check a ' .. type(value))
end
if self._lastFrame[value] and not self._thisFrame[value] then
return true
end
end
return false
end,
-- Method: allPressed
-- Returns all buttons currently pressed this frame.
--
-- Arguments:
-- none
--
-- Returns:
-- string button descriptions; if nothing is pressed, nil
allPressed = function (self)
local result = {}
for key, value in pairs(self._thisFrame) do
if value then table.insert(result, key) end
end
return unpack(result)
end,
-- Method: allJustPressed
-- Returns all buttons just pressed this frame.
--
-- Arguments:
-- none
--
-- Returns:
-- string button descriptions; if nothing is just pressed, nil
allJustPressed = function (self)
local result = {}
for key, value in pairs(self._thisFrame) do
if value and not self._lastFrame[key] then table.insert(result, key) end
end
return unpack(result)
end,
-- Method: allJustReleased
-- Returns all buttons just released this frame.
--
-- Arguments:
-- none
--
-- Returns:
-- string buttons descriptions; if nothing is just pressed, nil
allJustPressed = function (self)
local result = {}
for key, value in pairs(self._thisFrame) do
if not value and self._lastFrame[key] then table.insert(result, key) end
end
return unpack(result)
end,
mousePressed = function (self, button)
self._thisFrame[button] = true
end,
mouseReleased = function (self, button)
-- we ignore wheel-related events to force the button
-- to be detectable for at least one frame
if button ~= 'wd' and button ~= 'wu' then
self._thisFrame[button] = false
end
end,
endFrame = function (self, elapsed)
for key, value in pairs(self._thisFrame) do
self._lastFrame[key] = value
end
-- now force mouse wheel buttons to be released
self._thisFrame.wd = false
self._thisFrame.wu = false
self.x = love.mouse.getX() - the.app.inset.x
self.y = love.mouse.getY() - the.app.inset.y
Sprite.endFrame(self)
end,
update = function() end
}
|