Configuration.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 <AzCore/Asset/AssetManagerBus.h>
  9. #include <AzCore/Component/ComponentApplicationBus.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <AzCore/Serialization/IdUtils.h>
  12. #include <AzCore/Serialization/Json/RegistrationContext.h>
  13. #include <AzCore/Serialization/Utils.h>
  14. #include <AzFramework/Asset/AssetSystemBus.h>
  15. #include <AzToolsFramework/API/EditorAssetSystemAPI.h>
  16. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  17. #include <LyViewPaneNames.h>
  18. #include <ScriptCanvas/Assets/ScriptCanvasFileHandling.h>
  19. #include <Editor/Framework/Configuration.h>
  20. #include <ScriptCanvas/Components/EditorUtils.h>
  21. #include <ScriptCanvas/Bus/RequestBus.h>
  22. namespace ScriptCanvasEditor
  23. {
  24. class OnScopeEnd
  25. {
  26. public:
  27. using ScopeEndFunctor = std::function<void()>;
  28. private:
  29. ScopeEndFunctor m_functor;
  30. public:
  31. OnScopeEnd(ScopeEndFunctor&& functor)
  32. : m_functor(AZStd::move(functor))
  33. {}
  34. OnScopeEnd(const ScopeEndFunctor& functor)
  35. : m_functor(functor)
  36. {}
  37. ~OnScopeEnd()
  38. {
  39. m_functor();
  40. }
  41. };
  42. Configuration::Configuration()
  43. : Configuration(SourceHandle())
  44. {
  45. }
  46. Configuration::Configuration(const SourceHandle& sourceHandle)
  47. : m_sourceHandle(sourceHandle)
  48. {
  49. AzFramework::AssetCatalogEventBus::Handler::BusConnect();
  50. Refresh(m_sourceHandle);
  51. }
  52. Configuration::~Configuration()
  53. {
  54. ScriptCanvasBuilder::DataSystemSourceNotificationsBus::Handler::BusDisconnect();
  55. AzFramework::AssetCatalogEventBus::Handler::BusDisconnect();
  56. }
  57. void Configuration::ClearVariables()
  58. {
  59. m_propertyOverrides.Clear();
  60. }
  61. const ScriptCanvasBuilder::BuildVariableOverrides* Configuration::CompileLatest()
  62. {
  63. return CompileLatestInternal() == BuildStatusValidation::Good ? &m_propertyOverrides : nullptr;
  64. }
  65. Configuration::BuildStatusValidation Configuration::CompileLatestInternal()
  66. {
  67. ScriptCanvasBuilder::BuilderSourceResult result;
  68. ScriptCanvasBuilder::DataSystemSourceRequestsBus::BroadcastResult
  69. ( result
  70. , &ScriptCanvasBuilder::DataSystemSourceRequests::CompileBuilderData
  71. , m_sourceHandle);
  72. const auto validation = ValidateBuildResult(result);
  73. if (validation == BuildStatusValidation::Good)
  74. {
  75. MergeWithLatestCompilation(*result.data);
  76. }
  77. return validation;
  78. }
  79. AZ::EventHandler<const Configuration&> Configuration::ConnectToPropertiesChanged(AZStd::function<void(const Configuration&)>&& function) const
  80. {
  81. AZ::EventHandler<const Configuration&> handler(function);
  82. handler.Connect(m_eventPropertiesChanged);
  83. return handler;
  84. }
  85. AZ::EventHandler<const Configuration&> Configuration::ConnectToSourceCompiled(AZStd::function<void(const Configuration&)>&& function) const
  86. {
  87. AZ::EventHandler<const Configuration&> handler(function);
  88. handler.Connect(m_eventSourceCompiled);
  89. return handler;
  90. }
  91. AZ::EventHandler<const Configuration&> Configuration::ConnectToSourceFailed(AZStd::function<void(const Configuration&)>&& function) const
  92. {
  93. AZ::EventHandler<const Configuration&> handler(function);
  94. handler.Connect(m_eventSourceFailed);
  95. return handler;
  96. }
  97. const ScriptCanvasBuilder::BuildVariableOverrides& Configuration::GetOverrides() const
  98. {
  99. return m_propertyOverrides;
  100. }
  101. const SourceHandle& Configuration::GetSource() const
  102. {
  103. return m_sourceHandle;
  104. }
  105. bool Configuration::HasSource() const
  106. {
  107. return m_sourceHandle.IsDescriptionValid();
  108. }
  109. void Configuration::MergeWithLatestCompilation(const ScriptCanvasBuilder::BuildVariableOverrides& buildData)
  110. {
  111. ScriptCanvasBuilder::BuildVariableOverrides source(buildData);
  112. if (!m_propertyOverrides.IsEmpty())
  113. {
  114. source.CopyPreviousOverriddenValues(m_propertyOverrides);
  115. }
  116. m_propertyOverrides = AZStd::move(source);
  117. m_propertyOverrides.SetHandlesToDescription();
  118. }
  119. AZ::u32 Configuration::OnEditorChangeProperties()
  120. {
  121. m_eventPropertiesChanged.Signal(*this);
  122. return AZ::Edit::PropertyRefreshLevels::ValuesOnly;
  123. }
  124. AZ::u32 Configuration::OnEditorChangeSource()
  125. {
  126. ClearVariables();
  127. Refresh(m_sourceHandle);
  128. return AZ::Edit::PropertyRefreshLevels::EntireTree;
  129. }
  130. void Configuration::OpenEditor([[maybe_unused]] const AZ::Data::AssetId& assetId, const AZ::Data::AssetType&)
  131. {
  132. AzToolsFramework::OpenViewPane(LyViewPane::ScriptCanvas);
  133. if (m_sourceHandle.IsDescriptionValid())
  134. {
  135. AZ::Outcome<int, AZStd::string> openOutcome = AZ::Failure(AZStd::string());
  136. GeneralRequestBus::BroadcastResult(openOutcome, &GeneralRequests::OpenScriptCanvasAsset, m_sourceHandle, Tracker::ScriptCanvasFileState::UNMODIFIED, -1);
  137. if (!openOutcome)
  138. {
  139. AZ_Warning("Script Canvas", openOutcome, "%s", openOutcome.GetError().data());
  140. }
  141. }
  142. }
  143. void Configuration::Reflect(AZ::ReflectContext* context)
  144. {
  145. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  146. {
  147. serializeContext->Class<Configuration>()
  148. ->Field("sourceHandle", &Configuration::m_sourceHandle)
  149. ->Field("sourceName", &Configuration::m_sourceName)
  150. ->Field("propertyOverrides", &Configuration::m_propertyOverrides)
  151. ;
  152. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  153. {
  154. editContext->Class<Configuration>("Script Canvas Configuration", "Select a Script Canvas graph and configure its properties.")
  155. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  156. ->Attribute(AZ::Edit::Attributes::Category, "Scripting")
  157. ->Attribute(AZ::Edit::Attributes::Icon, "Icons/ScriptCanvas/ScriptCanvas.svg")
  158. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Icons/ScriptCanvas/Viewport/ScriptCanvas.svg")
  159. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  160. ->DataElement(AZ::Edit::UIHandlers::Default, &Configuration::m_sourceHandle, "Source", "Script Canvas source file associated with this component")
  161. ->Attribute("BrowseIcon", ":/stylesheet/img/UI20/browse-edit-select-files.svg")
  162. ->Attribute("EditButton", "")
  163. ->Attribute("EditDescription", "Open in Script Canvas Editor")
  164. ->Attribute("EditCallback", &Configuration::OpenEditor)
  165. ->Attribute(AZ::Edit::Attributes::AssetPickerTitle, "Script Canvas")
  166. ->Attribute(AZ::Edit::Attributes::SourceAssetFilterPattern, "*.scriptcanvas")
  167. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &Configuration::OnEditorChangeSource)
  168. // ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::HideChildren) // or just ::Hide
  169. ->DataElement(AZ::Edit::UIHandlers::Default, &Configuration::m_propertyOverrides, "Properties", "Script Canvas Graph Properties")
  170. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &Configuration::OnEditorChangeProperties)
  171. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  172. ;
  173. }
  174. }
  175. }
  176. void Configuration::Refresh()
  177. {
  178. Refresh(m_sourceHandle);
  179. }
  180. void Configuration::Refresh(const SourceHandle& sourceHandle)
  181. {
  182. ScriptCanvasBuilder::DataSystemSourceNotificationsBus::Handler::BusDisconnect();
  183. m_sourceHandle = sourceHandle.Describe();
  184. CompleteDescriptionInPlace(m_sourceHandle);
  185. if (m_sourceHandle.IsDescriptionValid())
  186. {
  187. m_sourceName = m_sourceHandle.RelativePath().Filename().Native();
  188. }
  189. m_eventPropertiesChanged.Signal(*this);
  190. if (!m_sourceHandle.Id().IsNull())
  191. {
  192. ScriptCanvasBuilder::DataSystemSourceNotificationsBus::Handler::BusConnect(m_sourceHandle.Id());
  193. if (!m_sourceHandle.RelativePath().empty())
  194. {
  195. const auto validation = CompileLatestInternal();
  196. if (validation == BuildStatusValidation::Good)
  197. {
  198. m_eventSourceCompiled.Signal(*this);
  199. return;
  200. }
  201. else if (validation == BuildStatusValidation::IncompatibleScript)
  202. {
  203. AZ_Error("ScriptCanvas", false, "Selected Script is not compatible with this configuration.");
  204. m_eventIncompatibleScript.Signal(*this);
  205. return;
  206. }
  207. else
  208. {
  209. AZ_Warning("ScriptCanvasBuilder", false, "Runtime information did not build for ScriptCanvas Component using source: %s"
  210. , m_sourceHandle.ToString().c_str());
  211. }
  212. }
  213. else
  214. {
  215. AZ_Warning("ScriptCanvasBuilder", false, "Configuration had no valid path for %s and won't compile or expose variables."
  216. , m_sourceHandle.ToString().c_str());
  217. }
  218. }
  219. else
  220. {
  221. AZ_Warning("ScriptCanvas", m_sourceHandle.RelativePath().empty()
  222. , "Configuration had no valid ID for %s and won't compile or expose variables.", m_sourceHandle.RelativePath().c_str());
  223. }
  224. m_eventSourceFailed.Signal(*this);
  225. }
  226. void Configuration::SourceFileChanged
  227. ( const ScriptCanvasBuilder::BuilderSourceResult& result
  228. , [[maybe_unused]] AZStd::string_view relativePath
  229. , [[maybe_unused]] AZStd::string_view scanFolder)
  230. {
  231. const auto validation = ValidateBuildResult(result);
  232. if (validation == BuildStatusValidation::Good)
  233. {
  234. MergeWithLatestCompilation(*result.data);
  235. m_eventSourceCompiled.Signal(*this);
  236. }
  237. else if (validation == BuildStatusValidation::Bad)
  238. {
  239. m_eventSourceFailed.Signal(*this);
  240. }
  241. else if (validation == BuildStatusValidation::IncompatibleScript)
  242. {
  243. m_eventIncompatibleScript.Signal(*this);
  244. }
  245. }
  246. void Configuration::SourceFileFailed([[maybe_unused]] AZStd::string_view relativePath
  247. , [[maybe_unused]] AZStd::string_view scanFolder)
  248. {
  249. m_eventSourceFailed.Signal(*this);
  250. // display error icon
  251. }
  252. void Configuration::SourceFileRemoved([[maybe_unused]] AZStd::string_view relativePath
  253. , [[maybe_unused]] AZStd::string_view scanFolder)
  254. {
  255. m_eventSourceFailed.Signal(*this);
  256. // display removed icon
  257. }
  258. Configuration::BuildStatusValidation Configuration::ValidateBuildResult(const ScriptCanvasBuilder::BuilderSourceResult& result) const
  259. {
  260. if (result.status != ScriptCanvasBuilder::BuilderSourceStatus::Good || !result.data)
  261. {
  262. AZ_Error
  263. ( "ScriptCanvas"
  264. , !(result.status == ScriptCanvasBuilder::BuilderSourceStatus::Good && result.data)
  265. , "Configuration::SourceFileChanged received good status with no data");
  266. return BuildStatusValidation::Bad;
  267. }
  268. else if (result.data->m_isComponentScript && !m_acceptsComponentScript)
  269. {
  270. // #scriptcanvas_component_extension
  271. return BuildStatusValidation::IncompatibleScript;
  272. }
  273. else
  274. {
  275. return BuildStatusValidation::Good;
  276. }
  277. }
  278. // #scriptcanvas_component_extension ...
  279. bool Configuration::AcceptsComponentScript() const
  280. {
  281. return m_acceptsComponentScript;
  282. }
  283. void Configuration::SetAcceptsComponentScript(bool value)
  284. {
  285. m_acceptsComponentScript = value;
  286. }
  287. AZ::EventHandler<const Configuration&> Configuration::ConnectToIncompatilbleScript(AZStd::function<void(const Configuration&)>&& function) const
  288. {
  289. AZ::EventHandler<const Configuration&> handler(function);
  290. handler.Connect(m_eventIncompatibleScript);
  291. return handler;
  292. }
  293. // ... #scriptcanvas_component_extension
  294. }