/ld27

To get this branch, use:
bzr branch /bzr/ld27

« back to all changes in this revision

Viewing changes to zoetrope/core/mouse.lua

  • Committer: Josh C
  • Date: 2013-08-25 02:04:52 UTC
  • Revision ID: josh@9ix.org-20130825020452-py8fijli8m5te5gv
improve performance with (sketchy, experimental) sprite batches

Show diffs side-by-side

added added

removed removed

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