I have a drag and drop interface using directbuttons. Whenever I click on an object it hops to the left or right, depending on where it is on the screen. Also, the the object doesn’t correctly follow the mouse in the x direction. I think this is all because of the aspect2d scaling issue. I tried scaling by the screen aspect ratio, but it doesn’t work. Any suggestions would be appreciated.
class demo(DirectObject):
def __init__(self):
#MODELS
jewelHook=loader.loadModel('jewels')
jewelSearch=jewelHook.findAllMatches('*/*')
jewelList=list()
for i in jewelSearch:
i.setHpr(0,90,0) # xz coordinates!!!
jewelList.append(list([i.getName(),i]))
jewelList.sort()
jewel=jewelList[2][1]
#SCREEN
self.scaleScreen=base.aspect2d.getScale()
def scaleScreenCoords(self):
scaleScreen= base.aspect2d.getScale()
scaleScreenCoords(self)
self.accept(base.win.getWindowEvent(),scaleScreenCoords)
#BUTTON/MOUSE
self.mouseHasObject=False
def printClicked():
taskMgr.add(buttonMove,'buttonGrabbed')
self.mouseHasObject=True
def buttonMove(task):
mouseX=0;mouseY=0
if base.mouseWatcherNode.hasMouse():
mouseX=base.mouseWatcherNode.getMouseX()
mouseY=base.mouseWatcherNode.getMouseY()
button.setPos(mouseX,0,mouseY)
return task.cont
def itemGrabber(mouseDown):
if mouseDown and self.mouseHasObject:
taskMgr.remove('buttonGrabbed')
button=DirectButton(geom=jewel,scale=2,command=printClicked,relief=None)
#INPUT
self.accept('mouse1-up',itemGrabber,[False])
self.accept('mouse1', itemGrabber,[True])
#CAMERA/LIGHTING
base.disableMouse()
light=AmbientLight('light1')
light.setColor((1,1,1,1))
render.setLight(render.attachNewNode(light))
#RUN
demoInstance=demo()
run()
Here’s the source with all the graphics.
sites.google.com/site/ambyra/all/button.zip