def mouseReleaseEvent(self, event): btn = event.button() if btn == QtCore.Qt.LeftButton: self.inputDevice.buttonUp(MouseButton.one()) elif btn == QtCore.Qt.MiddleButton: self.inputDevice.buttonUp(MouseButton.two()) elif btn == QtCore.Qt.RightButton: self.inputDevice.buttonUp(MouseButton.three()) QtWidgets.QWidget.mouseReleaseEvent(self, event)
def moveCamera( self, task ): is_down = base.mouseWatcherNode.is_button_down if base.mouseWatcherNode.hasMouse(): x = base.mouseWatcherNode.getMouseX() y = base.mouseWatcherNode.getMouseY() if is_down( MouseButton.two() ): dx = self.lastMousePos[0] - x self.ang -= dx #self.ang = max( 0, min( self.ang, math.pi*0.49 ) ) dy = self.lastMousePos[1] - y self.angY -= dy self.angY = max( 0.01, min( self.angY, math.pi ) ) self.lastMousePos = (x,y) speed = self.speed if is_down( self.bSpeed ): speed = speed*3 if self.attachmentNode and self.attached: self.focusPoint = self.attachmentNode.getPos() else: sideways = (is_down(self.bRight)-is_down(self.bLeft))*speed forward = (is_down(self.bForward)-is_down(self.bBackward))*speed quat = Quat() quat.setFromAxisAngle( -180*self.ang/math.pi, LVector3f.unitZ()) rotated = quat.xform( LVector3f( -forward, sideways, 0 )) self.focusPoint += rotated self.focusNode.setPos( self.focusPoint ) radius = self.zoom self.angY = max( 0, min( math.pi*0.4, self.angY ) ) rY = math.sin( self.angY ) self.nodePos = self.focusPoint + LVector3f( rY*math.cos(self.ang)*radius, -rY*math.sin(self.ang)*radius, radius*math.cos( self.angY) ) self.node.setPos( self.nodePos ) self.node.lookAt( self.focusNode, LVector3f.unitZ() ) return Task.cont
def moveCamera(self, task): is_down = base.mouseWatcherNode.is_button_down if base.mouseWatcherNode.hasMouse(): x = base.mouseWatcherNode.getMouseX() y = base.mouseWatcherNode.getMouseY() if is_down(MouseButton.two()): dx = self.lastMousePos[0] - x self.heading += dx * 25 #self.ang = max( 0, min( self.ang, math.pi*0.49 ) ) dy = self.lastMousePos[1] - y self.pitch -= dy * 25 self.pitch = max(self.pitch, -80) self.pitch = min(self.pitch, 80) #self.angY = max( 0.01, min( self.angY, math.pi ) ) self.lastMousePos = (x, y) speed = self.speed if is_down(self.bSpeed): speed = speed * 3 if not self.attached: forward = (is_down(self.bRight) - is_down(self.bLeft)) * speed sideways = -(is_down(self.bForward) - is_down(self.bBackward)) * speed quat = Quat() quat.setFromAxisAngle(self.heading, LVector3f.unitZ()) rotated = quat.xform(LVector3f(-forward, sideways, 0)) self.headingNode.setPos(self.headingNode.getPos() - rotated) #self.angY = max( 0, min( math.pi*0.4, self.angY ) ) #rY = math.sin( self.angY ) #self.nodePos = self.focusPoint + LVector3f( math.sin(self.angY)*math.cos(self.ang)*radius, -math.sin(self.ang)*radius, radius*math.cos( self.angY) ) self.node.lookAt(self.headingNode) self.node.setPos(0, -self.zoom, 0) self.pitchNode.setHpr(0, self.pitch, 0) self.headingNode.setHpr(self.heading, 0, 0) return Task.cont
def test_mousebutton(): btns = [ MouseButton.one(), MouseButton.two(), MouseButton.three(), MouseButton.four(), MouseButton.five() ] for i, btn in enumerate(btns): assert MouseButton.button(i) == btn assert MouseButton.is_mouse_button(btn) assert MouseButton.button(5) == ButtonHandle.none() assert MouseButton.is_mouse_button(MouseButton.wheel_up()) assert MouseButton.is_mouse_button(MouseButton.wheel_down()) assert MouseButton.is_mouse_button(MouseButton.wheel_left()) assert MouseButton.is_mouse_button(MouseButton.wheel_right())
class DialogMask(DirectButton): '''used to generate a full-screen mask to prevent button-clicking below the focused window/dialog Added some tricks to make panda3d mouse events still available ''' B1PRESS = PGButton.getPressPrefix() + MouseButton.one().getName() + '-' B2PRESS = PGButton.getPressPrefix() + MouseButton.two().getName() + '-' B3PRESS = PGButton.getPressPrefix() + MouseButton.three().getName() + '-' B4PRESS = PGButton.getPressPrefix() + MouseButton.four().getName() + '-' B5PRESS = PGButton.getPressPrefix() + MouseButton.five().getName() + '-' WHEELUP = PGButton.getReleasePrefix() + MouseButton.wheelUp().getName( ) + '-' WHEELDOWN = PGButton.getReleasePrefix() + MouseButton.wheelDown().getName( ) + '-' WHEELLEFT = PGButton.getReleasePrefix() + MouseButton.wheelLeft().getName( ) + '-' WHEELRIGHT = PGButton.getReleasePrefix() + MouseButton.wheelRight( ).getName() + '-' B1RELEASE = PGButton.getReleasePrefix() + MouseButton.one().getName() + '-' B2RELEASE = PGButton.getReleasePrefix() + MouseButton.two().getName() + '-' B3RELEASE = PGButton.getReleasePrefix() + MouseButton.three().getName( ) + '-' B4RELEASE = PGButton.getReleasePrefix() + MouseButton.four().getName( ) + '-' B5RELEASE = PGButton.getReleasePrefix() + MouseButton.five().getName( ) + '-' def __init__(self): DirectButton.__init__(self, parent=aspect2d, frameColor=(1, 1, 1, 0), relief=DGG.FLAT, commandButtons=[]) self.accept('window-event', self.windowResize) self.windowResize(None) #trick: make this re-throw mouse events self.bind(self.B1PRESS, self.rethrowEvent, ['mouse1']) self.bind(self.B2PRESS, self.rethrowEvent, ['mouse2']) self.bind(self.B3PRESS, self.rethrowEvent, ['mouse3']) self.bind(self.B4PRESS, self.rethrowEvent, ['mouse4']) self.bind(self.B5PRESS, self.rethrowEvent, ['mouse5']) self.bind(self.WHEELUP, self.rethrowEvent, ['wheel_up']) self.bind(self.WHEELDOWN, self.rethrowEvent, ['wheel_down']) self.bind(self.WHEELLEFT, self.rethrowEvent, ['wheel_left']) self.bind(self.WHEELRIGHT, self.rethrowEvent, ['wheel_right']) self.bind(self.B1RELEASE, self.rethrowEvent, ['mouse1-up']) self.bind(self.B2RELEASE, self.rethrowEvent, ['mouse2-up']) self.bind(self.B3RELEASE, self.rethrowEvent, ['mouse3-up']) self.bind(self.B4RELEASE, self.rethrowEvent, ['mouse4-up']) self.bind(self.B5RELEASE, self.rethrowEvent, ['mouse5-up']) def windowResize(self, arg): #fill the screen aspect = base.getAspectRatio() if aspect > 1: self['frameSize'] = (-aspect, aspect, -1, 1) elif aspect: hh = 1.0 / aspect self['frameSize'] = (-1, 1, -hh, hh) def setCommandButtons(self, *args, **kwargs): #inherited pass def rethrowEvent(self, sevent, event): messenger.send(sevent) def destroy(self): self.ignoreAll() DirectButton.destroy(self)
def __init__(self, name): NodePath.__init__(self, name) DirectObject.__init__(self) self.setPythonTag('Sprite', self) global SpriteCounter SpriteCounter += 1 self.__id = int(SpriteCounter) #- Use PGItem to detect mouse and keyboard input via PGTop (eg, aspect2d, pixel2d, etc) self.__pgItem = PGItem(name) self.__pgItem.setActive(True) self.__pgItemNp = self.attachNewNode(self.__pgItem) #- Use TextNode to generate both text and cards for displaying background images self.__textNode = TextNode(name) self.__textNodeNp = None #self.__textNodeNp = self.attachNewNode(self.__textNode) #self.__textNode.setCardDecal(True) #- This is what we would do, should Sprite support being non-under PGTop self.accept(self.__pgItem.getPressEvent(MouseButton.one()), self.__onMouse, [Sprite.MouseLeftDown]) self.accept(self.__pgItem.getPressEvent(MouseButton.two()), self.__onMouse, [Sprite.MouseCenterDown]) self.accept(self.__pgItem.getPressEvent(MouseButton.three()), self.__onMouse, [Sprite.MouseRightDown]) self.accept(self.__pgItem.getPressEvent(MouseButton.four()), self.__onMouse, [Sprite.MouseFourDown]) self.accept(self.__pgItem.getPressEvent(MouseButton.five()), self.__onMouse, [Sprite.MouseFiveDown]) self.accept(self.__pgItem.getReleaseEvent(MouseButton.one()), self.__onMouse, [Sprite.MouseLeftUp]) self.accept(self.__pgItem.getReleaseEvent(MouseButton.two()), self.__onMouse, [Sprite.MouseCenterUp]) self.accept(self.__pgItem.getReleaseEvent(MouseButton.three()), self.__onMouse, [Sprite.MouseRightUp]) self.accept(self.__pgItem.getReleaseEvent(MouseButton.four()), self.__onMouse, [Sprite.MouseFourUp]) self.accept(self.__pgItem.getReleaseEvent(MouseButton.five()), self.__onMouse, [Sprite.MouseFiveUp]) self.accept(self.__pgItem.getPressEvent(MouseButton.wheelDown()), self.__onMouse, [Sprite.MouseScrollDown]) self.accept(self.__pgItem.getPressEvent(MouseButton.wheelUp()), self.__onMouse, [Sprite.MouseScrollUp]) self.accept(self.__pgItem.getEnterEvent(), self.__onMouse, [Sprite.MouseEnter]) self.accept(self.__pgItem.getExitEvent(), self.__onMouse, [Sprite.MouseExit]) self.accept(self.__pgItem.getWithinEvent(), self.__onMouse, [Sprite.MouseWithin]) self.accept(self.__pgItem.getWithoutEvent(), self.__onMouse, [Sprite.MouseWithout]) self.__beastDebug = ConfigVariableBool('beast-debug', False).getValue() self.__mouseInside = False self.__disabled = False #- Setup state configuration self.__lastStateOptions = None self.__state = None self.__states = { 'default': SpriteOptions(), 'hover': SpriteOptions(), 'click': SpriteOptions(), 'focus': SpriteOptions(), 'disabled': SpriteOptions(), } self.updateToState('default')
class _beastGlobals(DirectObject): enter = PGButton.getEnterPrefix() exit = PGButton.getExitPrefix() within = PGButton.getWithinPrefix() without = PGButton.getWithoutPrefix() leftPress = PGButton.getPressPrefix() + MouseButton.one().getName() + '-' centerPress = PGButton.getPressPrefix() + MouseButton.two().getName() + '-' rightPress = PGButton.getPressPrefix() + MouseButton.three().getName( ) + '-' leftClick = PGButton.getClickPrefix() + MouseButton.one().getName() + '-' centerClick = PGButton.getClickPrefix() + MouseButton.two().getName() + '-' rightClick = PGButton.getClickPrefix() + MouseButton.three().getName( ) + '-' leftRelease = PGButton.getReleasePrefix() + MouseButton.one().getName( ) + '-' centerRelease = PGButton.getReleasePrefix() + MouseButton.two().getName( ) + '-' rightRelease = PGButton.getReleasePrefix() + MouseButton.three().getName( ) + '-' wheelUp = PGButton.getPressPrefix() + MouseButton.wheelUp().getName() + '-' wheelDown = PGButton.getPressPrefix() + MouseButton.wheelDown().getName( ) + '-' def __init__(self, taskMgr, base): DirectObject.__init__(self) self.taskMgr = taskMgr self.base = base self.setupPoint2d() self.beastPointSize = ConfigVariableDouble('beast-point-size', 1.0).getValue() self.beastPointSizeAuto = ConfigVariableBool('beast-point-size-auto', False).getValue() self.beastRenderBruteForce = ConfigVariableBool( 'beast-render-brute-force', False).getValue() self.beastRenderDebug = ConfigVariableBool('beast-render-debug', False).getValue() self.setPointSize(self.beastPointSize, update=False) self.setPointSizeAuto(self.beastPointSizeAuto, update=False) self.accept('window-event', self.windowEvent) self.originalResolution = (float(self.base.win.getXSize()), float(self.base.win.getYSize())) self.buffer = None #- If bruteForce == False then we will setup beast frame rendering system if self.beastRenderBruteForce == False: self._setupTextureBuffer() taskMgr.add(self.renderTask, 'beastRender', sort=-100000000) self.windowEvent() def _setupTextureBuffer(self): if self.buffer: self.card.remove() self.camera2d.remove() self.base.graphicsEngine.removeWindow(self.buffer) self.buffer = self.base.win.makeTextureBuffer("beastBuffer", self.base.win.getXSize(), self.base.win.getYSize()) self.buffer.setActive(True) self.buffer.getTexture().setOrigFileSize(self.base.win.getXSize(), self.base.win.getYSize()) self.camera2d = self.base.makeCamera2d(self.buffer) self.camera2d.reparentTo(self.point2d.getParent()) cm = CardMaker('beastDisplay') cm.setFrame(-1, 1, -1, 1) cm.setUvRange(self.buffer.getTexture()) self.card = self.base.render2d.attachNewNode(cm.generate()) self.card.setTransparency(TransparencyAttrib.MAlpha) self.card.setTexture(self.buffer.getTexture()) def renderTask(self, task): if self.point2d.getRequiresUpdate(): debug = self.beastRenderBruteForce == False and self.beastRenderDebug == True if debug: print ':beast::render' self.buffer.setActive(True) self.point2d.setRequiresUpdate(False) else: self.buffer.setActive(False) return task.cont def setupPoint2d(self): fakeRender2d = NodePath('render2d') fakeRender2d.setDepthTest(False) fakeRender2d.setDepthWrite(False) fakeRender2d.setMaterialOff(True) fakeRender2d.setTwoSided(True) self.point2d = beastNodePath(PGTop('point2d')) self.point2d.reparentTo(fakeRender2d) self.point2d.node().setMouseWatcher(self.base.mouseWatcherNode) self.p2dTopLeft = self.point2d.attachNewNode('p2dTopLeft') self.p2dTopRight = self.point2d.attachNewNode('p2dTopRight') self.p2dTopCenter = self.point2d.attachNewNode('p2dTopCenter') self.p2dCenterLeft = self.point2d.attachNewNode('p2dCenterLeft') self.p2dCenterRight = self.point2d.attachNewNode('p2dCenterRight') self.p2dBottomLeft = self.point2d.attachNewNode('p2dBottomLeft') self.p2dBottomRight = self.point2d.attachNewNode('p2dBottomRight') self.p2dBottomCenter = self.point2d.attachNewNode('p2dBottomCenter') def getMousePos(self): md = self.win.getPointer(0) x = md.getX() y = md.getY() p = self.point2d.getRelativePoint(self.pixel2d, (x, 0, -y)) return p.getX(), p.getZ() def hasMouse(self): return self.mouseWatcherNode.hasMouse() def getAbsolutePointSize(self): fakePointSize = None if self.getPointSizeAuto(): oldX, oldY = self.originalResolution newX, newY = float(self.win.getXSize()), float(self.win.getYSize()) ratio = newY / oldY fakePointSize = ratio if self.getPointSizeAuto(): ps = fakePointSize else: ps = self.getPointSize() return ps def windowEvent(self, win=None): ps = self.getAbsolutePointSize() x = self.base.win.getXSize() y = self.base.win.getYSize() xActual = (x / ps) yActual = (y / ps) self.point2d.setScale(2.0 / xActual, 1.0, 2.0 / yActual) #- Finish actuals self.p2dTopLeft.setPos(-xActual / 2.0, 1.0, yActual / 2.0) self.p2dTopRight.setPos(xActual / 2.0, 1.0, yActual / 2.0) self.p2dTopCenter.setPos(0, 1.0, yActual / 2.0) self.p2dCenterLeft.setPos(-xActual / 2.0, 1.0, 0.0) self.p2dCenterRight.setPos(xActual / 2.0, 1.0, 0.0) self.p2dBottomLeft.setPos(-xActual / 2.0, 1.0, -yActual / 2.0) self.p2dBottomRight.setPos(xActual / 2.0, 1.0, -yActual / 2.0) self.p2dBottomCenter.setPos(0, 1.0, -yActual / 2.0) if self.beastRenderBruteForce == False: debug = self.beastRenderBruteForce == False and self.beastRenderDebug == True bx = self.buffer.getTexture().getOrigFileXSize() by = self.buffer.getTexture().getOrigFileYSize() cx = self.base.win.getXSize() cy = self.base.win.getYSize() if bx != cx or by != by: if debug: print ':beast::render::resolutionChanged from(%s, %s) to(%s, %s)' % ( bx, by, cx, cy) self._setupTextureBuffer() messenger.send('beast.windowEvent') ''' Point size ''' def getPointSize(self): return self.pointSize def setPointSize(self, value, update=True): self.pointSize = float(value) if update: self.update() ''' Point size auto resolution ''' def setPSAutoResolution(self, x, y): self.originalResolution = (float(base.win.getXSize()), float(base.win.getYSize())) def getPSAutoResolution(self): return self.originalResolution ''' Point size auto ''' def setPointSizeAuto(self, value, update=True): self.pointSizeAuto = bool(value) if update: self.update() def getPointSizeAuto(self): return self.pointSizeAuto def togglePointSizeAuto(self): if self.getPointSizeAuto(): self.setPointSizeAuto(False) else: self.setPointSizeAuto(True) ''' Data folder ''' def getDataFolder(self): if sys.platform.startswith('win'): slash = '\\' else: slash = '/' point = __file__.rindex(slash) f = __file__[:point] + slash + 'data' + slash return Filename().fromOsSpecific(f).getFullpath() ''' Destroy beast now ''' def destroy(self): self.ignoreAll()