Class: Reight::SpriteEditor::Fill

Inherits:
Tool show all
Defined in:
lib/reight/app/sprite/fill.rb

Instance Attribute Summary

Attributes inherited from Tool

#app

Attributes inherited from Button

#icon, #label, #name

Instance Method Summary collapse

Methods inherited from Tool

#canvas, #canvas_dragged, #canvas_moved, #canvas_released, #history, #pick_color

Methods inherited from Button

#click, #disabled?, #disabled_icon, #draw, #enabled?, #hover, #pressed, #pressing?, #released, #sprite

Methods included from HasHelp

#help, #name, #set_help

Methods included from Hookable

#hook

Methods included from Activatable

#activated, #activated!, #active=, #active?

Constructor Details

#initialize(app, &block) ⇒ Fill

Returns a new instance of Fill.



6
7
8
9
# File 'lib/reight/app/sprite/fill.rb', line 6

def initialize(app, &block)
  super app, icon: app.icon(2, 2, 8), &block
  set_help left: 'Fill', right: 'Pick Color'
end

Instance Method Details

#canvas_clicked(x, y, button) ⇒ Object



41
42
43
# File 'lib/reight/app/sprite/fill.rb', line 41

def canvas_clicked(x, y, button)
  pick_color x, y if button == RIGHT
end

#canvas_pressed(x, y, button) ⇒ Object



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
# File 'lib/reight/app/sprite/fill.rb', line 11

def canvas_pressed(x, y, button)
  return unless button == LEFT
  x, y           = [x, y].map &:to_i
  fx, fy, fw,    = canvas.frame
  sx, sy, sw, sh = canvas.selection || canvas.frame
  sx -= fx
  sy -= fy
  return unless (sx...(sx + sw)).include?(x) && (sy...(sy + sh)).include?(y)
  canvas.begin_editing
  count = 0
  canvas.update_pixels do |pixels|
    from = pixels[y * fw + x]
    to   = color(*canvas.color)
    rest = [[x, y]]
    until rest.empty?
      xx, yy = rest.shift
      next if pixels[yy * fw + xx] == to
      pixels[yy * fw + xx] = to
      count += 1
      _x, x_ = xx - 1, xx + 1
      _y, y_ = yy - 1, yy + 1
      rest << [_x, yy] if _x >= sx      && pixels[yy * fw + _x] == from
      rest << [x_, yy] if x_ <  sx + sw && pixels[yy * fw + x_] == from
      rest << [xx, _y] if _y >= sy      && pixels[_y * fw + xx] == from
      rest << [xx, y_] if y_ <  sy + sh && pixels[y_ * fw + xx] == from
    end
  end
  canvas.end_editing if count > 0
end