Class: Reight::SpriteEditor

Inherits:
App
  • Object
show all
Defined in:
lib/reight/app/sprite/editor.rb

Defined Under Namespace

Classes: Brush, Canvas, Color, Fill, Line, Select, Shape, Tool

Constant Summary

Constants inherited from App

App::BUTTON_SIZE, App::CHIPS_WIDTH, App::INDEX_SIZE, App::NAVIGATOR_HEIGHT, App::PALETTE_COLORS, App::SCREEN_HEIGHT, App::SCREEN_WIDTH, App::SPACE

Instance Attribute Summary

Attributes inherited from App

#project

Instance Method Summary collapse

Methods inherited from App

#activated, #active?, #deactivated, #double_clicked, #flash, #group, #history, #icon, #initialize, #inspect, #key_released, #key_typed, #label, #mouse_clicked, #mouse_dragged, #mouse_moved, #mouse_pressed, #mouse_released, #mouse_wheel, #pressing?, #touch_ended, #touch_moved, #touch_started, #window_moved

Constructor Details

This class inherits a constructor from Reight::App

Instance Method Details

#can_copy?Boolean

Returns:

  • (Boolean)


169
# File 'lib/reight/app/sprite/editor.rb', line 169

def can_copy?  = true

#can_cut?Boolean

Returns:

  • (Boolean)


168
# File 'lib/reight/app/sprite/editor.rb', line 168

def can_cut?   = true

#can_paste?Boolean

Returns:

  • (Boolean)


170
# File 'lib/reight/app/sprite/editor.rb', line 170

def can_paste? = @copy

#canvasObject



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

def canvas()
  @canvas ||= Canvas.new(
    self,
    project.chips_image,
    project.chips_image_path
  ).tap do |canvas|
    canvas.frame_changed do |old, new|
      history.append [:frame, old, new]
    end
    canvas.selection_changed do |old, new|
      history.append [new ? :select : :deselect, old, new]
    end
  end
end

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



172
173
174
# File 'lib/reight/app/sprite/editor.rb', line 172

def clear_canvas(x, y, w, h)
  canvas.clear [x, y, w, h], color: colors.first.color
end

#copy(flash: true) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/reight/app/sprite/editor.rb', line 145

def copy(flash: true)
  sel   = canvas.selection || canvas.frame
  image = canvas.capture_frame(sel) || return
  x, y, = sel
  @copy = [image, x - canvas.x, y - canvas.y]
  self.flash 'Copy!' if flash
end

#cut(flash: true) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/reight/app/sprite/editor.rb', line 136

def cut(flash: true)
  copy flash: false
  image, x, y = @copy || return
  canvas.begin_editing do
    clear_canvas x, y, image.width, image.height
  end
  self.flash 'Cut!' if flash
end

#drawObject



31
32
33
34
35
# File 'lib/reight/app/sprite/editor.rb', line 31

def draw()
  background 200
  sprite(*sprites)
  super
end

#key_pressedObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/reight/app/sprite/editor.rb', line 37

def key_pressed()
  super
  shift, ctrl, cmd = [SHIFT, CONTROL, COMMAND].map {pressing? _1}
  ch               = chips
  case key_code
  when LEFT  then ch.set_frame ch.x - ch.size, ch.y, ch.size, ch.size
  when RIGHT then ch.set_frame ch.x + ch.size, ch.y, ch.size, ch.size
  when UP    then ch.set_frame ch.x, ch.y - ch.size, ch.size, ch.size
  when DOWN  then ch.set_frame ch.x, ch.y + ch.size, ch.size, ch.size
  when :c    then copy  if ctrl || cmd
  when :x    then cut   if ctrl || cmd
  when :v    then paste if ctrl || cmd
  when :z    then shift ? self.redo : undo if ctrl || cmd
  when :s    then select.click
  when :b    then  brush.click
  when :l    then   line.click
  when :f    then   fill.click
  when :r    then (shift ? fill_rect    : stroke_rect   ).click
  when :e    then (shift ? fill_ellipse : stroke_ellipse).click
  end
end

#paste(flash: true) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/reight/app/sprite/editor.rb', line 153

