Advertisement
dragonbane

[CryVideo] FG Node

Feb 24th, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.51 KB | None | 0 0
  1. FlowCryVideoPlayer.cpp
  2.  
  3. #include "StdAfx.h"
  4. #include "Nodes/G2FlowBaseNode.h"
  5. #include "HUD/CryVideo/VideoManager.h"
  6. #include "Game.h"
  7. #include "CryVideo.h"
  8. #include "IRenderer.h"
  9. #include "UIDraw/UIDraw.h"
  10.  
  11. class CFlowCryVideoPlayer : public CFlowBaseNode<eNCT_Instanced>, public IGameFrameworkListener
  12. {
  13. private:
  14.     SActivationInfo m_ActInfo;
  15.     IRenderer* m_pRenderer;
  16.     CVideoManager* m_pVideoManager;
  17.     bool m_bEnabled;
  18.     bool m_bPaused;
  19.     bool m_bLooped;
  20.  
  21. public:
  22.     CFlowCryVideoPlayer( SActivationInfo * pActInfo )
  23.     {
  24.         m_pRenderer = gEnv->pRenderer;
  25.         m_pVideoManager = NULL;
  26.         m_bEnabled = false;
  27.         m_bPaused = false;
  28.         m_bLooped = false;
  29.     }
  30.  
  31.     ~CFlowCryVideoPlayer()
  32.     {
  33.         if (g_pCryVideo && g_pCryVideo->m_pFramework)
  34.             g_pCryVideo->m_pFramework->UnregisterListener(this);
  35.  
  36.         SAFE_DELETE(m_pVideoManager);
  37.     }
  38.  
  39.     void Serialize(SActivationInfo* pActInfo, TSerialize ser)
  40.     {
  41.         ser.Value("m_bEnabled", m_bEnabled);
  42.         ser.Value("m_bPaused", m_bPaused);
  43.         ser.Value("m_bLooped", m_bLooped);
  44.    
  45.         if (ser.IsReading())
  46.         {
  47.             m_ActInfo = *pActInfo;
  48.             //Listener
  49.             if (g_pCryVideo && g_pCryVideo->m_pFramework && m_bEnabled && !m_bPaused && m_pVideoManager->IsPlaying() && !m_pVideoManager->IsPaused())
  50.                 g_pCryVideo->m_pFramework->RegisterListener(this, "FlowNode_CryVideoPlayer", eFLPriority_Default);
  51.         }
  52.     }
  53.  
  54.     enum EInputPorts
  55.     {
  56.         EIP_Play = 0,
  57.         EIP_Stop,
  58.         EIP_Pause,
  59.         EIP_Resume,
  60.         EIP_Slot,
  61.         EIP_SubMtlId,
  62.         EIP_TexSlot,
  63.         EIP_VideoName,
  64.         EIP_AudioVolume,
  65.         EIP_Looped,
  66.         EIP_KeepLastFrame,
  67.     };
  68.  
  69.     enum EOutputPorts
  70.     {
  71.         EOP_OnPlay = 0,
  72.         EOP_OnStop,
  73.         EOP_OnPause,
  74.         EOP_OnResume,
  75.         EOP_OnVideoNotFound,
  76.     };
  77.  
  78.     virtual void GetConfiguration(SFlowNodeConfig& config)
  79.     {
  80.         static const SInputPortConfig inputs[] = {
  81.             InputPortConfig_Void    ("Play", _HELP("Start playback")),
  82.             InputPortConfig_Void    ("Stop", _HELP("Stop playback")),
  83.             InputPortConfig_Void    ("Pause", _HELP("Pause playback")),
  84.             InputPortConfig_Void    ("Resume", _HELP("Resume playback")),
  85.             InputPortConfig<int>    ("Slot", 0,  _HELP("Material Slot (e.g. to use CryVideoPlayer on dynamic textures)")),
  86.             InputPortConfig<int>    ("SubMtlId", 0,  _HELP("Sub Material Id (e.g. to use CryVideoPlayer on dynamic textures)")),
  87.             InputPortConfig<int>    ("TexSlot", 0,  _HELP("Texture Slot (e.g. to use CryVideoPlayer on dynamic textures)")),
  88.             InputPortConfig<string> ("VideoName", _HELP("Name of video to play"), 0, _UICONFIG("enum_global:video")), //Patch 0.9.8: Changed
  89.             InputPortConfig<float>    ("Volume", 1.0f,  _HELP("Volume (dynamic)")),
  90.             InputPortConfig<bool>    ("Loop", false,  _HELP("Video is looped")),
  91.             InputPortConfig<bool>    ("KeepLastFrame", false,  _HELP("The video stops at the last frame. Can be used together with loop to create a seemless looping video")),
  92.             {0}
  93.         };
  94.  
  95.         static const SOutputPortConfig outputs[] =
  96.         {
  97.             OutputPortConfig_Void  ("OnPlay",   _HELP("Triggered once the video started")),
  98.             OutputPortConfig<bool> ("OnStop",   _HELP("Triggered once the video stopped. True if the video was finished, false if skipped")),
  99.             OutputPortConfig_Void  ("OnPause",  _HELP("Triggered once the video is paused")),
  100.             OutputPortConfig_Void  ("OnResume", _HELP("Triggered once the video resumes")),
  101.             OutputPortConfig_Void  ("OnVideoNotFound",  _HELP("Triggered on Video was not found")),
  102.             {0}
  103.         };
  104.  
  105.         config.pInputPorts = inputs;
  106.         config.pOutputPorts = outputs;
  107.         config.sDescription = _HELP( "CryVideo player node" );
  108.         config.nFlags |= EFLN_TARGET_ENTITY;
  109.         config.SetCategory(EFLN_APPROVED);
  110.     }
  111.  
  112.     virtual void ProcessEvent( EFlowEvent event, SActivationInfo *pActInfo )
  113.     {
  114.         switch (event)
  115.         {
  116.             case eFE_Initialize:
  117.             {
  118.                 m_ActInfo = *pActInfo;
  119.                 m_pRenderer = gEnv->pRenderer;
  120.  
  121.                 if (m_pVideoManager == NULL)
  122.                     m_pVideoManager = new CVideoManager();
  123.             }
  124.             break;
  125.  
  126.             case eFE_Activate:
  127.             {
  128.                 if (IsPortActive(pActInfo, EIP_Play) && false == m_bEnabled && false == m_bPaused)
  129.                 {
  130.                     IUIDraw *pDraw = NULL;
  131.                     if (g_pCryVideo)
  132.                         if (IGameFramework *pFW = g_pCryVideo->m_pFramework)
  133.                             pDraw = pFW->GetIUIDraw();
  134.                     if (NULL == pDraw) return;
  135.  
  136.                     string videoName = GetPortString(pActInfo, EIP_VideoName);
  137.                     float volume = GetPortFloat(pActInfo, EIP_AudioVolume);
  138.  
  139.                     //0.9.6: Loop and KeepLastFrame option added
  140.                     m_bLooped = GetPortBool(pActInfo, EIP_Looped);
  141.                     bool keepLastFrame = GetPortBool(pActInfo, EIP_KeepLastFrame);
  142.  
  143.                     int slot = GetPortInt(pActInfo, EIP_Slot);
  144.                     int subMtlId = GetPortInt(pActInfo, EIP_SubMtlId);
  145.                     int texSlot = GetPortInt(pActInfo, EIP_TexSlot);
  146.        
  147.                     if (!videoName || videoName == "")
  148.                     {  
  149.                         ActivateOutput(pActInfo, EOP_OnVideoNotFound, true);
  150.                         return;
  151.                     }
  152.  
  153.                     IEntity* pEntity = pActInfo->pEntity;
  154.                     if(pEntity)
  155.                     {
  156.                         bool success = m_pVideoManager->PlaySingleVideo(videoName, true, "", 1.0f, pEntity, slot, subMtlId, texSlot, false, keepLastFrame);
  157.                         if (success)
  158.                         {
  159.                             m_bEnabled = true;
  160.                             if (g_pCryVideo && g_pCryVideo->m_pFramework)
  161.                                 g_pCryVideo->m_pFramework->RegisterListener(this, "FlowNode_CryVideoPlayer", eFLPriority_Default);
  162.  
  163.                             ActivateOutput(pActInfo, EOP_OnPlay, true);
  164.                             pActInfo->pGraph->SetRegularlyUpdated(pActInfo->myID, true);
  165.                         }
  166.                         else
  167.                         {
  168.                             ActivateOutput(pActInfo, EOP_OnVideoNotFound, true);
  169.                         }
  170.                     }
  171.                 }
  172.                 if (IsPortActive(pActInfo, EIP_Stop) && true == m_bEnabled)
  173.                 {
  174.                     m_bEnabled = false;
  175.                     m_bPaused = false;
  176.                     if (g_pCryVideo && g_pCryVideo->m_pFramework)
  177.                         g_pCryVideo->m_pFramework->UnregisterListener(this);
  178.  
  179.                     pActInfo->pGraph->SetRegularlyUpdated(pActInfo->myID, false);
  180.  
  181.                     if (!m_pVideoManager->IsPlaying())
  182.                     {
  183.                         ActivateOutput(pActInfo, EOP_OnStop, false);
  184.                     }
  185.                     else
  186.                     {
  187.                         m_pVideoManager->StopVideo();
  188.                         ActivateOutput(pActInfo, EOP_OnStop, false);
  189.                     }
  190.                 }
  191.  
  192.                 if (IsPortActive(pActInfo, EIP_Pause) && true == m_bEnabled && false == m_bPaused)
  193.                 {
  194.                     m_bPaused = true;
  195.                     if (g_pCryVideo && g_pCryVideo->m_pFramework)
  196.                         g_pCryVideo->m_pFramework->UnregisterListener(this);
  197.  
  198.                     pActInfo->pGraph->SetRegularlyUpdated(pActInfo->myID, false);
  199.  
  200.                     if (m_pVideoManager->IsPlaying() && !m_pVideoManager->IsPaused())
  201.                     {
  202.                         m_pVideoManager->PauseVideo();
  203.                         ActivateOutput(pActInfo, EOP_OnPause, true);
  204.                     }
  205.                 }
  206.  
  207.                 if (IsPortActive(pActInfo, EIP_Resume) && true == m_bEnabled && true == m_bPaused)
  208.                 {
  209.                     m_bPaused = false;
  210.                    
  211.                     if (m_pVideoManager->IsPlaying() && m_pVideoManager->IsPaused())
  212.                     {
  213.                         if (g_pCryVideo && g_pCryVideo->m_pFramework)
  214.                             g_pCryVideo->m_pFramework->RegisterListener(this, "FlowNode_CryVideoPlayer", eFLPriority_Default);
  215.  
  216.                         pActInfo->pGraph->SetRegularlyUpdated(pActInfo->myID, true);
  217.  
  218.                         m_pVideoManager->ResumeVideo();
  219.                         ActivateOutput(pActInfo, EOP_OnResume, true);
  220.                     }
  221.                 }
  222.             }
  223.             break;
  224.  
  225.             case eFE_Update:
  226.             {
  227.                 if (m_bEnabled && !m_bPaused)
  228.                 {
  229.                     if (m_pVideoManager->IsPlaying() && !m_pVideoManager->IsPaused())
  230.                     {
  231.                         if (m_pVideoManager->pIngameVideoTargetEntity && m_pVideoManager->pIngameVideoTargetEntity->GetProxy(ENTITY_PROXY_SOUND) && m_pVideoManager->pSoundId)
  232.                         {
  233.                             IEntitySoundProxy *pSoundProxy = (IEntitySoundProxy *)m_pVideoManager->pIngameVideoTargetEntity->GetProxy(ENTITY_PROXY_SOUND);
  234.                             if (pSoundProxy)
  235.                             {
  236.                                 ISound *pSound = pSoundProxy->GetSound(m_pVideoManager->pSoundId);
  237.                                 if (pSound)
  238.                                 {
  239.                                     float newVolume = GetPortFloat(pActInfo, EIP_AudioVolume);
  240.                                     pSound->GetInterfaceExtended()->SetVolume(newVolume);
  241.                                 }
  242.                             }
  243.                         }
  244.                     }
  245.                 }
  246.             }
  247.             break;
  248.         }
  249.     }
  250.  
  251.     IFlowNodePtr Clone( SActivationInfo * pActInfo )
  252.     {
  253.         return new CFlowCryVideoPlayer(pActInfo);
  254.     }
  255.  
  256.     virtual void GetMemoryUsage(ICrySizer * s) const
  257.     {
  258.         s->Add(*this);
  259.     }
  260.  
  261.     ////////////////////////////////////////////////////
  262.     // ~IGameFrameworkListener
  263.     virtual void OnSaveGame(ISaveGame* pSaveGame) {}
  264.     virtual void OnLoadGame(ILoadGame* pLoadGame) {}
  265.     virtual void OnLevelEnd(const char* nextLevel) {}
  266.     virtual void OnActionEvent(const SActionEvent& event) {}
  267.     virtual void OnPostUpdate(float fDeltaTime)
  268.     {
  269.         if (false == m_bEnabled || true == m_bPaused) return;
  270.  
  271.         IUIDraw *pDraw;
  272.         if (g_pCryVideo)
  273.             if (IGameFramework *pFW = g_pCryVideo->m_pFramework)
  274.                 pDraw = pFW->GetIUIDraw();
  275.         if (!pDraw) return;
  276.         if (!m_pVideoManager) return;
  277.  
  278.         if (!m_pVideoManager->IsPlaying() && !m_pVideoManager->IsPaused())
  279.         {
  280.             if (m_pVideoManager->lastVideoFinishedReason == "Finished")
  281.             {
  282.                 ActivateOutput(&m_ActInfo, EOP_OnStop, true);
  283.             }
  284.             else if (m_pVideoManager->lastVideoFinishedReason == "Skipped")
  285.             {
  286.                 ActivateOutput(&m_ActInfo, EOP_OnStop, false);
  287.             }
  288.  
  289.             m_bEnabled = false;
  290.             m_bPaused = false;
  291.             if (g_pCryVideo && g_pCryVideo->m_pFramework)
  292.                 g_pCryVideo->m_pFramework->UnregisterListener(this);
  293.  
  294.             m_ActInfo.pGraph->SetRegularlyUpdated(m_ActInfo.myID, false);
  295.  
  296.             //0.9.6: Loop and KeepLastFrame option added
  297.             if (m_pVideoManager->lastVideoFinishedReason == "Finished" && m_bLooped)
  298.             {
  299.                 string videoName = GetPortString(&m_ActInfo, EIP_VideoName);
  300.                 float volume = GetPortFloat(&m_ActInfo, EIP_AudioVolume);
  301.                
  302.                 m_bLooped = GetPortBool(&m_ActInfo, EIP_Looped);
  303.                 bool keepLastFrame = GetPortBool(&m_ActInfo, EIP_KeepLastFrame);
  304.  
  305.                 int slot = GetPortInt(&m_ActInfo, EIP_Slot);
  306.                 int subMtlId = GetPortInt(&m_ActInfo, EIP_SubMtlId);
  307.                 int texSlot = GetPortInt(&m_ActInfo, EIP_TexSlot);
  308.        
  309.                 if (!videoName || videoName == "")
  310.                 {  
  311.                     ActivateOutput(&m_ActInfo, EOP_OnVideoNotFound, true);
  312.                     return;
  313.                 }
  314.  
  315.                 IEntity* pEntity = m_ActInfo.pEntity;
  316.                 if(pEntity)
  317.                 {
  318.                     bool success = m_pVideoManager->PlaySingleVideo(videoName, true, "", 1.0f, pEntity, slot, subMtlId, texSlot, false, keepLastFrame);
  319.                     if (success)
  320.                     {
  321.                         m_bEnabled = true;
  322.                         if (g_pCryVideo && g_pCryVideo->m_pFramework)
  323.                             g_pCryVideo->m_pFramework->RegisterListener(this, "FlowNode_CryVideoPlayer", eFLPriority_Default);
  324.  
  325.                         ActivateOutput(&m_ActInfo, EOP_OnPlay, true);
  326.                         m_ActInfo.pGraph->SetRegularlyUpdated(m_ActInfo.myID, true);
  327.                     }
  328.                     else
  329.                     {
  330.                         ActivateOutput(&m_ActInfo, EOP_OnVideoNotFound, true);
  331.                     }
  332.                 }
  333.             }
  334.         }
  335.     }
  336. };
  337.  
  338.  
  339. ////////////////////////////////////////////////////
  340. ////////////////////////////////////////////////////
  341. REGISTER_FLOW_NODE("Video:CryVideoPlayer", CFlowCryVideoPlayer);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement