The MATLAB Event Queue (Elements of GUI Design) Part 2

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.

The root, figure, and axes objects keep track of the pointer's location.

Figure 10.50 The root, figure, and axes objects keep track of the pointer’s location.


tmpb8bf-197

 

 

 

tmpb8bf-198

 

 

 

 

tmpb8bf-199

To test or try out this routine, create a simple plot and a text object like

tmpb8bf-200_thumb[2]

then activate the moving and resizing feature with

tmpb8bf-201_thumb[2]

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

tmpb8bf-202_thumb[2]

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.

tmpb8bf-203

 

 

tmpb8bf-204

To test out this routine, type

tmpb8bf-205_thumb[2]

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

tmpb8bf-206_thumb[2]

and once again clicking and dragging in the Figure Window.

Next post:

Previous post: