EditorScriptCanvasComponent.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/Component/NamedEntityId.h>
  11. #include <AzCore/IO/FileIO.h>
  12. #include <AzCore/Script/ScriptSystemBus.h>
  13. #include <AzCore/Serialization/EditContext.h>
  14. #include <AzCore/Serialization/IdUtils.h>
  15. #include <AzCore/Serialization/Json/RegistrationContext.h>
  16. #include <AzCore/Serialization/Utils.h>
  17. #include <AzFramework/Asset/AssetSystemBus.h>
  18. #include <AzFramework/StringFunc/StringFunc.h>
  19. #include <AzToolsFramework/API/EditorAssetSystemAPI.h>
  20. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  21. #include <Builder/ScriptCanvasBuilder.h>
  22. #include <Core/ScriptCanvasBus.h>
  23. #include <LyViewPaneNames.h>
  24. #include <ScriptCanvas/Asset/RuntimeAsset.h>
  25. #include <ScriptCanvas/Asset/RuntimeAsset.h>
  26. #include <ScriptCanvas/Assets/ScriptCanvasFileHandling.h>
  27. #include <ScriptCanvas/Bus/RequestBus.h>
  28. #include <ScriptCanvas/Components/EditorDeprecationData.h>
  29. #include <ScriptCanvas/Components/EditorGraph.h>
  30. #include <ScriptCanvas/Components/EditorGraphVariableManagerComponent.h>
  31. #include <ScriptCanvas/Components/EditorScriptCanvasComponent.h>
  32. #include <ScriptCanvas/Components/EditorUtils.h>
  33. #include <ScriptCanvas/Execution/RuntimeComponent.h>
  34. namespace ScriptCanvasEditor
  35. {
  36. EditorScriptCanvasComponent::EditorScriptCanvasComponent()
  37. : EditorScriptCanvasComponent(SourceHandle())
  38. {
  39. }
  40. EditorScriptCanvasComponent::EditorScriptCanvasComponent(const SourceHandle& sourceHandle)
  41. : m_configuration(sourceHandle)
  42. {
  43. }
  44. EditorScriptCanvasComponent::~EditorScriptCanvasComponent()
  45. {
  46. AzToolsFramework::EditorEntityContextNotificationBus::Handler::BusDisconnect();
  47. }
  48. void EditorScriptCanvasComponent::Activate()
  49. {
  50. using namespace AzToolsFramework;
  51. EditorComponentBase::Activate();
  52. AzToolsFramework::EditorEntityContextNotificationBus::Handler::BusConnect();
  53. m_handlerSourceCompiled = m_configuration.ConnectToSourceCompiled([this](const Configuration&)
  54. {
  55. this->InvalidatePropertyDisplay(AzToolsFramework::Refresh_EntireTree);
  56. });
  57. m_configuration.Refresh();
  58. InvalidatePropertyDisplay(AzToolsFramework::Refresh_EntireTree);
  59. }
  60. void EditorScriptCanvasComponent::Deactivate()
  61. {
  62. m_handlerSourceCompiled.Disconnect();
  63. EditorComponentBase::Deactivate();
  64. AzToolsFramework::EditorEntityContextNotificationBus::Handler::BusDisconnect();
  65. }
  66. void EditorScriptCanvasComponent::BuildGameEntity(AZ::Entity* gameEntity)
  67. {
  68. if (!m_configuration.HasSource())
  69. {
  70. return;
  71. }
  72. if (auto buildVariableOptional = m_configuration.CompileLatest())
  73. {
  74. auto runtimeComponent = gameEntity->CreateComponent<ScriptCanvas::RuntimeComponent>();
  75. runtimeComponent->TakeRuntimeDataOverrides(ConvertToRuntime(*buildVariableOptional));
  76. }
  77. else
  78. {
  79. AZ_Error("ScriptCanvasBuilder", false, "Runtime information did not build for ScriptCanvas Component using source: %s"
  80. , m_configuration.GetSource().ToString().c_str());
  81. }
  82. }
  83. void EditorScriptCanvasComponent::Reflect(AZ::ReflectContext* context)
  84. {
  85. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  86. {
  87. serializeContext->Class<EditorScriptCanvasComponent, EditorComponentBase>()
  88. ->Version(Version::Current, &Deprecated::EditorScriptCanvasComponentVersionConverter::Convert)
  89. ->Field("configuration", &EditorScriptCanvasComponent::m_configuration)
  90. ;
  91. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  92. {
  93. editContext->Class<EditorScriptCanvasComponent>("Script Canvas", "The Script Canvas component allows you to add a Script Canvas asset to a component, and have it execute on the specified entity.")
  94. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  95. ->Attribute(AZ::Edit::Attributes::Category, "Scripting")
  96. ->Attribute(AZ::Edit::Attributes::Icon, "Icons/ScriptCanvas/ScriptCanvas.svg")
  97. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Icons/ScriptCanvas/Viewport/ScriptCanvas.svg")
  98. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  99. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  100. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("UI"))
  101. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Level"))
  102. ->Attribute(AZ::Edit::Attributes::HelpPageURL, "https://o3de.org/docs/user-guide/components/reference/scripting/script-canvas/")
  103. ->DataElement(AZ::Edit::UIHandlers::Default, &EditorScriptCanvasComponent::m_configuration, "Configuration", "Script Selection and Property Overrides")
  104. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  105. ;
  106. }
  107. }
  108. if (AZ::JsonRegistrationContext* jsonContext = azrtti_cast<AZ::JsonRegistrationContext*>(context))
  109. {
  110. jsonContext->Serializer<AZ::EditorScriptCanvasComponentSerializer>()->HandlesType<EditorScriptCanvasComponent>();
  111. }
  112. }
  113. void EditorScriptCanvasComponent::SetPrimaryAsset(const AZ::Data::AssetId& assetId)
  114. {
  115. m_configuration.Refresh(SourceHandle(nullptr, assetId.m_guid));
  116. }
  117. }