Improved handling of case where child is killed by a signal
[tcl-tlc.git] / scripts / interpolate.itcl
blob08dcb67e617fc86032f22a5df70677ce50e6ce08
1 # vim: foldmarker=<<<,>>> foldmethod=marker
3 class tlc::Interpolate {
4 inherit tlc::Handlers
6 constructor {args} {}
7 destructor {}
9 public {
10 variable from 0
11 variable to 1
12 variable interval 10
13 variable function {
14 set res $x
16 variable dir 1
18 method over_time {seconds a_cb}
19 method reverse {}
20 method get_dir {}
23 private {
24 variable afterid ""
25 variable start
26 variable duration
27 variable cb
28 variable delta
30 method fire_cb {}
35 body tlc::Interpolate::constructor {args} { #<<<1
36 eval configure $args
40 body tlc::Interpolate::destructor {} { #<<<1
41 #puts "$this dieing"
42 after cancel $afterid; set afterid ""
43 invoke_handlers done
47 body tlc::Interpolate::over_time {seconds a_cb} { #<<<1
48 set cb $a_cb
49 after cancel $afterid; set afterid ""
50 set start [clock clicks -milliseconds]
51 set duration [expr {int($seconds * 1000)}]
52 set delta [expr {abs($from - $to)}]
53 set afterid [after $interval [code $this fire_cb]]
57 body tlc::Interpolate::fire_cb {} { #<<<1
58 after cancel $afterid; set afterid ""
59 set elapsed [expr {[clock clicks -milliseconds] - $start}]
60 if {$elapsed < $duration} {
61 set afterid [after $interval [code $this fire_cb]]
62 if {$from < $to} {
63 set x [expr {$delta * (double($elapsed) / $duration) + $from}]
64 } else {
65 set x [expr {$from - $delta * (double($elapsed) / $duration)}]
67 set res 0
68 eval $function
69 uplevel #0 $cb $res
70 } else {
71 set x $to
72 set res 0
73 eval $function
74 uplevel #0 $cb $res
75 delete object $this
80 body tlc::Interpolate::reverse {} { #<<<1
81 set dir [expr {!$dir}]
82 set elapsed [expr {[clock clicks -milliseconds] - $start}]
83 set start [expr {
84 [clock clicks -milliseconds] -
85 ($duration - $elapsed)
88 set hold $from
89 set from $to
90 set to $hold
94 body tlc::Interpolate::get_dir {} { #<<<1
95 return $dir