/* * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or * its licensors. * * For complete copyright and license terms please see the LICENSE at the root of this * distribution (the "License"). All use of this software is governed by the License, * or, if provided, by the license below or the license accompanying this file. Do not * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ #pragma once #include #include #include #include #include #include namespace ScriptCanvasEditor { class ScriptCanvasMemoryAsset; // This class tracks all things related to the assets that the Script Canvas editor // has in play. It also provides helper functionality to quickly getting asset information // from GraphCanvas // // The AssetTracker will be the ONLY allowed place to connect Script Canvas to the Asset System and any of its buses. // // The goal is to centralize all of the asset operations to a single place in order to simplify Script Canvas' // interactions with the asset system as well as keeping any transient cache of information that is // only important while Script Canvas graphs are open. class AssetTracker : AssetTrackerRequestBus::Handler , Internal::MemoryAssetSystemNotificationBus::Handler { public: AssetTracker() { AssetTrackerRequestBus::Handler::BusConnect(); Internal::MemoryAssetSystemNotificationBus::Handler::BusConnect(); } ~AssetTracker() { Internal::MemoryAssetSystemNotificationBus::Handler::BusDisconnect(); AssetTrackerRequestBus::Handler::BusDisconnect(); } // AssetTrackerRequestBus AZ::Data::AssetId Create(AZStd::string_view assetAbsolutePath, AZ::Data::AssetType assetType, Callbacks::OnAssetCreatedCallback onAssetCreatedCallback) override; bool IsSaving(AZ::Data::AssetId assetId) const override; void Save(AZ::Data::AssetId assetId, Callbacks::OnSave onSaveCallback) override; void SaveAs(AZ::Data::AssetId assetId, const AZStd::string& path, Callbacks::OnSave onSaveCallback) override; bool Load(AZ::Data::AssetId assetId, AZ::Data::AssetType assetType, Callbacks::OnAssetReadyCallback onAssetReadyCallback) override; void Close(AZ::Data::AssetId assetId) override; void CreateView(AZ::Data::AssetId assetId, QWidget* parent) override; void ClearView(AZ::Data::AssetId assetId) override; void UntrackAsset(AZ::Data::AssetId assetId) override; // Getters // Given the assetId it returns a reference to the in-memory asset, returns false if the asset is not tracked ScriptCanvasMemoryAsset::pointer GetAsset(AZ::Data::AssetId assetId) override; // Given a ScriptCanvas EntityId it will lookup the AssetId AZ::Data::AssetId GetAssetId(ScriptCanvas::ScriptCanvasId scriptCanvasSceneId) override; // Given a ScriptCanvas EntityId it will lookup the asset's type AZ::Data::AssetType GetAssetType(ScriptCanvas::ScriptCanvasId scriptCanvasSceneId) override; // Retrieves the ScriptCanvasEntity for a given asset ScriptCanvas::ScriptCanvasId GetScriptCanvasId(AZ::Data::AssetId assetId) override; // Retrieves the Graph Canvas scene Id for a given asset AZ::EntityId GetGraphCanvasId(AZ::EntityId scriptCanvasEntityId) override; ScriptCanvas::ScriptCanvasId GetScriptCanvasIdFromGraphId(AZ::EntityId graphId) override; ScriptCanvas::ScriptCanvasId GetGraphId(AZ::Data::AssetId assetId) override; Tracker::ScriptCanvasFileState GetFileState(AZ::Data::AssetId assetId) override; AZStd::string GetTabName(AZ::Data::AssetId assetId) override; ScriptCanvasEditor::ScriptCanvasAssetHandler* GetAssetHandlerForType(AZ::Data::AssetType assetType) override; void UpdateFileState(AZ::Data::AssetId assetId, Tracker::ScriptCanvasFileState state) override; AssetTrackerRequests::AssetList GetUnsavedAssets() override; AssetTrackerRequests::AssetList GetAssets(); AssetTrackerRequests::AssetList GetAssetsIf(AZStd::function pred = []() { return true; }) override; AZ::EntityId GetSceneEntityIdFromEditorEntityId(AZ::Data::AssetId assetId, AZ::EntityId editorEntityId) override; AZ::EntityId GetEditorEntityIdFromSceneEntityId(AZ::Data::AssetId assetId, AZ::EntityId sceneEntityId) override; // private: void SignalSaveComplete(const AZ::Data::AssetId& fileAssetId); // Verifies if an asset Id has been remapped, this happens when we save a new graph because the AssetId will // change on save, but there may be some UX elements still referring to the initial asset Id, this ensures // we are getting the right key into m_assetsInUse AZ::Data::AssetId CheckAssetId(AZ::Data::AssetId assetId) const; // Map of all the currently tracked in-memory assets using MemoryAssetMap = AZStd::unordered_map; using MemoryAssetMapIterator = AZStd::pair>, bool>; AZStd::unordered_set m_savingAssets; AZStd::unordered_set m_queuedCloses; // Map of all assets being tracked MemoryAssetMap m_assetsInUse; // When a graph has been saved to file, its Id will change but it may still have external references with the old Id, this maps the file Id to the in-memory Id AZStd::unordered_map m_remappedAsset; // Invoked when an asset is loaded from file and becomes ready Callbacks::OnAssetReadyCallback m_onAssetReadyCallback; // Internal::MemoryAssetNotificationBus void OnAssetReady(const ScriptCanvasMemoryAsset* asset) override; void OnAssetReloaded(const ScriptCanvasMemoryAsset* asset) override; void OnAssetSaved(const ScriptCanvasMemoryAsset* asset, bool isSuccessful) override; void OnAssetError(const ScriptCanvasMemoryAsset* asset) override; }; }