Thread: Mouse problem
View Single Post
  #6  
Old 06-23-2005, 04:52 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
Hi,

I've never used Kaydara, but I have a sample script that I believe does something similar to what you want. It creates a custom mouse navigation mode. To rotate about the center of the scene click and drag the left mouse button. To pan left/right and up/down click and drag the right mouse button. Use the mouse wheel to zoom in/out. You can also recenter the viewpoint by holding down the left shift button and clicking on an object in the scene. This will center the viewpoint rotations around the exact 3d location that was clicked. Let me know if this doesn't help you.
Code:
import viz
viz.go()

viz.add('mini.osgx')
viz.clearcolor(viz.GRAY)

class VizMouseNavigator(viz.EventClass):

	PAN_SCALE = 20.0
	MOVE_SCALE = 1.0
	
	def __init__(self):
		viz.EventClass.__init__(self)

		viz.mouse(0)

		self.__view = viz.get(viz.MAIN_VIEWPOINT)
		self.__view.translate(0,0,-10)
		
		self.__center = [0,0,0]
		self.__lastmousepos = [0,0]
		
		self.callback(viz.TIMER_EVENT,self.__ontimer)
		self.callback(viz.MOUSEWHEEL_EVENT,self.__onmousewheel)
		self.callback(viz.MOUSEDOWN_EVENT,self.__onmousedown)
		self.starttimer(0,0,viz.FOREVER)
		
	def __ontimer(self,num):
		button = viz.buttonstate()
		mouse = viz.mousepos()
		if button & viz.MOUSEBUTTON_RIGHT:
			self.panRight(-(mouse[0] - self.__lastmousepos[0])*self.PAN_SCALE)
			self.panUp(-(mouse[1] - self.__lastmousepos[1])*self.PAN_SCALE)
		if button & viz.MOUSEBUTTON_LEFT:
			self.rotateRight((mouse[0] - self.__lastmousepos[0])*self.MOVE_SCALE)
			self.rotateUp((mouse[1] - self.__lastmousepos[1])*self.MOVE_SCALE)
		self.__lastmousepos = mouse
		
	def __onmousewheel(self,dir):
		distance = vizmat.Distance(self.__view.get(viz.HEAD_POS),self.__center) / 8.0
		self.__view.move(0,0,dir*distance)

	def __onmousedown(self,button):
		if button == viz.MOUSEBUTTON_LEFT and viz.iskeydown(viz.KEY_SHIFT_L):
			info = viz.pick(1)
			if info.intersected:
				self.setCenter(info.intersectPoint)

	def rotateRight(self,amount):
		distance = vizmat.Distance(self.__view.get(viz.HEAD_POS),self.__center)
		self.__view.move(-distance*amount,0,0)
		self.__view.lookat(self.__center)
		headPos = viz.Vector(self.__view.get(viz.HEAD_POS))
		headPos = headPos - self.__center
		headPos.normalize()
		headPos *= distance
		headPos = headPos + self.__center
		self.__view.translate(headPos.get())
		
	def rotateUp(self,amount):
		oldPos = self.__view.get(viz.HEAD_POS)
		oldRot = self.__view.get(viz.HEAD_QUAT)
		distance = vizmat.Distance(self.__view.get(viz.HEAD_POS),self.__center)
		self.__view.move(0,-distance*amount,0)
		self.__view.lookat(self.__center)
		if abs(self.__view.get(viz.HEAD_PITCH)) > 85.0:
			self.__view.translate(oldPos)
			self.__view.rotatequat(oldRot)
			return
		headPos = viz.Vector(self.__view.get(viz.HEAD_POS))
		headPos = headPos - self.__center
		headPos.normalize()
		headPos *= distance
		headPos = headPos + self.__center
		self.__view.translate(headPos.get())

	def panRight(self,amount):
		mat = viz.Transform(self.__view.get(viz.HEAD_MAT))
		mat.setTrans(self.__center)
		mat.preTrans(amount,0,0)
		self.__center = mat.getTrans()
		self.__view.move(amount,0,0)
		
	def panUp(self,amount):
		mat = viz.Transform(self.__view.get(viz.HEAD_MAT))
		mat.setTrans(self.__center)
		mat.preTrans(0,amount,0)
		self.__center = mat.getTrans()
		self.__view.move(0,amount,0)
		
	def setCenter(self,center):
		diff = viz.Vector(center)
		diff = diff - self.__center
		pos = self.__view.get(viz.HEAD_POS)
		self.__view.translate(pos[0]+diff[0],pos[1]+diff[1],pos[2]+diff[2])
		self.__center = center


VizMouseNavigator()
Reply With Quote