Class: Pongo::Renderer::TkRenderer

Inherits:
Renderer
  • Object
show all
Defined in:
lib/pongo/renderer/tk_renderer.rb

Instance Method Summary collapse

Methods inherited from Renderer

#cleanup, #cleanup_circle, #cleanup_rectangle, #cleanup_spring, #cleanup_wheel, #draw, #with

Constructor Details

#initialize(canvas) ⇒ TkRenderer

Returns a new instance of TkRenderer.



6
7
8
# File 'lib/pongo/renderer/tk_renderer.rb', line 6

def initialize(canvas)
  @canvas = canvas
end

Instance Method Details

#draw_circle(item) ⇒ Object Also known as: draw_wheel



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pongo/renderer/tk_renderer.rb', line 10

def draw_circle(item)
  if item.user_data[:shape]
    item.user_data[:shape].coords(
      item.px - item.radius, 
      item.py - item.radius,
      item.px + item.radius, 
      item.py + item.radius
    )
  else
    item.user_data[:shape] =TkcOval.new(@canvas, 
      item.px - item.radius, 
      item.py - item.radius,
      item.px + item.radius, 
      item.py + item.radius,
      :fill => 'black'
    )
  end
end

#draw_rectangle(item) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pongo/renderer/tk_renderer.rb', line 30

def draw_rectangle(item)
  c = Vector.new(item.px, item.py)
  x1 = -item.width/2 
  y1 = -item.height/2 
  x2 = item.width/2 
  y2 = item.height/2
  p1 = Vector.new(x1, y1).rotate!(item.radian) + c
  p2 = Vector.new(x1, y2).rotate!(item.radian) + c
  p3 = Vector.new(x2, y2).rotate!(item.radian) + c
  p4 = Vector.new(x2, y1).rotate!(item.radian) + c
  if item.user_data[:shape]
    item.user_data[:shape].coords(
      p1.x, p1.y,
      p2.x, p2.y,
      p3.x, p3.y,
      p4.x, p4.y
    )
  else
    item.user_data[:shape] = TkcPolygon.new(@canvas,
      p1.x, p1.y,
      p2.x, p2.y,
      p3.x, p3.y,
      p4.x, p4.y,
      :fill => 'black'
    )
  end
end

#draw_spring(item) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pongo/renderer/tk_renderer.rb', line 58

def draw_spring(item)
  if item.collidable?
    draw_rectangle(item.scp)
  else
    if item.user_data[:shape]
      item.user_data[:shape].coords(item.p1.px, 
        item.p1.py, item.p2.px, item.p2.py)
    else
      item.user_data[:shape] =TkcLine.new(@canvas, item.p1.px, 
        item.p1.py, item.p2.px, item.p2.py)
    end
  end
end