source: trunk/gui/scripts/animover.tcl @ 154

Last change on this file since 154 was 115, checked in by mmc, 18 years ago

Updated all copyright notices.

File size: 7.6 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: animover - animated move
3#
4#  This widget shows an animation of movement, to show that something
5#  is happening when files are being copied or remote machines are
6#  being queried.
7# ======================================================================
8#  AUTHOR:  Michael McLennan, Purdue University
9#  Copyright (c) 2004-2005  Purdue Research Foundation
10#
11#  See the file "license.terms" for information on usage and
12#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13# ======================================================================
14package require Itk
15
16option add *Animover.width 200 widgetDefault
17option add *Animover.direction both widgetDefault
18option add *Animover.delta 5 widgetDefault
19option add *Animover.delay 50 widgetDefault
20
21itcl::class Rappture::Animover {
22    inherit itk::Widget
23
24    itk_option define -leftimage leftImage LeftImage ""
25    itk_option define -rightimage rightImage RightImage ""
26    itk_option define -middleimage middleImage MiddleImage ""
27    itk_option define -direction direction Direction ""
28    itk_option define -delta delta Delta ""
29    itk_option define -delay delay Delay 0
30    itk_option define -running running Running "disabled"
31
32    constructor {args} { # defined below }
33                                                                               
34    protected method _redraw {}
35    protected variable _x0 0   ;# min value for middle image
36    protected variable _x1 0   ;# max value for middle image
37    protected variable _x  -1  ;# current position between _x0/_x1
38    protected variable _dx 0   ;# delta for x when anim is running
39
40    protected method _animate {}
41
42    #
43    # Load various images used as backgrounds for the map
44    #
45    set dir [file dirname [info script]]
46    private common images
47    set images(bgl) [image create photo -file \
48        [file join $dir images bgl.gif]]
49    set images(bgr) [image create photo -file \
50        [file join $dir images bgr.gif]]
51}
52                                                                               
53itk::usual Animover {
54}
55
56# ----------------------------------------------------------------------
57# CONSTRUCTOR
58# ----------------------------------------------------------------------
59itcl::body Rappture::Animover::constructor {args} {
60    itk_component add area {
61        canvas $itk_interior.area \
62            -height [expr {[image height $images(bgl)]+4}]
63    } {
64        usual
65        keep -width
66    }
67    pack $itk_component(area) -expand yes -fill both
68    bind $itk_component(area) <Configure> [itcl::code $this _redraw]
69
70    eval itk_initialize $args
71}
72
73# ----------------------------------------------------------------------
74# USAGE: _redraw
75#
76# Called automatically whenever the widget changes size to redraw
77# all elements within it.
78# ----------------------------------------------------------------------
79itcl::body Rappture::Animover::_redraw {} {
80    set c $itk_component(area)
81    $c delete all
82
83    set w [winfo width $c]
84    set h [winfo height $c]
85    set hmid [expr {$h/2}]
86
87    $c create rectangle 2 0 [expr {$w-2}] $h -outline "" -fill #E5CFA1
88    $c create image 0 $hmid -anchor w -image $images(bgl)
89    $c create image $w $hmid -anchor e -image $images(bgr)
90
91    $c create line 0 2 $w 2 -width 2 -fill black
92    $c create line 0 [expr {$h-2}] $w [expr {$h-2}] -width 2 -fill black
93    $c create line 20 $hmid [expr {$w-20}] $hmid -width 2 -fill #82765B
94
95    set midw 0
96    if {$itk_option(-middleimage) != ""} {
97        set midw [expr {[image width $itk_option(-middleimage)]/2}]
98    }
99
100    if {$itk_option(-leftimage) != ""} {
101        $c create image 4 $hmid -anchor w \
102            -image $itk_option(-leftimage)
103        set _x0 [expr {4+[image width $itk_option(-leftimage)]+$midw}]
104    } else {
105        set _x0 [expr {4+$midw}]
106    }
107    if {$_x0 < 0} { set _x0 0 }
108
109    if {$itk_option(-rightimage) != ""} {
110        $c create image [expr {$w-4}] $hmid -anchor e \
111            -image $itk_option(-rightimage)
112        set _x1 [expr {$w-4-[image width $itk_option(-rightimage)]-$midw}]
113    } else {
114        set _x1 [expr {$w-4-$midw}]
115    }
116
117    if {$_x >= 0} {
118        if {$_x < $_x0} {
119            set _x $_x0
120        } elseif {$_x > $_x1} {
121            set _x $_x1
122        }
123        if {$itk_option(-middleimage) != ""} {
124            $c create image $_x $hmid -anchor c \
125                -image $itk_option(-middleimage) -tags middle
126        }
127    }
128}
129
130# ----------------------------------------------------------------------
131# USAGE: _animate
132#
133# Called periodically when an animation is in progress to update
134# the position of the middle element.  If the element reaches the
135# left or right side, then it starts over on the other side or
136# going the other direction.
137# ----------------------------------------------------------------------
138itcl::body Rappture::Animover::_animate {} {
139    if {$_x >= 0 && $_x0 < $_x1} {
140        if {$_x+$_dx <= $_x0} {
141            if {$itk_option(-direction) == "left"} {
142                set _x $_x1
143            } elseif {$itk_option(-direction) == "both"} {
144                set _dx [expr {-$_dx}]
145                set _x [expr {$_x+$_dx}]
146            }
147        } elseif {$_x+$_dx >= $_x1} {
148            if {$itk_option(-direction) == "right"} {
149                set _x $_x0
150            } elseif {$itk_option(-direction) == "both"} {
151                set _dx [expr {-$_dx}]
152                set _x [expr {$_x+$_dx}]
153            }
154        } else {
155            set _x [expr {$_x+$_dx}]
156        }
157
158        set c $itk_component(area)
159        set h [winfo height $c]
160        set hmid [expr {$h/2}]
161
162        if {[$c find withtag middle] == ""} {
163            $c create image $_x $hmid -anchor c \
164                -image $itk_option(-middleimage) -tags middle
165        } else {
166            $c coords middle $_x $hmid
167        }
168    }
169    if {$_x >= 0} {
170        after $itk_option(-delay) [itcl::code $this _animate]
171    }
172}
173
174# ----------------------------------------------------------------------
175# OPTION: -running
176# Used to start/stop the animation.
177# ----------------------------------------------------------------------
178itcl::configbody Rappture::Animover::running {
179    switch -- $itk_option(-running) {
180        normal {
181            if {$_x < 0} {
182                set _x $_x0
183                after idle [itcl::code $this _animate]
184            }
185        }
186        disabled {
187            if {$_x > 0} {
188                set _x -1
189                after cancel [itcl::code $this _animate]
190                $itk_component(area) delete middle
191            }
192        }
193        default {
194            error "bad value \"$itk_option(-running)\": should be normal or disabled"
195        }
196    }
197}
198
199# ----------------------------------------------------------------------
200# OPTION: -direction
201# Used to control the direction of movement for the animation.
202# ----------------------------------------------------------------------
203itcl::configbody Rappture::Animover::direction {
204    switch -- $itk_option(-direction) {
205        left  { set _dx [expr {-1*$itk_option(-delta)}] }
206        right { set _dx $itk_option(-delta) }
207        both  { set _dx $itk_option(-delta) }
208        default {
209            error "bad value \"$itk_option(-direction)\": should be left, right, or both"
210        }
211    }
212}
213
214# ----------------------------------------------------------------------
215# OPTION: -delta
216# Used to control the amount of movement for the animation.
217# ----------------------------------------------------------------------
218itcl::configbody Rappture::Animover::delta {
219    if {$itk_option(-delta) < 1} {
220        error "bad value \"$itk_option(-delta)\": should be int >= 1"
221    }
222}
Note: See TracBrowser for help on using the repository browser.