PassCanvasApplication.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 <Atom/RPI.Reflect/Image/StreamingImageAsset.h>
  9. #include <AtomToolsFramework/Document/AtomToolsAnyDocument.h>
  10. #include <AtomToolsFramework/Document/AtomToolsDocumentSystemRequestBus.h>
  11. #include <AtomToolsFramework/Graph/DynamicNode/DynamicNodeUtil.h>
  12. #include <AtomToolsFramework/Graph/GraphDocument.h>
  13. #include <AtomToolsFramework/Graph/GraphDocumentView.h>
  14. #include <AtomToolsFramework/Util/Util.h>
  15. #include <AzCore/Math/Color.h>
  16. #include <AzCore/Math/Vector2.h>
  17. #include <AzCore/Math/Vector3.h>
  18. #include <AzCore/Math/Vector4.h>
  19. #include <AzCore/RTTI/RTTI.h>
  20. #include <AzCore/Utils/Utils.h>
  21. #include <AzCore/std/smart_ptr/make_shared.h>
  22. #include <Document/PassGraphCompiler.h>
  23. #include <GraphModel/Model/DataType.h>
  24. #include <PassCanvasApplication.h>
  25. #include <Window/PassCanvasMainWindow.h>
  26. #include <QLabel>
  27. #if defined(EXTERNAL_CRASH_REPORTING)
  28. #include <ToolsCrashHandler.h>
  29. #endif
  30. void InitPassCanvasResources()
  31. {
  32. // Must register qt resources from other modules
  33. Q_INIT_RESOURCE(PassCanvas);
  34. Q_INIT_RESOURCE(InspectorWidget);
  35. Q_INIT_RESOURCE(AtomToolsAssetBrowser);
  36. Q_INIT_RESOURCE(GraphView);
  37. }
  38. namespace PassCanvas
  39. {
  40. static const char* GetBuildTargetName()
  41. {
  42. #if !defined(LY_CMAKE_TARGET)
  43. #error "LY_CMAKE_TARGET must be defined in order to add this source file to a CMake executable target"
  44. #endif
  45. return LY_CMAKE_TARGET;
  46. }
  47. PassCanvasApplication::PassCanvasApplication(int* argc, char*** argv)
  48. : Base(GetBuildTargetName(), argc, argv)
  49. {
  50. #if defined(EXTERNAL_CRASH_REPORTING)
  51. CrashHandler::ToolsCrashHandler::InitCrashHandler("PassCanvas", {});
  52. #endif
  53. InitPassCanvasResources();
  54. QApplication::setOrganizationName("O3DE");
  55. QApplication::setApplicationName("O3DE Pass Canvas");
  56. QApplication::setWindowIcon(QIcon(":/Icons/application.svg"));
  57. AzToolsFramework::EditorWindowRequestBus::Handler::BusConnect();
  58. AtomToolsFramework::AtomToolsDocumentNotificationBus::Handler::BusConnect(m_toolId);
  59. }
  60. PassCanvasApplication::~PassCanvasApplication()
  61. {
  62. AtomToolsFramework::AtomToolsDocumentNotificationBus::Handler::BusDisconnect();
  63. AzToolsFramework::EditorWindowRequestBus::Handler::BusDisconnect();
  64. m_window.reset();
  65. }
  66. void PassCanvasApplication::Reflect(AZ::ReflectContext* context)
  67. {
  68. Base::Reflect(context);
  69. PassGraphCompiler::Reflect(context);
  70. if (auto serialize = azrtti_cast<AZ::SerializeContext*>(context))
  71. {
  72. serialize->RegisterGenericType<AZStd::array<AZ::Vector2, 2>>();
  73. serialize->RegisterGenericType<AZStd::array<AZ::Vector3, 3>>();
  74. serialize->RegisterGenericType<AZStd::array<AZ::Vector4, 3>>();
  75. serialize->RegisterGenericType<AZStd::array<AZ::Vector4, 4>>();
  76. }
  77. }
  78. const char* PassCanvasApplication::GetCurrentConfigurationName() const
  79. {
  80. #if defined(_RELEASE)
  81. return "ReleasePassCanvas";
  82. #elif defined(_DEBUG)
  83. return "DebugPassCanvas";
  84. #else
  85. return "ProfilePassCanvas";
  86. #endif
  87. }
  88. void PassCanvasApplication::StartCommon(AZ::Entity* systemEntity)
  89. {
  90. Base::StartCommon(systemEntity);
  91. InitDynamicNodeManager();
  92. InitDynamicNodeEditData();
  93. InitSharedGraphContext();
  94. InitGraphViewSettings();
  95. InitPassGraphDocumentType();
  96. InitMainWindow();
  97. InitDefaultDocument();
  98. }
  99. void PassCanvasApplication::Destroy()
  100. {
  101. // Save all of the graph view configuration settings to the settings registry.
  102. AtomToolsFramework::SetSettingsObject("/O3DE/Atom/GraphView/ViewSettings", m_graphViewSettingsPtr);
  103. m_graphViewSettingsPtr.reset();
  104. m_window.reset();
  105. m_viewportSettingsSystem.reset();
  106. m_graphContext.reset();
  107. m_dynamicNodeManager.reset();
  108. Base::Destroy();
  109. }
  110. AZStd::vector<AZStd::string> PassCanvasApplication::GetCriticalAssetFilters() const
  111. {
  112. return AZStd::vector<AZStd::string>({ "passes/", "config/", "MaterialEditor/", "PassCanvas/" });
  113. }
  114. QWidget* PassCanvasApplication::GetAppMainWindow()
  115. {
  116. return m_window.get();
  117. }
  118. void PassCanvasApplication::InitDynamicNodeManager()
  119. {
  120. // Instantiate the dynamic node manager to register all dynamic node configurations and data types used in this tool
  121. m_dynamicNodeManager.reset(aznew AtomToolsFramework::DynamicNodeManager(m_toolId));
  122. // Register all data types required by Pass Canvas nodes with the dynamic node manager
  123. m_dynamicNodeManager->RegisterDataTypes({
  124. AZStd::make_shared<GraphModel::DataType>(AZ_CRC_CE("bool"), bool{}, "bool"),
  125. AZStd::make_shared<GraphModel::DataType>(AZ_CRC_CE("int"), int32_t{}, "int"),
  126. AZStd::make_shared<GraphModel::DataType>(AZ_CRC_CE("uint"), uint32_t{}, "uint"),
  127. AZStd::make_shared<GraphModel::DataType>(AZ_CRC_CE("float"), float{}, "float"),
  128. AZStd::make_shared<GraphModel::DataType>(AZ_CRC_CE("float2"), AZ::Vector2{}, "float2"),
  129. AZStd::make_shared<GraphModel::DataType>(AZ_CRC_CE("float3"), AZ::Vector3{}, "float3"),
  130. AZStd::make_shared<GraphModel::DataType>(AZ_CRC_CE("float4"), AZ::Vector4{}, "float4"),
  131. AZStd::make_shared<GraphModel::DataType>(AZ_CRC_CE("float2x2"), AZStd::array<AZ::Vector2, 2>{ AZ::Vector2(1.0f, 0.0f), AZ::Vector2(0.0f, 1.0f) }, "float2x2"),
  132. AZStd::make_shared<GraphModel::DataType>(AZ_CRC_CE("float3x3"), AZStd::array<AZ::Vector3, 3>{ AZ::Vector3(1.0f, 0.0f, 0.0f), AZ::Vector3(0.0f, 1.0f, 0.0f), AZ::Vector3(0.0f, 0.0f, 1.0f) }, "float3x3"),
  133. AZStd::make_shared<GraphModel::DataType>(AZ_CRC_CE("float4x3"), AZStd::array<AZ::Vector4, 3>{ AZ::Vector4(1.0f, 0.0f, 0.0f, 0.0f), AZ::Vector4(0.0f, 1.0f, 0.0f, 0.0f), AZ::Vector4(0.0f, 0.0f, 1.0f, 0.0f) }, "float4x3"),
  134. AZStd::make_shared<GraphModel::DataType>(AZ_CRC_CE("float4x4"), AZStd::array<AZ::Vector4, 4>{ AZ::Vector4(1.0f, 0.0f, 0.0f, 0.0f), AZ::Vector4(0.0f, 1.0f, 0.0f, 0.0f), AZ::Vector4(0.0f, 0.0f, 1.0f, 0.0f), AZ::Vector4(0.0f, 0.0f, 0.0f, 1.0f) }, "float4x4"),
  135. AZStd::make_shared<GraphModel::DataType>(AZ_CRC_CE("color"), AZ::Color::CreateOne(), "color"),
  136. AZStd::make_shared<GraphModel::DataType>(AZ_CRC_CE("string"), AZStd::string{}, "string"),
  137. });
  138. // Search the project and gems for dynamic node configurations and register them with the manager
  139. m_dynamicNodeManager->LoadConfigFiles("passgraphnode");
  140. }
  141. void PassCanvasApplication::InitDynamicNodeEditData()
  142. {
  143. }
  144. void PassCanvasApplication::InitSharedGraphContext()
  145. {
  146. // Each graph document creates its own graph context but we want to use a shared graph context instead to avoid data duplication
  147. m_graphContext = AZStd::make_shared<GraphModel::GraphContext>(
  148. "Pass Graph", ".passgraph", m_dynamicNodeManager->GetRegisteredDataTypes());
  149. m_graphContext->CreateModuleGraphManager();
  150. }
  151. void PassCanvasApplication::InitGraphViewSettings()
  152. {
  153. // This configuration data is passed through the main window and graph views to setup translation data, styling, and node palettes
  154. m_graphViewSettingsPtr = AtomToolsFramework::GetSettingsObject(
  155. "/O3DE/Atom/GraphView/ViewSettings", AZStd::make_shared<AtomToolsFramework::GraphViewSettings>());
  156. // Initialize the application specific graph view settings that are not serialized.
  157. m_graphViewSettingsPtr->m_translationPath = "@products@/passcanvas/translation/passcanvas_en_us.qm";
  158. m_graphViewSettingsPtr->m_styleManagerPath = "PassCanvas/StyleSheet/passcanvas_style.json";
  159. m_graphViewSettingsPtr->m_nodeMimeType = "PassCanvas/node-palette-mime-event";
  160. m_graphViewSettingsPtr->m_nodeSaveIdentifier = "PassCanvas/ContextMenu";
  161. m_graphViewSettingsPtr->m_createNodeTreeItemsFn = [](const AZ::Crc32& toolId)
  162. {
  163. GraphCanvas::GraphCanvasTreeItem* rootTreeItem = {};
  164. AtomToolsFramework::DynamicNodeManagerRequestBus::EventResult(
  165. rootTreeItem, toolId, &AtomToolsFramework::DynamicNodeManagerRequestBus::Events::CreateNodePaletteTree);
  166. return rootTreeItem;
  167. };
  168. // Initialize the default group preset names and colors needed by the graph canvas view to create node groups.
  169. const AZStd::map<AZStd::string, AZ::Color> defaultGroupPresets = AtomToolsFramework::GetSettingsObject(
  170. "/O3DE/Atom/GraphView/DefaultGroupPresets",
  171. AZStd::map<AZStd::string, AZ::Color>{});
  172. // Connect the graph view settings to the required buses so that they can be accessed throughout the application.
  173. m_graphViewSettingsPtr->Initialize(m_toolId, defaultGroupPresets);
  174. }
  175. void PassCanvasApplication::InitPassGraphDocumentType()
  176. {
  177. // Acquiring default Pass Canvas document type info so that it can be customized before registration
  178. auto documentTypeInfo = AtomToolsFramework::GraphDocument::BuildDocumentTypeInfo(
  179. "Pass Graph",
  180. { "passgraph" },
  181. { "passgraphtemplate" },
  182. AtomToolsFramework::GetPathWithoutAlias(AtomToolsFramework::GetSettingsValue<AZStd::string>(
  183. "/O3DE/Atom/PassCanvas/DefaultPassGraphTemplate",
  184. "@gemroot:PassCanvas@/Assets/PassCanvas/GraphData/blank_graph.passgraphtemplate")),
  185. m_graphContext,
  186. [toolId = m_toolId](){ return AZStd::make_shared<PassGraphCompiler>(toolId); });
  187. // Overriding documentview factory function to create graph view
  188. documentTypeInfo.m_documentViewFactoryCallback = [this](const AZ::Crc32& toolId, const AZ::Uuid& documentId)
  189. {
  190. m_window->AddDocumentTab(
  191. documentId, aznew AtomToolsFramework::GraphDocumentView(toolId, documentId, m_graphViewSettingsPtr, m_window.get()));
  192. return true;
  193. };
  194. AtomToolsFramework::AtomToolsDocumentSystemRequestBus::Event(
  195. m_toolId, &AtomToolsFramework::AtomToolsDocumentSystemRequestBus::Handler::RegisterDocumentType, documentTypeInfo);
  196. }
  197. void PassCanvasApplication::InitMainWindow()
  198. {
  199. m_viewportSettingsSystem.reset(aznew AtomToolsFramework::EntityPreviewViewportSettingsSystem(m_toolId));
  200. m_window.reset(aznew PassCanvasMainWindow(m_toolId, m_graphViewSettingsPtr));
  201. m_window->show();
  202. }
  203. void PassCanvasApplication::InitDefaultDocument()
  204. {
  205. // Create an untitled, empty graph document as soon as the application starts so the user can begin creating immediately.
  206. if (AtomToolsFramework::GetSettingsValue("/O3DE/Atom/PassCanvas/CreateDefaultDocumentOnStart", true))
  207. {
  208. AZ::Uuid documentId = AZ::Uuid::CreateNull();
  209. AtomToolsFramework::AtomToolsDocumentSystemRequestBus::EventResult(
  210. documentId,
  211. m_toolId,
  212. &AtomToolsFramework::AtomToolsDocumentSystemRequestBus::Handler::CreateDocumentFromTypeName,
  213. "Pass Graph");
  214. AtomToolsFramework::AtomToolsDocumentNotificationBus::Event(
  215. m_toolId, &AtomToolsFramework::AtomToolsDocumentNotificationBus::Handler::OnDocumentOpened, documentId);
  216. }
  217. }
  218. } // namespace PassCanvas