Class: Canvas

Inherits:
Gtk::DrawingArea
  • Object
show all
Defined in:
lib/rtl/canvas.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCanvas

Returns a new instance of Canvas.



4
5
6
7
8
9
10
11
12
# File 'lib/rtl/canvas.rb', line 4

def initialize
  super()

  @running=false
  set_size_request(800,100)
  signal_connect('draw') do
    redraw @graph
  end
end

Instance Attribute Details

#runningObject

Returns the value of attribute running.



3
4
5
# File 'lib/rtl/canvas.rb', line 3

def running
  @running
end

Instance Method Details

#clear(cr) ⇒ Object



14
15
16
17
# File 'lib/rtl/canvas.rb', line 14

def clear cr
  cr.set_source_rgb(0.1, 0.1, 0.1)
  cr.paint
end

#redraw(graph = nil, zoom_factor = 1, shift = Vector.new(0,0)) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rtl/canvas.rb', line 19

def redraw graph=nil,zoom_factor=1,shift=Vector.new(0,0)
  @graph=graph
  cr = window.create_cairo_context
  cr.set_line_width(0.8)

  w = allocation.width
  h = allocation.height

  cr.translate(w/2, h/2)

  clear cr

  if graph
    cr.set_source_rgb(0.4, 0.4, 0.4)
    @graph.edges.each do |edge|
      n1,n2=edge.source,edge.sink
      cr.move_to(shift.x + n1.x*zoom_factor,shift.y + n1.y*zoom_factor)
      cr.line_to(shift.x + n2.x*zoom_factor,shift.y + n2.y*zoom_factor)
      cr.stroke
    end

    cr.set_source_rgb(0.9, 0.5, 0.2)
    @graph.nodes.each do |node|
      cr.arc(shift.x+node.x*zoom_factor, shift.y+node.y*zoom_factor, 10*zoom_factor, 0, 2.0 * Math::PI)
      cr.fill_preserve()
      cr.stroke
    end
  end

end