ScriptEventsAssetRef.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "ScriptEvents/ScriptEventsAssetRef.h"
  9. #include <AzCore/Asset/AssetSerializer.h>
  10. #include <AzCore/RTTI/BehaviorContext.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. namespace ScriptEvents
  13. {
  14. void ScriptEventsAssetRef::Reflect(AZ::ReflectContext* context)
  15. {
  16. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  17. {
  18. serializeContext->Class<ScriptEventsAssetRef>()->Version(0)->Field("Asset", &ScriptEventsAssetRef::m_asset);
  19. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  20. {
  21. editContext->Class<ScriptEventsAssetRef>("Script Event Asset", "")
  22. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  23. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  24. ->DataElement(AZ::Edit::UIHandlers::Default, &ScriptEventsAssetRef::m_asset, "Script Event Asset", "")
  25. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &ScriptEventsAssetRef::OnAssetChanged)
  26. // TODO #lsempe: hook up to open Asset Editor when ready
  27. //->Attribute("EditButton", "")
  28. //->Attribute("EditDescription", "Open in Script Canvas Editor")
  29. //->Attribute("EditCallback", &ScriptEventsAssetRef::LaunchScriptCanvasEditor)
  30. ;
  31. }
  32. }
  33. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  34. {
  35. behaviorContext->Class<ScriptEventsAssetRef>()
  36. ->Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::ExcludeFlags::All)
  37. ->Attribute(AZ::Script::Attributes::Storage, AZ::Script::Attributes::StorageType::Value)
  38. ->Attribute(AZ::Script::Attributes::ConstructibleFromNil, false)
  39. ->Method("Get", &ScriptEventsAssetRef::GetDefinition);
  40. }
  41. }
  42. void ScriptEventsAssetRef::SetAsset(const AZ::Data::Asset<ScriptEventsAsset>& asset)
  43. {
  44. m_asset = asset;
  45. if (m_asset.IsReady())
  46. {
  47. if (ScriptEventsAsset* scriptEventAsset = m_asset.GetAs<ScriptEventsAsset>())
  48. {
  49. scriptEventAsset->m_definition.RegisterInternal();
  50. }
  51. }
  52. else
  53. {
  54. if (AZ::Data::AssetBus::Handler::BusIsConnectedId(m_asset.GetId()))
  55. {
  56. AZ::Data::AssetBus::Handler::BusDisconnect(m_asset.GetId());
  57. }
  58. AZ::Data::AssetBus::Handler::BusConnect(m_asset.GetId());
  59. }
  60. }
  61. void ScriptEventsAssetRef::Load(bool loadBlocking /*= false*/)
  62. {
  63. if (!m_asset.IsReady())
  64. {
  65. AZ::Data::AssetInfo assetInfo;
  66. AZ::Data::AssetCatalogRequestBus::BroadcastResult(assetInfo, &AZ::Data::AssetCatalogRequests::GetAssetInfoById, m_asset.GetId());
  67. if (assetInfo.m_assetId.IsValid())
  68. {
  69. auto& assetManager = AZ::Data::AssetManager::Instance();
  70. m_asset = assetManager.GetAsset(m_asset.GetId(), azrtti_typeid<ScriptEventsAsset>(), m_asset.GetAutoLoadBehavior());
  71. if(loadBlocking)
  72. {
  73. m_asset.BlockUntilLoadComplete();
  74. }
  75. }
  76. }
  77. }
  78. AZ::u32 ScriptEventsAssetRef::OnAssetChanged()
  79. {
  80. SetAsset(m_asset);
  81. Load(false);
  82. if (m_assetNotifyCallback)
  83. {
  84. m_assetNotifyCallback(m_asset, m_userData);
  85. }
  86. return AZ::Edit::PropertyRefreshLevels::None;
  87. }
  88. void ScriptEventsAssetRef::OnAssetReady(AZ::Data::Asset<AZ::Data::AssetData> asset)
  89. {
  90. if (ScriptEventsAsset* scriptEventAsset = m_asset.GetAs<ScriptEventsAsset>())
  91. {
  92. scriptEventAsset->m_definition.RegisterInternal();
  93. }
  94. }
  95. void ScriptEventsAssetRef::OnAssetReloaded(AZ::Data::Asset<AZ::Data::AssetData> asset)
  96. {
  97. SetAsset(asset);
  98. if (m_assetNotifyCallback)
  99. {
  100. m_assetNotifyCallback(m_asset, m_userData);
  101. }
  102. }
  103. void ScriptEventsAssetRef::OnAssetUnloaded(
  104. [[maybe_unused]] const AZ::Data::AssetId assetId, [[maybe_unused]] const AZ::Data::AssetType assetType)
  105. {
  106. }
  107. } // namespace ScriptEvents