123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- /*
- * 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 "EntityMimeDataHandler.h"
- #include <QMimeData>
- #include <QDragEnterEvent>
- #include <QDragMoveEvent>
- #include <QDropEvent>
- #include <QGraphicsView>
- #include <Editor/Nodes/NodeCreateUtils.h>
- #include <ScriptCanvas/Bus/RequestBus.h>
- #include <ScriptCanvas/Bus/NodeIdPair.h>
- #include <AzCore/Component/ComponentApplicationBus.h>
- #include <AzToolsFramework/ToolsComponents/EditorEntityIdContainer.h>
- #include <AzToolsFramework/API/ToolsApplicationAPI.h>
- #include <AzCore/Component/Entity.h>
- #include <GraphCanvas/Components/SceneBus.h>
- #include <GraphCanvas/Components/GridBus.h>
- #include <GraphCanvas/Components/ViewBus.h>
- #include <GraphCanvas/Components/VisualBus.h>
- #include <Core/GraphBus.h>
- #include <ScriptCanvas/Variable/VariableBus.h>
- #include <GraphCanvas/Utils/GraphUtils.h>
- namespace ScriptCanvasEditor
- {
- namespace EntityMimeData
- {
- static QString GetMimeType()
- {
- return AzToolsFramework::EditorEntityIdContainer::GetMimeType();
- }
- }
- void EntityMimeDataHandler::Reflect(AZ::ReflectContext* context)
- {
- AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
- if (!serializeContext)
- {
- return;
- }
- serializeContext->Class<EntityMimeDataHandler, AZ::Component>()
- ->Version(1)
- ;
- }
- EntityMimeDataHandler::EntityMimeDataHandler()
- {}
- void EntityMimeDataHandler::Activate()
- {
- GraphCanvas::SceneMimeDelegateHandlerRequestBus::Handler::BusConnect(GetEntityId());
- }
- void EntityMimeDataHandler::Deactivate()
- {
- GraphCanvas::SceneMimeDelegateHandlerRequestBus::Handler::BusDisconnect();
- }
-
- bool EntityMimeDataHandler::IsInterestedInMimeData(const AZ::EntityId& sceneId, const QMimeData* mimeData)
- {
- (void)sceneId;
- return mimeData->hasFormat(EntityMimeData::GetMimeType());
- }
- void EntityMimeDataHandler::HandleMove(const AZ::EntityId&, const QPointF&, const QMimeData*)
- {
- }
- void EntityMimeDataHandler::HandleDrop(const AZ::EntityId& graphCanvasGraphId, const QPointF& dropPoint, const QMimeData* mimeData)
- {
- if (!mimeData->hasFormat(EntityMimeData::GetMimeType()))
- {
- return;
- }
- QByteArray arrayData = mimeData->data(EntityMimeData::GetMimeType());
- AzToolsFramework::EditorEntityIdContainer entityIdListContainer;
- if (!entityIdListContainer.FromBuffer(arrayData.constData(), arrayData.size()) || entityIdListContainer.m_entityIds.empty())
- {
- return;
- }
- bool areEntitiesEditable = true;
- AzToolsFramework::ToolsApplicationRequests::Bus::BroadcastResult(areEntitiesEditable, &AzToolsFramework::ToolsApplicationRequests::AreEntitiesEditable, entityIdListContainer.m_entityIds);
- if (!areEntitiesEditable)
- {
- return;
- }
- ScriptCanvas::ScriptCanvasId scriptCanvasId;
- GeneralRequestBus::BroadcastResult(scriptCanvasId, &GeneralRequests::GetScriptCanvasId, graphCanvasGraphId);
- ScriptCanvas::GraphVariableManagerRequests* variableManagerRequests = ScriptCanvas::GraphVariableManagerRequestBus::FindFirstHandler(scriptCanvasId);
- if (variableManagerRequests == nullptr)
- {
- return;
- }
- AZStd::vector< ScriptCanvas::VariableId > variableIds;
- variableIds.reserve(entityIdListContainer.m_entityIds.size());
- {
- GraphCanvas::ScopedGraphUndoBlocker undoBlocker(graphCanvasGraphId);
- AZ::Vector2 pos(aznumeric_cast<float>(dropPoint.x()), aznumeric_cast<float>(dropPoint.y()));
- for (const AZ::EntityId& entityId : entityIdListContainer.m_entityIds)
- {
- AZStd::string variableName;
- AZ::Entity* entity = nullptr;
- AZ::ComponentApplicationBus::BroadcastResult(entity, &AZ::ComponentApplicationRequests::FindEntity, entityId);
- if (entity)
- {
- // Because we add in the entity id to this.
- // we make the name mostly unique.
- // If we just use the name, we'll run into some potential rename issues when looking things up.
- variableName = AZStd::string::format("%s %s", entity->GetName().c_str(), entityId.ToString().c_str());
-
- ScriptCanvas::GraphVariable* graphVariable = variableManagerRequests->FindVariable(variableName);
- int counter = 0;
- AZStd::string baseName = variableName;
- baseName.append(" (Copy)");
- // If the variable datum already exists. That means we already have a reference to that. So we don't need to create it.
- while (graphVariable != nullptr)
- {
- if (graphVariable->GetDataType() == ScriptCanvas::Data::Type::EntityID())
- {
- variableIds.emplace_back(graphVariable->GetVariableId());
- break;
- }
- if (counter == 0)
- {
- variableName = baseName;
- }
- else
- {
- variableName = AZStd::string::format("%s (%i)", baseName.c_str(), counter);
- }
- ++counter;
-
- graphVariable = variableManagerRequests->FindVariable(variableName);
- }
- if (graphVariable == nullptr)
- {
- ScriptCanvas::Datum datum = ScriptCanvas::Datum(entityId);
- AZ::Outcome<ScriptCanvas::VariableId, AZStd::string > addVariableOutcome = variableManagerRequests->AddVariable(variableName, datum, false);
- if (addVariableOutcome.IsSuccess())
- {
- variableIds.emplace_back(addVariableOutcome.GetValue());
- }
- }
- }
- }
- if (!variableIds.empty())
- {
- AZ::EntityId gridId;
- GraphCanvas::SceneRequestBus::EventResult(gridId, graphCanvasGraphId, &GraphCanvas::SceneRequests::GetGrid);
- AZ::Vector2 gridStep;
- GraphCanvas::GridRequestBus::EventResult(gridStep, gridId, &GraphCanvas::GridRequests::GetMinorPitch);
- for (const ScriptCanvas::VariableId& variableId : variableIds)
- {
- NodeIdPair nodePair = Nodes::CreateGetVariableNode(variableId, scriptCanvasId);
- GraphCanvas::SceneRequestBus::Event(graphCanvasGraphId, &GraphCanvas::SceneRequests::AddNode, nodePair.m_graphCanvasId, pos, false);
- pos += gridStep;
- }
-
- }
- }
- if (!variableIds.empty())
- {
- GeneralRequestBus::Broadcast(&GeneralRequests::PostUndoPoint, scriptCanvasId);
- }
- }
- void EntityMimeDataHandler::HandleLeave(const AZ::EntityId&, const QMimeData*)
- {
- }
- }
|