~ubuntu-branches/ubuntu/hardy/ruby1.8/hardy-updates

« back to all changes in this revision

Viewing changes to ext/tk/sample/tkline.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
require "tkclass"
 
3
 
 
4
$tkline_init = FALSE
 
5
def start_random
 
6
  return if $tkline_init
 
7
  $tkline_init = TRUE
 
8
  if defined? Thread
 
9
    Thread.start do
 
10
      loop do
 
11
        sleep 2
 
12
        Line.new($c, rand(400), rand(200), rand(400), rand(200))
 
13
      end
 
14
    end
 
15
  end
 
16
end
 
17
 
 
18
Label.new('text'=>'Please press or drag button-1').pack
 
19
 
 
20
$c = Canvas.new
 
21
$c.pack
 
22
$start_x = start_y = 0
 
23
 
 
24
def do_press(x, y)
 
25
  $start_x = x
 
26
  $start_y = y
 
27
  $current_line = Line.new($c, x, y, x, y)
 
28
  start_random
 
29
end
 
30
def do_motion(x, y)
 
31
  if $current_line
 
32
    $current_line.coords $start_x, $start_y, x, y
 
33
  end
 
34
end
 
35
 
 
36
def do_release(x, y)
 
37
  if $current_line
 
38
    $current_line.coords $start_x, $start_y, x, y
 
39
    $current_line.fill 'black'
 
40
    $current_line = nil
 
41
  end
 
42
end
 
43
 
 
44
$c.bind("1", proc{|e| do_press e.x, e.y})
 
45
$c.bind("B1-Motion", proc{|x, y| do_motion x, y}, "%x %y")
 
46
$c.bind("ButtonRelease-1", proc{|x, y| do_release x, y}, "%x %y")
 
47
Tk.mainloop