Moving Objects with the Mouse
Although you can easily move text in a plot using the plot editing mode in the Figure Window, here we are going to develop a function that allows you to move any graphics objects with the mouse. When the user clicks the mouse, we determine the current object’s Type property, and then set the WindowButtonMotionFcn and WindowButtonUpFcn properties appropriately to allow the user to move the selected object. The user is notified that the object has been selected by a box that appears when the Selected property is set to "on". In addition, the type of operation (move/resize) is identified by the pointer type. The task of programming such a routine relies on knowing the points where position and location data is measured with respect to, and structuring a function to respond to, the user’s actions as summarized in Figure 10.50.
Figure 10.50 The root, figure, and axes objects keep track of the pointer’s location.
To test or try out this routine, create a simple plot and a text object like
then activate the moving and resizing feature with
Look at how the resize object feature is incorporated into the program. As an assignment try putting in the functionality that allows you to move uicontrol, line, and patch objects as well. You could also set the ButtonDownFcn of the objects (axes and uicontrol objects only) that you want to be able to interactively move to the function selectmoveresize. For example, to be able to move all text around, type
Then, after altering the object, set the same property to an empty string so that you don’t accidentally move something you don’t want to move.
Dynamic Boxes Using the RBBOX Function
In some situations, you may want to add a feature that allows the user to click and drag out a dynamic and temporary box for the purpose of selecting objects or identifying regions of a figure. Usually this box is drawn from the location at which the mouse button was first pressed to the current location of the mouse pointer. After the mouse button is released, the box disappears. This is what the code below implements.
To test out this routine, type
and then click and drag in the Figure Window. After you have established the size of the box just dragged out, move it around by typing
and once again clicking and dragging in the Figure Window.