Ignore:
Timestamp:
Dec 8, 2010, 12:39:39 AM (14 years ago)
Author:
dkearney
Message:

video widget updates

RpVideo?.c - adding support for seeking backwards one frame.
previously, seeking backwards left us at the closest previous
key frame. we now seek forward again to get to the frame we
really wanted.

switch.tcl - added -showtext and -showimage flags so we can
use this widget as a checkbox. used in video drawing tool's
popup menus.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gui/scripts/videoparticle.tcl

    r1916 r1979  
    2020
    2121    itk_option define -halo halo Halo "10"
    22     itk_option define -name name Name ""
    2322    itk_option define -color color Color "green"
    2423    itk_option define -fncallback fncallback Fncallback ""
     24    itk_option define -bindentercb bindentercb Bindentercb ""
     25    itk_option define -bindleavecb bindleavecb Bindleavecb ""
    2526    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 } {
    2832        # defined below
    2933    }
     
    3236    }
    3337
    34     public method Add {args}
    3538    public method Delete {args}
    3639    public method Show {args}
    3740    public method Hide {args}
    3841    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 {}
    4148
    4249    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
    4352    public variable  trajcallback ""    ;# trajectory callback - calculates and draws trajectory
    4453
     
    4655    public method Menu {args}
    4756
    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}
    5164
    5265    private variable _canvas        ""  ;# canvas which owns the particle
    5366    private variable _name          ""  ;# id of the particle
    5467    private variable _color         ""  ;# color of the particle
    55     private variable _framexy       ""  ;# list of frame numbers and coords
    56                                         ;# where the particle lives
     68    private variable _frame          0  ;# frame number where this object lives
     69    private variable _coords        ""  ;# list of coords where the object lives
    5770    private variable _halo           0  ;# about the diameter of the particle
    5871    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
    5974    private variable _nextnode      ""  ;# particle this particle points to
    6075    private variable _prevnode      ""  ;# particle this particle is pointed to by
    6176    private variable _link          ""  ;# tag of vector linking this and nextnode
     77    private variable _units         ""  ;#
     78    private variable _px2dist       ""  ;# variable associated with -px2dist
    6279}
    6380
     
    7087# CONSTRUCTOR
    7188# ----------------------------------------------------------------------
    72 itcl::body Rappture::VideoParticle::constructor {win args} {
     89itcl::body Rappture::VideoParticle::constructor {name win args} {
     90
     91    set _name $name
     92    set _canvas $win
    7393
    7494    # setup the particle control menu
     
    96116    grid columnconfigure $controls 0  -weight 1
    97117
    98     set _canvas $win
    99 
    100118    # finish configuring the particle
    101119    eval itk_initialize $args
     120
     121    set _frame [uplevel \#0 $fncallback]
     122
     123    bind ${_name}-FrameEvent <<Frame>> [itcl::code $this CatchEvent Frame]
    102124}
    103125
     
    106128# ----------------------------------------------------------------------
    107129itcl::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
    146131}
    147132
     
    153138    Menu deactivate
    154139
    155     set _framexy ""
    156     Hide particle
     140    Hide object
    157141
    158142    # delete the vectors originating from this particle
     
    169153}
    170154
     155
     156# ----------------------------------------------------------------------
     157#   Enter - bindings if the mouse enters the object's space
     158# ----------------------------------------------------------------------
     159itcl::body Rappture::VideoParticle::Enter {} {
     160    uplevel \#0 $bindentercb
     161}
     162
     163
     164# ----------------------------------------------------------------------
     165#   Leave - bindings if the mouse leaves the object's space
     166# ----------------------------------------------------------------------
     167itcl::body Rappture::VideoParticle::Leave {} {
     168    uplevel \#0 $bindleavecb
     169}
     170
     171
     172# ----------------------------------------------------------------------
     173#   CatchEvent - bindings for caught events
     174# ----------------------------------------------------------------------
     175itcl::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
    171192# ----------------------------------------------------------------------
    172193# Show - draw the particle
     
    177198    set option [lindex $args 0]
    178199    switch -- $option {
    179         "particle" {
    180             foreach {x y} [lindex ${_framexy} 1] break
     200        "object" {
     201            foreach {x y} ${_coords} break
    181202            set coords [list [expr $x-${_halo}] [expr $y-${_halo}] \
    182203                             [expr $x+${_halo}] [expr $y+${_halo}]]
     
    184205                -fill ${_color} \
    185206                -width 0 \
    186                 -tags "particle ${_name}"
    187         }
    188         name {
     207                -tags "particle ${_name} ${_name}-particle"
     208        }
     209        "name" {
    189210
    190211        }
    191212        default {
    192             error "bad option \"$option\": should be \"frame <frameNumber>\"."
     213            error "bad option \"$option\": should be one of object, name."
    193214        }
    194215    }
     
    203224    set option [lindex $args 0]
    204225    switch -- $option {
    205         "particle" {
     226        "object" {
    206227            if {[llength $args] != 1} {
    207228                error "wrong # args: should be \"particle\""
    208229            }
    209             puts "hiding ${_name}"
    210230            ${_canvas} delete "${_name}"
    211231        }
    212         name {
     232        "name" {
    213233
    214234        }
    215235        default {
    216             error "bad option \"$option\": should be \"particle or name\"."
    217         }
    218     }
    219 }
    220 
    221 # ----------------------------------------------------------------------
    222 # Move - move the particle to a new location
     236            error "bad option \"$option\": should be one of object, name."
     237        }
     238    }
     239}
     240
     241# ----------------------------------------------------------------------
     242# Move - move the object to a new location
    223243# ----------------------------------------------------------------------
    224244itcl::body Rappture::VideoParticle::Move {status x y} {
    225245    switch -- $status {
    226246        "press" {
     247            set _x $x
     248            set _y $y
    227249        }
    228250        "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            }
    229260        }
    230261        "release" {
     
    233264            error "bad option \"$option\": should be one of press, motion, release."
    234265        }
    235     }
    236     set coords [list [expr $x-${_halo}] [expr $y-${_halo}] \
    237                      [expr $x+${_halo}] [expr $y+${_halo}]]
    238     eval ${_canvas} coords ${_name} $coords
    239     set _framexy [lreplace ${_framexy} 1 1 [list $x $y]]
    240     drawVectors
    241     if {[string compare "" ${_prevnode}] != 0} {
    242         ${_prevnode} drawVectors
    243266    }
    244267}
     
    274297        }
    275298    }
    276 
    277 
    278299}
    279300
     
    293314#               to all particles it is linked to.
    294315# ----------------------------------------------------------------------
    295 itcl::body Rappture::VideoParticle::drawVectors {args} {
     316itcl::body Rappture::VideoParticle::drawVectors {} {
    296317
    297318    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# ----------------------------------------------------------------------
     327itcl::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# ----------------------------------------------------------------------
     360itcl::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
    317373
    318374# ----------------------------------------------------------------------
     
    339395}
    340396
     397# ----------------------------------------------------------------------
     398# name - get the name of the particle
     399# ----------------------------------------------------------------------
     400itcl::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# ----------------------------------------------------------------------
     410itcl::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# ----------------------------------------------------------------------
     424itcl::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# ----------------------------------------------------------------------
     437itcl::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
    341477
    342478# ----------------------------------------------------------------------
     
    349485        error "bad value: \"$itk_option(-halo)\": halo should be a number"
    350486    }
    351 }
    352 
    353 # ----------------------------------------------------------------------
    354 # CONFIGURATION OPTION: -name
    355 # ----------------------------------------------------------------------
    356 itcl::configbody Rappture::VideoParticle::name {
    357     set _name $itk_option(-name)
    358487}
    359488
     
    371500
    372501# ----------------------------------------------------------------------
     502# CONFIGURE: -px2dist
     503# ----------------------------------------------------------------------
     504itcl::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# ----------------------------------------------------------------------
     527itcl::configbody Rappture::VideoParticle::units {
     528    set _units $itk_option(-units)
     529    # _fixValue
     530}
     531
     532
     533# ----------------------------------------------------------------------
     534# CONFIGURE: -bindings
     535# ----------------------------------------------------------------------
     536itcl::configbody Rappture::VideoParticle::bindings {
     537    _fixBindings $itk_option(-bindings)
     538}
     539
     540# ----------------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.