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

Last change on this file since 5091 was 3330, checked in by gah, 12 years ago

merge (by hand) with Rappture1.2 branch

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