source: trunk/builder/scripts/tweener.tcl @ 2081

Last change on this file since 2081 was 2081, checked in by mmc, 14 years ago

Part 2 of the major reorganization to group all of the rappture utilties
under a single rappture command. Builds better now. Still need to fix
up the builder to work with the objects in a different location now.

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