/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: 2014-07-06 22:51:43 UTC
  • Revision ID: josh@9ix.org-20140706225143-q72jn0va7v4ssvyy
ignore lovebird, inspect

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.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)
 
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()
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
95
91
                else
96
92
                        obj.name = 'NO DEVICE CONNECTED'
97
93
                        obj.numAxes = 0
98
 
                        obj.numBalls = 0
99
94
                        obj.numButtons = 0
100
95
                        obj.numHats = 0
101
96
                end
123
118
                                return true
124
119
                        end
125
120
                end
126
 
                
 
121
 
127
122
                return false
128
123
        end,
129
124
 
144
139
                                return true
145
140
                        end
146
141
                end
147
 
                
 
142
 
148
143
                return false
149
144
        end,
150
145
 
159
154
 
160
155
        released = function (self, ...)
161
156
                local buttons = {...}
162
 
        
 
157
 
163
158
                for _, value in pairs(buttons) do
164
159
                        if self._thisFrame[value] then
165
160
                                return false
166
161
                        end
167
162
                end
168
 
                
 
163
 
169
164
                return true
170
165
        end,
171
166
 
180
175
 
181
176
        justReleased = function (self, ...)
182
177
                local buttons = {...}
183
 
        
 
178
 
184
179
                for _, value in pairs(buttons) do
185
180
                        if self._lastFrame[value] and not self._thisFrame[value] then
186
181
                                return true
187
182
                        end
188
183
                end
189
 
                
 
184
 
190
185
                return false
191
186
        end,
192
187
 
205
200
                for key, value in pairs(self._thisFrame) do
206
201
                        if value then table.insert(result, key) end
207
202
                end
208
 
                
 
203
 
209
204
                return unpack(result)
210
205
        end,
211
206
 
224
219
                for key, value in pairs(self._thisFrame) do
225
220
                        if value and not self._lastFrame[key] then table.insert(result, key) end
226
221
                end
227
 
                
 
222
 
228
223
                return unpack(result)
229
224
        end,
230
225
 
243
238
                for key, value in pairs(self._thisFrame) do
244
239
                        if not value and self._lastFrame[key] then table.insert(result, key) end
245
240
                end
246
 
                
 
241
 
247
242
                return unpack(result)
248
243
        end,
249
244
 
265
260
                -- set values
266
261
 
267
262
                for i = 1, self.numAxes do
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)
 
263
                        self.axes[i] = Joystick:getAxis(i)
273
264
                end
274
265
 
275
266
                for i = 1, self.numHats do
276
 
                        self.hats[i] = love.joystick.getHat(self.number, i)
 
267
                        self.hats[i] = Joystick:getHat(i)
277
268
                end
278
269
 
279
270
                -- simulate digital controls
329
320
                                end
330
321
                        end
331
322
                end
332
 
        
 
323
 
333
324
                Sprite.endFrame(self)
334
325
        end,
335
326