3
0

PassCanvasApplication.cpp 11 KB

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