1 | # -*- mode: tcl; indent-tabs-mode: nil -*- |
---|
2 | # ---------------------------------------------------------------------- |
---|
3 | # COMPONENT: tweener - used for animating smooth movements |
---|
4 | # |
---|
5 | # Each Tweener executes a command repeatedly through a series of |
---|
6 | # values from one extreme to another. For example, the command may |
---|
7 | # change the size of a rectangle, and the values may go from 10 to |
---|
8 | # 200. The Tweener will make that happen over a specified interval |
---|
9 | # of time. The animation can be stopped or changed at any point, |
---|
10 | # and if the Tweener is destroyed, the action is cancelled. |
---|
11 | # ====================================================================== |
---|
12 | # AUTHOR: Michael McLennan, Purdue University |
---|
13 | # Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
14 | # |
---|
15 | # See the file "license.terms" for information on usage and |
---|
16 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
17 | # ====================================================================== |
---|
18 | package require Itcl |
---|
19 | |
---|
20 | itcl::class Rappture::Tweener { |
---|
21 | public variable from 0 ;# value goes from this... |
---|
22 | public variable to 1 ;# ...to this |
---|
23 | public variable duration 500 ;# during this time interval |
---|
24 | public variable steps 10 ;# with this many steps |
---|
25 | public variable command "" ;# gets executed with %v for value |
---|
26 | public variable finalize "" ;# gets executed at the end to finish up |
---|
27 | |
---|
28 | constructor {args} { eval configure $args } |
---|
29 | destructor { # defined below } |
---|
30 | |
---|
31 | public method go {{how "-resume"}} |
---|
32 | public method stop {} |
---|
33 | |
---|
34 | private method _next {} |
---|
35 | |
---|
36 | private variable _nstep 0 ;# current step number |
---|
37 | private variable _afterid "" ;# pending after event |
---|
38 | } |
---|
39 | |
---|
40 | # ---------------------------------------------------------------------- |
---|
41 | # DESTRUCTOR |
---|
42 | # ---------------------------------------------------------------------- |
---|
43 | itcl::body Rappture::Tweener::destructor {} { |
---|
44 | stop |
---|
45 | } |
---|
46 | |
---|
47 | # ---------------------------------------------------------------------- |
---|
48 | # USAGE: go ?-resume|-restart? |
---|
49 | # |
---|
50 | # Causes the current animation to start running, either where it |
---|
51 | # left off (-resume) or fromt the beginning (-restart). |
---|
52 | # ---------------------------------------------------------------------- |
---|
53 | itcl::body Rappture::Tweener::go {{how -resume}} { |
---|
54 | switch -- $how { |
---|
55 | -resume { |
---|
56 | # leave _nstep alone |
---|
57 | } |
---|
58 | -restart { |
---|
59 | set _nstep 0 |
---|
60 | } |
---|
61 | default { |
---|
62 | error "bad option \"$how\": should be -restart or -resume" |
---|
63 | } |
---|
64 | } |
---|
65 | stop |
---|
66 | set _afterid [after idle [itcl::code $this _next]] |
---|
67 | } |
---|
68 | |
---|
69 | # ---------------------------------------------------------------------- |
---|
70 | # USAGE: stop |
---|
71 | # |
---|
72 | # Causes the animation to stop by cancelling any pending after event. |
---|
73 | # ---------------------------------------------------------------------- |
---|
74 | itcl::body Rappture::Tweener::stop {} { |
---|
75 | if {"" != $_afterid} { |
---|
76 | after cancel $_afterid |
---|
77 | set _afterid "" |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | # ---------------------------------------------------------------------- |
---|
82 | # USAGE: _next |
---|
83 | # |
---|
84 | # Used internally to advance the animation. Executes the -command |
---|
85 | # option with the current value substituted in place of any %v values. |
---|
86 | # ---------------------------------------------------------------------- |
---|
87 | itcl::body Rappture::Tweener::_next {} { |
---|
88 | set value [expr {($to-$from)/double($steps)*$_nstep + $from}] |
---|
89 | set cmd $command |
---|
90 | regsub -all %v $cmd $value cmd |
---|
91 | if {[catch {uplevel #0 $cmd} result]} { |
---|
92 | bgerror "$result\n (while managing tweener $this)" |
---|
93 | } |
---|
94 | |
---|
95 | if {[incr _nstep] <= $steps} { |
---|
96 | set delay [expr {round($duration/double($steps))}] |
---|
97 | set _afterid [after $delay [itcl::code $this _next]] |
---|
98 | } elseif {[string length $finalize] > 0} { |
---|
99 | set cmd $finalize |
---|
100 | regsub -all %v $cmd $value cmd |
---|
101 | if {[catch {uplevel #0 $cmd} result]} { |
---|
102 | bgerror "$result\n (while finalizing tweener $this)" |
---|
103 | } |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | # ---------------------------------------------------------------------- |
---|
108 | # CONFIGURATION OPTION: -command |
---|
109 | # ---------------------------------------------------------------------- |
---|
110 | itcl::configbody Rappture::Tweener::command { |
---|
111 | if {[string length $command] == 0} { |
---|
112 | stop |
---|
113 | } |
---|
114 | } |
---|