aboutsummaryrefslogtreecommitdiff
path: root/demos/contours.tcl
diff options
context:
space:
mode:
Diffstat (limited to 'demos/contours.tcl')
-rw-r--r--demos/contours.tcl92
1 files changed, 45 insertions, 47 deletions
diff --git a/demos/contours.tcl b/demos/contours.tcl
index 07fa3c3..ea3aab3 100644
--- a/demos/contours.tcl
+++ b/demos/contours.tcl
@@ -82,16 +82,17 @@ $w.zinc translate $curve3 -130 0
# cloning curve0 as curve4 and moving it slightly
set curve4 [$w.zinc clone $curve0 -visible 1 -fillcolor grey50 -tags grouped -fillrule positive]
- # the tag "grouped" is used for both curve4 and
- # a handle (see just below)
- # It is used for translating both easily
+# the tag "grouped" is used for both curve4 and
+# a handle (see just below)
+# It is used for translating both easily
set index 2; ## index of the vertex associated to the handle
set coord [$w.zinc coords $curve4 0 $index]
set x [lindex $coord 0]
set y [lindex $coord 1]
-set handle [$w.zinc add rectangle 1 "[expr $x-5] [expr $y-5] [expr $x+5] [expr $y+5]" -fillcolor black -filled 1 -tags {grouped}]
+set handle [$w.zinc add rectangle 1 "[expr $x-5] [expr $y-5] [expr $x+5] [expr $y+5]" \
+ -fillcolor black -filled 1 -tags {grouped}]
# adding a 'difference' contour to the curve4
@@ -121,71 +122,68 @@ $w.zinc raise $curve1
# adding drag and drop callback to each visible curve!
foreach item "$curve1 $curve2 $curve3 $curve4" {
# Some bindings for dragging the items
- $w.zinc bind $item <1> "itemStartDrag $item %x %y"
- $w.zinc bind $item <B1-Motion> "itemDrag $item %x %y"
- # <ButtonRelease-1> release
+ $w.zinc bind $item <ButtonPress-1> "press $item motion %x %y"
+ $w.zinc bind $item <ButtonRelease-1> release
}
# adding drag and drop on curve4 which also moves handle
-$w.zinc bind $curve4 <ButtonPress-1> {press $curve4 motionWithHandle}
+$w.zinc bind $curve4 <ButtonPress-1> "press $curve4 motionWithHandle %x %y"
$w.zinc bind $curve4, <ButtonRelease-1> release
# adding drag and drop on handle which also modify curve4
-$w.zinc bind handle <ButtonPress-1> {press $handle moveHandle}
-$w.zinc bind handle <ButtonRelease-1> release
+$w.zinc bind $handle <ButtonPress-1> "press $handle moveHandle %x %y"
+$w.zinc bind $handle <ButtonRelease-1> release
# callback for starting a drag
-set x_orig ""
-set y_orig ""
+set xOrig 0
+set yOrig 0
-proc itemStartDrag {item x y} {
- global x_orig y_orig
- set x_orig $x
- set y_orig $y
+proc press {item action x y} {
+ global w xOrig yOrig
+
+ puts "item $item"
+ set xOrig $x
+ set yOrig $y
+ bind $w.zinc <Motion> "$action $item %x %y"
}
# Callback for moving an item
-proc itemDrag {item x y} {
- global x_orig y_orig
- global w
- $w.zinc translate $item [expr $x-$x_orig] [expr $y-$y_orig];
- set x_orig $x;
- set y_orig $y;
+proc motion {item x y} {
+ global w xOrig yOrig
+
+ $w.zinc translate $item [expr $x - $xOrig] [expr $y - $yOrig]
+ set xOrig $x
+ set yOrig $y
}
# Callback for moving an item and its handle
-proc motionWithHandle {item} {
- global x_orig
- global y_orig
- set ev [$w.zinc XEvent]
- set x_orig $ev->x;
- set y_orig $ev->y;
-
- set tag [$w.zinc itemcget $item -tags]
- $w.zinc translate $tag [epxr $x-$x_orig] [expr $y-$y_orig]
- set x_orig $x;
- set y_orig $y;
+proc motionWithHandle {item x y} {
+ global w xOrig yOrig
+
+ set tag [lindex [$w.zinc itemcget $item -tags] 0]
+ $w.zinc translate $tag [expr $x-$xOrig] [expr $y-$yOrig]
+ set xOrig $x;
+ set yOrig $y;
}
# Callback for moving the handle and modifying curve4
# this code is far from being generic. Only for demonstrating how we can
# modify a contour with a unique handle!
-proc moveHandle {handle} {
- global x_orig
- global y_orig
- set ev [$w.zinc XEvent]
- set x_orig $ev->x;
- set y_orig $ev->y;
-
- $w.zinc translate $handle [expr $x-$x_orig] [expr $y-$y_orig];
-
- set ($vertxX,$vertxY) [$w.zinc coords $curve4 0 $index]
- $w.zinc coords $curve4 0 $index "[expr $vertxX+($x-$x_orig)] [expr $vertxY+($y-$y_orig)]"
- set x_orig $x
- set y_orig $y
+proc moveHandle {handle x y} {
+ global w xOrig yOrig curve4 index
+
+ puts "$handle $x $y"
+ $w.zinc translate $handle [expr $x - $xOrig] [expr $y - $yOrig];
+
+ foreach {vertxX vertxY} [$w.zinc coords $curve4 0 $index] break
+ $w.zinc coords $curve4 0 $index "[expr $vertxX+($x-$xOrig)] [expr $vertxY+($y-$yOrig)]"
+ set xOrig $x
+ set yOrig $y
}
proc release {} {
- $w.zinc Tk::bind('<Motion>', '');
+ global w
+
+ bind $w.zinc <Motion> {}
}