Class: Rabbit::Graffiti::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/rabbit/graffiti/processor.rb

Constant Summary collapse

DEFAULT_COLOR =
Renderer::Color.parse("black")
DEFAULT_LINE_WIDTH =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_config = {}) ⇒ Processor

Returns a new instance of Processor.



13
14
15
16
17
18
# 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

Instance Attribute Details

#colorObject

Returns the value of attribute color.



12
13
14
# File 'lib/rabbit/graffiti/processor.rb', line 12

def color
  @color
end

#line_widthObject

Returns the value of attribute line_width.



12
13
14
# File 'lib/rabbit/graffiti/processor.rb', line 12

def line_width
  @line_width
end

Instance Method Details

#button_motion(x, y, width, height) ⇒ Object



39
40
41
42
43
# 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) ⇒ Object



28
29
30
31
32
# 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) ⇒ Object



34
35
36
37
# File 'lib/rabbit/graffiti/processor.rb', line 34

def button_release(x, y, width, height)
  @pressed = false
  @undo_stack << [:push]
end

#can_undo?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/rabbit/graffiti/processor.rb', line 24

def can_undo?
  not @undo_stack.empty?
end

#change_color(&block) ⇒ Object



83
84
85
86
87
88
89
90
# 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

#clearObject



76
77
78
79
80
81
# File 'lib/rabbit/graffiti/processor.rb', line 76

def clear
  @pressed = false
  @segments = []
  @undo_stack = []
  @undo_index = nil
end

#clear_configObject



92
93
94
95
# File 'lib/rabbit/graffiti/processor.rb', line 92

def clear_config
  @color = @default_color
  @line_width = @default_line_width
end

#dragging?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/rabbit/graffiti/processor.rb', line 72

def dragging?
  @pressed
end

#draw_all_segment(renderer) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# 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) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# 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?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/rabbit/graffiti/processor.rb', line 20

def have_graffiti?
  not @segments.empty?
end

#undoObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# 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