rubydraw 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/examples/image_ex.rb CHANGED
@@ -6,10 +6,13 @@ class MyWindow < Rubydraw::Window
6
6
  def initialize(width, height)
7
7
  super(width, height)
8
8
  @image = Rubydraw::Image.new("media/bug.png")
9
- @max = 100
10
-
11
- register_action(Rubydraw::Events::MouseMove) {|event| @mouse_position = event.position}
12
- register_action(Rubydraw::Events::QuitRequest) {close}
9
+ @focused = true
10
+ whenever Rubydraw::Events::QuitRequest do
11
+ close
12
+ end
13
+ whenever Rubydraw::Events::MouseMove do |event|
14
+ @mouse_position = event.position
15
+ end
13
16
  end
14
17
 
15
18
  def mouse_moved(event)
@@ -17,9 +20,8 @@ class MyWindow < Rubydraw::Window
17
20
  puts "Mouse moved! New position: #{new_position.x}, #{new_position.y}"
18
21
  end
19
22
 
20
- # Draw the image inside this window.
21
23
  def tick
22
- @image.draw(self, @mouse_position)
24
+ @image.draw(self, @mouse_position) #if @focused
23
25
  end
24
26
  end
25
27
 
@@ -20,10 +20,12 @@ module Rubydraw
20
20
  # Translate the given SDL event to its corresponding Rubydraw event, by asking
21
21
  # each event class if it matches the SDL event. No case statements here.
22
22
  def self.match(sdl_event)
23
- sdl_event_type = sdl_event.type
24
23
  event_classes = Event.all_subclasses.compact
25
24
  rubydraw_event = UnknownEvent.new
26
- event_classes.each { |event| rubydraw_event = event.from_sdl_event(sdl_event) if event.matches?(sdl_event_type) }
25
+ # Remove all the classes that don't want to be included in the search
26
+ event_classes.delete_if {|event_class| not event_class.wants_to_match?}
27
+ event_classes.each {|event_class| rubydraw_event = event_class.from_sdl_event(sdl_event) if event_class.matches?(sdl_event)}
28
+ #puts rubydraw_event.class
27
29
  rubydraw_event
28
30
  end
29
31
 
@@ -31,6 +33,11 @@ module Rubydraw
31
33
  # won't be recognized as an event and therefore will *not* participate in event
32
34
  # matching.
33
35
  class Event
36
+ # Include the class for searching by default.
37
+ def self.wants_to_match?
38
+ true
39
+ end
40
+
34
41
  # Just creates a new instance of this class by default. Override this if the subclass
35
42
  # requires parameters in +initialize,+ like Rubydraw::Events::MouseDown::from_sdl_event.
36
43
  def self.from_sdl_event(sdl_event)
@@ -40,7 +47,7 @@ module Rubydraw
40
47
  # Returns true if this is the overlaying class for the given SDL event. Override this
41
48
  # for custom matching.
42
49
  def self.matches?(sdl_event)
43
- sdl_event == matching_sdl_event
50
+ sdl_event.type == matching_sdl_type
44
51
  end
45
52
 
46
53
  # Returns true for all event objects. Also see Object#event?
@@ -50,7 +57,7 @@ module Rubydraw
50
57
 
51
58
  # Returns the matching SDL event, override this in subclasses. Returns nil by
52
59
  # default.
53
- def self.matching_sdl_event
60
+ def self.matching_sdl_type
54
61
  nil
55
62
  end
56
63
  end
@@ -64,6 +71,10 @@ module Rubydraw
64
71
  # Rubydraw::Events::KeyReleased. No instances of this class should be created,
65
72
  # but instances of subclasses are fine.
66
73
  class KeyboardEvent < Event
74
+ def self.wants_to_match?
75
+ false
76
+ end
77
+
67
78
  def self.from_sdl_event(sdl_event)
68
79
  self.new(sdl_event.keysym.sym)
69
80
  end
@@ -84,7 +95,11 @@ module Rubydraw
84
95
  # @key:: An integer specifying the key, which can be matched with a Rubydraw
85
96
  # button constants.
86
97
  class KeyPressed < KeyboardEvent
87
- def self.matching_sdl_event
98
+ def self.wants_to_match?
99
+ true
100
+ end
101
+
102
+ def self.matching_sdl_type
88
103
  SDL::KEYDOWN
89
104
  end
90
105
  end
@@ -94,7 +109,11 @@ module Rubydraw
94
109
  # @key:: An integer specifying the key, which can be matched with a Rubydraw
95
110
  # button constants.
