/ld27

To get this branch, use:
bzr branch http://9ix.org/bzr/ld27

« back to all changes in this revision

Viewing changes to zoetrope/input/gamepad.lua

  • Committer: Josh C
  • Date: 2013-08-25 15:35:26 UTC
  • Revision ID: josh@9ix.org-20130825153526-dcl4okzlmbyhmziz
cruft

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
-- Class: Gamepad
 
2
-- This represents a single gamepad connected to the user's computer. This offers the 
2
3
-- usual functions to check on the status of buttons; you can also inspect other
3
4
-- controls, like analog joysticks, by checking properties like <axes> and <hats>.
 
5
-- (Incidentally, a hat is used for things like a digital control pad.) 
4
6
--
5
7
-- Normally, gamepad buttons are indexed by number. This class also adds virtual buttons
6
8
-- named 'left', 'right', 'up', 'down'. This consults the first two analog axes
74
74
                obj.balls = {}
75
75
                obj.hats = {}
76
76
 
77
 
                if obj.number <= love.joystick.getJoystickCount() then
78
 
                        -- if not love.joystick.isOpen(obj.number) then love.joystick.open(obj.number) end
79
 
                        Joystick = love.joystick.getJoysticks()[obj.number]
80
 
                        obj.name = Joystick:getName()
81
 
                        obj.numAxes = Joystick:getAxisCount()
82
 
                        obj.numButtons = Joystick:getButtonCount()
83
 
                        obj.numHats = Joystick:getHatCount()
 
77
                if obj.number <= love.joystick.getNumJoysticks() then
 
78
                        if not love.joystick.isOpen(obj.number) then love.joystick.open(obj.number) end
 
79
                        obj.name = love.joystick.getName(obj.number)
 
80
                        obj.numAxes = love.joystick.getNumAxes(obj.number)
 
81
                        obj.numBalls = love.joystick.getNumBalls(obj.number)
 
82
                        obj.numButtons = love.joystick.getNumButtons(obj.number)
 
83
                        obj.numHats = love.joystick.getNumHats(obj.number)
84
84
 
85
85
                        -- set initial values for axes and balls
86
86
                        -- hat values are strings so nil comparisons are safe
88
88
                        for i = 1, obj.numAxes do
89
89
                                obj.axes[i] = 0
90
90
                        end
 
91
 
 
92
                        for i = 1, obj.numBalls do
 
93
                                obj.balls[i] = { x = 0, y = 0 }
 
94
                        end
91
95
                else
92
96
                        obj.name = 'NO DEVICE CONNECTED'
93
97
                        obj.numAxes = 0
 
98
                        obj.numBalls = 0
94
99
                        obj.numButtons = 0
95
100
                        obj.numHats = 0
96
101
                end
118
123
                                return true
119
124
                        end
120
125
                end
121
 
 
 
126
                
122
127
                return false
123
128
        end,
124
129
 
139
144
                                return true
140
145
                        end
141
146
                end
142
 
 
 
147
                
143
148
                return false
144
149
        end,
145
150
 
154
159
 
155
160
        released = function (self, ...)
156
161
                local buttons = {...}
157
 
 
 
162
        
158
163
                for _, value in pairs(buttons) do
159
164
                        if self._thisFrame[value] then
160
165
                                return false
161
166
                        end
162
167
                end
163
 
 
 
168
                
164
169
                return true
165
170
        end,
166
171
 
175
180
 
176
181
        justReleased = function (self, ...)
177
182
                local buttons = {...}
178
 
 
 
183
        
179
184
                for _, value in pairs(buttons) do
180
185
                        if self._lastFrame[value] and not self._thisFrame[value] then
181
186
                                return true
182
187
                        end
183
188
                end
184
 
 
 
189
                
185
190
                return false
186
191
        end,
187
192
 
200
205
                for key, value in pairs(self._thisFrame) do
201
206
                        if value then table.insert(result, key) end
202
207
                end
203
 
 
 
208
                
204
209
                return unpack(result)
205
210
        end,
206
211
 
219
224
                for key, value in pairs(self._thisFrame) do
220
225
                        if value and not self._lastFrame[key] then table.insert(result, key) end
221
226
                end
222
 
 
 
227
                
223
228
                return unpack(result)
224
229
        end,
225
230
 
238
243
                for key, value in pairs(self._thisFrame) do
239
244
                        if not value and self._lastFrame[key] then table.insert(result, key) end
240
245
                end
241
 
 
 
246
                
242
247
                return unpack(result)
243
248
        end,
244
249
 
260
265
                -- set values
261
266
 
262
267
                for i = 1, self.numAxes do
263
 
                        self.axes[i] = Joystick:getAxis(i)
 
268
                        self.axes[i] = love.joystick.getAxis(self.number, i)
 
269
                end
 
270
 
 
271
                for i = 1, self.numBalls do
 
272
                        self.balls[i].x, self.balls[i].y = love.joystick.getBall(self.number, i)
264
273
                end
265
274
 
266
275
                for i = 1, self.numHats do
267
 
                        self.hats[i] = Joystick:getHat(i)
 
276
                        self.hats[i] = love.joystick.getHat(self.number, i)
268
277
                end
269
278
 
270
279
                -- simulate digital controls
320
329
                                end
321
330
                        end
322
331
                end
323
 
 
 
332
        
324
333
                Sprite.endFrame(self)
325
334
        end,
326
335