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
# 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

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

  ext_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)


35
36
37
# File 'lib/ruby2d/image.rb', line 35

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

#ext_init(path) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
# File 'ext/ruby2d/ruby2d-opal.rb', line 140

def ext_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

#ext_renderObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'ext/ruby2d/ruby2d-opal.rb', line 153

def ext_render
  `
  #{self}.data.x = #{self}.x;
  #{self}.data.y = #{self}.y;

  if (#{self}.width  != Opal.nil) #{self}.data.width  = #{self}.width;
  if (#{self}.height != Opal.nil) #{self}.data.height = #{self}.height;

  #{self}.data.color.r = #{self}.color.r;
  #{self}.data.color.g = #{self}.color.g;
  #{self}.data.color.b = #{self}.color.b;
  #{self}.data.color.a = #{self}.color.a;

  S2D.DrawImage(#{self}.data);
  `
end