summaryrefslogtreecommitdiffhomepage
path: root/lib/ruby2d/image.rb
blob: 5490d5339f9e47158afc7ae53c7fa86c3a66ad2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# image.rb

module Ruby2D
  class Image
    include Renderable

    attr_reader :path, :color
    attr_accessor :x, :y, :width, :height, :rotate, :data

    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
      @rotate = 0

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

      ext_init(@path)
      add
    end

    def color=(c)
      @color = Color.new(c)
    end

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