Class: Ruby2D::Image

Inherits:
Object
  • Object
show all
Includes:
Renderable
Defined in:
lib/ruby2d/image.rb,
ext/ruby2d/ruby2d-opal.rb

Instance Attribute Summary collapse

Attributes included from Renderable

#z

Instance Method Summary collapse

Methods included from Renderable

#add, #opacity, #opacity=, #remove

Constructor Details

#initialize(opts = {}) ⇒ Image

Returns a new instance of Image.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ruby2d/image.rb', line 10

def initialize(opts = {})
  @path = opts[:path]

  unless RUBY_ENGINE == 'opal'
    unless File.exists? @path
      raise Error, "Cannot find image file `#{@path}`"
    end
  end

  @x = opts[:x] || 0
  @y = opts[:y] || 0
  @z = opts[:z] || 0
  @width  = opts[:width]  || nil
  @height = opts[:height] || nil

  @type_id = 4

  self.color = opts[:color] || 'white'

  ext_image_init(@path)
  add
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



8
9
10
# File 'lib/ruby2d/image.rb', line 8

def color
  @color
end

#dataObject

Returns the value of attribute data.



7
8
9
# File 'lib/ruby2d/image.rb', line 7

def data
  @data
end

#heightObject

Returns the value of attribute height.



7
8
9
# File 'lib/ruby2d/image.rb', line 7

def height
  @height
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/ruby2d/image.rb', line 8

def path
  @path
end

#widthObject

Returns the value of attribute width.



7
8
9
# File 'lib/ruby2d/image.rb', line 7

def width
  @width
end

#xObject

Returns the value of attribute x.



7
8
9
# File 'lib/ruby2d/image.rb', line 7

def x
  @x
end

#yObject

Returns the value of attribute y.



7
8
9
# File 'lib/ruby2d/image.rb', line 7

def y
  @y
end

Instance Method Details

#contains?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/ruby2d/image.rb', line 37

def contains?(x, y)
  @x < x and @x + @width > x and @y < y and @y + @height > y
end

#ext_image_init(path) ⇒ Object



189
190
191
192
193
194
195
196
197
198
# File 'ext/ruby2d/ruby2d-opal.rb', line 189

def ext_image_init(path)
  `#{self}.data = S2D.CreateImage(path, function() {
    if (#{@width} == Opal.nil) {
      #{@width} = #{self}.data.width;
    }
    if (#{@height} == Opal.nil) {
      #{@height} = #{self}.data.height;
    }
  });`
end