Interactive/CorrectSyntaxCustom.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.

 
# In the context menu of Alternatives, Behaviours, Shots and Storyboard
# the item "Show syntax for Custom UI" will call CorrectSyntaxDialog.py
# to pop-up a corresponding syntax dialog to show its correct syntax.
# 

__all__ = ['SlidesParameters', 'CorrectSyntaxCustom', 'instantiate']

from DialogInterpreter      import DialogFactory
from UserCustomization      import UserCustomBase, CustomInfo
from GenericPopupMenu       import GenericPopupMenuItem
from MessageInterpreter     import MessageInterpreter

from CorrectSyntaxDialog    import CorrectSyntaxDialog


import ShotIO
import BehaviorIO
import AlternativeIO

class CorrectSyntaxCustom( UserCustomBase ):

    def __init__( self ):
        self.__myInterpreter = LocalInterpreter()
        self._mySyntaxParameters = {}

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


    # Alternative Syntax
    def __findAlternativeLabelAndId( self, item ):
        (altSetId, altId) = item
        altSets = self.__myInterpreter.document().get(AlternativeIO.id)
        for altSet in altSets:
            if altSet.name == altSetId:
                altSetLabel = altSet.label
                altLabel = altSet.getAlternative(altId).label
                return (altLabel,altId,altSetLabel,altSetId)
        return ("Unlabelled", "Unknown", "Unlabelled", "Unknown")


    def __computeAlternativeSyntax( self, item ):
        (itemLabel,itemId,itemSetLabel,itemSetId) = self.__findAlternativeLabelAndId( item )

        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyTitle] = "Alternatives"
        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyHtmlItemId] = \
            '<A href="autodesk:ALTERNATIVE_CHOICE:\'%s\',\'%s\',[]"> %%s </A>' % (itemSetId,itemId)
        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyHtmlItemLabel] = \
            '<A href="autodesk:ALTERNATIVE_CHOICE_BYLABEL:\'%s\',\'%s\'"> %%s </A>' % (itemSetLabel,itemLabel)
        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyHtmlNonItem] = ''

        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyAjaxItemId] = \
            '<A href="#" onclick="send_request(\'ALTERNATIVE_CHOICE::%s::%s::[]\');"> %%s </A>' % (itemSetId,itemId)
        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyAjaxItemLabel] = \
            '<A href="#" onclick="send_request(\'ALTERNATIVE_CHOICE_BYLABEL::%s::%s\');"> %%s </A>' % (itemSetLabel,itemLabel)
        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyAjaxNonItem] = ''

        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyScaleformItemId] = \
            'flash.external.ExternalInterface.call("ALTERNATIVE_CHOICE","(\'%s\',\'%s\')"); // %%s' % (itemSetId,itemId)
        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyScaleformItemLabel] = \
            'flash.external.ExternalInterface.call("ALTERNATIVE_CHOICE_BYLABEL","(\'%s\',\'%s\')"); // %%s' % (itemSetLabel,itemLabel)

        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyScaleformNonItem] = ''


    def __onShowDialogForParameters( self ):
        Diag = DialogFactory.instance()._get(CorrectSyntaxDialog)
        if Diag is None:
            DialogFactory.instance().show(CorrectSyntaxDialog,
                                          title=_('Custom UI Syntax for %s'),
                                          parameters = self._mySyntaxParameters)
        else:
            Diag.doUpdate(self._mySyntaxParameters)
            DialogFactory.instance().show(CorrectSyntaxDialog)   


    def __onShowAlternativesDialog(self, item):
        """
        Called when user chooses "Show syntax for Custom UI" from
        a shot context menu.
        """
        if DialogFactory.ready():
            self.__computeAlternativeSyntax( item )
            self.__onShowDialogForParameters()


    # Behavior Syntax
    def __findBehaviorLabelAndId( self, item ):
        name = item.name()
        if name is not None:
            behaviours = self.__myInterpreter.document().get(BehaviorIO.id)
            label = behaviours[name].getLabel()
            return (label,name)
        else:
            return ("Unlabelled", "Unknown")


    def __computeBehaviorSyntax( self, item ):
        (itemLabel,itemId) = self.__findBehaviorLabelAndId( item )

        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyTitle] = "Behaviors"

        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyHtmlItemId] = []
        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyHtmlItemLabel] = []
        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyHtmlNonItem] = []

        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyAjaxItemId] = []
        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyAjaxItemLabel] = []
        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyAjaxNonItem] = []

        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyScaleformItemId] = []
        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyScaleformItemLabel] = []
        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyScaleformNonItem] = []

        for method in ("PlayFromStart", "Pause", "Continue", "StopAndResetToStart"):

            self._mySyntaxParameters[CorrectSyntaxDialog.kKeyHtmlItemId].append( \
                '<A href="autodesk:BEHAVIOR_METHOD_EXECUTE:\'%s\',\'%s\'"> %%s </A>\n' % (itemId,method) )
            self._mySyntaxParameters[CorrectSyntaxDialog.kKeyHtmlItemLabel].append( \
                '<A href="autodesk:BEHAVIOR_METHOD_EXECUTE_BYLABEL:\'%s\',\'%s\'"> %%s </A>\n' % (itemLabel,method) )

            self._mySyntaxParameters[CorrectSyntaxDialog.kKeyAjaxItemId].append( \
                '<A href="#" onclick="send_request(\'BEHAVIOR_METHOD_EXECUTE::%s::%s\');"> %%s </A>\n' % (itemId, method) )
            self._mySyntaxParameters[CorrectSyntaxDialog.kKeyAjaxItemLabel].append( \
                '<A href="#" onclick="send_request(\'BEHAVIOR_METHOD_EXECUTE_BYLABEL::%s::%s\');"> %%s </A>\n' % (itemLabel, method) )

            self._mySyntaxParameters[CorrectSyntaxDialog.kKeyScaleformItemId].append( \
                'flash.external.ExternalInterface.call("BEHAVIOR_METHOD_EXECUTE","(\'%s\',\'%s\')"); // %%s' % (itemId,method) )
            self._mySyntaxParameters[CorrectSyntaxDialog.kKeyScaleformItemLabel].append( \
                'flash.external.ExternalInterface.call("BEHAVIOR_METHOD_EXECUTE_BYLABEL","(\'%s\',\'%s\')"); // %%s' % (itemLabel,method) )


    def __onShowBehaviorsDialog(self, item):
        """
        Called when user chooses "Show syntax for Custom UI" from
        a shot context menu.
        """
        if DialogFactory.ready():
            self.__computeBehaviorSyntax( item )
            self.__onShowDialogForParameters()


    # Shots Syntax
    def __findShotLabelAndId( self, item ):
        shots = self.__myInterpreter.document().get(ShotIO.id)
        for shot in shots.shots:
            if shot.getName() == item:
                return (shot.getLabel(),shot.getName())
        return ("Unlabelled", "Unknown")


    def __computeShotSyntax( self, item ):
        (itemLabel,itemId) = self.__findShotLabelAndId( item )

        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyTitle] = "Shots"
        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyHtmlItemId] = \
            '<A href="autodesk:SHOT_PLAY:\'%s\',False"> %%s </A>' % (itemId)
        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyHtmlItemLabel] = \
            '<A href="autodesk:SHOT_PLAY_BYLABEL:\'%s\',False"> %%s </A>' % (itemLabel)
        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyHtmlNonItem] = ''

        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyAjaxItemId] = \
            '<A href="#" onclick="send_request(\'SHOT_PLAY::%s::False\');"> %s </A>' % (itemId, "%s")
        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyAjaxItemLabel] = \
            '<A href="#" onclick="send_request(\'SHOT_PLAY_BYLABEL::%s::False\');"> %s </A>' % (itemLabel, "%s")
        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyAjaxNonItem] = ''

        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyScaleformItemId] = \
            'flash.external.ExternalInterface.call("SHOT_PLAY","(\'%s\',False)"); // %s' % (itemId,"%s")
        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyScaleformItemLabel] = \
            'flash.external.ExternalInterface.call("SHOT_PLAY_BYLABEL","(\'%s\',False)"); // %s' % (itemLabel,"%s")
        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyScaleformNonItem] = ''


    def __onShowShotsDialog(self, item):
        """
        Called when user chooses "Show syntax for Custom UI" from
        a shot context menu.
        """
        if DialogFactory.ready():
            self.__computeShotSyntax( item )
            self.__onShowDialogForParameters()


    # Slides Syntax
    def __findSlideLabelAndId( self, item ):
        if item.getId is not None:
            return (item.getName(),item.getId())
        else:
            return ("Unlabelled", "Unknown")


    def __computeSlideSyntax( self, item ):
        (itemLabel,itemId) = self.__findSlideLabelAndId( item )

        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyTitle] = "Slides"
        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyHtmlItemId] = \
            '<A href="autodesk:SLIDE_PLAY:(\'%s\',)"> %%s </A>' % (itemId)
        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyHtmlItemLabel] = \
            '<A href="autodesk:SLIDE_PLAY_BYLABEL:(\'%s\',)"> %%s </A>' % (itemLabel)
        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyHtmlNonItem] = \
            '<A href="autodesk:SLIDE_PLAY_FIRST:()">play the first slide</A>\n\n'+\
            '<A href="autodesk:SLIDE_PLAY_LAST:()">play the last slide</A>\n\n' +\
            '<A href="autodesk:SLIDE_PLAY_NEXT:()">play the next slide</A>\n\n' +\
            '<A href="autodesk:SLIDE_PLAY_ALL:()">play all slides</A>\n\n' +\
            '<A href="autodesk:SLIDE_PLAY_STOP:()">stop playing slides</A>\n\n' +\
            '<A href="autodesk:SLIDE_PLAY_PAUSE:()">pause playing slides</A>\n\n' +\
            '<A href="autodesk:SLIDE_PLAY_RESUME:()">resume playing paused slides</A>'

        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyAjaxItemId] = \
            '<A href="#" onclick="send_request(\'SLIDE_PLAY::%s\');"> %s </A>' % (itemId, "%s")
        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyAjaxItemLabel] = \
            '<A href="#" onclick="send_request(\'SLIDE_PLAY_BYLABEL::%s\');"> %s </A>' % (itemLabel, "%s")
        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyAjaxNonItem] = \
            '<A href="#" onclick="send_request(\'SLIDE_PLAY_FIRST\');">play the first slide</A>\n\n'+\
            '<A href="#" onclick="send_request(\'SLIDE_PLAY_LAST\');">play the last slide</A>\n\n'+\
            '<A href="#" onclick="send_request(\'SLIDE_PLAY_NEXT\');">play the next slide</A>\n\n'+\
            '<A href="#" onclick="send_request(\'SLIDE_PLAY_ALL\');">play all slides</A>\n\n'+\
            '<A href="#" onclick="send_request(\'SLIDE_PLAY_STOP\');">stops playing slides</A>\n\n'+\
            '<A href="#" onclick="send_request(\'SLIDE_PLAY_PAUSE\');">pauses playing slides</A>\n\n'+\
            '<A href="#" onclick="send_request(\'SLIDE_PLAY_RESUME\');">resumes playing paused slides</A>'

        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyScaleformItemId] = \
            'flash.external.ExternalInterface.call("SLIDE_PLAY","(\'%s\',)"); // %s' % (itemId,"%s")
        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyScaleformItemLabel] = \
            'flash.external.ExternalInterface.call("SLIDE_PLAY_BYLABEL","(\'%s\',)"); // %s' % (itemLabel,"%s")
        self._mySyntaxParameters[CorrectSyntaxDialog.kKeyScaleformNonItem] = \
            'flash.external.ExternalInterface.call("SLIDE_PLAY_FIRST","()"); // play the first slide'+\
            'flash.external.ExternalInterface.call("SLIDE_PLAY_LAST","()"); // play the last slide'+\
            'flash.external.ExternalInterface.call("SLIDE_PLAY_NEXT","()"); // play the next slide'+\
            'flash.external.ExternalInterface.call("SLIDE_PLAY_ALL","()"); // play all slides'+\
            'flash.external.ExternalInterface.call("SLIDE_PLAY_STOP","()"); // stop playing slides'+\
            'flash.external.ExternalInterface.call("SLIDE_PLAY_PAUSE","()"); // pauses playing slides'+\
            'flash.external.ExternalInterface.call("SLIDE_PLAY_RESUME","()"); // resumes playing slides'


    def __onShowSlidesDialog(self, item):
        """
        Called when user chooses "Show syntax for Custom UI" from
        a shot context menu.
        """
        if DialogFactory.ready():
            self.__computeSlideSyntax( item )
            self.__onShowDialogForParameters()


    #Context Menu
    def appendPopupMenuItems( self, id, popupMenu, item ):
        menuItem = None
        if "ShotSelector" == id:
            menuItem = GenericPopupMenuItem(_( 'Show syntax for Custom UI'),
                                            self.__onShowShotsDialog, item )
        elif "StoryboardSelector" == id:
            menuItem = GenericPopupMenuItem(_( 'Show syntax for Custom UI'),
                                            self.__onShowSlidesDialog, item )
        elif "BehaviorSelector" == id:
            menuItem = GenericPopupMenuItem(_( 'Show syntax for Custom UI'),
                                            self.__onShowBehaviorsDialog, item )
        elif "AlternativeSelector" == id:
            menuItem = GenericPopupMenuItem(_( 'Show syntax for Custom UI'),
                                            self.__onShowAlternativesDialog, item )

        else:
            print "Unrecognized id %s" % id

        if menuItem is not None:
            popupMenu.append( menuItem )


class LocalInterpreter( MessageInterpreter ):

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

    def SET_DOCUMENT( self, message ):
        (self.__myDocument,) = message.data

    def APPLICATION_CLOSE_SCENE( self, message ):
        self.__myDocument = None

    def document(self):
        return self.__myDocument


def instantiate():
        return CorrectSyntaxCustom()


def info():
    customInfo = CustomInfo()
    customInfo.vendor = 'Autodesk'
    customInfo.version = '1.0'
    customInfo.api = '2013'
    customInfo.shortInfo = "Pop up a dialog to show the correct syntax."
    customInfo.longInfo = \
"""In the context menu of Alternatives, Behaviours, Shots and Storyboard, the item "Show syntax \
for Custom UI" will pop up a corresponding syntax dialog to show its correct syntax.
"""
    return customInfo