Example #1
0
def loadVideo(videoFileName, loop=False):
    videoPathStr = 'Video/{}'
    videoPathStr = videoPathStr.format(videoFileName)

    try:
        tex = MovieTexture(videoFileName)
        success = tex.read(videoPathStr)
        assert success, "Failed to load video!"

        # Set up a fullscreen card to set the video texture on.
        cm = CardMaker("My Fullscreen Card")
        cm.setFrameFullscreenQuad()
        # Tell the CardMaker to create texture coordinates that take into
        # account the padding region of the texture.
        cm.setUvRange(tex)
        # Now place the card in the scene graph and apply the texture to it.
        card = render2d.attachNewNode(cm.generate())
        card.setTexture(tex)
        card.hide()
        sound = loader.loadMusic(videoPathStr)
        # set loop false
        sound.setLoop(loop)
        # Synchronize the video to the sound.
        tex.synchronizeTo(sound)

        return sound, card
    except Exception as e:
        #logging.debug("loadvideo: {}".format(traceback.format_exc()))
        pass
    return sound, card
class DistributedCinemaInterior(DistributedToonInterior.DistributedToonInterior
                                ):
    notify = directNotify.newCategory("DistributedCinemaInterior")

    def __init__(self, cr):
        DistributedToonInterior.DistributedToonInterior.__init__(self, cr)
        self.fsm = ClassicFSM.ClassicFSM('DCinemaInterior', [
            State.State('off', self.enterOff, self.exitOff),
            State.State('show', self.enterShow, self.exitShow),
            State.State('intermission', self.enterIntermission,
                        self.exitIntermission)
        ], 'off', 'off')
        self.fsm.enterInitialState()
        self.state = None
        self.cinemaIndex = None
        self.movieTex = None
        self.movieCard = None
        self.movieSound = None
        self.movieTrack = None
        self.intermissionText = None

    def makeInterior(self):
        # Always use the same room for cinemas.
        DistributedToonInterior.DistributedToonInterior.makeInterior(
            self, roomIndex=0)

    def announceGenerate(self):
        DistributedToonInterior.DistributedToonInterior.announceGenerate(self)
        self.sendUpdate('requestStateAndTimestamp')

    def disable(self):
        self.fsm.requestFinalState()
        self.fsm = None
        self.state = None
        self.cinemaIndex = None
        self.movieTex = None
        self.movieCard = None
        self.movieSound = None
        self.movieTrack = None
        self.intermissionText = None
        self.cr.playGame.hood.loader.interiorMusic.stop()
        DistributedToonInterior.DistributedToonInterior.disable(self)

    def darkenInterior(self):
        darkenIval = self.interior.colorScaleInterval(
            3.0,
            colorScale=(0.3, 0.3, 0.3, 1.0),
            startColorScale=(1.0, 1.0, 1.0, 1.0),
            blendType='easeInOut')
        darkenIval.start()

    def lightenInterior(self):
        lightenIval = self.interior.colorScaleInterval(
            3.0,
            colorScale=(1, 1, 1, 1.0),
            startColorScale=(0.3, 0.3, 0.3, 1.0),
            blendType='easeInOut')
        lightenIval.start()

    def enterShow(self, ts=0):
        self.darkenInterior()
        self.cr.playGame.hood.loader.interiorMusic.stop()

        videoFile = CinemaGlobals.Cinemas[self.cinemaIndex][0]
        audioFile = CinemaGlobals.Cinemas[self.cinemaIndex][1]

        self.movieTex = MovieTexture(self.uniqueName("movieTex"))
        self.movieTex.read(videoFile)
        card = CardMaker(self.uniqueName('movieCard'))
        card.setFrame(-1.5, 1.5, -1, 1)
        self.movieCard = NodePath(card.generate())
        self.movieCard.reparentTo(render)
        self.movieCard.setPos(
            self.interior.find('**/sign_origin;+s').getPos(render))
        #self.movieCard.setX(self.movieCard, -0.05)
        self.movieCard.setHpr(
            self.interior.find('**/sign_origin;+s').getHpr(render))
        self.movieCard.setDepthWrite(1, 1)
        self.movieCard.setTwoSided(True)
        self.movieCard.setTexture(self.movieTex)
        self.movieCard.setTexScale(TextureStage.getDefault(),
                                   self.movieTex.getTexScale())
        self.movieCard.setScale(2.5)
        self.movieSound = base.loadSfx(audioFile)
        self.movieTex.synchronizeTo(self.movieSound)
        self.movieTrack = SoundInterval(self.movieSound,
                                        name=self.uniqueName('movieTrack'))
        self.movieTrack.setDoneEvent(self.movieTrack.getName())
        self.acceptOnce(self.movieTrack.getDoneEvent(), self.fsm.request,
                        ['off'])
        self.movieTrack.start(ts)

    def exitShow(self):
        self.ignore(self.movieTrack.getDoneEvent())
        self.movieTrack.finish()
        self.movieTrack = None
        self.movieSound = None
        self.movieCard.removeNode()
        self.movieCard = None
        self.movieTex = None

        self.lightenInterior()
        self.cr.playGame.hood.loader.interiorMusic.play()

    def enterIntermission(self, ts=0):
        self.intermissionText = DirectLabel(
            relief=None,
            text_decal=True,
            text="",
            scale=0.7,
            parent=self.interior.find('**/sign_origin;+s'),
            text_font=CIGlobals.getMickeyFont(),
            text_fg=(1, 0.9, 0, 1))
        self.movieTrack = Sequence(name=self.uniqueName('intermissionTrack'))
        for second in range(CinemaGlobals.IntermissionLength + 1):
            timeRemaining = CinemaGlobals.IntermissionLength - second
            self.movieTrack.append(
                Func(self.setIntermissionText,
                     "Next show in:\n%d" % timeRemaining))
            self.movieTrack.append(Wait(1.0))
        self.movieTrack.setDoneEvent(self.movieTrack.getName())
        self.acceptOnce(self.movieTrack.getDoneEvent(), self.fsm.request,
                        ['off'])
        self.movieTrack.start(ts)

    def setIntermissionText(self, text):
        self.intermissionText['text'] = text

    def exitIntermission(self):
        self.ignore(self.movieTrack.getDoneEvent())
        self.movieTrack.finish()
        self.movieTrack = None
        self.intermissionText.destroy()
        self.intermissionText = None

    def enterOff(self):
        pass

    def exitOff(self):
        pass

    def setCinemaIndex(self, index):
        self.cinemaIndex = index

    def getCinemaIndex(self):
        return self.cinemaIndex

    def setState(self, state, timestamp):
        ts = globalClockDelta.localElapsedTime(timestamp)
        self.state = state
        self.fsm.request(state, [ts])

    def getState(self):
        return self.state
