Module: Ray::Helper
- Includes:
- DSL::EventListener, DSL::EventRaiser, Matchers
- Defined in:
- lib/ray/helper.rb,
lib/ray/animation/float_variation.rb,
lib/ray/animation/vector_variation.rb
Overview
Event handling
This module is used to allow both raising events and handling thereof. You can raise them simply with raise_event:
raise_event(:event_name, arugments)
(You can add as many extra arguments as you want/need to)
Those events can trigger a block.
on :event_name do
# Do something useful
end
If you want your block to be triggered only when a condition is met, you can pass extra arguments to on. They will be compared to those that were passed to raise_event using === or == if
returned false.
on :event_name, /foo/ do |str| puts str end
raise_event "foobar" # Will trigger the above
Handlers can be grouped:
event_group :name do
# Register for some events
end
Notice event groups cannot be nested. The following code:
event_group :name do
event_group :foo do
# ...
end
# ...
end
Will create two totally unrelated event groups (:name and :foo).
An event group is enabled by default, but it can be disabled:
disable_event_group :name
And re-enabled afterwards:
enable_event_group :name
It is also possible to remove the handlers that belong to a group when they’re not needed anymore:
remove_event_group :name
Shared resources
This module contains helper methods that provide access to shared resources, i.e. they will always return the same object until the cache is cleared:
obj = image "test.png"
other_obj = image "test.png"
obj.equal? other_obj # => true
Notice different resources sets are used. This allows to get resources from other places than from the disk. For instance, Ray provides a way to get resources from the network using open-uri:
obj = image "http://www.example.com/some_image.png" # Download, takes some time
other_image = image "http://www.example.com/some_image.png" # Doesn't download
obj.equal? other_image
You can define your own resource sets pretty easily:
Ray.image_set(/some (regexp)/) do |capture| # Captures are yielded
# Block that returns an image
end
Be careful when using a shared resource. Call #dup instead of mutating it (even though that would not raise an error).
Class Method Summary collapse
- .effect_generator(version = 110) { ... } ⇒ Object
- .font(name) ⇒ Object
-
.holding?(val) ⇒ true, false
True if the user is holding key.
- .image(name) ⇒ Object
- .image_target(img = nil) {|target| ... } ⇒ Object
-
.mouse_pos ⇒ Ray::Vector2
The position of the mouse.
- .music(file) ⇒ Object
- .sound(file) ⇒ Object
- .sound_buffer(file) ⇒ Object
-
.sprite(image, opts = {}) ⇒ Object
Creates a sprite.
- .text(content, opts = {}) ⇒ Object
Instance Method Summary collapse
-
#create_event_runner ⇒ Object
Creates an event runner for this object.
-
#disable_event_group(name) ⇒ Object
Disables an event group.
-
#enable_event_group(name) ⇒ Object
Enables an event group.
-
#event_runner ⇒ DSL::EventRunner
The event runner used to handle event.
-
#event_runner=(value) ⇒ Object
Sets the event runner for this object.
-
#remove_event_group(name) ⇒ Object
Removes all the handlers belonging to an event group.
-
#rotation(opts) ⇒ Ray::Animation::FloatVariation
Same as #float_variation, but attribute is set to angle.
-
#scale_variation(opts) ⇒ Object
Same as #vector_variation, but :attribute is set to :scale.
-
#translation(opts) ⇒ Object
Same as #vector_variation, but :attribute is set to :pos.
Methods included from Matchers
Methods included from DSL::EventListener
#add_hook, #current_event_group, #current_event_group=, #event_group, #listener_runner, #listener_runner=, #on
Methods included from DSL::EventRaiser
#raise_event, #raiser_runner, #raiser_runner=
Class Method Details
.effect_generator(version = 110) { ... } ⇒ Object
147 148 149 |
# File 'lib/ray/helper.rb', line 147 def effect_generator(version = 110, &block) Ray::Effect::Generator.new(version, &block) end |
.font(name) ⇒ Object
137 138 139 |
# File 'lib/ray/helper.rb', line 137 def font(name) Ray::FontSet[name] end |
.holding?(val) ⇒ true, false
Returns True if the user is holding key.
154 155 156 157 158 159 160 161 162 |
# File 'lib/ray/helper.rb', line 154 def holding?(val) if val.is_a? Integer window.input.holding? val else key(val.to_sym).to_a.any? do |key| window.input.holding? key end end end |
.image(name) ⇒ Object
107 108 109 |
# File 'lib/ray/helper.rb', line 107 def image(name) Ray::ImageSet[name] end |
.image_target(img = nil) {|target| ... } ⇒ Object
112 113 114 |
# File 'lib/ray/helper.rb', line 112 def image_target(img = nil, &block) Ray::ImageTarget.new(img, &block) end |
.mouse_pos ⇒ Ray::Vector2
Returns The position of the mouse.
165 166 167 |
# File 'lib/ray/helper.rb', line 165 def mouse_pos window.input.mouse_pos end |
.music(file) ⇒ Object
132 133 134 |
# File 'lib/ray/helper.rb', line 132 def music(file) Ray::Music.new(file) end |
.sound(file) ⇒ Object
127 128 129 |
# File 'lib/ray/helper.rb', line 127 def sound(file) Ray::Sound.new file end |
.sound_buffer(file) ⇒ Object
122 123 124 |
# File 'lib/ray/helper.rb', line 122 def sound_buffer(file) Ray::SoundBufferSet[file] end |
Instance Method Details
#create_event_runner ⇒ Object
Creates an event runner for this object
85 86 87 |
# File 'lib/ray/helper.rb', line 85 def create_event_runner self.event_runner = Ray::DSL::EventRunner.new end |
#disable_event_group(name) ⇒ Object
Disables an event group
95 96 97 |
# File 'lib/ray/helper.rb', line 95 def disable_event_group(name) event_runner.disable_group(name) end |
#enable_event_group(name) ⇒ Object
Enables an event group
90 91 92 |
# File 'lib/ray/helper.rb', line 90 def enable_event_group(name) event_runner.enable_group(name) end |
#event_runner ⇒ DSL::EventRunner
Returns The event runner used to handle event.
80 81 82 |
# File 'lib/ray/helper.rb', line 80 def event_runner listener_runner end |
#event_runner=(value) ⇒ Object
Sets the event runner for this object.
74 75 76 77 |
# File 'lib/ray/helper.rb', line 74 def event_runner=(value) self.listener_runner = value self.raiser_runner = value end |
#remove_event_group(name) ⇒ Object
Removes all the handlers belonging to an event group
100 101 102 |
# File 'lib/ray/helper.rb', line 100 def remove_event_group(name) event_runner.remove_group(name) end |
#rotation(opts) ⇒ Ray::Animation::FloatVariation
Returns Same as #float_variation, but attribute is set to angle.
72 73 74 |
# File 'lib/ray/animation/float_variation.rb', line 72 def rotation(opts) float_variation(opts.merge(:attribute => :angle)) end |
#scale_variation(opts) ⇒ Object
Same as #vector_variation, but :attribute is set to :scale.
107 108 109 |
# File 'lib/ray/animation/vector_variation.rb', line 107 def scale_variation(opts) vector_variation(opts.merge(:attribute => :scale)) end |
#translation(opts) ⇒ Object
Same as #vector_variation, but :attribute is set to :pos.
102 103 104 |
# File 'lib/ray/animation/vector_variation.rb', line 102 def translation(opts) vector_variation(opts.merge(:attribute => :pos)) end |