Interactive/NudgeCameraCustom.py

##
## (C) Copyright 2008 Autodesk, Inc.  All rights reserved.
##
## Use of this software is subject to the terms of the Autodesk license
## agreement provided at the time of installation or download, or which
## otherwise accompanies this software in either electronic or hard copy
## form.
##

#           module = __import__(custom)
#           instance = module.instantiate()
#           self.__myCustoms.append(instance)

__all__ = ['NudgeCameraCustom', 'instantiate']

from ActionRegistry     import theActionRegistry
from MessageInterpreter import MessageInterpreter
from RTFapi             import Event
from UserCustomization  import UserCustomBase, CustomInfo
from MessageRegistry    import theMessageRegistry
from DisplayUtilities   import GetCameras

NUDGE_CAMERA_Doc=\
[(
"""
Shows Nudge Camera Left by 0.1 or Nudge Camera Right by 0.1.
"""
),
[[("direction"),("left or right")]
 ]
]
theMessageRegistry.register("NUDGE_CAMERA",
    (unicode,), 0, NUDGE_CAMERA_Doc)

class NudgeCameraCustom (UserCustomBase):
    def __init__(self):
        self.__myNudgeMenu = None
        self.__myNudgeLeftId = None
        self.__myNudgeRightId = None
        self.__myInterpreter = NudgeInterpreter()

    def getInterpreter(self,isInteractive):
        if isInteractive:
            return self.__myInterpreter
        return None

    def appendMenuItems(self,id,menu):
        if "View" == id:
            self.__myNudgeMenu = menu
            self.__myNudgeLeftId = menu.appendItem(_( 'Nudge Camera Left by 0.1\tQ' ),
                                                   self.__onNudgeCameraLeft )

            self.__myNudgeRightId = menu.appendItem(_( 'Nudge Camera Right by 0.1\tW' ),
                                                 self.__onNudgeCameraRight )

            # While at it, add the hotkey...
            theActionRegistry.register( 'Global',
                                        Event.BUTTON_q, (), True,
                                        Event.BUTTONUP,
                                        'NudgeCameraLeft' )
            theActionRegistry.register( 'Spectating',
                                        Event.BUTTON_q, (), True,
                                        Event.BUTTONUP,
                                        'NudgeCameraLeft' )

            theActionRegistry.register( 'Global',
                                        Event.BUTTON_w, (), True,
                                        Event.BUTTONUP,
                                        'NudgeCameraRight' )
            theActionRegistry.register( 'Spectating',
                                        Event.BUTTON_w, (), True,
                                        Event.BUTTONUP,
                                        'NudgeCameraRight' )

    def enableMenuStates(self,id,enableStates):
        if "View" == id:
            enableStates[self.__myNudgeLeftId] = True
            enableStates[self.__myNudgeRightId] = True

    # ------------------------------------------------
    def __onNudgeCameraLeft( self, event ):
        self.__myInterpreter.NudgeCameraLeft( None )

    def __onNudgeCameraRight( self, event ):
        self.__myInterpreter.NudgeCameraRight( None )


# -----------------------------------------------------------------------

class NudgeInterpreter( MessageInterpreter ):

    def __init__( self ):
        MessageInterpreter.__init__( self )
        self.__myDisplay = None

    def NudgeCameraLeft( self, event ):
        # This is the last navigation state we have
        if self.__myDisplay:
            for camera in GetCameras( self.__myDisplay ):
                ( x, y ) = camera.getWindowOffset()
                camera.setWindowOffset( x-0.1, y )

    def NudgeCameraRight( self, event ):
        # This is the last navigation state we have
        if self.__myDisplay:
            for camera in GetCameras( self.__myDisplay ):
                ( x, y ) = camera.getWindowOffset()
                camera.setWindowOffset( x+0.1, y )

    def SET_DISPLAY( self, message ):
        ( self.__myDisplay, ) = message.data

    def NUDGE_CAMERA( self, message ):
        ( direction, ) = message.data
        if direction=='Left':
            self.NudgeCameraLeft(None)
        else:
            self.NudgeCameraRight(None)


# --------------------------------------------------------------------

def instantiate():
    # Are the relevant files elsewhere?
    # sys.path.append( "where the files are" )
    return NudgeCameraCustom()


def info():
    customInfo = CustomInfo()
    customInfo.vendor = 'Autodesk'
    customInfo.version = '3.0'
    customInfo.api = '2010'
    customInfo.shortInfo = "Nudge the camera left or right."
    customInfo.longInfo = \
"""Add two menu items under "View" menu to nudge the camera left or right.
"""
    return customInfo