mouseInteractor.py :  » Game-2D-3D » PyOpenGL » PyOpenGL-Demo-3.0.1b1 » PyOpenGL-Demo » proesch » interactionMatrix » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Game 2D 3D » PyOpenGL 
PyOpenGL » PyOpenGL Demo 3.0.1b1 » PyOpenGL Demo » proesch » interactionMatrix » mouseInteractor.py
# helper class for mouse interaction
# 
# Copyright (C) 2007  "Peter Roesch" <Peter.Roesch@fh-augsburg.de>
#
# This code is licensed under the PyOpenGL License.
# Details are given in the file license.txt included in this distribution.

import sys
import math

from interactionMatrix import InteractionMatrix

try:
  from OpenGL.GLUT import *
  from OpenGL.GL import *
  from OpenGL.GLU import *
except:
  print ''' Error: PyOpenGL not installed properly !!'''
  sys.exit(  )

class MouseInteractor ( object ):
  """Connection between mouse motion and transformation matrix"""
  def __init__( self, translationScale=0.1, rotationScale=.2):
    self.scalingFactorRotation = rotationScale
    self.scalingFactorTranslation = translationScale
    self.rotationMatrix = InteractionMatrix( )
    self.translationMatrix = InteractionMatrix( )
    self.mouseButtonPressed = None
    self.oldMousePos = [ 0, 0 ]

  def mouseButton( self, button, mode, x, y ):
    """Callback function for mouse button."""
    if mode == GLUT_DOWN:
      self.mouseButtonPressed = button
    else:
      self.mouseButtonPressed = None
    self.oldMousePos[0], self.oldMousePos[1] = x, y
    glutPostRedisplay( )

  def mouseMotion( self, x, y ):
    """Callback function for mouse motion.

    Depending on the button pressed, the displacement of the
    mouse pointer is either converted into a translation vector
    or a rotation matrix."""

    deltaX = x - self.oldMousePos[ 0 ]
    deltaY = y - self.oldMousePos[ 1 ]
    if self.mouseButtonPressed == GLUT_RIGHT_BUTTON:
      tX = deltaX * self.scalingFactorTranslation
      tY = deltaY * self.scalingFactorTranslation
      self.translationMatrix.addTranslation(tX, -tY, 0)
    elif self.mouseButtonPressed == GLUT_LEFT_BUTTON:
      rY = deltaX * self.scalingFactorRotation
      self.rotationMatrix.addRotation(rY, 0, 1, 0)
      rX = deltaY * self.scalingFactorRotation
      self.rotationMatrix.addRotation(rX, 1, 0, 0)
    else:
      tZ = deltaY * self.scalingFactorTranslation
      self.translationMatrix.addTranslation(0, 0, tZ)
    self.oldMousePos[0], self.oldMousePos[1] = x, y
    glutPostRedisplay( )

  def applyTransformation( self ):
    """Concatenation of the current translation and rotation
    matrices with the current OpenGL transformation matrix"""

    glMultMatrixf( self.translationMatrix.getCurrentMatrix() )
    glMultMatrixf( self.rotationMatrix.getCurrentMatrix() )

  def registerCallbacks( self ):
    """Initialise glut callback functions."""
    glutMouseFunc( self.mouseButton )
    glutMotionFunc( self.mouseMotion )
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.