Drawing on the slate -- Interactors

So far, all of the pictures we have drawn have been ``dead'' -- they don't respond to the mouse at all. VL Toolkit support two mechanisms for mouse interaction: first, it has event bindings that are similar (although both more complicated and less functional than the Tk bindings because of interaction with the picture hierarchy), and a new mechanism implemented on top of bindings called interactors. In this tutorial, I'm going to cover only interactors.

You create an interactor just like a picture:

set follower [$slate create Follower]
(You won't see anything happen yet.) A Follower is the most basic kind of interactor: it ``follows'' the mouse. To make the interactor actually do something, you bind it to a picture, like this:
$follower bind $frame
Now, if you click mouse button one on the green frame you can drag it about the place. To use a different button, you can set configuration options of the interactor. An interactor cannot have these options changed while it is bound to anything, so we unbind it before doing so:
$follower unbind $frame
$follower configure -button 2
$follower bind $frame
Now the middle button will drag the frame. Unfortunately, modifier keys are broken at the moment, although in theory you should be able to set the -modifiers options to control this.

There are several other interactors; here's a selection, being bound to some of the other pictures:

set stepper [$slate create Stepper]
$stepper bind $polygon

set gridder [$slate create Gridder]
$gridder bind $line

set alonger [$slate create Alonger]
$alonger bind $image

set bounder [$slate create Bounder]
$bounder bind $arrow

set diagonal [$slate create Diagonal -along negative]
$diagonal bind $oval
Try moving the objects to see the effect. The distinction between the Gridder and Stepper is a little subtle and not easily observed in this example: Gridder moves the cursor along gridlines, whereas Stepper move the cursor in increments of the grid size (from its starting point). Notice that the arrow cannot be dragged off the slate, whereas the other pictures can. This is because Bounder, by default, moves a picture inside the region defined by the edge of the canvas. We can override this, as follows:
$bounder configure -bounds [$border query bounds]
Now the arrow can only move inside the grey boundary.

Usually, we are not going to be interested in binding interactors to individual picture: we want to bind to a whole class of pictures. Since the follower moves with the second button, we'll bind follower to all of the picture on the slate using a tag. First, add a tag to all pictures (usually, the tag will be set as a configuration option when the picture is created):

foreach p {polygon frame arrow image line oval} {
    [set $p] tag add "moveable"
}
Now, bind the follower to the tag ``moveable'':
$follower bind "moveable"
Use the middle button to move the pictures. The left button works as it did before. (There's a bug here which you'll notice if you try to move the frame: the first time, it doesn't work; thereafter, it does.)

More to come: selection interactor, tracing interactor.

Next