rubydraw 0.3.1.8 → 0.3.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/ext/object.rb +42 -31
- data/lib/rubydraw/events.rb +2 -10
- data/lib/rubydraw/image.rb +9 -21
- data/lib/rubydraw/sound.rb +4 -4
- data/lib/rubydraw/surface.rb +12 -1
- data/lib/rubydraw/text.rb +12 -1
- data/lib/rubydraw/window.rb +15 -5
- metadata +2 -2
data/lib/ext/object.rb
CHANGED
@@ -1,34 +1,45 @@
|
|
1
1
|
class Object
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
26
|
-
event_block = window.registered_actions[event]
|
27
|
-
# If nobody has registered a block for this event, prepare the way.
|
28
|
-
if event_block.nil?
|
29
|
-
window.registered_actions[event] = []
|
30
|
-
end
|
31
|
-
# Now add the block.
|
32
|
-
window.registered_actions[event] << block
|
2
|
+
# Execute the given block on the appearance of an instance of +event+ and pass that
|
3
|
+
# instance to the block.
|
4
|
+
#
|
5
|
+
# Example:
|
6
|
+
# class MyWindow < Rubydraw::Window
|
7
|
+
# def initialize
|
8
|
+
# super(300, 300)
|
9
|
+
# whenever(Rubydraw::Events::QuitRequest) do
|
10
|
+
# puts "Goodbye!"
|
11
|
+
# close
|
12
|
+
# end
|
13
|
+
# whenever(Rubydraw::Events::MouseMove) do |event|
|
14
|
+
# new_pos = event.position
|
15
|
+
# puts "Mouse moved to #{new_pos.x}, #{new_pos.y}.}"
|
16
|
+
# end
|
17
|
+
# end
|
18
|
+
# end
|
19
|
+
def register_action(event, window=self, &block)
|
20
|
+
# A very simple rule: +window+ must be a window. Therefore, since +window+ is self
|
21
|
+
# by default, instances of Rubydraw::Window calling this method don't have to set
|
22
|
+
# that parameter.
|
23
|
+
unless window.is_a?(Rubydraw::Window)
|
24
|
+
raise ArgumentError, "window must be a Rubydraw::Window"
|
33
25
|
end
|
26
|
+
event_block = window.registered_actions[event]
|
27
|
+
# If nobody has registered a block for this event, prepare the way.
|
28
|
+
if event_block.nil?
|
29
|
+
window.registered_actions[event] = {}
|
30
|
+
end
|
31
|
+
# Now add the block.
|
32
|
+
window.registered_actions[event][self] = block
|
33
|
+
end
|
34
|
+
|
35
|
+
alias whenever register_action
|
36
|
+
|
37
|
+
# Adds the ability to remove actions from registered_actions. Useful if, for example,
|
38
|
+
# one were to implement a button that only works if it is shown.
|
39
|
+
def unregister_action(event, window=self)
|
40
|
+
unless window.is_a?(Rubydraw::Window)
|
41
|
+
raise ArgumentError, "window must be a Rubydraw::Window"
|
42
|
+
end
|
43
|
+
window.registered_actions[event].delete(self)
|
44
|
+
end
|
34
45
|
end
|
data/lib/rubydraw/events.rb
CHANGED
@@ -66,7 +66,7 @@ module Rubydraw
|
|
66
66
|
class UnknownEvent < Event
|
67
67
|
# The SDL event should be passed so that the programmer using this library can still
|
68
68
|
# attempt to implement behavior, but it will have to match and SDL event, not a
|
69
|
-
# Rubydraw
|
69
|
+
# Rubydraw Event.
|
70
70
|
def from_sdl_event(sdl_event)
|
71
71
|
self.new(sdl_event)
|
72
72
|
end
|
@@ -215,7 +215,7 @@ module Rubydraw
|
|
215
215
|
# Rubydraw::Events::FocusGain and Rubydraw::Events::FocusLose.
|
216
216
|
class FocusEvent < Event
|
217
217
|
def self.wants_to_match?
|
218
|
-
|
218
|
+
not self == FocusEvent
|
219
219
|
end
|
220
220
|
|
221
221
|
def self.matching_sdl_type
|
@@ -226,10 +226,6 @@ module Rubydraw
|
|
226
226
|
# Created when the window gains focus, e.g. clicking on the window after previously using
|
227
227
|
# another application.
|
228
228
|
class FocusGain < FocusEvent
|
229
|
-
def self.wants_to_match?
|
230
|
-
true
|
231
|
-
end
|
232
|
-
|
233
229
|
# Redefine Event#matches? because both this class and Rubydraw::Events::FocusLoss use
|
234
230
|
# SDL::ActiveEvent.
|
235
231
|
def self.matches?(sdl_event)
|
@@ -242,10 +238,6 @@ module Rubydraw
|
|
242
238
|
|
243
239
|
# Created when the window loses focus.
|
244
240
|
class FocusLoss < FocusEvent
|
245
|
-
def self.wants_to_match?
|
246
|
-
true
|
247
|
-
end
|
248
|
-
|
249
241
|
# Redefine Event#matches? because both this class and Rubydraw::Events::FocuGain use
|
250
242
|
# SDL::ActiveEvent
|
251
243
|
def self.matches?(sdl_event)
|
data/lib/rubydraw/image.rb
CHANGED
@@ -5,21 +5,20 @@ module Rubydraw
|
|
5
5
|
# read the documentation for Image#draw), call it's #draw method
|
6
6
|
# and pass it the +x+ and +y+ coordinates.
|
7
7
|
class Image < Surface
|
8
|
-
# Create a new image and load the file from a path. Or, wrap an
|
9
|
-
# SDL::Surface.
|
10
8
|
def initialize(arg)
|
11
|
-
if arg.
|
12
|
-
# This
|
13
|
-
|
14
|
-
elsif arg.
|
15
|
-
|
9
|
+
if arg.kind_of?(SDL::Surface)
|
10
|
+
# This means we want to wrap an sdl surface.
|
11
|
+
@sdl_surface = arg
|
12
|
+
elsif arg.kind_of?(String)
|
13
|
+
# This means we want to load an image from a file.
|
14
|
+
initialize_from_path(arg)
|
16
15
|
else
|
17
|
-
|
16
|
+
# Raise an error if it is neither. I should figure out a less strict, more dynamic way to do this.
|
17
|
+
raise SDLError, "Argument to Rubydraw::Image.new was neither an image path nor an SDL::Surface to wrap"
|
18
18
|
end
|
19
|
-
self
|
20
19
|
end
|
21
20
|
|
22
|
-
def
|
21
|
+
def initialize_from_path(path)
|
23
22
|
# Check if this image has already been initialized. If it has, raise
|
24
23
|
# an error.
|
25
24
|
unless @sdl_surface.nil?
|
@@ -35,16 +34,5 @@ module Rubydraw
|
|
35
34
|
raise SDLError, "Failed to load image: #{SDL.GetError}"
|
36
35
|
end
|
37
36
|
end
|
38
|
-
|
39
|
-
def load_from_surface(surface) #:nodoc:
|
40
|
-
# Check if this image has already been initialized. If it has, raise
|
41
|
-
# an error.
|
42
|
-
unless @sdl_surface.nil?
|
43
|
-
raise SDLError, "Images may only be loaded once"
|
44
|
-
end
|
45
|
-
@sdl_surface = surface
|
46
|
-
end
|
47
|
-
|
48
|
-
private :load_from_path, :load_from_surface
|
49
37
|
end
|
50
38
|
end
|
data/lib/rubydraw/sound.rb
CHANGED
@@ -6,7 +6,7 @@ module Rubydraw
|
|
6
6
|
# Rubydraw::Sound#new: Creates a new sound with the given file path
|
7
7
|
#
|
8
8
|
# Rubydraw::Sound#play: Plays the sound. *Note*: this method exits
|
9
|
-
#
|
9
|
+
# immediately; it doesn't wait until the sound is finished.
|
10
10
|
class Sound
|
11
11
|
# Used to specify the volume (from 0 to 1) when this sound is played.
|
12
12
|
# Set at 1 by default
|
@@ -19,11 +19,11 @@ module Rubydraw
|
|
19
19
|
full_path = File.expand_path path
|
20
20
|
SDL::Mixer.OpenAudio(22050, SDL::AUDIO_S16SYS, 2, 1024)
|
21
21
|
@sdl_sound = SDL::Mixer::LoadWAV(full_path)
|
22
|
-
#
|
22
|
+
# Usually happens because the file doesn't exist.
|
23
23
|
if @sdl_sound.pointer.null?
|
24
|
-
raise SDLError "Failed to load sound from: #{full_path} because '#{SDL.GetError}'"
|
24
|
+
raise SDLError, "Failed to load sound from: #{full_path} because '#{SDL.GetError}'"
|
25
25
|
end
|
26
|
-
# The default volume. Can be changed with Sound's volume
|
26
|
+
# The default volume. Can be changed with Sound's volume attribute.
|
27
27
|
@volume = 1
|
28
28
|
# Allocate a new SDL channel all for this sound to use.
|
29
29
|
@channel = SDL::Mixer.AllocateChannels(SDL::Mixer.AllocateChannels(-1) + 1) - 1
|
data/lib/rubydraw/surface.rb
CHANGED
@@ -13,8 +13,17 @@ module Rubydraw
|
|
13
13
|
Text.new(contents, color)
|
14
14
|
end
|
15
15
|
|
16
|
+
def initialize(*args)
|
17
|
+
if args.size == 1
|
18
|
+
# Must mean to wrap an SDL surface.
|
19
|
+
@sdl_surface = args[0]
|
20
|
+
else
|
21
|
+
load_from_color(*args)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
16
25
|
# Create a new, blank surface with the given dimensions.
|
17
|
-
def
|
26
|
+
def load_from_color(dimensions, color=Rubydraw::Color::Black)
|
18
27
|
@dimensions = dimensions
|
19
28
|
pixel_format = SDL.GetVideoInfo.vfmt
|
20
29
|
rmsk, gmsk, bmsk, amsk = 0xff0000, 0x00ff00, 0x0000ff, 0x000000
|
@@ -27,6 +36,8 @@ module Rubydraw
|
|
27
36
|
self
|
28
37
|
end
|
29
38
|
|
39
|
+
private :load_from_color
|
40
|
+
|
30
41
|
# Blit (copy) into +surface at +position+ (see Rubydraw::Point).
|
31
42
|
# No graphical effects are applied.
|
32
43
|
def blit(surface, position)
|
data/lib/rubydraw/text.rb
CHANGED
@@ -7,7 +7,7 @@ module Rubydraw
|
|
7
7
|
|
8
8
|
# Create a new drawable Text object with the given font and contents.
|
9
9
|
def initialize(contents, color, font_name="Times New Roman", font_size = 25)
|
10
|
-
@
|
10
|
+
@contents, @font, @font_size, @color = contents, font_name, font_size, color
|
11
11
|
if File.exist?(font_name)
|
12
12
|
font_path = font_name
|
13
13
|
else
|
@@ -24,5 +24,16 @@ module Rubydraw
|
|
24
24
|
sdl_color = @color.to_sdl
|
25
25
|
@sdl_surface = SDL::TTF.RenderText_Blended(sdl_text, @contents, sdl_color)
|
26
26
|
end
|
27
|
+
|
28
|
+
# Redefined because if @text is an empty string, it would
|
29
|
+
# return nil.
|
30
|
+
def width
|
31
|
+
super or 0
|
32
|
+
end
|
33
|
+
|
34
|
+
# See Rubydraw::Text#width
|
35
|
+
def height
|
36
|
+
super or 0
|
37
|
+
end
|
27
38
|
end
|
28
39
|
end
|
data/lib/rubydraw/window.rb
CHANGED
@@ -58,6 +58,10 @@ module Rubydraw
|
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
|
+
def size
|
62
|
+
Point[width, height]
|
63
|
+
end
|
64
|
+
|
61
65
|
# Call this method to start updating and drawing.
|
62
66
|
def show
|
63
67
|
@open = true
|
@@ -125,14 +129,20 @@ module Rubydraw
|
|
125
129
|
end
|
126
130
|
|
127
131
|
# Collect and handle new events by executing blocks in +@regestered_events+. See
|
128
|
-
#
|
132
|
+
# Object#whenever on how to use it.
|
129
133
|
def handle_events
|
130
134
|
events = @event_queue.get_events
|
131
|
-
events.each {
|
132
|
-
blocks
|
135
|
+
events.each {|event|
|
136
|
+
# +blocks+ is a hashmap; the value being the object that registered it. This
|
137
|
+
# property is only used in Object#unregister_action.
|
138
|
+
events = @registered_actions
|
139
|
+
blocks = events[event.class]
|
133
140
|
unless blocks.nil?
|
134
|
-
blocks.each {
|
135
|
-
|
141
|
+
blocks.each {|obj, b|
|
142
|
+
b.call(event) unless b.nil?
|
143
|
+
}
|
144
|
+
end
|
145
|
+
}
|
136
146
|
end
|
137
147
|
|
138
148
|
# Causes the main loop to exit as soon as it get the chance to.
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: rubydraw
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.3.1
|
5
|
+
version: 0.3.2.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- J. Wostenberg
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date:
|
13
|
+
date: 2012-01-14 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|