class Rabbit::Graffiti::Processor

Constants

DEFAULT_COLOR
DEFAULT_LINE_WIDTH

Attributes

color[RW]
line_width[RW]

Public Class Methods

new(default_config={}) click to toggle source
# File lib/rabbit/graffiti/processor.rb, line 13
def initialize(default_config={})
  @default_color = default_config["color"] || DEFAULT_COLOR
  @default_line_width = default_config["line_width"] || DEFAULT_LINE_WIDTH
  clear
  clear_config
end

Public Instance Methods

button_motion(x, y, width, height) click to toggle source
# File lib/rabbit/graffiti/processor.rb, line 39
def button_motion(x, y, width, height)
  if @pressed
    @segments.last << [x.to_f / width, y.to_f / height]
  end
end
button_press(x, y, width, height) click to toggle source
# File lib/rabbit/graffiti/processor.rb, line 28
def button_press(x, y, width, height)
  @pressed = true
  @undo_index = nil
  @segments << [[x.to_f / width, y.to_f / height]]
end
button_release(x, y, width, height) click to toggle source
# File lib/rabbit/graffiti/processor.rb, line 34
def button_release(x, y, width, height)
  @pressed = false
  @undo_stack << [:push]
end
can_undo?() click to toggle source
# File lib/rabbit/graffiti/processor.rb, line 24
def can_undo?
  not @undo_stack.empty?
end
change_color(&block) click to toggle source
# File lib/rabbit/graffiti/processor.rb, line 83
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
# File lib/rabbit/graffiti/processor.rb, line 76
def clear
  @pressed = false
  @segments = []
  @undo_stack = []
  @undo_index = nil
end
clear_config() click to toggle source
# File lib/rabbit/graffiti/processor.rb, line 92
def clear_config
  @color = @default_color
  @line_width = @default_line_width
end
dragging?() click to toggle source
# File lib/rabbit/graffiti/processor.rb, line 72
def dragging?
  @pressed
end
draw_all_segment(renderer) click to toggle source
# File lib/rabbit/graffiti/processor.rb, line 59
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
# File lib/rabbit/graffiti/processor.rb, line 45
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
# File lib/rabbit/graffiti/processor.rb, line 20
def have_graffiti?
  not @segments.empty?
end
undo() click to toggle source
# File lib/rabbit/graffiti/processor.rb, line 97
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