Constraints

The Slate supports a simple constraint mechanism. The mechanism is really just a simple notification mechanism, and allows scripts to be executed when certain kinds of operations are performed on items.

First, we'll create a Slate with some items on it:

  source $tycho/editors/slate/doc/internals/newslate.itcl
  set a [$slate create Frame 30 30 70 50 -color green -tags moveable]
  set b [$slate create Frame 100 100 130 130 \
		-color peachpuff -tags moveable]
Now draw a line between them:
  set l [eval $slate create line \
		[$slate aspect $a se] \
		[$slate aspect $b nw] \
		-tags moveable -arrow last]
We want the line to remain connected to the boxes if we move either of the boxes. To do so, attach a constraint to each box, as follows:
  $slate constraint $a "move" {lambda id x y -> \
		$slate reshape $l $x $y "start"}
  $slate constraint $b "move" {lambda id x y -> \
		$slate reshape $l $x $y "end"}
(Try moving the boxes.) The first command here says that, when box a is moved, to move the start of the line by the same amount; the second line says that when box b is moved, to move the end of the line by the same amount.

Now, suppose we also want the boxes to move if we move the line. This is simple enough -- attach a constraint to the line, so that when it is moved, both boxes are moved:

  $slate constraint $l "move" {lambda id x y -> 
	$slate move $a $x $y; \
	$slate move $b $x $y }
(Try moving the line. You have to position the mouse fairly carefully to "grab" it.) You will notice the constraints here are circular: moving the line moves the boxes, which in turn will try to reshape the line. The Slate checks for this kind of circularity and simply stops propagating constraints if a constraint attempts to move or reshape an item that has already been moved or reshaped.

Next
Back up
Tycho Home Page


Copyright © 1996-1998, The Regents of the University of California. All rights reserved.
Last updated: 05/19/98, comments to: johnr@eecs.berkeley.edu