Interactive/BackgroundCustom.py

# (C) Copyright 2012 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.

# Change the background color from the usual blue, to the specified value
# 

__all__ = ['BackgroundCustom', 'instantiate']

from MessageRegistry     import theMessageRegistry
from MessageInterpreter  import MessageInterpreter
from UserCustomization   import UserCustomBase, CustomInfo

import os

SET_BACKGROUND_COLOR_Doc = \
[(
"""
There is already a SET_DISPLAY_BACKGROUND_COLOR message
with the same syntax as this one, but this is an example
of how you would implement something like that.
"""
),
[[("r"),("Red for the background color (in 0 - 1 range)")],
 [("g"),("Green for the background color (in 0 - 1 range)")],
 [("b"),("Blue for the background color (in 0 - 1 range)")],
 ]
]

theMessageRegistry.register("SET_BACKGROUND_COLOR",
    (float,float,float,), 0, SET_BACKGROUND_COLOR_Doc)


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

class BackgroundCustom (UserCustomBase):
    def __init__(self):
        self.__myCallMenu = None
        self.__myCallIdBlack = None
        self.__myCallIdWhite = None
        self.__myCallIdDef = None
        self.__myCall800x600 = None
        self.__myInterpreter = LocalInterpreter()
        bcol = os.environ.get( 'SHOWCASE_BACKGROUND_COLOR' )

        if bcol is not None:
            bcolor = bcol.split()
            if len(bcolor) == 3:
                try:
                    r = float(bcolor[0])
                    g = float(bcolor[1])
                    b = float(bcolor[2])
                    self.__myInterpreter.Background(r, g, b)
                except:
                    print "Invalid background color %s" % (bcol)


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


    def appendMenuItems(self,id,menu):
        if "View" == id:
            menu.appendSeparator()
            self.__myCallMenu = menu
            self.__myCallIdDef = menu.appendItem(_( 'Default background' ),
                                                 self.__onBackgroundDefault )
            self.__myCallIdBlack = menu.appendItem(_( 'Black background' ),
                                                   self.__onBackgroundBlack )
            self.__myCallIdWhite = menu.appendItem(_( 'White background' ),
                                                   self.__onBackgroundWhite )
            self.__myCall800x600 = menu.appendItem(_( 'Set size to 800x600'),
                                                   self.__onSize800x600)


    def enableMenuStates(self,id,enableStates):
        if "View" == id:
            enableStates[self.__myCallIdDef] = True
            enableStates[self.__myCallIdBlack] = True
            enableStates[self.__myCallIdWhite] = True
            enableStates[self.__myCall800x600] = True

    # ------------------------------------------------
    def __onBackgroundDefault( self, event ):
        self.__myInterpreter.BackgroundDefault( event )

    def __onBackgroundBlack( self, event ):
        self.__myInterpreter.Background( 0.0, 0.0, 0.0 )

    def __onBackgroundWhite( self, event ):
        self.__myInterpreter.Background( 1.0, 1.0, 1.0 )

    def __onSize800x600( self, event ):
        self.__myInterpreter.Size( 800, 600 )


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

class LocalInterpreter( MessageInterpreter ):

    def __init__( self ):
        MessageInterpreter.__init__( self )

    def SET_BACKGROUND_COLOR( self, message ):
        display = theApplication.getDisplay()
        if display is None:
            return
        (r,g,b,) = message.data
        display.setBackgroundColor( r, g, b, 0.0 )

    def Size( self, w, h ):
        self.sendMessage( "WINDOW_SET_SIZE", (800, 600 ) )

    def Background( self, r, g, b ):
        # Change these values if you want a different background colour
        self.sendMessage( "SET_BACKGROUND_COLOR", (r, g, b) )

    def BackgroundDefault( self, event ):
        # Change these values if you want a different background colour
        self.sendMessage( "RESET_DISPLAY_BACKGROUND_COLOR", () )


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

def instantiate():
    return BackgroundCustom()


def info():
    customInfo = CustomInfo()
    customInfo.vendor = 'Autodesk'
    customInfo.version = '3.0'
    customInfo.api = '2013'
    customInfo.shortInfo = "Set or preset the window background color or size."
    customInfo.longInfo = \
"""An example add-in as to how to set the window background color, or size. Menu items that \
set the background color to white, black and default have been created in the Display menu. \
If the environment variable SHOWCASE_BACKGROUND_COLOR is set to three space separated values, \
the background will be set on startup.  Additionally, a menu item is created, also in \
the Display menu, to set the window size to 800x600.  This add-in can be edited \
to allow more choices.
"""
    return customInfo