/* * Copyright (c) Contributors to the Open 3D Engine Project. * For complete copyright and license terms please see the LICENSE at the root of this distribution. * * SPDX-License-Identifier: Apache-2.0 OR MIT * */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace ScriptCanvasEditor { EditorScriptCanvasComponent::EditorScriptCanvasComponent() : EditorScriptCanvasComponent(SourceHandle()) { } EditorScriptCanvasComponent::EditorScriptCanvasComponent(const SourceHandle& sourceHandle) : m_configuration(sourceHandle) { } EditorScriptCanvasComponent::~EditorScriptCanvasComponent() { AzToolsFramework::EditorEntityContextNotificationBus::Handler::BusDisconnect(); } void EditorScriptCanvasComponent::Activate() { using namespace AzToolsFramework; EditorComponentBase::Activate(); auto entityId = GetEntityId(); if (entityId.IsValid()) { EditorScriptCanvasComponentRequestBus::Handler::BusConnect(entityId); } AzToolsFramework::EditorEntityContextNotificationBus::Handler::BusConnect(); m_handlerSourceCompiled = m_configuration.ConnectToSourceCompiled([this](const Configuration&) { this->InvalidatePropertyDisplay(AzToolsFramework::Refresh_EntireTree); }); m_handlerSourcePropertiesChanged = m_configuration.ConnectToPropertiesChanged([this](const Configuration&) { this->SetDirty(); }); m_configuration.Refresh(); InvalidatePropertyDisplay(AzToolsFramework::Refresh_EntireTree); } void EditorScriptCanvasComponent::Deactivate() { m_handlerSourceCompiled.Disconnect(); EditorComponentBase::Deactivate(); EditorScriptCanvasComponentRequestBus::Handler::BusDisconnect(); AzToolsFramework::EditorEntityContextNotificationBus::Handler::BusDisconnect(); } void EditorScriptCanvasComponent::BuildGameEntity(AZ::Entity* gameEntity) { if (!m_configuration.HasSource()) { return; } if (auto buildVariableOptional = m_configuration.CompileLatest()) { auto runtimeComponent = gameEntity->CreateComponent(); runtimeComponent->TakeRuntimeDataOverrides(ConvertToRuntime(*buildVariableOptional)); } else { AZ_Error("ScriptCanvasBuilder", false, "Runtime information did not build for ScriptCanvas Component using source: %s" , m_configuration.GetSource().ToString().c_str()); } } void EditorScriptCanvasComponent::Reflect(AZ::ReflectContext* context) { if (auto serializeContext = azrtti_cast(context)) { serializeContext->Class() ->Version(Version::Current, &Deprecated::EditorScriptCanvasComponentVersionConverter::Convert) ->Field("configuration", &EditorScriptCanvasComponent::m_configuration) ; if (AZ::EditContext* editContext = serializeContext->GetEditContext()) { editContext->Class("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.") ->ClassElement(AZ::Edit::ClassElements::EditorData, "") ->Attribute(AZ::Edit::Attributes::Category, "Scripting") ->Attribute(AZ::Edit::Attributes::Icon, "Icons/ScriptCanvas/ScriptCanvas.svg") ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Icons/ScriptCanvas/Viewport/ScriptCanvas.svg") ->Attribute(AZ::Edit::Attributes::AutoExpand, true) ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game")) ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("UI")) ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Level")) ->Attribute(AZ::Edit::Attributes::HelpPageURL, "https://www.o3de.org/docs/user-guide/components/reference/scripting/script-canvas/") ->DataElement(AZ::Edit::UIHandlers::Default, &EditorScriptCanvasComponent::m_configuration, "Configuration", "Script Selection and Property Overrides") ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly) ; } } if (AZ::JsonRegistrationContext* jsonContext = azrtti_cast(context)) { jsonContext->Serializer()->HandlesType(); } } void EditorScriptCanvasComponent::SetPrimaryAsset(const AZ::Data::AssetId& assetId) { SetDirty(); m_configuration.Refresh(SourceHandle(nullptr, assetId.m_guid)); } void EditorScriptCanvasComponent::SetAssetId(const SourceHandle& assetId) { if (assetId.IsDescriptionValid() && (!m_configuration.HasSource() || m_configuration.GetSource().Describe() != assetId.Describe())) { this->SetPrimaryAsset(assetId.Id()); } } bool EditorScriptCanvasComponent::HasAssetId() const { return m_configuration.HasSource() && m_configuration.GetSource().IsDescriptionValid(); } }