Changeset 1979 for trunk/gui/scripts/videoparticle.tcl
- Timestamp:
- Dec 8, 2010, 12:39:39 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gui/scripts/videoparticle.tcl
r1916 r1979 20 20 21 21 itk_option define -halo halo Halo "10" 22 itk_option define -name name Name ""23 22 itk_option define -color color Color "green" 24 23 itk_option define -fncallback fncallback Fncallback "" 24 itk_option define -bindentercb bindentercb Bindentercb "" 25 itk_option define -bindleavecb bindleavecb Bindleavecb "" 25 26 itk_option define -trajcallback trajcallback Trajcallback "" 26 27 constructor { args } { 27 itk_option define -px2dist px2dist Px2dist "" 28 itk_option define -units units Units "m/s" 29 itk_option define -bindings bindings Bindings "enable" 30 31 constructor { name win args } { 28 32 # defined below 29 33 } … … 32 36 } 33 37 34 public method Add {args}35 38 public method Delete {args} 36 39 public method Show {args} 37 40 public method Hide {args} 38 41 public method Link {args} 39 public method Coords {} 40 public method Frame {} 42 public method Coords {args} 43 public method Frame {args} 44 public method drawVectors {} 45 public method next {args} 46 public method prev {args} 47 public method name {} 41 48 42 49 public variable fncallback "" ;# framenumber callback - tells what frame we are on 50 public variable bindentercb "" ;# enter binding callback - call this when entering the object 51 public variable bindleavecb "" ;# leave binding callback - call this when leaving the object 43 52 public variable trajcallback "" ;# trajectory callback - calculates and draws trajectory 44 53 … … 46 55 public method Menu {args} 47 56 48 public method drawVectors {args} 49 public method next {args} 50 public method prev {args} 57 protected method Enter {} 58 protected method Leave {} 59 protected method CatchEvent {event} 60 61 protected method _fixValue {args} 62 protected method _fixPx2Dist {px2dist} 63 protected method _fixBindings {status} 51 64 52 65 private variable _canvas "" ;# canvas which owns the particle 53 66 private variable _name "" ;# id of the particle 54 67 private variable _color "" ;# color of the particle 55 private variable _frame xy "" ;# list of frame numbers and coords56 ;# where the particlelives68 private variable _frame 0 ;# frame number where this object lives 69 private variable _coords "" ;# list of coords where the object lives 57 70 private variable _halo 0 ;# about the diameter of the particle 58 71 private variable _menu "" ;# particle controls balloon widget 72 private variable _x 0 ;# x coord when "pressed" for motion 73 private variable _y 0 ;# y coord when "pressed" for motion 59 74 private variable _nextnode "" ;# particle this particle points to 60 75 private variable _prevnode "" ;# particle this particle is pointed to by 61 76 private variable _link "" ;# tag of vector linking this and nextnode 77 private variable _units "" ;# 78 private variable _px2dist "" ;# variable associated with -px2dist 62 79 } 63 80 … … 70 87 # CONSTRUCTOR 71 88 # ---------------------------------------------------------------------- 72 itcl::body Rappture::VideoParticle::constructor {win args} { 89 itcl::body Rappture::VideoParticle::constructor {name win args} { 90 91 set _name $name 92 set _canvas $win 73 93 74 94 # setup the particle control menu … … 96 116 grid columnconfigure $controls 0 -weight 1 97 117 98 set _canvas $win99 100 118 # finish configuring the particle 101 119 eval itk_initialize $args 120 121 set _frame [uplevel \#0 $fncallback] 122 123 bind ${_name}-FrameEvent <<Frame>> [itcl::code $this CatchEvent Frame] 102 124 } 103 125 … … 106 128 # ---------------------------------------------------------------------- 107 129 itcl::body Rappture::VideoParticle::destructor {} { 108 } 109 110 # ---------------------------------------------------------------------- 111 # Add - add attributes to the particle 112 # frame <frameNum> <x> <y> - add a frame and location 113 # ---------------------------------------------------------------------- 114 itcl::body Rappture::VideoParticle::Add {args} { 115 set option [lindex $args 0] 116 switch -- $option { 117 "frame" { 118 if {[llength $args] == 4} { 119 foreach { frNum x y } [lrange $args 1 end] break 120 if {([string is integer $frNum] != 1)} { 121 error "bad value: \"$frNum\": frame number should be an integer" 122 } 123 if {([string is double $x] != 1)} { 124 error "bad value: \"$frNum\": x coordinate should be a number" 125 } 126 if {([string is double $y] != 1)} { 127 error "bad value: \"$frNum\": y coordinate should be a number" 128 } 129 # if the particle is alread in the frame, 130 # update it's x,y coords. othrewise add it. 131 set idx [lsearch ${_framexy} $frNum] 132 if {$idx == -1} { 133 lappend _framexy $frNum [list $x $y] 134 } else { 135 incr idx 136 set _framexy [lreplace ${_framexy} $idx $idx [list $x $y]] 137 } 138 } else { 139 error "wrong # args: should be \"frame <frameNumber> <x> <y>\"" 140 } 141 } 142 default { 143 error "bad option \"$option\": should be frame." 144 } 145 } 130 configure -px2dist "" ;# remove variable trace 146 131 } 147 132 … … 153 138 Menu deactivate 154 139 155 set _framexy "" 156 Hide particle 140 Hide object 157 141 158 142 # delete the vectors originating from this particle … … 169 153 } 170 154 155 156 # ---------------------------------------------------------------------- 157 # Enter - bindings if the mouse enters the object's space 158 # ---------------------------------------------------------------------- 159 itcl::body Rappture::VideoParticle::Enter {} { 160 uplevel \#0 $bindentercb 161 } 162 163 164 # ---------------------------------------------------------------------- 165 # Leave - bindings if the mouse leaves the object's space 166 # ---------------------------------------------------------------------- 167 itcl::body Rappture::VideoParticle::Leave {} { 168 uplevel \#0 $bindleavecb 169 } 170 171 172 # ---------------------------------------------------------------------- 173 # CatchEvent - bindings for caught events 174 # ---------------------------------------------------------------------- 175 itcl::body Rappture::VideoParticle::CatchEvent {event} { 176 switch -- $event { 177 "Frame" { 178 if {[uplevel \#0 $fncallback] == ${_frame}} { 179 ${_canvas} itemconfigure ${_name}-particle -fill red 180 } else { 181 ${_canvas} itemconfigure ${_name}-particle -fill ${_color} 182 } 183 } 184 default { 185 error "bad event \"$event\": should be one of Frame." 186 } 187 188 } 189 } 190 191 171 192 # ---------------------------------------------------------------------- 172 193 # Show - draw the particle … … 177 198 set option [lindex $args 0] 178 199 switch -- $option { 179 " particle" {180 foreach {x y} [lindex ${_framexy} 1]break200 "object" { 201 foreach {x y} ${_coords} break 181 202 set coords [list [expr $x-${_halo}] [expr $y-${_halo}] \ 182 203 [expr $x+${_halo}] [expr $y+${_halo}]] … … 184 205 -fill ${_color} \ 185 206 -width 0 \ 186 -tags "particle ${_name} "187 } 188 name{207 -tags "particle ${_name} ${_name}-particle" 208 } 209 "name" { 189 210 190 211 } 191 212 default { 192 error "bad option \"$option\": should be \"frame <frameNumber>\"."213 error "bad option \"$option\": should be one of object, name." 193 214 } 194 215 } … … 203 224 set option [lindex $args 0] 204 225 switch -- $option { 205 " particle" {226 "object" { 206 227 if {[llength $args] != 1} { 207 228 error "wrong # args: should be \"particle\"" 208 229 } 209 puts "hiding ${_name}"210 230 ${_canvas} delete "${_name}" 211 231 } 212 name{232 "name" { 213 233 214 234 } 215 235 default { 216 error "bad option \"$option\": should be \"particle or name\"."217 } 218 } 219 } 220 221 # ---------------------------------------------------------------------- 222 # Move - move the particleto a new location236 error "bad option \"$option\": should be one of object, name." 237 } 238 } 239 } 240 241 # ---------------------------------------------------------------------- 242 # Move - move the object to a new location 223 243 # ---------------------------------------------------------------------- 224 244 itcl::body Rappture::VideoParticle::Move {status x y} { 225 245 switch -- $status { 226 246 "press" { 247 set _x $x 248 set _y $y 227 249 } 228 250 "motion" { 251 ${_canvas} move ${_name} [expr $x-${_x}] [expr $y-${_y}] 252 foreach {x0 y0 x1 y1} [${_canvas} coords ${_name}-particle] break 253 set _coords [list [expr $x0+${_halo}] [expr $y0+${_halo}]] 254 set _x $x 255 set _y $y 256 drawVectors 257 if {[string compare "" ${_prevnode}] != 0} { 258 ${_prevnode} drawVectors 259 } 229 260 } 230 261 "release" { … … 233 264 error "bad option \"$option\": should be one of press, motion, release." 234 265 } 235 }236 set coords [list [expr $x-${_halo}] [expr $y-${_halo}] \237 [expr $x+${_halo}] [expr $y+${_halo}]]238 eval ${_canvas} coords ${_name} $coords239 set _framexy [lreplace ${_framexy} 1 1 [list $x $y]]240 drawVectors241 if {[string compare "" ${_prevnode}] != 0} {242 ${_prevnode} drawVectors243 266 } 244 267 } … … 274 297 } 275 298 } 276 277 278 299 } 279 300 … … 293 314 # to all particles it is linked to. 294 315 # ---------------------------------------------------------------------- 295 itcl::body Rappture::VideoParticle::drawVectors { args} {316 itcl::body Rappture::VideoParticle::drawVectors {} { 296 317 297 318 if {[string compare "" $trajcallback] != 0} { 298 uplevel \#0 $trajcallback $this ${_nextnode} 299 } 300 } 301 302 303 # ---------------------------------------------------------------------- 304 # Coords - return the x and y coordinates as a list 305 # for the first frame this particle appears in 306 # ---------------------------------------------------------------------- 307 itcl::body Rappture::VideoParticle::Coords {} { 308 return [lindex ${_framexy} 1] 309 } 310 311 # ---------------------------------------------------------------------- 312 # Frame - return the frame this particle appears in 313 # ---------------------------------------------------------------------- 314 itcl::body Rappture::VideoParticle::Frame {} { 315 return [lindex ${_framexy} 0] 316 } 319 set _link [uplevel \#0 $trajcallback $this ${_nextnode}] 320 } 321 } 322 323 324 # ---------------------------------------------------------------------- 325 # Coords ?<x0> <y0>? - update the coordinates of this object 326 # ---------------------------------------------------------------------- 327 itcl::body Rappture::VideoParticle::Coords {args} { 328 if {[llength $args] == 0} { 329 return ${_coords} 330 } elseif {[llength $args] == 1} { 331 foreach {x0 y0} [lindex $args 0] break 332 } elseif {[llength $args] == 2} { 333 foreach {x0 y0} $args break 334 } else { 335 error "wrong # args: should be \"Coords ?<x0> <y0>?\"" 336 } 337 338 if {([string is double $x0] != 1)} { 339 error "bad value: \"$x0\": x coordinate should be a double" 340 } 341 if {([string is double $y0] != 1)} { 342 error "bad value: \"$y0\": y coordinate should be a double" 343 } 344 345 set _coords [list $x0 $y0] 346 set coords [list [expr $x0-${_halo}] [expr $y0-${_halo}] \ 347 [expr $x0+${_halo}] [expr $y0+${_halo}]] 348 349 if {[llength [${_canvas} find withtag ${_name}-particle]] > 0} { 350 eval ${_canvas} coords ${_name}-particle $coords 351 } 352 353 _fixValue 354 return ${_coords} 355 } 356 357 # ---------------------------------------------------------------------- 358 # Frame ?<frameNum>? - update the frame this object is in 359 # ---------------------------------------------------------------------- 360 itcl::body Rappture::VideoParticle::Frame {args} { 361 if {[llength $args] == 1} { 362 set val [lindex $args 0] 363 if {([string is integer $val] != 1)} { 364 error "bad value: \"$val\": frame number should be an integer" 365 } 366 set _frame $val 367 } elseif {[llength $args] != 0} { 368 error "wrong # args: should be \"Frame ?<number>?\"" 369 } 370 return ${_frame} 371 } 372 317 373 318 374 # ---------------------------------------------------------------------- … … 339 395 } 340 396 397 # ---------------------------------------------------------------------- 398 # name - get the name of the particle 399 # ---------------------------------------------------------------------- 400 itcl::body Rappture::VideoParticle::name {} { 401 return ${_name} 402 } 403 404 # ---------------------------------------------------------------------- 405 # USAGE: _fixValue 406 # Invoked automatically whenever the -px2dist associated with this 407 # widget is modified. Copies the value to the current settings for 408 # the widget. 409 # ---------------------------------------------------------------------- 410 itcl::body Rappture::VideoParticle::_fixValue {args} { 411 if {"" == $itk_option(-px2dist)} { 412 return 413 } 414 upvar #0 $itk_option(-px2dist) var 415 416 drawVectors 417 } 418 419 # ---------------------------------------------------------------------- 420 # USAGE: _fixPx2Dist 421 # Invoked whenever the length part of the trajectory for this object 422 # is changed by the user via the popup menu. 423 # ---------------------------------------------------------------------- 424 itcl::body Rappture::VideoParticle::_fixPx2Dist {px2dist} { 425 if {"" == $itk_option(-px2dist)} { 426 return 427 } 428 upvar #0 $itk_option(-px2dist) var 429 set var $px2dist 430 } 431 432 # ---------------------------------------------------------------------- 433 # _fixBindings - enable/disable bindings 434 # enable 435 # disable 436 # ---------------------------------------------------------------------- 437 itcl::body Rappture::VideoParticle::_fixBindings {status} { 438 switch -- $status { 439 "enable" { 440 ${_canvas} bind ${_name} <ButtonPress-1> [itcl::code $this Move press %x %y] 441 ${_canvas} bind ${_name} <B1-Motion> [itcl::code $this Move motion %x %y] 442 ${_canvas} bind ${_name} <ButtonRelease-1> [itcl::code $this Move release %x %y] 443 444 ${_canvas} bind ${_name} <ButtonPress-3> [itcl::code $this Menu activate %x %y] 445 446 ${_canvas} bind ${_name} <Enter> [itcl::code $this Enter] 447 ${_canvas} bind ${_name} <Leave> [itcl::code $this Leave] 448 449 ${_canvas} bind ${_name} <B1-Enter> { } 450 ${_canvas} bind ${_name} <B1-Leave> { } 451 # bind ${_canvas} <<Frame>> +[itcl::code $this CatchEvent Frame] 452 bindtags ${_canvas} [concat ${_name}-FrameEvent [bindtags ${_canvas}]] 453 } 454 "disable" { 455 ${_canvas} bind ${_name} <ButtonPress-1> { } 456 ${_canvas} bind ${_name} <B1-Motion> { } 457 ${_canvas} bind ${_name} <ButtonRelease-1> { } 458 459 ${_canvas} bind ${_name} <ButtonPress-3> { } 460 461 ${_canvas} bind ${_name} <Enter> { } 462 ${_canvas} bind ${_name} <Leave> { } 463 464 ${_canvas} bind ${_name} <B1-Enter> { } 465 ${_canvas} bind ${_name} <B1-Leave> { } 466 set tagnum [lsearch [bindtags ${_canvas}] "${_name}-FrameEvent"] 467 if {$tagnum >= 0} { 468 bindtags ${_canvas} [lreplace [bindtags ${_canvas} $tagnum $tagnum]] 469 } 470 } 471 default { 472 error "bad option \"$status\": should be one of enable, disable." 473 } 474 } 475 } 476 341 477 342 478 # ---------------------------------------------------------------------- … … 349 485 error "bad value: \"$itk_option(-halo)\": halo should be a number" 350 486 } 351 }352 353 # ----------------------------------------------------------------------354 # CONFIGURATION OPTION: -name355 # ----------------------------------------------------------------------356 itcl::configbody Rappture::VideoParticle::name {357 set _name $itk_option(-name)358 487 } 359 488 … … 371 500 372 501 # ---------------------------------------------------------------------- 502 # CONFIGURE: -px2dist 503 # ---------------------------------------------------------------------- 504 itcl::configbody Rappture::VideoParticle::px2dist { 505 if {"" != $_px2dist} { 506 upvar #0 $_px2dist var 507 trace remove variable var write [itcl::code $this _fixValue] 508 } 509 510 set _px2dist $itk_option(-px2dist) 511 512 if {"" != $_px2dist} { 513 upvar #0 $_px2dist var 514 trace add variable var write [itcl::code $this _fixValue] 515 516 # sync to the current value of this variable 517 if {[info exists var]} { 518 _fixValue 519 } 520 } 521 } 522 523 524 # ---------------------------------------------------------------------- 525 # CONFIGURE: -units 526 # ---------------------------------------------------------------------- 527 itcl::configbody Rappture::VideoParticle::units { 528 set _units $itk_option(-units) 529 # _fixValue 530 } 531 532 533 # ---------------------------------------------------------------------- 534 # CONFIGURE: -bindings 535 # ---------------------------------------------------------------------- 536 itcl::configbody Rappture::VideoParticle::bindings { 537 _fixBindings $itk_option(-bindings) 538 } 539 540 # ----------------------------------------------------------------------
Note: See TracChangeset
for help on using the changeset viewer.