PDA

View Full Version : Mouse-drag an object


Jerry
02-04-2007, 09:40 AM
What's the best way to click on and mouse-drag an object
around the screen so that the object stays under the cursor?

Gladsomebeast
02-05-2007, 03:07 PM
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."

farshizzo
02-05-2007, 03:19 PM
Here is a sample script that should work with 2.5import 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.interse ctPoint)
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)