def paste(flash: true)
  image, x, y = @copy || return
  w, h        = image.width, image.height
  history.group do
    canvas.deselect
    canvas.begin_editing do
      canvas.paint do |g|
        g.blend image, 0, 0, w, h, x, y, w, h, REPLACE
      end
    end
    canvas.select canvas.x + x, canvas.y + y, w, h
  end
  self.flash 'Paste!' if flash
end

#redo(flash: true) ⇒ Object



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

def redo(flash: true)
  history.redo do |action|
    case action
    in [:frame, _, [x, y, w, h]]  then chips.set_frame x, y, w, h
    in [:capture, _, after, x, y] then canvas.apply_frame after, x, y
    in [  :select, _, sel]        then canvas.select(*sel)
    in [:deselect, _]             then canvas.deselect
    end
    self.flash 'Redo!' if flash
  end
end

#setupObject



21
22
23
24
25
26
27
28
29
# File 'lib/reight/app/sprite/editor.rb', line 21

def setup()
  super
  history.disable do
    colors[7].click
    tools[1].click
    chip_sizes[0].click
    brush_sizes[0].click
  end
end

#undo(flash: true) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/reight/app/sprite/editor.rb', line 112

def undo(flash: true)
  history.undo do |action|
    case action
    in [:frame, [x, y, w, h], _]   then chips.set_frame x, y, w, h
    in [:capture, before, _, x, y] then canvas.apply_frame before, x, y
    in [  :select, sel, _]         then sel ? canvas.select(*sel) : canvas.deselect
    in [:deselect, sel]            then       canvas.select(*sel)
    end
    self.flash 'Undo!' if flash
  end
end

#window_resizedObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/reight/app/sprite/editor.rb', line 59

def window_resized()
  super
  [chip_sizes, tools, colors, brush_sizes].flatten.map(&:sprite)
    .each {|sp| sp.w = sp.h = BUTTON_SIZE}

  chips_index.sprite.tap do |sp|
    sp.w, sp.h = INDEX_SIZE, BUTTON_SIZE
    sp.x       = SPACE
    sp.y       = NAVIGATOR_HEIGHT + SPACE
  end
  chip_sizes.reverse.map {_1.sprite}.each.with_index do |sp, index|
    sp.x = SPACE + CHIPS_WIDTH - (sp.w + (sp.w + 1) * index)
    sp.y = chips_index.sprite.y
  end
  chips.sprite.tap do |sp|
    sp.x      = SPACE
    sp.y      = chip_sizes.last.sprite.bottom + SPACE
    sp.right  = chip_sizes.last.sprite.right
    sp.bottom = height - SPACE
  end
  colors.map {_1.sprite}.each.with_index do |sp, index|
    sp.h *= 0.8
    sp.x  = chips.sprite.right + SPACE + sp.w * (index % 8)
    sp.y  = height - (SPACE + sp.h * (4 - index / 8))
  end
  tools.map {_1.sprite}.each.with_index do |sp, index|
    sp.x = colors.first.sprite.x + (sp.w + 1) * index
    sp.y = colors.first.sprite.y - (SPACE / 2 + sp.h)
  end
  canvas.sprite.tap do |sp|
    sp.x      = chip_sizes.last.sprite.right + SPACE
    sp.y      = chip_sizes.last.sprite.y
    sp.bottom = tools.first.sprite.top - SPACE / 2
    sp.w      = sp.h
  end
  brush_sizes.map {_1.sprite}.each.with_index do |sp, index|
    sp.x = canvas.sprite.right + SPACE + (sp.w + 1) * index
    sp.y = canvas.sprite.y
  end
  shapes.map {_1.sprite}.each.with_index do |sp, index|
    sp.w = 30
    sp.h = BUTTON_SIZE
    sp.x = brush_sizes.first.sprite.x + (sp.w + 1) * index
    sp.y = brush_sizes.last.sprite.bottom + SPACE
  end
  types.map {_1.sprite}.each.with_index do |sp, index|
    sp.w = 50
    sp.h = BUTTON_SIZE
    sp.x = shapes.first.sprite.x + (sp.w + 1) * index
    sp.y = shapes.last.sprite.bottom + SPACE
  end
end