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

Last change on this file since 1 was 1, checked in by mmc, 17 years ago

initial import

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