Initial commit

This commit is contained in:
alexpete
2021-03-05 11:26:34 -08:00
commit a10351f38d
27091 changed files with 5521199 additions and 0 deletions
@@ -0,0 +1,277 @@
/*
* 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.
*
*/
#include <Atom/RPI.Edit/Common/JsonUtils.h>
#include <Atom/RPI.Reflect/Asset/AssetUtils.h>
#include <Document/ShaderManagementConsoleDocument.h>
#include <Atom/Document/ShaderManagementConsoleDocumentNotificationBus.h>
#include <AzFramework/StringFunc/StringFunc.h>
#include <AzToolsFramework/API/EditorAssetSystemAPI.h>
#include <AzToolsFramework/SourceControl/SourceControlAPI.h>
namespace ShaderManagementConsole
{
ShaderManagementConsoleDocument::ShaderManagementConsoleDocument()
{
ShaderManagementConsoleDocumentRequestBus::Handler::BusConnect(m_id);
ShaderManagementConsoleDocumentNotificationBus::Broadcast(&ShaderManagementConsoleDocumentNotificationBus::Events::OnDocumentCreated, m_id);
}
ShaderManagementConsoleDocument::~ShaderManagementConsoleDocument()
{
ShaderManagementConsoleDocumentNotificationBus::Broadcast(&ShaderManagementConsoleDocumentNotificationBus::Events::OnDocumentDestroyed, m_id);
ShaderManagementConsoleDocumentRequestBus::Handler::BusDisconnect();
}
const AZ::Uuid& ShaderManagementConsoleDocument::GetId() const
{
return m_id;
}
AZStd::string_view ShaderManagementConsoleDocument::GetAbsolutePath() const
{
return m_absolutePath;
}
AZStd::string_view ShaderManagementConsoleDocument::GetRelativePath() const
{
return m_relativePath;
}
size_t ShaderManagementConsoleDocument::GetShaderOptionCount() const
{
auto layout = m_shaderAsset->GetShaderOptionGroupLayout();
auto& shaderOptionDescriptors = layout->GetShaderOptions();
return shaderOptionDescriptors.size();
}
const AZ::RPI::ShaderOptionDescriptor& ShaderManagementConsoleDocument::GetShaderOptionDescriptor(size_t index) const
{
auto layout = m_shaderAsset->GetShaderOptionGroupLayout();
auto& shaderOptionDescriptors = layout->GetShaderOptions();
return shaderOptionDescriptors[index];
}
size_t ShaderManagementConsoleDocument::GetShaderVariantCount() const
{
return m_shaderVariantListSourceData.m_shaderVariants.size();
}
const AZ::RPI::ShaderVariantListSourceData::VariantInfo& ShaderManagementConsoleDocument::GetShaderVariantInfo(size_t index) const
{
return m_shaderVariantListSourceData.m_shaderVariants[index];
}
ShaderManagementConsoleDocumentResult ShaderManagementConsoleDocument::Open(AZStd::string_view loadPath)
{
Clear();
m_absolutePath = loadPath;
if (!AzFramework::StringFunc::Path::Normalize(m_absolutePath))
{
return AZ::Failure(AZStd::string::format("Document path could not be normalized: '%s'.", m_absolutePath.c_str()));
}
if (AzFramework::StringFunc::Path::IsRelative(m_absolutePath.c_str()))
{
return AZ::Failure(AZStd::string::format("Document path must be absolute: '%s'.", m_absolutePath.c_str()));
}
if (AzFramework::StringFunc::Path::IsExtension(m_absolutePath.c_str(), AZ::RPI::ShaderVariantListSourceData::Extension))
{
// Load the shader config data and create a shader config asset from it
if (!AZ::RPI::JsonUtils::LoadObjectFromFile(m_absolutePath, m_shaderVariantListSourceData))
{
return AZ::Failure(AZStd::string::format("Failed loading shader variant list data: '%s.'", m_absolutePath.c_str()));
}
}
bool result = false;
AZ::Data::AssetInfo sourceAssetInfo;
AZStd::string watchFolder;
AzToolsFramework::AssetSystemRequestBus::BroadcastResult(result, &AzToolsFramework::AssetSystem::AssetSystemRequest::GetSourceInfoBySourcePath,
m_absolutePath.c_str(), sourceAssetInfo, watchFolder);
if (!result)
{
return AZ::Failure(AZStd::string::format("Could not find source data: '%s'.", m_absolutePath.c_str()));
}
m_relativePath = sourceAssetInfo.m_relativePath;
if (!AzFramework::StringFunc::Path::Normalize(m_relativePath))
{
return AZ::Failure(AZStd::string::format("Shader path could not be normalized: '%s'.", m_relativePath.c_str()));
}
AZStd::string shaderPath = m_relativePath;
AzFramework::StringFunc::Path::ReplaceExtension(shaderPath, AZ::RPI::ShaderAsset::Extension);
m_shaderAsset = AZ::RPI::AssetUtils::LoadAssetByProductPath<AZ::RPI::ShaderAsset>(shaderPath.c_str());
if (!m_shaderAsset)
{
return AZ::Failure(AZStd::string::format("Could not load shader asset: %s.", shaderPath.c_str()));
}
ShaderManagementConsoleDocumentNotificationBus::Broadcast(&ShaderManagementConsoleDocumentNotificationBus::Events::OnDocumentOpened, m_id);
return AZ::Success(AZStd::string::format("Document loaded: '%s'", m_absolutePath.c_str()));
}
ShaderManagementConsoleDocumentResult ShaderManagementConsoleDocument::Save()
{
if (!IsOpen())
{
return AZ::Failure(AZStd::string::format("Document is not open to be saved: '%s'.", m_absolutePath.c_str()));
}
if (!IsSavable())
{
return AZ::Failure(AZStd::string::format("Document can not be saved: '%s'.", m_absolutePath.c_str()));
}
return AZ::Failure(AZStd::string::format("%s is not implemented!", __FUNCTION__));
// Auto add or checkout saved file
//AzToolsFramework::SourceControlCommandBus::Broadcast(&AzToolsFramework::SourceControlCommandBus::Events::RequestEdit,
// m_absolutePath.c_str(), true,
// [](bool, const AzToolsFramework::SourceControlFileInfo&) {});
//ShaderManagementConsoleDocumentNotificationBus::Broadcast(&ShaderManagementConsoleDocumentNotificationBus::Events::OnDocumentSaved, m_id);
//return AZ::Success(AZStd::string::format("Document saved: %s", m_absolutePath.data()));
}
ShaderManagementConsoleDocumentResult ShaderManagementConsoleDocument::SaveAsCopy(AZStd::string_view savePath)
{
if (!IsOpen())
{
return AZ::Failure(AZStd::string::format("Document is not open to be saved: '%s'.", m_absolutePath.c_str()));
}
if (!IsSavable())
{
return AZ::Failure(AZStd::string::format("Document can not be saved: '%s'.", m_absolutePath.c_str()));
}
AZStd::string normalizedSavePath = savePath;
if (!AzFramework::StringFunc::Path::Normalize(normalizedSavePath))
{
return AZ::Failure(AZStd::string::format("Document save path could not be normalized: '%s'.", normalizedSavePath.c_str()));
}
return AZ::Failure(AZStd::string::format("%s is not implemented!", __FUNCTION__));
// Auto add or checkout saved file
//AzToolsFramework::SourceControlCommandBus::Broadcast(&AzToolsFramework::SourceControlCommandBus::Events::RequestEdit,
// normalizedSavePath.c_str(), true,
// [](bool, const AzToolsFramework::SourceControlFileInfo&) {});
//ShaderManagementConsoleDocumentNotificationBus::Broadcast(&ShaderManagementConsoleDocumentNotificationBus::Events::OnDocumentSaved, m_id);
//return AZ::Success(AZStd::string::format("Document saved: %s", normalizedSavePath.c_str()));
}
ShaderManagementConsoleDocumentResult ShaderManagementConsoleDocument::Close()
{
if (!IsOpen())
{
return AZ::Failure(AZStd::string("Document is not open"));
}
Clear();
ShaderManagementConsoleDocumentNotificationBus::Broadcast(&ShaderManagementConsoleDocumentNotificationBus::Events::OnDocumentClosed, m_id);
return AZ::Success(AZStd::string("Document was closed"));
}
bool ShaderManagementConsoleDocument::IsOpen() const
{
return !m_absolutePath.empty() && !m_relativePath.empty();
}
bool ShaderManagementConsoleDocument::IsModified() const
{
return false;
}
bool ShaderManagementConsoleDocument::IsSavable() const
{
return true;
}
bool ShaderManagementConsoleDocument::CanUndo() const
{
// Undo will only be allowed if something has been recorded and we're not at the beginning of history
return IsOpen() && !m_undoHistory.empty() && m_undoHistoryIndex > 0;
}
bool ShaderManagementConsoleDocument::CanRedo() const
{
// Redo will only be allowed if something has been recorded and we're not at the end of history
return IsOpen() && !m_undoHistory.empty() && m_undoHistoryIndex < m_undoHistory.size();
}
bool ShaderManagementConsoleDocument::Undo()
{
if (CanUndo())
{
// The history index is one beyond the last executed command. Decrement the index then execute undo.
m_undoHistory[--m_undoHistoryIndex].first();
ShaderManagementConsoleDocumentNotificationBus::Broadcast(&ShaderManagementConsoleDocumentNotificationBus::Events::OnDocumentUndoStateChanged, m_id);
return true;
}
return false;
}
bool ShaderManagementConsoleDocument::Redo()
{
if (CanRedo())
{
// Execute the current redo command then move the history index to the next position.
m_undoHistory[m_undoHistoryIndex++].second();
ShaderManagementConsoleDocumentNotificationBus::Broadcast(&ShaderManagementConsoleDocumentNotificationBus::Events::OnDocumentUndoStateChanged, m_id);
return true;
}
return false;
}
bool ShaderManagementConsoleDocument::BeginEdit()
{
return true;
}
bool ShaderManagementConsoleDocument::EndEdit()
{
// Wipe any state beyond the current history index
m_undoHistory.erase(m_undoHistory.begin() + m_undoHistoryIndex, m_undoHistory.end());
// Add undo and redo operations using lambdas that will capture property state and restore it when executed
m_undoHistory.emplace_back(
[this]() { /**/ },
[this]() { /**/ });
// Assign the index to the end of history
m_undoHistoryIndex = aznumeric_cast<int>(m_undoHistory.size());
ShaderManagementConsoleDocumentNotificationBus::Broadcast(&ShaderManagementConsoleDocumentNotificationBus::Events::OnDocumentUndoStateChanged, m_id);
return true;
}
void ShaderManagementConsoleDocument::Clear()
{
m_absolutePath.clear();
m_relativePath.clear();
}
} // namespace ShaderManagementConsole
@@ -0,0 +1,97 @@
/*
* 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 <AzCore/RTTI/RTTI.h>
#include <AzCore/Asset/AssetCommon.h>
#include <Atom/RPI.Edit/Shader/ShaderVariantListSourceData.h>
#include <Atom/Document/ShaderManagementConsoleDocumentRequestBus.h>
namespace ShaderManagementConsole
{
/**
* ShaderManagementConsoleDocument provides an API for modifying and saving document properties.
*/
class ShaderManagementConsoleDocument
: public ShaderManagementConsoleDocumentRequestBus::Handler
{
public:
AZ_RTTI(ShaderManagementConsoleDocument, "{DBA269AE-892B-415C-8FA1-166B94B0E045}");
AZ_CLASS_ALLOCATOR(ShaderManagementConsoleDocument, AZ::SystemAllocator, 0);
AZ_DISABLE_COPY(ShaderManagementConsoleDocument);
ShaderManagementConsoleDocument();
virtual ~ShaderManagementConsoleDocument();
const AZ::Uuid& GetId() const;
////////////////////////////////////////////////////////////////////////
// ShaderManagementConsoleDocumentRequestBus::Handler implementation
AZStd::string_view GetAbsolutePath() const override;
AZStd::string_view GetRelativePath() const override;
size_t GetShaderOptionCount() const override;
const AZ::RPI::ShaderOptionDescriptor& GetShaderOptionDescriptor(size_t index) const override;
size_t GetShaderVariantCount() const override;
const AZ::RPI::ShaderVariantListSourceData::VariantInfo& GetShaderVariantInfo(size_t index) const override;
ShaderManagementConsoleDocumentResult Open(AZStd::string_view loadPath) override;
ShaderManagementConsoleDocumentResult Save() override;
ShaderManagementConsoleDocumentResult SaveAsCopy(AZStd::string_view savePath) override;
ShaderManagementConsoleDocumentResult Close() override;
bool IsOpen() const override;
bool IsModified() const override;
bool IsSavable() const override;
bool CanUndo() const override;
bool CanRedo() const override;
bool Undo() override;
bool Redo() override;
bool BeginEdit() override;
bool EndEdit() override;
////////////////////////////////////////////////////////////////////////
private:
// Function to be bound for undo and redo
using UndoRedoFunction = AZStd::function<void()>;
// A pair of functions, where first is the undo operation and second is the redo operation
using UndoRedoFunctionPair = AZStd::pair<UndoRedoFunction, UndoRedoFunction>;
// Container for all of the active undo and redo functions and state
using UndoRedoHistory = AZStd::vector<UndoRedoFunctionPair>;
void Clear();
// Unique id of this document
AZ::Uuid m_id = AZ::Uuid::CreateRandom();
// Relative path to the document
AZStd::string m_relativePath;
// Absolute path to the document
AZStd::string m_absolutePath;
// Source data for shader variant list
AZ::RPI::ShaderVariantListSourceData m_shaderVariantListSourceData;
// Shader asset for the corresponding shader variant list
AZ::Data::Asset<AZ::RPI::ShaderAsset> m_shaderAsset;
// Variables needed for tracking the undo and redo state of this document
// Container of undo commands
UndoRedoHistory m_undoHistory;
// The current position in the undo redo history
int m_undoHistoryIndex = 0;
};
} // namespace ShaderManagementConsole
@@ -0,0 +1,38 @@
/*
* 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.
*
*/
#include <Atom/Document/ShaderManagementConsoleDocumentModule.h>
#include <Document/ShaderManagementConsoleDocumentSystemComponent.h>
#include <AzFramework/TargetManagement/TargetManagementComponent.h>
#include <AzToolsFramework/UI/PropertyEditor/PropertyManagerComponent.h>
#include <AzToolsFramework/Asset/AssetSystemComponent.h>
namespace ShaderManagementConsole
{
ShaderManagementConsoleDocumentModule::ShaderManagementConsoleDocumentModule()
{
// Push results of [MyComponent]::CreateDescriptor() into m_descriptors here.
m_descriptors.insert(m_descriptors.end(), {
ShaderManagementConsoleDocumentSystemComponent::CreateDescriptor(),
});
}
AZ::ComponentTypeList ShaderManagementConsoleDocumentModule::GetRequiredSystemComponents() const
{
return AZ::ComponentTypeList{
azrtti_typeid<AzToolsFramework::AssetSystem::AssetSystemComponent>(),
azrtti_typeid<ShaderManagementConsoleDocumentSystemComponent>(),
azrtti_typeid<AzFramework::TargetManagementComponent>(),
};
}
}
@@ -0,0 +1,345 @@
/*
* 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.
*
*/
#include <Document/ShaderManagementConsoleDocumentSystemComponent.h>
#include <AzCore/Serialization/SerializeContext.h>
#include <AzCore/Serialization/EditContext.h>
#include <AzCore/RTTI/BehaviorContext.h>
#include <AzFramework/Asset/AssetSystemBus.h>
#include <AzFramework/StringFunc/StringFunc.h>
#include <AzToolsFramework/AssetBrowser/AssetBrowserEntry.h>
#include <AzToolsFramework/AssetBrowser/AssetSelectionModel.h>
#include <AzToolsFramework/AssetBrowser/AssetBrowserBus.h>
#include <AzToolsFramework/API/ViewPaneOptions.h>
#include <AzToolsFramework/API/EditorAssetSystemAPI.h>
#include <AtomToolsFramework/Util/Util.h>
#include <Atom/Document/ShaderManagementConsoleDocumentSystemRequestBus.h>
#include <Atom/Document/ShaderManagementConsoleDocumentRequestBus.h>
#include <Atom/Document/ShaderManagementConsoleDocumentNotificationBus.h>
AZ_PUSH_DISABLE_WARNING(4251 4800, "-Wunknown-warning-option") // disable warnings spawned by QT
#include <QApplication>
#include <QStyle>
#include <QMessageBox>
#include <QFileDialog>
AZ_POP_DISABLE_WARNING
namespace ShaderManagementConsole
{
ShaderManagementConsoleDocumentSystemComponent::ShaderManagementConsoleDocumentSystemComponent()
{
}
void ShaderManagementConsoleDocumentSystemComponent::Reflect(AZ::ReflectContext* context)
{
if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
{
serialize->Class<ShaderManagementConsoleDocumentSystemComponent, AZ::Component>()
->Version(0);
if (AZ::EditContext* ec = serialize->GetEditContext())
{
ec->Class<ShaderManagementConsoleDocumentSystemComponent>("ShaderManagementConsoleDocumentSystemComponent", "Manages documents")
->ClassElement(AZ::Edit::ClassElements::EditorData, "")
->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("System", 0xc94d118b))
->Attribute(AZ::Edit::Attributes::AutoExpand, true)
;
}
}
if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
{
behaviorContext->EBus<ShaderManagementConsoleDocumentSystemRequestBus>("ShaderManagementConsoleDocumentSystemRequestBus")
->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common)
->Attribute(AZ::Script::Attributes::Category, "Editor")
->Attribute(AZ::Script::Attributes::Module, "shadermanagementconsole")
->Event("CreateDocument", &ShaderManagementConsoleDocumentSystemRequestBus::Events::CreateDocument)
->Event("DestroyDocument", &ShaderManagementConsoleDocumentSystemRequestBus::Events::DestroyDocument)
->Event("OpenDocument", &ShaderManagementConsoleDocumentSystemRequestBus::Events::OpenDocument)
->Event("CloseDocument", &ShaderManagementConsoleDocumentSystemRequestBus::Events::CloseDocument)
->Event("CloseAllDocuments", &ShaderManagementConsoleDocumentSystemRequestBus::Events::CloseAllDocuments)
->Event("SaveDocument", &ShaderManagementConsoleDocumentSystemRequestBus::Events::SaveDocument)
->Event("SaveDocumentAsCopy", &ShaderManagementConsoleDocumentSystemRequestBus::Events::SaveDocumentAsCopy)
->Event("SaveAllDocuments", &ShaderManagementConsoleDocumentSystemRequestBus::Events::SaveAllDocuments)
;
behaviorContext->EBus<ShaderManagementConsoleDocumentRequestBus>("ShaderManagementConsoleDocumentRequestBus")
->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common)
->Attribute(AZ::Script::Attributes::Category, "Editor")
->Attribute(AZ::Script::Attributes::Module, "shadermanagementconsole")
->Event("GetAbsolutePath", &ShaderManagementConsoleDocumentRequestBus::Events::GetAbsolutePath)
->Event("GetRelativePath", &ShaderManagementConsoleDocumentRequestBus::Events::GetRelativePath)
->Event("GetShaderOptionCount", &ShaderManagementConsoleDocumentRequestBus::Events::GetShaderOptionCount)
->Event("GetShaderOptionDescriptor", &ShaderManagementConsoleDocumentRequestBus::Events::GetShaderOptionDescriptor)
->Event("GetShaderVariantCount", &ShaderManagementConsoleDocumentRequestBus::Events::GetShaderVariantCount)
->Event("GetShaderVariantInfo", &ShaderManagementConsoleDocumentRequestBus::Events::GetShaderVariantInfo)
->Event("Open", &ShaderManagementConsoleDocumentRequestBus::Events::Open)
->Event("Close", &ShaderManagementConsoleDocumentRequestBus::Events::Close)
->Event("Save", &ShaderManagementConsoleDocumentRequestBus::Events::Save)
->Event("SaveAsCopy", &ShaderManagementConsoleDocumentRequestBus::Events::SaveAsCopy)
->Event("IsOpen", &ShaderManagementConsoleDocumentRequestBus::Events::IsOpen)
->Event("IsModified", &ShaderManagementConsoleDocumentRequestBus::Events::IsModified)
->Event("IsSavable", &ShaderManagementConsoleDocumentRequestBus::Events::IsSavable)
->Event("CanUndo", &ShaderManagementConsoleDocumentRequestBus::Events::CanUndo)
->Event("CanRedo", &ShaderManagementConsoleDocumentRequestBus::Events::CanRedo)
->Event("Undo", &ShaderManagementConsoleDocumentRequestBus::Events::Undo)
->Event("Redo", &ShaderManagementConsoleDocumentRequestBus::Events::Redo)
->Event("BeginEdit", &ShaderManagementConsoleDocumentRequestBus::Events::BeginEdit)
->Event("EndEdit", &ShaderManagementConsoleDocumentRequestBus::Events::EndEdit)
;
}
}
void ShaderManagementConsoleDocumentSystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
{
required.push_back(AZ_CRC("TargetManagerService", 0x6d5708bc));
required.push_back(AZ_CRC("AssetProcessorToolsConnection", 0x734669bc));
required.push_back(AZ_CRC("AssetDatabaseService", 0x3abf5601));
required.push_back(AZ_CRC("PropertyManagerService", 0x63a3d7ad));
required.push_back(AZ_CRC("RPISystem", 0xf2add773));
}
void ShaderManagementConsoleDocumentSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
{
provided.push_back(AZ_CRC("ShaderManagementConsoleDocumentSystemService"));
}
void ShaderManagementConsoleDocumentSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
{
incompatible.push_back(AZ_CRC("ShaderManagementConsoleDocumentSystemService"));
}
void ShaderManagementConsoleDocumentSystemComponent::Init()
{
}
void ShaderManagementConsoleDocumentSystemComponent::Activate()
{
m_documentMap.clear();
ShaderManagementConsoleDocumentSystemRequestBus::Handler::BusConnect();
AzFramework::TmMsgBus::Handler::BusConnect(AZ_CRC("OpenInShaderManagementConsole", 0x9f92aac8));
}
void ShaderManagementConsoleDocumentSystemComponent::Deactivate()
{
ShaderManagementConsoleDocumentSystemRequestBus::Handler::BusDisconnect();
AzFramework::TmMsgBus::Handler::BusDisconnect();
m_documentMap.clear();
}
AZ::Uuid ShaderManagementConsoleDocumentSystemComponent::CreateDocument()
{
auto document = AZStd::make_unique<ShaderManagementConsoleDocument>();
if (!document)
{
AZ_Error("ShaderManagementConsoleDocument", false, "Failed to create new document");
return AZ::Uuid::CreateNull();
}
AZ::Uuid documentId = document->GetId();
m_documentMap.emplace(documentId, document.release());
return documentId;
}
bool ShaderManagementConsoleDocumentSystemComponent::DestroyDocument(const AZ::Uuid& documentId)
{
return m_documentMap.erase(documentId) != 0;
}
void ShaderManagementConsoleDocumentSystemComponent::OnReceivedMsg(AzFramework::TmMsgPtr msg)
{
if (msg->GetId() == AZ_CRC("OpenInShaderManagementConsole", 0x9f92aac8))
{
const char* documentPath = reinterpret_cast<const char*>(msg->GetCustomBlob());
ShaderManagementConsoleDocumentSystemRequestBus::Broadcast(&ShaderManagementConsoleDocumentSystemRequestBus::Events::OpenDocument, documentPath);
}
else
{
AZ_Assert(false, "We received a message of an unrecognized class type!");
}
}
AZ::Uuid ShaderManagementConsoleDocumentSystemComponent::OpenDocument(AZStd::string_view path)
{
return OpenDocumentImpl(path, true);
}
bool ShaderManagementConsoleDocumentSystemComponent::CloseDocument(const AZ::Uuid& documentId)
{
bool isOpen = false;
ShaderManagementConsoleDocumentRequestBus::EventResult(isOpen, documentId, &ShaderManagementConsoleDocumentRequestBus::Events::IsOpen);
if (!isOpen)
{
return true;
}
bool isModified = false;
ShaderManagementConsoleDocumentRequestBus::EventResult(isModified, documentId, &ShaderManagementConsoleDocumentRequestBus::Events::IsModified);
if (isModified)
{
if (QMessageBox::question(QApplication::activeWindow(), "document has unsaved changes", "Would you like to close anyway?",
QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)
{
return false;
}
}
ShaderManagementConsoleDocumentResult closeResult = AZ::Success(AZStd::string("There is no active document"));
ShaderManagementConsoleDocumentRequestBus::EventResult(closeResult, documentId, &ShaderManagementConsoleDocumentRequestBus::Events::Close);
if (!closeResult)
{
QMessageBox::critical(QApplication::activeWindow(), "Failed to close document",
QString::fromUtf8(closeResult.GetError().data(), (int)closeResult.GetError().size()));
return false;
}
ShaderManagementConsoleDocumentSystemRequestBus::Broadcast(&ShaderManagementConsoleDocumentSystemRequestBus::Events::DestroyDocument, documentId);
return true;
}
bool ShaderManagementConsoleDocumentSystemComponent::CloseAllDocuments()
{
bool result = true;
auto documentMap = m_documentMap;
for (const auto& documentPair : documentMap)
{
if (!CloseDocument(documentPair.first))
{
result = false;
}
}
return result;
}
bool ShaderManagementConsoleDocumentSystemComponent::SaveDocument(const AZ::Uuid& documentId)
{
AZStd::string documentPath;
ShaderManagementConsoleDocumentRequestBus::EventResult(documentPath, documentId, &ShaderManagementConsoleDocumentRequestBus::Events::GetAbsolutePath);
const QFileInfo saveInfo(documentPath.c_str());
if (saveInfo.absoluteFilePath().isEmpty())
{
return false;
}
if (saveInfo.exists() && !saveInfo.isWritable())
{
QMessageBox::critical(QApplication::activeWindow(), "Error", QString("Unable to save document. File can not be overwritten."));
return false;
}
ShaderManagementConsoleDocumentResult result = AZ::Failure(AZStd::string("There is no active document"));
ShaderManagementConsoleDocumentRequestBus::EventResult(result, documentId, &ShaderManagementConsoleDocumentRequestBus::Events::Save);
if (!result)
{
QMessageBox::critical(QApplication::activeWindow(), "document not saved",
QString::fromUtf8(result.GetError().data(), (int)result.GetError().size()));
return false;
}
AZ_TracePrintf("ShaderManagementConsole", "%s\n", result.GetValue().c_str());
return true;
}
bool ShaderManagementConsoleDocumentSystemComponent::SaveDocumentAsCopy(const AZ::Uuid& documentId)
{
AZStd::string documentPath;
ShaderManagementConsoleDocumentRequestBus::EventResult(documentPath, documentId, &ShaderManagementConsoleDocumentRequestBus::Events::GetAbsolutePath);
const QFileInfo& saveInfo = AtomToolsFramework::GetSaveFileInfo(documentPath.c_str());
if (saveInfo.absoluteFilePath().isEmpty())
{
return false;
}
AZStd::string saveDocumentPath = saveInfo.absoluteFilePath().toUtf8().constData();
AzFramework::StringFunc::Path::Normalize(saveDocumentPath);
ShaderManagementConsoleDocumentResult result = AZ::Failure(AZStd::string("There is no active document"));
ShaderManagementConsoleDocumentRequestBus::EventResult(result, documentId, &ShaderManagementConsoleDocumentRequestBus::Events::SaveAsCopy, saveDocumentPath);
if (!result)
{
QMessageBox::critical(QApplication::activeWindow(), "document copy not saved",
QString::fromUtf8(result.GetError().data(), (int)result.GetError().size()));
return false;
}
AZ_TracePrintf("ShaderManagementConsole", "%s\n", result.GetValue().c_str());
return true;
}
bool ShaderManagementConsoleDocumentSystemComponent::SaveAllDocuments()
{
bool result = true;
for (const auto& documentPair : m_documentMap)
{
if (!SaveDocument(documentPair.first))
{
result = false;
}
}
return result;
}
AZ::Uuid ShaderManagementConsoleDocumentSystemComponent::OpenDocumentImpl(AZStd::string_view path, bool checkIfAlreadyOpen)
{
AZStd::string requestedPath = path;
if (requestedPath.empty() || !AzFramework::StringFunc::Path::Normalize(requestedPath))
{
QMessageBox::critical(QApplication::activeWindow(), "document path is invalid",
QString::fromUtf8(requestedPath.data(), (int)requestedPath.size()));
return AZ::Uuid::CreateNull();
}
// Determine if the file is already open and select it
if (checkIfAlreadyOpen)
{
for (const auto& documentPair : m_documentMap)
{
AZStd::string openDocumentPath;
ShaderManagementConsoleDocumentRequestBus::EventResult(openDocumentPath, documentPair.first, &ShaderManagementConsoleDocumentRequestBus::Events::GetAbsolutePath);
if (openDocumentPath == requestedPath)
{
ShaderManagementConsoleDocumentNotificationBus::Broadcast(&ShaderManagementConsoleDocumentNotificationBus::Events::OnDocumentOpened, documentPair.first);
return documentPair.first;
}
}
}
AZ::Uuid documentId = AZ::Uuid::CreateNull();
ShaderManagementConsoleDocumentSystemRequestBus::BroadcastResult(documentId, &ShaderManagementConsoleDocumentSystemRequestBus::Events::CreateDocument);
if (documentId.IsNull())
{
QMessageBox::critical(QApplication::activeWindow(), "Failed to create document",
QString::fromUtf8(requestedPath.data(), (int)requestedPath.size()));
return AZ::Uuid::CreateNull();
}
ShaderManagementConsoleDocumentResult openResult = AZ::Failure(AZStd::string("Failed to open document"));
ShaderManagementConsoleDocumentRequestBus::EventResult(openResult, documentId, &ShaderManagementConsoleDocumentRequestBus::Events::Open, requestedPath);
if (!openResult)
{
QMessageBox::critical(QApplication::activeWindow(), "Failed to open document",
QString::fromUtf8(openResult.GetError().data(), (int)openResult.GetError().size()));
ShaderManagementConsoleDocumentSystemRequestBus::Broadcast(&ShaderManagementConsoleDocumentSystemRequestBus::Events::DestroyDocument, documentId);
return AZ::Uuid::CreateNull();
}
return documentId;
}
}
@@ -0,0 +1,79 @@
/*
* 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 <AzCore/Component/Component.h>
#include <AzCore/std/smart_ptr/shared_ptr.h>
#include <AzFramework/TargetManagement/TargetManagementAPI.h>
#include <Atom/Document/ShaderManagementConsoleDocumentSystemRequestBus.h>
#include <Atom/RPI.Public/WindowContext.h>
#include <Document/ShaderManagementConsoleDocument.h>
AZ_PUSH_DISABLE_WARNING(4251 4800, "-Wunknown-warning-option") // disable warnings spawned by QT
#include <QFileInfo>
#include <QString>
AZ_POP_DISABLE_WARNING
namespace ShaderManagementConsole
{
//! ShaderManagementConsoleDocumentSystemComponent is the central component of the Shader Management Console Core gem
class ShaderManagementConsoleDocumentSystemComponent
: public AZ::Component
, private AzFramework::TmMsgBus::Handler
, private ShaderManagementConsoleDocumentSystemRequestBus::Handler
{
public:
AZ_COMPONENT(ShaderManagementConsoleDocumentSystemComponent, "{58ABE0AE-2710-41E2-ADFD-E2D67407427D}");
ShaderManagementConsoleDocumentSystemComponent();
~ShaderManagementConsoleDocumentSystemComponent() = default;
ShaderManagementConsoleDocumentSystemComponent(const ShaderManagementConsoleDocumentSystemComponent&) = delete;
ShaderManagementConsoleDocumentSystemComponent& operator =(const ShaderManagementConsoleDocumentSystemComponent&) = delete;
static void Reflect(AZ::ReflectContext* context);
static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
private:
////////////////////////////////////////////////////////////////////////
// AZ::Component interface implementation
void Init() override;
void Activate() override;
void Deactivate() override;
////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// TmMsgBus::Handler overrides...
void OnReceivedMsg(AzFramework::TmMsgPtr msg) override;
//////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
// ShaderManagementConsoleDocumentSystemRequestBus::Handler overrides...
AZ::Uuid CreateDocument() override;
bool DestroyDocument(const AZ::Uuid& documentId) override;
AZ::Uuid OpenDocument(AZStd::string_view path) override;
bool CloseDocument(const AZ::Uuid& documentId) override;
bool CloseAllDocuments() override;
bool SaveDocument(const AZ::Uuid& documentId) override;
bool SaveDocumentAsCopy(const AZ::Uuid& documentId) override;
bool SaveAllDocuments() override;
////////////////////////////////////////////////////////////////////////
AZ::Uuid OpenDocumentImpl(AZStd::string_view path, bool checkIfAlreadyOpen);
AZStd::unordered_map<AZ::Uuid, AZStd::shared_ptr<ShaderManagementConsoleDocument>> m_documentMap;
};
}