/ld26

To get this branch, use:
bzr branch /bzr/ld26
1 by Josh C
zoetrope 1.4
1
-- Class: Mouse
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.
4
--
5
-- See http://love2d.org/wiki/MouseConstant for a list of mouse button names.
6
--
7
-- Extends:
8
--		<Sprite>
9
10
Mouse = Sprite:extend{
11
	visible = false,
12
13
	-- private property: _thisFrame
14
	-- what buttons are pressed this frame
15
	-- if you are interested in this, use allPressed() instead
16
	_thisFrame = {},
17
18
	-- private property: lastFrame
19
	-- what mouse buttons were pressed last frame
20
	_lastFrame = {},
21
	
22
	new = function (self, obj)
23
		obj = self:extend(obj)
24
		the.mouse = 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
28
		return obj
29
	end,
30
31
	-- Method: pressed
32
	-- Are *any* of the buttons passed held down this frame?
33
	--
34
	-- Arguments:
35
	--		string button descriptions passed as individual arguments;
36
	--		if none are passed, the left mouse button is assumed
37
	--
38
	-- Returns:
39
	-- 		boolean
40
41
	pressed = function (self, ...)
42
		local buttons = {...}
43
44
		if #buttons == 0 then buttons[1] = 'l' end
45
	
46
		for _, value in pairs(buttons) do
47
			if STRICT then
48
				assert(type(value) == 'string', 'all mouse buttons are strings; asked to check a ' .. type(value))
49
			end
50
51
			if self._thisFrame[value] then
52
				return true
53
			end
54
		end
55
		
56
		return false
57
	end,
58
59
	-- Method: justPressed
60
	-- Are *any* of the buttons passed pressed for the first time this frame?
61
	--
62
	-- Arguments:
63
	--		string button descriptions passed as individual arguments;
64
	--		if none are passed, the left mouse button is assumed
65
	--
66
	-- Returns:
67
	-- 		boolean
68
69
	justPressed = function (self, ...)
70
		local buttons = {...}
71
72
		if #buttons == 0 then buttons[1] = 'l' end
73
	
74
		for _, value in pairs(buttons) do
75
			if STRICT then
76
				assert(type(value) == 'string', 'all mouse buttons are strings; asked to check a ' .. type(value))
77
			end
78
79
			if self._thisFrame[value] and not self._lastFrame[value] then
80
				return true
81
			end
82
		end
83
		
84
		return false
85
	end,
86
87
	-- Method: released
88
	-- Are *all* of the buttons passed not held down this frame?
89
	--
90
	-- Arguments:
91
	--		string button descriptions passed as individual arguments;
92
	--		if none are passed, the left mouse button is assumed
93
	--
94
	-- Returns:
95
	-- 		boolean
96
97
	released = function (self, ...)
98
		local buttons = {...}
99
		if #buttons == 0 then buttons[1] = 'l' end
100
	
101
		for _, value in pairs(buttons) do
102
			if STRICT then
103
				assert(type(value) == 'string', 'all mouse buttons are strings; asked to check a ' .. type(value))
104
			end
105
106
			if self._thisFrame[value] then
107
				return false
108
			end
109
		end
110
		
111
		return true
112
	end,
113
114
	-- Method: justReleased
115
	-- Are *any* of the buttons passed released after being held last frame?
116
	--
117
	-- Arguments:
118
	--		string button descriptions passed as individual arguments;
119
	--		if none are passed, the left mouse button is assumed
120
	--
121
	-- Returns:
122
	-- 		boolean
123
124
	justReleased = function (self, ...)
125
		local buttons = {...}
126
		if #buttons == 0 then buttons[1] = 'l' end	
127
	
128
		for _, value in pairs(buttons) do
129
			if STRICT then
130
				assert(type(value) == 'string', 'all mouse buttons are strings; asked to check a ' .. type(value))
131
			end
132
133
			if self._lastFrame[value] and not self._thisFrame[value] then
134
				return true
135
			end
136
		end
137
		
138
		return false
139
	end,
140
141
	-- Method: allPressed
142
	-- Returns all buttons currently pressed this frame.
143
	--
144
	-- Arguments:
145
	--		none
146
	--
147
	-- Returns:
148
	--		string button descriptions; if nothing is pressed, nil
149
150
	allPressed = function (self)
151
		local result = {}
152
153
		for key, value in pairs(self._thisFrame) do
154
			if value then table.insert(result, key) end
155
		end
156
		
157
		return unpack(result)
158
	end,
159
160
	-- Method: allJustPressed
161
	-- Returns all buttons just pressed this frame.
162
	--
163
	-- Arguments:
164
	--		none
165
	--
166
	-- Returns:
167
	--		string button descriptions; if nothing is just pressed, nil
168
169
	allJustPressed = function (self)
170
		local result = {}
171
172
		for key, value in pairs(self._thisFrame) do
173
			if value and not self._lastFrame[key] then table.insert(result, key) end
174
		end
175
		
176
		return unpack(result)
177
	end,
178
179
	-- Method: allJustReleased
180
	-- Returns all buttons just released this frame.
181
	--
182
	-- Arguments:
183
	--		none
184
	--
185
	-- Returns:
186
	--		string buttons descriptions; if nothing is just pressed, nil
187
188
	allJustPressed = function (self)
189
		local result = {}
190
191
		for key, value in pairs(self._thisFrame) do
192
			if not value and self._lastFrame[key] then table.insert(result, key) end
193
		end
194
		
195
		return unpack(result)
196
	end,
197
198
	mousePressed = function (self, button)
199
		self._thisFrame[button] = true
200
	end,
201
202
	mouseReleased = function (self, button)
203
		self._thisFrame[button] = false
204
	end,
205
206
	endFrame = function (self, elapsed)
207
		for key, value in pairs(self._thisFrame) do
208
			self._lastFrame[key] = value
209
		end
210
	
211
		self.x = love.mouse.getX() - the.app.inset.x
212
		self.y = love.mouse.getY() - the.app.inset.y
213
214
		Sprite.endFrame(self)
215
	end,
216
217
	update = function() end
218
}