|
If you're new to Python A VPython tutorial Pictures of 3D objects What's new in VPython 6 VPython web site |
Drag ExampleThere are two ways to handle dragging with the mouse, "polling" and "callbacks". Polling is the older way of handling mouse events, and it still works. Starting with VPython 6, there is another scheme involving callbacks. For more information about these two methods, see general mouse documentation. We show here two programs that achieve the same result by the two different methods. You can use either method, but you cannot mix polling and callback methods in the same program. Both the polling and callback versions of the program achieve the same goal: dragging a sphere across the screen.
Handling dragging with callbacks Here is the sequence of mouse events involved in dragging something: 1) On a 'mousedown' event, see what object (if any) has been "picked" (lies under the mouse). 2) On a 'mousemove' event, update the position of the object based on the new mouse position. 3) On a 'mouseup' event, stop dragging the object. Here is a callback version of a program in which you can drag a sphere across the screen. Copy this into an edit window and try it! from visual import * First, we bind 'mousedown' events to the function named grab, which checks to see whether the sphere has been touched. If so, it binds future 'mousemove' and 'mouseup' events to the move and drop functions. If you do a lot of processing of each mouse movement, or you are leaving a trail behind the moving object, you may need to check whether the "new" mouse position is in fact different from the previous position before processing the "move", as is done in the example above. For example, a trail drawn with a curve object that contains a huge number of points all at the same location may not display properly. Most VPython objects can be "picked" by touching them. Here is a more general routine which lets you drag either the tail or the tip of an arrow. Copy this into an edit window and try it! from visual import *
Handling dragging with polling Here is the sequence of mouse events involved in dragging something using polling, assuming that m1 = scene.mouse.getevent(): 1) m1.press is true when you depress the mouse button (it is 'left' if left button; any quantity that is nonzero is considered true in Python). 2) m1.drag is true when the mouse coordinates change from what they were at
the time of m1.press. 3) No events occur while dragging; you continually use scene.mouse.pos to update what you're dragging. 4) m1.drop is true when you release the mouse button. You can program dragging with the mouse simply by continually reading the current value of scene.mouse.pos. Here is a complete polling routine for dragging a sphere with the left button down. Copy this into an edit window and try it! from visual import * If you do a lot of processing of each mouse movement, or you are leaving a trail behind the moving object, you may need to check whether the "new" mouse position is in fact different from the previous position before processing the "move", as is done in the example above. For example, a trail drawn with a curve object that contains a huge number of points all at the same location may not display properly. Most VPython objects can be "picked" by touching them. Here is a more general routine which lets you drag either the tail or the tip of an arrow. Copy this into an edit window and try it! from visual import * Here is general mouse documentation. |
||