class Tutorial:
    def __init__(self, pat):
        self.t = Transitions(loader)
        self.pickAToon = pat

    def askTutorial(self):
        self.firstTimeMsg = YesNoDialog(text=CIGlobals.FirstTimeMsg,
                                        text_scale=0.07,
                                        text_wordwrap=18,
                                        buttonGeomList=[
                                            CIGlobals.getOkayBtnGeom(),
                                            CIGlobals.getCancelBtnGeom()
                                        ],
                                        button_relief=None,
                                        button_text_pos=(0, -0.1),
                                        command=self.handleTutorialDecision,
                                        image_color=CIGlobals.DialogColor,
                                        fadeScreen=1)

    def handleTutorialDecision(self, value):
        if value:
            self.firstTimeMsg.destroy()
            self.startTutorial()
            base.hoodBGM.stop()
        else:
            self.firstTimeMsg.destroy()
            self.enablePatButtons()

    def enablePatButtons(self):
        for btn in self.pickAToon.btnList:
            btn['state'] = DGG.NORMAL
        self.pickAToon.quit_btn['state'] = DGG.NORMAL

    def startTutorial(self):
        self.t.fadeOut(1)
        Sequence(Wait(1.2), Func(self.playVideo)).start()

    def playVideo(self):
        self.t.fadeIn(0)
        self.pickAToon.removeGui()
        self.movieTex = MovieTexture("tutorial")
        assert self.movieTex.read(
            "tutorial.avi"), "Failed to load tutorial video"

        cm = CardMaker("tutorialCard")
        cm.setFrameFullscreenQuad()
        self.card = NodePath(cm.generate())
        self.card.reparentTo(render2d)
        self.card.setTexture(self.movieTex)
        self.card.setTexScale(TextureStage.getDefault(),
                              self.movieTex.getTexScale())
        self.movieSound = loader.loadSfx("tutorial.avi")
        self.movieTex.synchronizeTo(self.movieSound)

        self.movieSound.play()
        taskMgr.add(self.checkMovieStatus, "checkMovieStatus")

    def checkMovieStatus(self, task):
        if self.movieSound.status() == AudioSound.READY:
            self.stopVideo()
            return task.done
        return task.cont

    def stopVideo(self):
        self.movieSound.stop()
        self.card.removeNode()
        self.t.fadeOut(0)
        self.pickAToon.createGui(1)
        Sequence(Wait(0.2), Func(self.t.fadeIn, 1)).start()
