Class: DXRubyRP5::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/dxruby_rp5/image.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, _color = [0, 0, 0, 0]) ⇒ Image

Returns a new instance of Image.



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dxruby_rp5/image.rb', line 32

def initialize(width, height, _color = [0, 0, 0, 0])
  _color = to_rp5_color(_color)
  @color = $app.color(*_color)

  if width == 0 && height == 0
    return
  end

  @_surface = $app.create_image(width, height, Processing::App::ARGB)
  @_surface.pixels.each_with_index do |px, i|
    @_surface.pixels[i] = @color
  end
end

Instance Attribute Details

#_surfaceObject (readonly)

Returns the value of attribute _surface.



4
5
6
# File 'lib/dxruby_rp5/image.rb', line 4

def _surface
  @_surface
end

Class Method Details

.load(filename, x = nil, y = nil, width = nil, height = nil) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/dxruby_rp5/image.rb', line 6

def self.load(filename, x = nil, y = nil, width = nil, height = nil)
  image = self.new(0, 0)
  surface = $app.load_image(filename)
  surface.format = Processing::App::ARGB
  image.instance_variable_set('@_surface', surface)
  return image
end

.load_tiles(filename, xcount, ycount, switch = true) ⇒ Object Also known as: loadTiles, load_to_array



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dxruby_rp5/image.rb', line 14

def self.load_tiles(filename, xcount, ycount, switch = true)
  surface = $app.load_image(filename)
  width = surface.width / xcount
  height = surface.height / ycount
  images = []
  ycount.times do |y|
    xcount.times do |x|
      image = new(0, 0)
      s = $app.create_image(width, height, Processing::App::ARGB)
      s.copy(surface, x * width, y * height, width, height,
                              0,          0, width, height)
      image.instance_variable_set('@_surface', s)
      images << image
    end
  end
  return images
end

Instance Method Details

#[](x, y) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/dxruby_rp5/image.rb', line 54

def [](x, y)
  px = @_surface.get(x, y)
  _color = [red(px).to_i, green(px).to_i, blue(px).to_i]
  if @_surface.format == Processing::App::ARGB
    _color.unshift(alpha(px).to_i)
  end
  return _color
end

#[]=(x, y, _color) ⇒ Object



63
64
65
66
# File 'lib/dxruby_rp5/image.rb', line 63

def []=(x, y, _color)
  _color = to_rp5_color(_color)
  return @_surface.set(x, y, $app.color(*_color))
end

#box(x1, y1, x2, y2, _color) ⇒ Object



97
98
99
# File 'lib/dxruby_rp5/image.rb', line 97

def box(x1, y1, x2, y2, _color)
  rect(x1, y1, x2, y2, _color)
end

#box_fill(x1, y1, x2, y2, _color) ⇒ Object Also known as: boxFill



101
102
103
# File 'lib/dxruby_rp5/image.rb', line 101

def box_fill(x1, y1, x2, y2, _color)
  rect(x1, y1, x2, y2, _color, true)
end

#circle(x, y, r, _color) ⇒ Object



105
106
107
# File 'lib/dxruby_rp5/image.rb', line 105

def circle(x, y, r, _color)
  ellipse(x, y, 2*r, 2*r, _color)
end

#circle_fill(x, y, r, _color) ⇒ Object Also known as: circleFill



109
110
111
# File 'lib/dxruby_rp5/image.rb', line 109

def circle_fill(x, y, r, _color)
  ellipse(x, y, 2*r, 2*r, _color, true)
end

#compare(x, y, _color) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/dxruby_rp5/image.rb', line 81

def compare(x, y, _color)
  _color = to_rp5_color(_color)
  max = _color.size - 1
  px = @_surface.get(x, y)
  c = [red(px),  green(px),
       blue(px), alpha(px)].map(&:to_i)
  return c[0..max] == _color
end

#heightObject



50
51
52
# File 'lib/dxruby_rp5/image.rb', line 50

def height
  return @_surface.height
end

#line(x1, y1, x2, y2, _color) ⇒ Object



90
91
92
93
94
95
# File 'lib/dxruby_rp5/image.rb', line 90

def line(x1, y1, x2, y2, _color)
  _color = to_rp5_color(_color)
  draw_on_image(_color, false) do |pg|
    pg.line(x1, y1, x2, y2)
  end
end

#set_color_key(_color) ⇒ Object Also known as: setColorKey



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dxruby_rp5/image.rb', line 68

def set_color_key(_color)
  r, g, b = *_color[-3..-1]
  alpha = $app.color(r, g, b, 0)

  @_surface.pixels.each_with_index do |px, i|
    if (r == red(px).to_i) &&
        (g == green(px).to_i) &&
        (b == blue(px).to_i)
      @_surface.pixels[i] = alpha
    end
  end
end

#slice(x, y, width, height) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/dxruby_rp5/image.rb', line 121

def slice(x, y, width, height)
  image = self.class.new(0, 0)
  s = $app.create_image(width, height, Processing::App::ARGB)
  s.copy(@_surface, x, y, width, height,
                    0, 0, width, height)
  image.instance_variable_set('@_surface', s)
  return image
end

#slice_tiles(xcount, ycount, switch = true) ⇒ Object Also known as: sliceTiles, slice_to_array



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/dxruby_rp5/image.rb', line 130

def slice_tiles(xcount, ycount, switch = true)
  width = @_surface.width / xcount
  height = @_surface.height / ycount
  images = []
  ycount.times do |y|
    xcount.times do |x|
      images << slice(x * width, y * height, width, height)
    end
  end
  return images
end

#triangle(x1, y1, x2, y2, x3, y3, _color) ⇒ Object



113
114
115
# File 'lib/dxruby_rp5/image.rb', line 113

def triangle(x1, y1, x2, y2, x3, y3, _color)
  _triangle(x1, y1, x2, y2, x3, y3, _color)
end

#triangle_fill(x1, y1, x2, y2, x3, y3, _color) ⇒ Object Also known as: triangleFill



117
118
119
# File 'lib/dxruby_rp5/image.rb', line 117

def triangle_fill(x1, y1, x2, y2, x3, y3, _color)
  _triangle(x1, y1, x2, y2, x3, y3, _color, true)
end

#widthObject



46
47
48
# File 'lib/dxruby_rp5/image.rb', line 46

def width
  return @_surface.width
end