|
#1
|
|||
|
|||
Mouse-drag an object
What's the best way to click on and mouse-drag an object
around the screen so that the object stays under the cursor? |
#2
|
|||
|
|||
In 3.0 you can link to a viz.Mouse linkable object. If the objects you want to drag are on the screen you could do a viz.grab( viz.Mouse, myScreenObj ) when you click on the object. This viz.grab() function would maintain the place where the mouse grabbed the object.
Also, you can do this all manualy. The vizinfo object does this and because it is implemnted in python all the code is there for you to see. Open the Vizard30/python/vizinfo.py file and search for "drag."
__________________
Paul Elliott WorldViz LLC |
#3
|
|||
|
|||
Here is a sample script that should work with 2.5
Code:
import viz viz.go() DRAG_TIMER = 1 balls = [] #Add 3 balls and place them in front of the user for x in range(3): ball = viz.add('ball.wrl') ball.translate(x-1,1.7,4) balls.append(ball) #Add a ground ground = viz.add('tut_ground.wrl') ground.disable(viz.PICKING) #Set background color viz.clearcolor(viz.SKYBLUE) #Disable mouse navigation viz.mouse(0) #The object that will be dragged dragObject = None def BeginDrag(): global dragObject #Pick an object info = viz.pick(1) #Make sure object is one of the balls if info.object in balls: #Set the drag object dragObject = info.object dragObject.offset = vizmat.Vector(dragObject.get(viz.POSITION)) - info.intersectPoint dragObject.dist = vizmat.Distance(viz.get(viz.HEAD_POS),info.intersectPoint) viz.starttimer(DRAG_TIMER,0,viz.FOREVER) vizact.onmousedown(viz.MOUSEBUTTON_LEFT,BeginDrag) def EndDrag(): global dragObject #Stop dragging dragObject = None viz.killtimer(DRAG_TIMER) vizact.onmouseup(viz.MOUSEBUTTON_LEFT,EndDrag) def onTimer(num): if num == DRAG_TIMER: #Move the object based on mouse movement line = viz.screentoworld(viz.mousepos()) begin = line[:3] end = line[3:] dir = vizmat.Vector(end) - begin dir.normalize() dir *= dragObject.dist pos = vizmat.Vector(begin) + dir + dragObject.offset dragObject.translate(pos.get()) viz.callback(viz.TIMER_EVENT,onTimer) |
|
|