class DistributedCinemaInterior(DistributedToonInterior.DistributedToonInterior):
    notify = directNotify.newCategory('DistributedCinemaInterior')

    def __init__(self, cr):
        DistributedToonInterior.DistributedToonInterior.__init__(self, cr)
        self.fsm = ClassicFSM.ClassicFSM('DCinemaInterior', [State.State('off', self.enterOff, self.exitOff), State.State('show', self.enterShow, self.exitShow), State.State('intermission', self.enterIntermission, self.exitIntermission)], 'off', 'off')
        self.fsm.enterInitialState()
        self.state = None
        self.cinemaIndex = None
        self.movieTex = None
        self.movieCard = None
        self.movieSound = None
        self.movieTrack = None
        self.intermissionText = None
        return

    def makeInterior(self):
        DistributedToonInterior.DistributedToonInterior.makeInterior(self, roomIndex=0)

    def announceGenerate(self):
        DistributedToonInterior.DistributedToonInterior.announceGenerate(self)
        self.sendUpdate('requestStateAndTimestamp')

    def disable(self):
        self.fsm.requestFinalState()
        self.fsm = None
        self.state = None
        self.cinemaIndex = None
        self.movieTex = None
        self.movieCard = None
        self.movieSound = None
        self.movieTrack = None
        self.intermissionText = None
        self.cr.playGame.hood.loader.interiorMusic.stop()
        DistributedToonInterior.DistributedToonInterior.disable(self)
        return

    def darkenInterior(self):
        darkenIval = self.interior.colorScaleInterval(3.0, colorScale=(0.3, 0.3, 0.3, 1.0), startColorScale=(1.0, 1.0, 1.0, 1.0), blendType='easeInOut')
        darkenIval.start()

    def lightenInterior(self):
        lightenIval = self.interior.colorScaleInterval(3.0, colorScale=(1, 1, 1, 1.0), startColorScale=(0.3, 0.3, 0.3, 1.0), blendType='easeInOut')
        lightenIval.start()

    def enterShow(self, ts = 0):
        self.darkenInterior()
        self.cr.playGame.hood.loader.interiorMusic.stop()
        videoFile = CinemaGlobals.Cinemas[self.cinemaIndex][0]
        audioFile = CinemaGlobals.Cinemas[self.cinemaIndex][1]
        self.movieTex = MovieTexture(self.uniqueName('movieTex'))
        self.movieTex.read(videoFile)
        card = CardMaker(self.uniqueName('movieCard'))
        card.setFrame(-1.5, 1.5, -1, 1)
        self.movieCard = NodePath(card.generate())
        self.movieCard.reparentTo(render)
        self.movieCard.setPos(self.interior.find('**/sign_origin;+s').getPos(render))
        self.movieCard.setHpr(self.interior.find('**/sign_origin;+s').getHpr(render))
        self.movieCard.setDepthWrite(1, 1)
        self.movieCard.setTwoSided(True)
        self.movieCard.setTexture(self.movieTex)
        self.movieCard.setTexScale(TextureStage.getDefault(), self.movieTex.getTexScale())
        self.movieCard.setScale(2.5)
        self.movieSound = base.loadSfx(audioFile)
        self.movieTex.synchronizeTo(self.movieSound)
        self.movieTrack = SoundInterval(self.movieSound, name=self.uniqueName('movieTrack'))
        self.movieTrack.setDoneEvent(self.movieTrack.getName())
        self.acceptOnce(self.movieTrack.getDoneEvent(), self.fsm.request, ['off'])
        self.movieTrack.start(ts)

    def exitShow(self):
        self.ignore(self.movieTrack.getDoneEvent())
        self.movieTrack.finish()
        self.movieTrack = None
        self.movieSound = None
        self.movieCard.removeNode()
        self.movieCard = None
        self.movieTex = None
        self.lightenInterior()
        self.cr.playGame.hood.loader.interiorMusic.play()
        return

    def enterIntermission(self, ts = 0):
        self.intermissionText = DirectLabel(relief=None, text_decal=True, text='', scale=0.7, parent=self.interior.find('**/sign_origin;+s'), text_font=CIGlobals.getMickeyFont(), text_fg=(1, 0.9, 0, 1))
        self.movieTrack = Sequence(name=self.uniqueName('intermissionTrack'))
        for second in range(CinemaGlobals.IntermissionLength + 1):
            timeRemaining = CinemaGlobals.IntermissionLength - second
            self.movieTrack.append(Func(self.setIntermissionText, 'Next show in:\n%d' % timeRemaining))
            self.movieTrack.append(Wait(1.0))

        self.movieTrack.setDoneEvent(self.movieTrack.getName())
        self.acceptOnce(self.movieTrack.getDoneEvent(), self.fsm.request, ['off'])
        self.movieTrack.start(ts)
        return

    def setIntermissionText(self, text):
        self.intermissionText['text'] = text

    def exitIntermission(self):
        self.ignore(self.movieTrack.getDoneEvent())
        self.movieTrack.finish()
        self.movieTrack = None
        self.intermissionText.destroy()
        self.intermissionText = None
        return

    def enterOff(self):
        pass

    def exitOff(self):
        pass

    def setCinemaIndex(self, index):
        self.cinemaIndex = index

    def getCinemaIndex(self):
        return self.cinemaIndex

    def setState(self, state, timestamp):
        ts = globalClockDelta.localElapsedTime(timestamp)
        self.state = state
        self.fsm.request(state, [ts])

    def getState(self):
        return self.state