can_undo?()
click to toggle source
def can_undo?
not @undo_stack.empty?
end
change_color(&block)
click to toggle source
def change_color(&block)
dialog = Graffiti::ConfigDialog.new(@color, @line_width)
dialog.run do |color, line_width|
@color = color if color
@line_width = line_width if line_width
block.call
end
end
clear()
click to toggle source
def clear
@pressed = false
@segments = []
@undo_stack = []
@undo_index = nil
end
clear_config()
click to toggle source
def clear_config
@color = @default_color
@line_width = @default_line_width
end
dragging?()
click to toggle source
def dragging?
@pressed
end
draw_all_segment(renderer)
click to toggle source
def draw_all_segment(renderer)
return if @segments.empty?
args = [@color, {:line_width => @line_width, :opened => true}]
width = renderer.width
height = renderer.height
@segments.each do |points|
converted_points = points.collect do |x, y|
[x * width, y * height]
end
renderer.draw_lines(converted_points, *args)
end
end
draw_last_segment(renderer)
click to toggle source
def draw_last_segment(renderer)
points = @segments.last
if points.size >= 2
width = renderer.width
height = renderer.height
prev, current = points[-2..-1]
prev_x, prev_y = prev
x, y = current
renderer.draw_line(prev_x * width, prev_y * height,
x * width, y * height,
@color, {:line_width => @line_width})
end
end
have_graffiti?()
click to toggle source
def have_graffiti?
not @segments.empty?
end
undo()
click to toggle source
def undo
@undo_index ||= @undo_stack.size - 1
command, segment = @undo_stack[@undo_index]
case command
when :push
@undo_stack << [:pop, @segments.pop]
when :pop
@segments << segment
@undo_stack << [:push]
end
if @undo_index > 0
@undo_index -= 1
else
@undo_index = nil
end
end