rubygame 2.3.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,44 @@
1
+ #--
2
+ # This file is one part of:
3
+ # Rubygame -- Ruby code and bindings to SDL to facilitate game creation
4
+ #
5
+ # Copyright (C) 2008 John Croisant
6
+ #
7
+ # This library is free software; you can redistribute it and/or
8
+ # modify it under the terms of the GNU Lesser General Public
9
+ # License as published by the Free Software Foundation; either
10
+ # version 2.1 of the License, or (at your option) any later version.
11
+ #
12
+ # This library is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
+ # Lesser General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public
18
+ # License along with this library; if not, write to the Free Software
19
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
+ #++
21
+
22
+
23
+ module Rubygame
24
+
25
+ # The Events module contains classes representing various
26
+ # hardware events (e.g. keyboard presses, mouse clicks)
27
+ # and software events (e.g. clock tick, window becomes active)
28
+ #
29
+ # This event classes are meant as a full replacement for
30
+ # the older event classes defined in the Rubygame module
31
+ # (e.g. KeyDownEvent, QuitEvent). The old classes are
32
+ # deprecated and should not be used anymore.
33
+ #
34
+ module Events
35
+ end
36
+
37
+ end
38
+
39
+
40
+ # Load all the ruby files in events/
41
+ glob = File.join( File.dirname(__FILE__), "events", "*.rb" )
42
+ Dir.glob( glob ).each do |path|
43
+ require path
44
+ end
@@ -0,0 +1,342 @@
1
+ #--
2
+ # This file is one part of:
3
+ # Rubygame -- Ruby code and bindings to SDL to facilitate game creation
4
+ #
5
+ # Copyright (C) 2008 John Croisant
6
+ #
7
+ # This library is free software; you can redistribute it and/or
8
+ # modify it under the terms of the GNU Lesser General Public
9
+ # License as published by the Free Software Foundation; either
10
+ # version 2.1 of the License, or (at your option) any later version.
11
+ #
12
+ # This library is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
+ # Lesser General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public
18
+ # License along with this library; if not, write to the Free Software
19
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
+ # ++
21
+
22
+
23
+ module Rubygame
24
+
25
+ module Events
26
+
27
+
28
+ # JoystickAxisMoved is an event that occurs when a
29
+ # joystick's control stick has changed position on
30
+ # one of its axes.
31
+ #
32
+ # A joystick axis measures the movement of the control
33
+ # stick. Most joysticks have at least 2 axes for each
34
+ # stick, one for horizontal movement, and one for
35
+ # vertical movement. Fancy ones have a third axis for
36
+ # measuring twist, and controllers with two sticks have
37
+ # at least 4 axes.
38
+ #
39
+ # Unlike simple buttons or keys, which have only 2 values
40
+ # (pressed or not-pressed), a joystick axis has a smooth
41
+ # spectrum of possible values, ranging from -1.0 to 1.0.
42
+ # This allows for smoother, more precise control than
43
+ # is possible with the keyboard.
44
+ #
45
+ class JoystickAxisMoved
46
+
47
+ attr_reader :joystick_id
48
+ attr_reader :axis
49
+ attr_reader :value
50
+
51
+
52
+ # Creates a new JoystickAxisMoved instance.
53
+ #
54
+ # joystick_id:: an integer identifying which joystick
55
+ # changed. The first joystick is 0.
56
+ # axis:: an integer identifying which axis changed.
57
+ # The first axis on each joystick is 0.
58
+ # value:: a Float representing the current value
59
+ # of the axis. Ranges from -1.0 to 1.0.
60
+ #
61
+ def initialize( joystick_id, axis, value )
62
+
63
+ unless joystick_id.kind_of?(Fixnum) and joystick_id >= 0
64
+ raise ArgumentError, "joystick_id must be an integer >= 0"
65
+ end
66
+
67
+ @joystick_id = joystick_id
68
+
69
+ unless axis.kind_of?(Fixnum) and axis >= 0
70
+ raise ArgumentError, "axis must be an integer >= 0"
71
+ end
72
+
73
+ @axis = axis
74
+
75
+ unless value.kind_of?(Numeric) and value.between?(-1.0, 1.0)
76
+ raise ArgumentError, "value must be a number in the range (-1.0)..(1.0)"
77
+ end
78
+
79
+ @value = value.to_f
80
+
81
+ end
82
+
83
+ end
84
+
85
+
86
+
87
+ # JoystickButtonEvent is a mixin module included in
88
+ # the JoystickButtonPressed and JoystickButtonReleased
89
+ # classes. It defines the #joystick_id and #button
90
+ # attribute readers.
91
+ #
92
+ module JoystickButtonEvent
93
+
94
+ attr_reader :joystick_id, :button
95
+
96
+
97
+ # Initializes the JoystickButtonEvent.
98
+ #
99
+ # joystick_id:: an integer identifying which joystick
100
+ # changed. The first joystick is 0.
101
+ # button:: an integer identifying which button was
102
+ # pressed or released. The first button
103
+ # on each joystick is 0.
104
+ #
105
+ def initialize( joystick_id, button )
106
+
107
+ unless joystick_id.kind_of?(Fixnum) and joystick_id >= 0
108
+ raise ArgumentError, "joystick_id must be an integer >= 0"
109
+ end
110
+
111
+ @joystick_id = joystick_id
112
+
113
+ unless button.kind_of?(Fixnum) and button >= 0
114
+ raise ArgumentError, "button must be an integer >= 0"
115
+ end
116
+
117
+ @button = button
118
+
119
+ end
120
+
121
+ end
122
+
123
+
124
+
125
+ # JoystickButtonPressed is an event that occurs when a
126
+ # joystick button has been pressed.
127
+ #
128
+ # A joystick button is a button that can be pressed
129
+ # by the user's thumbs or fingers. The number of buttons
130
+ # varies with the joystick. Most joysticks have at least
131
+ # two buttons; fancy ones can have up to 12 or even 20
132
+ # buttons.
133
+ #
134
+ # Like a mouse button, a joystick button is either "pressed"
135
+ # or "not pressed", with no middle states.
136
+ #
137
+ class JoystickButtonPressed
138
+ include JoystickButtonEvent
139
+
140
+
141
+ # Creates a new JoystickButtonPressed instance.
142
+ #
143
+ # joystick_id:: an integer identifying which joystick
144
+ # changed. The first joystick is 0.
145
+ # button:: an integer identifying which button was
146
+ # pressed. The first button on each
147
+ # joystick is 0.
148
+ #
149
+ def initialize( joystick_id, button )
150
+ super
151
+ end
152
+
153
+ end
154
+
155
+
156
+
157
+ # JoystickButtonReleased is an event that occurs when a
158
+ # joystick button is released (no longer being pressed).
159
+ #
160
+ # See also JoystickButtonPressed.
161
+ #
162
+ class JoystickButtonReleased
163
+ include JoystickButtonEvent
164
+
165
+
166
+ # Creates a new JoystickButtonPressed instance.
167
+ #
168
+ # joystick_id:: an integer identifying which joystick
169
+ # changed. The first joystick is 0.
170
+ # button:: an integer identifying which button was
171
+ # released. The first button on each
172
+ # joystick is 0.
173
+ #
174
+ def initialize( joystick_id, button )
175
+ super
176
+ end
177
+
178
+ end
179
+
180
+
181
+
182
+ # JoystickBallMoved is an event that occurs when a
183
+ # joystick's trackball has changed position.
184
+ #
185
+ # A joystick trackball is a ball which rotates freely in
186
+ # a socket, controlled by the user's fingers or thumb.
187
+ #
188
+ # A trackball reports movement on x and y axes, measured
189
+ # in pixels, just like a mouse does. However, a trackball
190
+ # does not report its current position, only its movement
191
+ # since the previous event.
192
+ #
193
+ class JoystickBallMoved
194
+
195
+ attr_reader :joystick_id
196
+ attr_reader :ball
197
+ attr_reader :rel
198
+
199
+
200
+ # Creates a new JoystickBallMoved instance.
201
+ #
202
+ # joystick_id:: an integer identifying which joystick
203
+ # changed. The first joystick is 0.
204
+ # ball:: an integer identifying which ball changed.
205
+ # The first ball on each joystick is 0.
206
+ # rel:: relative position (how much the ball moved
207
+ # since the previous event). [x,y], in pixels.
208
+ #
209
+ def initialize( joystick_id, ball, rel )
210
+
211
+ unless joystick_id.kind_of?(Fixnum) and joystick_id >= 0
212
+ raise ArgumentError, "joystick_id must be an integer >= 0"
213
+ end
214
+
215
+ @joystick_id = joystick_id
216
+
217
+ unless ball.kind_of?(Fixnum) and ball >= 0
218
+ raise ArgumentError, "ball must be an integer >= 0"
219
+ end
220
+
221
+ @ball = ball
222
+
223
+ @rel = rel.to_ary.dup
224
+ @rel.freeze
225
+
226
+ unless @rel.length == 2
227
+ raise ArgumentError, "rel must have exactly 2 parts (got %s)"%@rel.length
228
+ end
229
+
230
+ end
231
+
232
+ end
233
+
234
+
235
+
236
+ # JoystickHatMoved is an event that occurs when a
237
+ # joystick's hat switch has changed direction.
238
+ #
239
+ # A joystick hat switch is a round switch that can be pressed
240
+ # in 8 possible directions: up, down, left, right, or the four
241
+ # diagonal directions. (Some hat switches support extra diagonal
242
+ # directions, but only those 8 directions are supported by
243
+ # Rubygame.)
244
+ #
245
+ class JoystickHatMoved
246
+
247
+ attr_reader :joystick_id, :hat, :direction
248
+ attr_reader :horizontal, :vertical
249
+
250
+
251
+ # Mapping direction symbol to horizontal and vertical parts
252
+ @@direction_map = {
253
+ :up => [ 0, -1],
254
+ :up_right => [ 1, -1],
255
+ :right => [ 1, 0],
256
+ :down_right => [ 1, 1],
257
+ :down => [ 0, 1],
258
+ :down_left => [-1, 1],
259
+ :left => [-1, 0],
260
+ :up_left => [-1, -1],
261
+ nil => [ 0, 0]
262
+ }
263
+
264
+
265
+ # Creates a new JoystickHatMoved instance.
266
+ #
267
+ # joystick_id:: an integer identifying which joystick
268
+ # changed. The first joystick is 0.
269
+ # hat:: an integer identifying which hat switch
270
+ # changed. The first hat switch on each joystick
271
+ # is 0.
272
+ # direction:: a symbol telling the direction the hat switch
273
+ # is being pressed. The direction is either nil
274
+ # or one of these 8 symbols:
275
+ #
276
+ # :up
277
+ # :up_right
278
+ # :right
279
+ # :down_right
280
+ # :down
281
+ # :down_left
282
+ # :left
283
+ # :up_left
284
+ #
285
+ def initialize( joystick_id, hat, direction )
286
+
287
+ unless joystick_id.kind_of?(Fixnum) and joystick_id >= 0
288
+ raise ArgumentError, "joystick_id must be an integer >= 0"
289
+ end
290
+
291
+ @joystick_id = joystick_id
292
+
293
+ unless hat.kind_of?(Fixnum) and hat >= 0
294
+ raise ArgumentError, "hat must be an integer >= 0"
295
+ end
296
+
297
+ @hat = hat
298
+
299
+ unless @@direction_map.keys.include? direction
300
+ raise ArgumentError,
301
+ "invalid direction '%s'. "%[direction.inspect] +\
302
+ "Check the docs for valid directions."
303
+ end
304
+
305
+ @direction = direction
306
+
307
+ @horizontal, @vertical = @@direction_map[direction]
308
+
309
+ end
310
+
311
+
312
+ # True if the hat is in the center (not pressed in any
313
+ # direction).
314
+ def center?
315
+ @direction == nil
316
+ end
317
+
318
+
319
+ # True if the hat is pressed left, up-left, or down-left.
320
+ def left?
321
+ @horizontal == -1
322
+ end
323
+
324
+ # True if the hat is pressed right, up-right, or down-right.
325
+ def right?
326
+ @horizontal == 1
327
+ end
328
+
329
+ # True if the hat is pressed up, up-right, or up-left.
330
+ def up?
331
+ @vertical == -1
332
+ end
333
+
334
+ # True if the hat is pressed down, down-right, or down-left.
335
+ def down?
336
+ @vertical == 1
337
+ end
338
+
339
+ end
340
+
341
+ end
342
+ end
@@ -0,0 +1,132 @@
1
+ #--
2
+ # This file is one part of:
3
+ # Rubygame -- Ruby code and bindings to SDL to facilitate game creation
4
+ #
5
+ # Copyright (C) 2008 John Croisant
6
+ #
7
+ # This library is free software; you can redistribute it and/or
8
+ # modify it under the terms of the GNU Lesser General Public
9
+ # License as published by the Free Software Foundation; either
10
+ # version 2.1 of the License, or (at your option) any later version.
11
+ #
12
+ # This library is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
+ # Lesser General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public
18
+ # License along with this library; if not, write to the Free Software
19
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
+ # ++
21
+
22
+
23
+ module Rubygame
24
+
25
+ module Events
26
+
27
+ #
28
+ # KeyboardEvent is a mixin module included in the KeyPressed
29
+ # and KeyReleased classes. It defines the #key and #modifiers
30
+ # accessors.
31
+ #
32
+ module KeyboardEvent
33
+ attr_reader :key, :modifiers
34
+
35
+ #
36
+ # Initialize the KeyboardEvent.
37
+ #
38
+ # key:: a key symbol for the key that was pressed or
39
+ # released. (Symbol, required)
40
+ #
41
+ # modifiers:: an Array of key symbols for the modifier keys
42
+ # that were active when the event occured.
43
+ # (Array, optional)
44
+ #
45
+ def initialize( key, modifiers=[] )
46
+
47
+ unless key.kind_of? Symbol
48
+ raise ArgumentError, "key must be a :symbol"
49
+ end
50
+
51
+ @key = key
52
+
53
+ @modifiers = modifiers.to_ary.dup
54
+ @modifiers.freeze
55
+
56
+ end
57
+ end
58
+
59
+
60
+ #
61
+ # KeyPressed is an event class which occurs when a key
62
+ # on the keyboard is pressed down.
63
+ #
64
+ # This class gains #key and #modifiers readers from
65
+ # the KeyboardEvent mixin module.
66
+ #
67
+ # It also has #string, which is a UTF8 string containing
68
+ # the text character that was generated by the keystroke;
69
+ # if nothing was generated, #string will be the empty string,
70
+ # "".
71
+ #
72
+ # The #string attribute is useful for for taking text input
73
+ # (e.g. in a GUI). It supports UTF8 Unicode characters, and
74
+ # works correctly on many different types of keyboards.
75
+ #
76
+ class KeyPressed
77
+ include KeyboardEvent
78
+
79
+ attr_reader :string
80
+
81
+ #
82
+ # Create a new KeyPressed instance.
83
+ #
84
+ # key:: a key symbol for the key that was pressed or
85
+ # released. (Symbol, required)
86
+ #
87
+ # modifiers:: an Array of key symbols for the modifier keys
88
+ # that were active when the event occured.
89
+ # (Array, optional)
90
+ #
91
+ # string:: a String containing the text character that
92
+ # was generated by the keystroke, or "" if
93
+ # nothing was generated. (String, optional)
94
+ #
95
+ def initialize( key, modifiers=[], string="" )
96
+ super( key, modifiers )
97
+
98
+ @string = string.to_str.dup
99
+ @string.freeze
100
+
101
+ end
102
+ end
103
+
104
+
105
+ #
106
+ # KeyReleased is an event class which occurs when a key
107
+ # on the keyboard is released (no longer being pressed).
108
+ #
109
+ # This class gains #key and #modifiers readers from
110
+ # the KeyboardEvent mixin module.
111
+ #
112
+ class KeyReleased
113
+ include KeyboardEvent
114
+
115
+ #
116
+ # Create a new KeyReleased instance.
117
+ #
118
+ # key:: a key symbol for the key that was pressed or
119
+ # released. (Symbol, required)
120
+ #
121
+ # modifiers:: an Array of key symbols for the modifier keys
122
+ # that were active when the event occured.
123
+ # (Array, optional)
124
+ #
125
+ def initialize( key, modifiers=[] )
126
+ super
127
+ end
128
+
129
+ end
130
+
131
+ end
132
+ end