Class: Reight::SpriteEditor::Canvas

Inherits:
Object
  • Object
show all
Includes:
Hookable
Defined in:
lib/reight/app/sprite/canvas.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Hookable

#hook

Constructor Details

#initialize(app, image, path) ⇒ Canvas

Returns a new instance of Canvas.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/reight/app/sprite/canvas.rb', line 8

def initialize(app, image, path)
  hook :frame_changed
  hook :selection_changed
  hook :color_changed

  @app, @image, @path       = app, image, path
  @tool, @color, @selection = nil, [255, 255, 255, 255], nil

  @app.history.disable do
    set_frame 0, 0, 16, 16
  end
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



23
24
25
# File 'lib/reight/app/sprite/canvas.rb', line 23

def color
  @color
end

#hObject (readonly)

Returns the value of attribute h.



23
24
25
# File 'lib/reight/app/sprite/canvas.rb', line 23

def h
  @h
end

#toolObject

Returns the value of attribute tool.



21
22
23
# File 'lib/reight/app/sprite/canvas.rb', line 21

def tool
  @tool
end

#wObject (readonly)

Returns the value of attribute w.



23
24
25
# File 'lib/reight/app/sprite/canvas.rb', line 23

def w
  @w
end

#xObject (readonly)

Returns the value of attribute x.



23
24
25
# File 'lib/reight/app/sprite/canvas.rb', line 23

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



23
24
25
# File 'lib/reight/app/sprite/canvas.rb', line 23

def y
  @y
end

Instance Method Details

#apply_frame(image, x, y) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/reight/app/sprite/canvas.rb', line 135

def apply_frame(image, x, y)
  @image.begin_draw do |g|
    w, h = image.width, image.height
    g.blend image, 0, 0, w, h, x, y, w, h, REPLACE
  end
  save
end

#begin_editing(&block) ⇒ Object



113
114
115
116
117
118
# File 'lib/reight/app/sprite/canvas.rb', line 113

def begin_editing(&block)
  @before = capture_frame
  block.call if block
ensure
  end_editing if block
end

#capture_frame(frame = self.frame) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/reight/app/sprite/canvas.rb', line 126

def capture_frame(frame = self.frame)
  x, y, w, h = frame
  create_graphics(w, h).tap do |g|
    g.begin_draw do
      g.blend @image, x, y, w, h, 0, 0, w, h, REPLACE
    end
  end
end

#clear(frame, color:) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/reight/app/sprite/canvas.rb', line 105

def clear(frame, color:)
  paint do |g|
    g.fill(*color)
    g.no_stroke
    g.rect(*frame)
  end
end

#deselectObject



67
68
69
70
71
# File 'lib/reight/app/sprite/canvas.rb', line 67

def deselect()
  return if @selection == nil
  old, @selection = @selection, nil
  selection_changed! old, nil
end

#end_editingObject



120
121
122
123
124
# File 'lib/reight/app/sprite/canvas.rb', line 120

def end_editing()
  return unless @before
  @app.history.append [:capture, @before, capture_frame, x, y]
  save
end

#frameObject



42
# File 'lib/reight/app/sprite/canvas.rb', line 42

def frame = [x, y, w, h]

#heightObject



27
# File 'lib/reight/app/sprite/canvas.rb', line 27

def height = @image.height

#paint(&block) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/reight/app/sprite/canvas.rb', line 73

def paint(&block)
  @image.begin_draw do |g|
    g.clip(*(selection || frame))
    g.push do
      g.translate x, y
      g.blend_mode REPLACE
      block.call g
    end
  end
end

#pixel_at(x, y) ⇒ Object



98
99
100
101
102
103
# File 'lib/reight/app/sprite/canvas.rb', line 98

def pixel_at(x, y)
  img = sub_image self.x + x, self.y + y, 1, 1
  img.load_pixels
  c = img.pixels[0]
  [red(c), green(c), blue(c), alpha(c)].map &:to_i
end

#saveObject



29
30
31
32
# File 'lib/reight/app/sprite/canvas.rb', line 29

def save()
  @image.save @path
  @app.project.save
end

#select(x, y, w, h) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/reight/app/sprite/canvas.rb', line 50

def select(x, y, w, h)
  old        = @selection
  new        = correct_bounds x, y, w, h
  return if new == old
  @selection = new
  selection_changed! old, new
end

#selectionObject



58
59
60
61
62
63
64
65
# File 'lib/reight/app/sprite/canvas.rb', line 58

def selection()
  xrange, yrange = x..(x + w), y..(y + h)
  sx, sy, sw, sh = @selection
  return nil unless
    xrange.include?(sx) && xrange.include?(sx + sw) &&
    yrange.include?(sy) && yrange.include?(sy + sh)
  @selection
end

#set_frame(x, y, w, h) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/reight/app/sprite/canvas.rb', line 34

def set_frame(x, y, w, h)
  old            = [@x, @y, @w, @h]
  new            = correct_bounds x, y, w, h
  return if new == old
  @x, @y, @w, @h = new
  frame_changed! old, new
end

#spriteObject



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/reight/app/sprite/canvas.rb', line 143

def sprite()
  @sprite ||= RubySketch::Sprite.new.tap do |sp|
    pos = -> {to_image sp.mouse_x, sp.mouse_y}
    sp.draw           {draw}
    sp.mouse_pressed  {tool&.canvas_pressed( *pos.call, sp.mouse_button)}
    sp.mouse_released {tool&.canvas_released(*pos.call, sp.mouse_button)}
    sp.mouse_moved    {tool&.canvas_moved(   *pos.call)}
    sp.mouse_dragged  {tool&.canvas_dragged( *pos.call, sp.mouse_button)}
    sp.mouse_clicked  {tool&.canvas_clicked( *pos.call, sp.mouse_button)}
  end
end

#sub_image(x, y, w, h) ⇒ Object



92
93
94
95
96
# File 'lib/reight/app/sprite/canvas.rb', line 92

def sub_image(x, y, w, h)
  create_graphics(w, h).tap do |g|
    g.begin_draw {g.blend @image, x, y, w, h, 0, 0, w, h, REPLACE}
  end
end

#update_pixels(&block) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/reight/app/sprite/canvas.rb', line 84

def update_pixels(&block)
  tmp = sub_image x, y, w, h
  tmp.update_pixels {|pixels| block.call pixels}
  @image.begin_draw do |g|
    g.blend tmp, 0, 0, w, h, x, y, w, h, REPLACE
  end
end

#widthObject



25
# File 'lib/reight/app/sprite/canvas.rb', line 25

def width  = @image.width