1 # -*- coding: euc-jp -*-
3 # animated wave demo (called by 'widget')
5 # based on Tcl/Tk8.5a2 widget demos
7 # destroy toplevel widget for this demo script
8 if defined?($aniwave_demo) && $aniwave_demo
13 # create toplevel widget
14 $aniwave_demo = TkToplevel.new {|w|
15 title("Animated Wave Demonstration")
20 base_frame = TkFrame.new($aniwave_demo).pack(:fill=>:both, :expand=>true)
23 msg = TkLabel.new(base_frame) {
27 text '¤³¤Î¥Ç¥â¤Ç¤Ï¡¢¥é¥¤¥ó¥¢¥¤¥Æ¥à¤¬°ì¤Ä¤À¤±ÉÁ¤«¤ì¤¿¥¥ã¥ó¥Ð¥¹¥¦¥£¥¸¥§¥Ã¥È¤¬É½¼¨¤µ¤ì¤Æ¤¤¤Þ¤¹¡£¥¢¥Ë¥á¡¼¥·¥ç¥ó½èÍý¤Ï¡¢¤½¤Î¥é¥¤¥ó¥¢¥¤¥Æ¥à¤ÎºÂɸÃͤòÊѹ¹¤¹¤ë¤³¤È¤Ç¼Â¸½¤·¤Æ¤¤¤Þ¤¹¡£'
29 msg.pack('side'=>'top')
32 TkFrame.new(base_frame) {|frame|
37 tmppath = $aniwave_demo
41 }.pack('side'=>'left', 'expand'=>'yes')
45 command proc{showCode 'aniwave'}
46 }.pack('side'=>'left', 'expand'=>'yes')
48 }.pack('side'=>'bottom', 'fill'=>'x', 'pady'=>'2m')
51 class AnimatedWaveDemo
52 def initialize(frame, dir=:left)
55 # create canvas widget
56 @c = TkCanvas.new(frame, :width=>300, :height=>200,
58 @c.pack(:padx=>10, :pady=>10, :expand=>true)
60 # Creates a coordinates list of a wave.
64 (-10..300).step(5){|n| @waveCoords << [n, 100]; @backupCoords << [n, 100] }
66 @waveCoords << [n, 0]; @backupCoords << [n, 0]
67 @waveCoords << [n+5, 200]; @backupCoords << [n+5, 200]
68 @coordsLen = @waveCoords.length
70 # Create a smoothed line and arrange for its coordinates to be the
71 # contents of the variable waveCoords.
72 @line = TkcLine.new(@c, @waveCoords,
73 :width=>1, :fill=>'green', :smooth=>true)
75 # Main animation "loop".
76 # Theoretically 100 frames-per-second (==10ms between frames)
77 @timer = TkTimer.new(10){ basicMotion; reverser }
79 # Arrange for the animation loop to stop when the canvas is deleted
80 @c.bindtags_unshift(TkBindTag.new('Destroy'){ @timer.stop })
83 # Basic motion handler. Given what direction the wave is travelling
84 # in, it advances the y coordinates in the coordinate-list one step in
87 @backupCoords, @waveCoords = @waveCoords, @backupCoords
88 (0...@coordsLen).each{|idx|
89 if @direction == :left
90 @waveCoords[idx][1] = @backupCoords[(idx+1 == @coordsLen)? 0: idx+1][1]
92 @waveCoords[idx][1] = @backupCoords[(idx == 0)? -1: idx-1][1]
95 @line.coords(@waveCoords)
98 # Oscillation handler. This detects whether to reverse the direction
99 # of the wave by checking to see if the peak of the wave has moved off
100 # the screen (whose size we know already.)
102 if @waveCoords[0][1] < 10
104 elsif @waveCoords[-1][1] < 10
119 # Start the animation processing
120 AnimatedWaveDemo.new(base_frame, :left).move