Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cant understand procedure 1

Status
Not open for further replies.

zskater

Programmer
Apr 14, 2001
22
GB
The following code is code that a member of my team passed to me and i have modified for our project.
I understand everything apart how the utag procedure works and in proc makeHorse i dont understand
what information is contained within the var set Cell(id) / .......
I know that every cell i create gets assigned a utag id so they can be identified seperatley



# on my GUI I have buttons for different sized cells that when pressed set the cellsize and call proc startCell
#################CELL SECTION##########################################

proc startCell {} {
global Cell Graphics


bind .c <Button-1> {
set x [.c canvasx %x $Graphics(grid,snap)]
set y [.c canvasy %y $Graphics(grid,snap)]
set Cell(coords) &quot;$x $y&quot;
set Cell(options) [list -width $Graphics(line,width) -outline $Graphics(line,color) -tags {Cell obj} ]

bind .c <B1-Motion> {
}

if {$cellsize==1} { set z 1
set Graphics(shape) HorseSmall
makehorse $x $y $z
}

if {$cellsize==2} { set z 2
makehorse $x $y $z
}

if {$cellsize==3} { set z 3
makehorse $x $y $z
}


bind .c <B1-ButtonRelease> {
if ![info exists Cell(id)] {return}
set utag [Utag assign $Cell(id)]
unset Cell

}

}

}


proc makehorse {x y z} {
global Cell Graphics totalCost


if { $z == 1 } {set sizeX 80
set sizeY 120
set totalCost [expr $totalCost+50]
}
if { $z == 2 } {set sizeX 120
set sizeY 120
}
if { $z == 3 } {set sizeX 160
set sizeY 120
}


set x2 [expr abs($x + $sizeX)]
set y2 [expr abs($y + $sizeY)]

set Cell(coords) &quot;$x $y $x2 $y2&quot;

set Cell(id) [eval .c create rectangle $Cell(coords) $Cell(options) -fill #D60000]

###########ADD CODE HERE TO PASS TO C FUNCTION##############
}




#####################UTAG SECTION######################################

proc Utag {mode id} {
global utagCounter
if ![regexp {^[1-9][0-9]*$} $id] {
return &quot;&quot;
}
switch $mode {
assign {
incr utagCounter #this is a var that has already been declared
set utag utag$utagCounter
.c addtag $utag withtag $id
return $utag
}
find {
set tags [.c gettags $id]
set n [lsearch -regexp $tags {utag[0-9]+}]
return [lindex $tags $n]
}
}
}
 
set Cell(id) [eval .c create rectangle $Cell(coords) $Cell(options) -fill #D60000] creates a rectangle on the canvas (.c) and returns the ID of that rectangle to Cell(id).


In the code you included, Utag is only ever called with mode = &quot;assign&quot;. Apparently, some other proc will call Utag with mode = &quot;find&quot;. In this (assign) case Utag assigns a tag (tags are good for refering to and binding to objects) to the object (like the rectangle). Bob Rashkin
rrashkin@csc.com
 
Is the id a single integer or is a list of some sort ?
 
An integer (I think) Bob Rashkin
rrashkin@csc.com
 
From the canvas reference page:

&quot;Items in a canvas widget may be named in either of two ways: by id or by tag. Each item has a unique identifying number which is assigned to that item when it is created. The id of an item never changes and id numbers are never re-used within the lifetime of a canvas widget.

&quot;Each item may also have any number of tags associated with it. A tag is just a string of characters, and it may take any form except that of an integer. For example, “x123” is OK but “123” isn't. The same tag may be associated with many different items.&quot;

The canvas create operation returns the id of the item it creates.

As for your Utag procedure, Bob correctly ascertained that it assigns a tag to a canvas item if you pass it a mode argument of &quot;assign&quot;. The &quot;find&quot; mode returns the utag assigned to a given canvas item. Although it doesn't do any explicit error checking, it will simply return an empty string if the canvas item in question doesn't have a tag assigned to it of the form &quot;utag&quot; followed by a number. (The lindex command gives you an empty string return value if you give it an index that's negative or greater than or equal to the length of a list.) - Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top