96
111
  class KeyReleased < KeyboardEvent
97
- def self.matching_sdl_event
112
+ def self.wants_to_match?
113
+ true
114
+ end
115
+
116
+ def self.matching_sdl_type
98
117
  SDL::KEYUP
99
118
  end
100
119
  end
@@ -120,7 +139,7 @@ module Rubydraw
120
139
  self.new(Point[sdl_event.x, sdl_event.y], sdl_event.button)
121
140
  end
122
141
 
123
- def self.matching_sdl_event
142
+ def self.matching_sdl_type
124
143
  SDL::MOUSEBUTTONDOWN
125
144
  end
126
145
 
@@ -140,7 +159,7 @@ module Rubydraw
140
159
  self.new(Point[sdl_event.x, sdl_event.y], sdl_event.button)
141
160
  end
142
161
 
143
- def self.matching_sdl_event
162
+ def self.matching_sdl_type
144
163
  SDL::MOUSEBUTTONUP
145
164
  end
146
165
 
@@ -159,7 +178,7 @@ module Rubydraw
159
178
  self.new(Point[sdl_event.x, sdl_event.y], Point[sdl_event.xrel, sdl_event.yrel])
160
179
  end
161
180
 
162
- def self.matching_sdl_event
181
+ def self.matching_sdl_type
163
182
  SDL::MOUSEMOTION
164
183
  end
165
184
 
@@ -169,22 +188,62 @@ module Rubydraw
169
188
  #
170
189
  # +x+ and +y+: The new position of the cursor.
171
190
  #
172
- # +relative_x+ and +relative_y+: The relative movement of the cursor since the last tick.
191
+ # +relative_x+ and +relative_y+: The relative movement of the cursor since the lasttick.
173
192
  def initialize(position, relative_position)
174
193
  @position, @relative_position = position, relative_position
175
194
  end
176
195
  end
177
196
 
178
- # Created either when the window gains or loses focus.
179
- class WindowFocus
180
- def from_sdl_event(sdl_event)
181
- self.new(sdl_event.gain)
197
+ # Created either when the window gains or loses focus. This is the parent class for
198
+ # Rubydraw::Events::FocusGain and Rubydraw::Events::FocusLose.
199
+ class FocusEvent < Event
200
+ def self.wants_to_match?
201
+ false
202
+ end
203
+
204
+ def self.matching_sdl_type
205
+ SDL::ACTIVEEVENT
206
+ end
207
+ end
208
+
209
+ # Created when the window gains focus.
210
+ class FocusGain < FocusEvent
211
+ def self.wants_to_match?
212
+ true
213
+ end
214
+
215
+ # Redefine Event#matches? because both this class and Rubydraw::Events::FocusLoss use
216
+ # SDL::ActiveEvent.
217
+ def self.matches?(sdl_event)
218
+ result = false
219
+ if super(sdl_event)
220
+ result = sdl_event.gain == 1
221
+ end
222
+ result
223
+ end
224
+ end
225
+
226
+ # Created when the window loses focus.
227
+ class FocusLoss < FocusEvent
228
+ def self.wants_to_match?
229
+ true
230
+ end
231
+
232
+ # Redefine Event#matches? because both this class and Rubydraw::Events::FocuGain use
233
+ # SDL::ActiveEvent
234
+ def self.matches?(sdl_event)
235
+ result = false
236
+ if super(sdl_event)
237
+ result = sdl_event.gain == 0
238
+ end
239
+ result
182
240
  end
183
241
  end
184
242
 
185
243
  # Created when the user attempts to close the window.
186
244
  class QuitRequest < Event
187
- def self.matching_sdl_event
245
+
246
+ def self.matching_sdl_type
188
247
  SDL::QUIT
189
248
  end
190
249
  end
@@ -17,8 +17,6 @@ module Rubydraw
17
17
  # exist.
18
18
  raise Rubydraw::SDLError "Failed to load image from: '#{full_path}'"
19
19
  end
20
- puts @sdl_image.class
21
- puts @sdl_image.pointer.class
22
20
  end
23
21
 
24
22
  # Blit (copy) into the window at +position+ (see point.rb).
@@ -114,7 +114,7 @@ module Rubydraw
114
114
  # register_action(Rubydraw::Events::QuitRequest) {puts "Goodbye!"; close}
115
115
  # end
116
116
  # end
117
- def register_action(event, &block)
117
+ def whenever(event, &block)
118
118
  @registered_actions[event] = block
119
119
  end
120
120
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubydraw
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - J. Wostenberg