2
-- This tracks the state of the mouse, i.e. its coordinates onscreen
3
-- and if a button was just pressed or released this frame.
5
-- See http://love2d.org/wiki/MouseConstant for a list of mouse button names.
10
Mouse = Sprite:extend{
13
-- private property: _thisFrame
14
-- what buttons are pressed this frame
15
-- if you are interested in this, use allPressed() instead
18
-- private property: lastFrame
19
-- what mouse buttons were pressed last frame
22
new = function (self, obj)
23
obj = self:extend(obj)
25
love.mousepressed = function (x, y, button) obj:mousePressed(button) end
26
love.mousereleased = function (x, y, button) obj:mouseReleased(button) end
27
if obj.onNew then obj:onNew() end
32
-- Are *any* of the buttons passed held down this frame?
35
-- string button descriptions passed as individual arguments;
36
-- if none are passed, the left mouse button is assumed
41
pressed = function (self, ...)
44
if #buttons == 0 then buttons[1] = 'l' end
46
for _, value in pairs(buttons) do
48
assert(type(value) == 'string', 'all mouse buttons are strings; asked to check a ' .. type(value))
51
if self._thisFrame[value] then
59
-- Method: justPressed
60
-- Are *any* of the buttons passed pressed for the first time this frame?
63
-- string button descriptions passed as individual arguments;
64
-- if none are passed, the left mouse button is assumed
69
justPressed = function (self, ...)
72
if #buttons == 0 then buttons[1] = 'l' end
74
for _, value in pairs(buttons) do
76
assert(type(value) == 'string', 'all mouse buttons are strings; asked to check a ' .. type(value))
79
if self._thisFrame[value] and not self._lastFrame[value] then
88
-- Are *all* of the buttons passed not held down this frame?
91
-- string button descriptions passed as individual arguments;
92
-- if none are passed, the left mouse button is assumed
97
released = function (self, ...)
99
if #buttons == 0 then buttons[1] = 'l' end
101
for _, value in pairs(buttons) do
103
assert(type(value) == 'string', 'all mouse buttons are strings; asked to check a ' .. type(value))
106
if self._thisFrame[value] then
114
-- Method: justReleased
115
-- Are *any* of the buttons passed released after being held last frame?
118
-- string button descriptions passed as individual arguments;
119
-- if none are passed, the left mouse button is assumed
124
justReleased = function (self, ...)
125
local buttons = {...}
126
if #buttons == 0 then buttons[1] = 'l' end
128
for _, value in pairs(buttons) do
130
assert(type(value) == 'string', 'all mouse buttons are strings; asked to check a ' .. type(value))
133
if self._lastFrame[value] and not self._thisFrame[value] then
141
-- Method: allPressed
142
-- Returns all buttons currently pressed this frame.
148
-- string button descriptions; if nothing is pressed, nil
150
allPressed = function (self)
153
for key, value in pairs(self._thisFrame) do
154
if value then table.insert(result, key) end
157
return unpack(result)
160
-- Method: allJustPressed
161
-- Returns all buttons just pressed this frame.
167
-- string button descriptions; if nothing is just pressed, nil
169
allJustPressed = function (self)
172
for key, value in pairs(self._thisFrame) do
173
if value and not self._lastFrame[key] then table.insert(result, key) end
176
return unpack(result)
179
-- Method: allJustReleased
180
-- Returns all buttons just released this frame.
186
-- string buttons descriptions; if nothing is just pressed, nil
188
allJustPressed = function (self)
191
for key, value in pairs(self._thisFrame) do
192
if not value and self._lastFrame[key] then table.insert(result, key) end
195
return unpack(result)
198
mousePressed = function (self, button)
199
self._thisFrame[button] = true
202
mouseReleased = function (self, button)
203
self._thisFrame[button] = false
206
endFrame = function (self, elapsed)
207
for key, value in pairs(self._thisFrame) do
208
self._lastFrame[key] = value
211
self.x = love.mouse.getX() - the.app.inset.x
212
self.y = love.mouse.getY() - the.app.inset.y
214
Sprite.endFrame(self)
217
update = function() end