123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
-
- #include "ScriptEvents/ScriptEventsAssetRef.h"
- #include <AzCore/Asset/AssetSerializer.h>
- #include <AzCore/RTTI/BehaviorContext.h>
- #include <AzCore/Serialization/SerializeContext.h>
- namespace ScriptEvents
- {
- void ScriptEventsAssetRef::Reflect(AZ::ReflectContext* context)
- {
- if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
- {
- serializeContext->Class<ScriptEventsAssetRef>()->Version(0)->Field("Asset", &ScriptEventsAssetRef::m_asset);
- if (AZ::EditContext* editContext = serializeContext->GetEditContext())
- {
- editContext->Class<ScriptEventsAssetRef>("Script Event Asset", "")
- ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
- ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
- ->DataElement(AZ::Edit::UIHandlers::Default, &ScriptEventsAssetRef::m_asset, "Script Event Asset", "")
- ->Attribute(AZ::Edit::Attributes::ChangeNotify, &ScriptEventsAssetRef::OnAssetChanged)
- // TODO #lsempe: hook up to open Asset Editor when ready
- //->Attribute("EditButton", "")
- //->Attribute("EditDescription", "Open in Script Canvas Editor")
- //->Attribute("EditCallback", &ScriptEventsAssetRef::LaunchScriptCanvasEditor)
- ;
- }
- }
- if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
- {
- behaviorContext->Class<ScriptEventsAssetRef>()
- ->Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::ExcludeFlags::All)
- ->Attribute(AZ::Script::Attributes::Storage, AZ::Script::Attributes::StorageType::Value)
- ->Attribute(AZ::Script::Attributes::ConstructibleFromNil, false)
- ->Method("Get", &ScriptEventsAssetRef::GetDefinition);
- }
- }
- void ScriptEventsAssetRef::SetAsset(const AZ::Data::Asset<ScriptEventsAsset>& asset)
- {
- m_asset = asset;
- if (m_asset.IsReady())
- {
- if (ScriptEventsAsset* scriptEventAsset = m_asset.GetAs<ScriptEventsAsset>())
- {
- scriptEventAsset->m_definition.RegisterInternal();
- }
- }
- else
- {
- if (AZ::Data::AssetBus::Handler::BusIsConnectedId(m_asset.GetId()))
- {
- AZ::Data::AssetBus::Handler::BusDisconnect(m_asset.GetId());
- }
- AZ::Data::AssetBus::Handler::BusConnect(m_asset.GetId());
- }
- }
- void ScriptEventsAssetRef::Load(bool loadBlocking /*= false*/)
- {
- if (!m_asset.IsReady())
- {
- AZ::Data::AssetInfo assetInfo;
- AZ::Data::AssetCatalogRequestBus::BroadcastResult(assetInfo, &AZ::Data::AssetCatalogRequests::GetAssetInfoById, m_asset.GetId());
- if (assetInfo.m_assetId.IsValid())
- {
- auto& assetManager = AZ::Data::AssetManager::Instance();
- m_asset = assetManager.GetAsset(m_asset.GetId(), azrtti_typeid<ScriptEventsAsset>(), m_asset.GetAutoLoadBehavior());
- if(loadBlocking)
- {
- m_asset.BlockUntilLoadComplete();
- }
- }
- }
- }
- AZ::u32 ScriptEventsAssetRef::OnAssetChanged()
- {
- SetAsset(m_asset);
- Load(false);
- if (m_assetNotifyCallback)
- {
- m_assetNotifyCallback(m_asset, m_userData);
- }
- return AZ::Edit::PropertyRefreshLevels::None;
- }
- void ScriptEventsAssetRef::OnAssetReady(AZ::Data::Asset<AZ::Data::AssetData> asset)
- {
- if (ScriptEventsAsset* scriptEventAsset = m_asset.GetAs<ScriptEventsAsset>())
- {
- scriptEventAsset->m_definition.RegisterInternal();
- }
- }
- void ScriptEventsAssetRef::OnAssetReloaded(AZ::Data::Asset<AZ::Data::AssetData> asset)
- {
- SetAsset(asset);
- if (m_assetNotifyCallback)
- {
- m_assetNotifyCallback(m_asset, m_userData);
- }
- }
- void ScriptEventsAssetRef::OnAssetUnloaded(
- [[maybe_unused]] const AZ::Data::AssetId assetId, [[maybe_unused]] const AZ::Data::AssetType assetType)
- {
- }
- } // namespace ScriptEvents
|