Module: Ray::Helper

Includes:
DSL::EventListener, DSL::EventRaiser, Matchers
Included in:
Animation, Game, Scene
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

Instance Method Summary collapse

Methods included from Matchers

key, key_mod, where

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

Parameters:

  • version (Integer) (defaults to: 110)

    GLSL version to use

Yields:

  • Yields itself if a block is given



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.

Parameters:

  • val (Symbol, Key, Integer)

    A symbol to find the key (its name), a Key object, or one of the KEY_* constant.

Returns:

  • (true, false)

    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

Parameters:

Yields:

  • (target)

    Yields itself if a block is given

Yield Parameters:



112
113
114
# File 'lib/ray/helper.rb', line 112

def image_target(img = nil, &block)
  Ray::ImageTarget.new(img, &block)
end

.mouse_posRay::Vector2

Returns The position of the mouse.

Returns:



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

.sprite(image, opts = {}) ⇒ Object

Creates a sprite.

Parameters:

  • img (String, Ray::Image)

    The image this object will use

Options Hash (opts):

  • :at (Ray::Vecor2, #to_Vector2) — default: (0,0)

    Position of the sprite

  • :rect (Ray::Rect, Array<Integer>)

    Rect of the image which will be drawn.

  • :angle (Float) — default: 0

    The angle which will be used to draw the image in degrees. Defaults to 0.

  • :zoom (Ray::Vector2) — default: 1, 1

    The zoom level which will be used to draw the image.

  • :scale (Ray::Vector2)

    Alias for :zoom

  • :color (Ray::Color) — default: Ray::Color.white

    Color used, multiplying each pixel of the sprite.

  • :flip_x (true, false) — default: false

    Set to true to flip the sprite horizontally.

  • :flip_y (true, false) — default: false

    Set to true to flip the sprite vertically.

  • :origin (Ray::Vector2) — default: (0, 0)

    The origin of transformations

  • :shader (Ray::Shader) — default: nil

    Shader



117
118
119
# File 'lib/ray/helper.rb', line 117

def sprite(image, opts = {})
  Ray::Sprite.new(image, opts)
end

.text(content, opts = {}) ⇒ Object

Parameters:

  • string (String)

    The content of the text

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :encoding (Object) — default: "utf-8"

    The encoding for the text. Unneeded in 1.9.

  • :size (Object) — default: 12

    The character size.

  • :style (Object) — default: Font::STYLE_NORMAL

    Style of the text.

  • :at (Object) — default: (0, 0)

    Position of the text.

  • :scale (Object) — default: (1, 1)

    Scale applied to the text.

  • :zoom (Object)

    Same as :scale

  • :angle (Object) — default: 0

    Rotation applied to the text.

  • :color (Object) — default: Ray::Color.white

    The color used to draw the text

  • :font (Ray::Font, String) — default: Ray::Font.default

    Font used to draw

  • :shader (Ray::Shader) — default: nil

    Shader



142
143
144
# File 'lib/ray/helper.rb', line 142

def text(content, opts = {})
  Ray::Text.new(content, opts)
end

Instance Method Details

#create_event_runnerObject

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_runnerDSL::EventRunner

Returns The event runner used to handle event.

Returns:



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.

Returns:



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