BVB Source Codes

CRYENGINE Show FlashUIAction.cpp Source code

Return Download CRYENGINE: download FlashUIAction.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. // -------------------------------------------------------------------------
  4. //  File name:   FlashUIAction.cpp
  5. //  Version:     v1.00
  6. //  Created:     10/9/2010 by Paul Reindell.
  7. //  Description:
  8. // -------------------------------------------------------------------------
  9. //  History:
  10. //
  11. ////////////////////////////////////////////////////////////////////////////
  12. #include "StdAfx.h"
  13. #include "FlashUIAction.h"
  14. #include "FlashUI.h"
  15. #include "ScriptBind_UIAction.h"
  16.  
  17. //------------------------------------------------------------------------------------
  18. CFlashUIAction::CFlashUIAction(EUIActionType type)
  19.         : m_bIsValid(false)
  20.         , m_bEnabled(true)
  21.         , m_type(type)
  22. {
  23.         if (m_type == eUIAT_FlowGraph)
  24.         {
  25.                 CRY_ASSERT_MESSAGE(gEnv->pFlowSystem, "FlowSystem not initialized, crash will follow!");
  26.                 m_pFlowGraph = gEnv->pFlowSystem->CreateFlowGraph();
  27.                 m_pFlowGraph->UnregisterFromFlowSystem();
  28.                 m_pFlowGraph->AddRef();
  29.                 m_pFlowGraph->SetType(IFlowGraph::eFGT_UIAction);
  30.         }
  31.         else
  32.         {
  33.                 CRY_ASSERT_MESSAGE(gEnv->pScriptSystem, "ScriptSystem not initialized, crash will follow!");
  34.                 m_pScript = gEnv->pScriptSystem->CreateTable();
  35.         }
  36. }
  37.  
  38. //------------------------------------------------------------------------------------
  39. CFlashUIAction::~CFlashUIAction()
  40. {
  41.         if (m_type == eUIAT_FlowGraph)
  42.         {
  43.                 assert(m_pFlowGraph != NULL);
  44.                 m_pFlowGraph->Release();
  45.         }
  46. }
  47.  
  48. //------------------------------------------------------------------------------------
  49. bool CFlashUIAction::Init()
  50. {
  51.         if (!m_bIsValid) return false;
  52.  
  53.         UIACTION_LOG("UIAction %s Init (%s)", GetName(), m_type == eUIAT_FlowGraph ? "FG Action" : "Lua Action");
  54.         if (m_type == eUIAT_FlowGraph)
  55.         {
  56.                 assert(m_pFlowGraph != NULL);
  57.                 m_pFlowGraph->SetEnabled(true);
  58.                 m_pFlowGraph->InitializeValues();
  59.                 m_pFlowGraph->SetEnabled(m_bEnabled);
  60.         }
  61.         return true;
  62. }
  63.  
  64. //------------------------------------------------------------------------------------
  65. void CFlashUIAction::SetEnabled(bool bEnabled)
  66. {
  67.         if (!m_bIsValid) return;
  68.  
  69.         m_bEnabled = bEnabled;
  70.         if (m_type == eUIAT_FlowGraph)
  71.         {
  72.                 assert(m_pFlowGraph != NULL);
  73.                 m_pFlowGraph->SetEnabled(bEnabled);
  74.         }
  75.         else
  76.         {
  77.                 assert(m_pScript.GetPtr() != NULL);
  78.                 if (m_scriptAvail[eSF_OnInit] && bEnabled)
  79.                 {
  80.                         Script::CallMethod(m_pScript, "OnInit");
  81.                         m_scriptAvail[eSF_OnInit] = false;
  82.                 }
  83.                 if (m_scriptAvail[eSF_OnEnabled])
  84.                         Script::CallMethod(m_pScript, "OnEnabled", bEnabled);
  85.                 m_pScript->SetValue("enabled", bEnabled);
  86.         }
  87. }
  88.  
  89. //------------------------------------------------------------------------------------
  90. bool CFlashUIAction::Serialize(XmlNodeRef& xmlNode, bool bIsLoading)
  91. {
  92.         CRY_ASSERT_MESSAGE(m_type == eUIAT_FlowGraph, "Try to serialize Flowgraph of Lua UI Action");
  93.         bool ok = m_pFlowGraph->SerializeXML(xmlNode, bIsLoading);
  94.         SetValid(ok);
  95.         return ok;
  96. }
  97.  
  98. //------------------------------------------------------------------------------------
  99. bool CFlashUIAction::Serialize(const char* scriptFile, bool bIsLoading)
  100. {
  101.         CRY_ASSERT_MESSAGE(m_type == eUIAT_LuaScript, "Try to serialize Script of FG UI Action");
  102.         m_sScriptFile = scriptFile;
  103.         return ReloadScript();
  104. }
  105.  
  106. //-------------------------------------------------------------------
  107. bool CFlashUIAction::ReloadScript()
  108. {
  109.         if (m_type == eUIAT_LuaScript)
  110.         {
  111.                 bool ok = gEnv->pScriptSystem->ExecuteFile(m_sScriptFile.c_str(), true, true);
  112.                 if (ok)
  113.                         ok = gEnv->pScriptSystem->GetGlobalValue(GetName(), m_pScript);
  114.                 m_scriptAvail[eSF_OnInit] = ok && m_pScript->GetValueType("OnInit") == svtFunction;
  115.                 m_scriptAvail[eSF_OnStart] = ok && m_pScript->GetValueType("OnStart") == svtFunction;
  116.                 m_scriptAvail[eSF_OnUpdate] = ok && m_pScript->GetValueType("OnUpdate") == svtFunction;
  117.                 m_scriptAvail[eSF_OnEnd] = ok && m_pScript->GetValueType("OnEnd") == svtFunction;
  118.                 m_scriptAvail[eSF_OnEnabled] = ok && m_pScript->GetValueType("OnEnabled") == svtFunction;
  119.                 SetValid(ok);
  120.                 if (ok)
  121.                 {
  122.                         m_pScript->GetValue("enabled", m_bEnabled);
  123.                         m_pScript->SetValue("__ui_action_name", GetName());
  124.                 }
  125.         }
  126.         return m_bIsValid;
  127. }
  128.  
  129. //-------------------------------------------------------------------
  130. void CFlashUIAction::GetMemoryUsage(ICrySizer* s) const
  131. {
  132.         SIZER_SUBCOMPONENT_NAME(s, "UIAction");
  133.         s->AddObject(this, sizeof(*this));
  134.         if (m_pFlowGraph != NULL)
  135.                 m_pFlowGraph->GetMemoryUsage(s);
  136. }
  137.  
  138. //--------------------------------------------------------------------------------------------
  139. void CFlashUIAction::Update()
  140. {
  141.         if (!m_bEnabled || !m_bIsValid) return;
  142.  
  143.         if (m_type == eUIAT_FlowGraph)
  144.         {
  145.                 assert(m_pFlowGraph != NULL);
  146.                 m_pFlowGraph->Update();
  147.         }
  148.         else
  149.         {
  150.                 assert(m_pScript.GetPtr() != NULL);
  151.                 if (m_scriptAvail[eSF_OnUpdate])
  152.                         Script::CallMethod(m_pScript, "OnUpdate");
  153.         }
  154. }
  155.  
  156. //--------------------------------------------------------------------------------------------
  157. void CFlashUIAction::StartScript(const SUIArguments& args)
  158. {
  159.         if (!m_bIsValid) return;
  160.  
  161.         assert(m_pScript.GetPtr() != NULL);
  162.         SmartScriptTable table = gEnv->pScriptSystem->CreateTable();
  163.         SUIToLuaConversationHelper::UIArgsToLuaTable(args, table);
  164.         if (m_scriptAvail[eSF_OnStart])
  165.                 Script::CallMethod(m_pScript, "OnStart", table);
  166. }
  167.  
  168. //--------------------------------------------------------------------------------------------
  169. void CFlashUIAction::EndScript()
  170. {
  171.         if (!m_bIsValid) return;
  172.  
  173.         assert(m_pScript.GetPtr() != NULL);
  174.         if (m_scriptAvail[eSF_OnEnd])
  175.                 Script::CallMethod(m_pScript, "OnEnd");
  176. }
  177.  
  178. //--------------------------------------------------------------------------------------------
  179. void CUIActionManager::Init()
  180. {
  181.         for (TActionMap::iterator it = m_actionStateMap.begin(); it != m_actionStateMap.end(); ++it)
  182.                 it->second = false;
  183.  
  184.         m_bAcceptRequests = true;
  185.         m_actionStartMap.clear();
  186.         m_actionEndMap.clear();
  187.         m_actionEnableMap.clear();
  188. }
  189.  
  190. //------------------------------------------------------------------------------------
  191. void CUIActionManager::StartAction(IUIAction* pAction, const SUIArguments& args)
  192. {
  193.         if (!m_bAcceptRequests) return;
  194.  
  195.         if (pAction && pAction->IsValid())
  196.         {
  197. #ifndef _RELEASE
  198.                 if (m_actionStartMap.find(pAction) != m_actionStartMap.end())
  199.                 {
  200.                         UIACTION_WARNING("UIAction %s already started! Old args will be discarded!", pAction->GetName());
  201.                 }
  202. #endif
  203.                 m_actionStartMap[pAction] = args;
  204.                 stl::member_find_and_erase(m_actionEndMap, pAction);
  205.                 UIACTION_LOG("UIAction %s start request (Args: %s)", pAction->GetName(), args.GetAsString());
  206.                 return;
  207.         }
  208.         UIACTION_ERROR("StartAction failed! UIAction %s not valid!", pAction ? pAction->GetName() : "NULL");
  209. }
  210.  
  211. //------------------------------------------------------------------------------------
  212. void CUIActionManager::EndAction(IUIAction* pAction, const SUIArguments& args)
  213. {
  214.         if (!m_bAcceptRequests) return;
  215.  
  216.         if (pAction && pAction->IsValid())
  217.         {
  218. #ifndef _RELEASE
  219.                 if (m_actionEndMap.find(pAction) != m_actionEndMap.end())
  220.                 {
  221.                         UIACTION_WARNING("UIAction %s already ended! Old args will be discarded!", pAction->GetName());
  222.                 }
  223. #endif
  224.                 m_actionEndMap[pAction] = args;
  225.                 stl::member_find_and_erase(m_actionStartMap, pAction);
  226.                 UIACTION_LOG("UIAction %s end request (Args: %s)", pAction->GetName(), args.GetAsString());
  227.                 return;
  228.         }
  229.         UIACTION_ERROR("EndAction failed! UIAction %s not valid!", pAction ? pAction->GetName() : "NULL");
  230. }
  231.  
  232. //------------------------------------------------------------------------------------
  233. void CUIActionManager::EnableAction(IUIAction* pAction, bool bEnable)
  234. {
  235.         if (!m_bAcceptRequests) return;
  236.  
  237.         if (pAction && pAction->IsValid())
  238.         {
  239.                 m_actionEnableMap[pAction] = bEnable;
  240.                 UIACTION_LOG("UIAction %s %s request", pAction->GetName(), bEnable ? "enable" : "disable");
  241.                 return;
  242.         }
  243.         UIACTION_ERROR("EnableAction failed! UIAction %s not valid!", pAction ? pAction->GetName() : "NULL");
  244. }
  245.  
  246. //------------------------------------------------------------------------------------
  247. void CUIActionManager::AddListener(IUIActionListener* pListener, const char* name)
  248. {
  249.         const bool ok = m_listener.Add(pListener, name);
  250.         CRY_ASSERT_MESSAGE(ok, "Listener already registered!");
  251. }
  252.  
  253. //------------------------------------------------------------------------------------
  254. void CUIActionManager::RemoveListener(IUIActionListener* pListener)
  255. {
  256.         CRY_ASSERT_MESSAGE(m_listener.Contains(pListener), "Listener was never registered or already unregistered!");
  257.         m_listener.Remove(pListener);
  258. }
  259.  
  260. //-------------------------------------------------------------------
  261. void CUIActionManager::GetMemoryUsage(ICrySizer* s) const
  262. {
  263.         SIZER_SUBCOMPONENT_NAME(s, "UIActionManager");
  264.         s->AddObject(this, sizeof(*this));
  265.         s->AddObject(&m_listener, m_listener.MemSize());
  266.         s->AddContainer(m_actionStateMap);
  267.         s->AddContainer(m_actionStartMap);
  268.         s->AddContainer(m_actionEndMap);
  269.         s->AddContainer(m_actionEnableMap);
  270. }
  271.  
  272. //------------------------------------------------------------------------------------
  273. void CUIActionManager::Update()
  274. {
  275.         FUNCTION_PROFILER(GetISystem(), PROFILE_ACTION);
  276.  
  277.         if (!m_actionEnableMap.empty())
  278.         {
  279.                 TActionMap actionEnableMap = m_actionEnableMap;
  280.                 m_actionEnableMap.clear();
  281.                 for (TActionMap::iterator it = actionEnableMap.begin(); it != actionEnableMap.end(); ++it)
  282.                 {
  283.                         EnableActionInt(it->first, it->second);
  284.                 }
  285.         }
  286.  
  287.         if (!m_actionEndMap.empty())
  288.         {
  289.                 TActionArgMap actionEndMap = m_actionEndMap;
  290.                 m_actionEndMap.clear();
  291.                 for (TActionArgMap::iterator it = actionEndMap.begin(); it != actionEndMap.end(); ++it)
  292.                 {
  293.                         EndActionInt(it->first, it->second);
  294.                 }
  295.         }
  296.  
  297.         if (!m_actionStartMap.empty())
  298.         {
  299.                 TActionArgMap actionStartMap = m_actionStartMap;
  300.                 m_actionStartMap.clear();
  301.                 for (TActionArgMap::iterator it = actionStartMap.begin(); it != actionStartMap.end(); ++it)
  302.                 {
  303.                         StartActionInt(it->first, it->second);
  304.                 }
  305.         }
  306. }
  307.  
  308. //------------------------------------------------------------------------------------
  309. void CUIActionManager::StartActionInt(IUIAction* pAction, const SUIArguments& args)
  310. {
  311.         FUNCTION_PROFILER(GetISystem(), PROFILE_ACTION);
  312.  
  313.         if (pAction && pAction->IsValid())
  314.         {
  315.                 m_actionStateMap[pAction] = true;
  316.  
  317.                 EnableActionInt(pAction, true);
  318.  
  319.                 UIACTION_LOG("UIAction %s start (Args: %s)", pAction->GetName(), args.GetAsString());
  320.  
  321.                 if (pAction->GetType() == IUIAction::eUIAT_LuaScript)
  322.                         ((CFlashUIAction*)pAction)->StartScript(args);
  323.  
  324.                 for (TActionListener::Notifier notifier(m_listener); notifier.IsValid(); notifier.Next())
  325.                         notifier->OnStart(pAction, args);
  326.         }
  327. }
  328.  
  329. //------------------------------------------------------------------------------------
  330. void CUIActionManager::EndActionInt(IUIAction* pAction, const SUIArguments& args)
  331. {
  332.         FUNCTION_PROFILER(GetISystem(), PROFILE_ACTION);
  333.  
  334.         if (pAction && m_actionStateMap[pAction])     // only allow to end actions that are started
  335.         {
  336.                 m_actionStateMap[pAction] = false;
  337.  
  338.                 UIACTION_LOG("UIAction %s end (Args: %s)", pAction->GetName(), args.GetAsString());
  339.  
  340.                 if (pAction->GetType() == IUIAction::eUIAT_LuaScript)
  341.                         ((CFlashUIAction*)pAction)->EndScript();
  342.  
  343.                 for (TActionListener::Notifier notifier(m_listener); notifier.IsValid(); notifier.Next())
  344.                         notifier->OnEnd(pAction, args);
  345.         }
  346. }
  347.  
  348. //------------------------------------------------------------------------------------
  349. void CUIActionManager::EnableActionInt(IUIAction* pAction, bool bEnable)
  350. {
  351.         if (pAction && pAction->IsValid() && pAction->IsEnabled() != bEnable)
  352.         {
  353.                 UIACTION_LOG("UIAction %s %s", pAction->GetName(), bEnable ? "enabled" : "disabled");
  354.                 m_bAcceptRequests = false;
  355.                 pAction->Init();
  356.                 m_bAcceptRequests = true;
  357.                 pAction->SetEnabled(bEnable);
  358.         }
  359. }
  360.  
  361. //------------------------------------------------------------------------------------
  362.  
downloadFlashUIAction.cpp Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top