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) "$x $y"
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) "$x $y $x2 $y2"
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 ""
}
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]
}
}
}
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) "$x $y"
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) "$x $y $x2 $y2"
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 ""
}
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]
}
}
}