ScriptCanvasEntityIdDataInterface.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #pragma once
  9. #include <GraphCanvas/Components/NodePropertyDisplay/EntityIdDataInterface.h>
  10. #include "ScriptCanvasDataInterface.h"
  11. #include <QWidget>
  12. #include <QMenu>
  13. #include <QAction>
  14. namespace ScriptCanvasEditor
  15. {
  16. class ScriptCanvasEntityIdDataInterface
  17. : public ScriptCanvasDataInterface<GraphCanvas::EntityIdDataInterface>
  18. {
  19. public:
  20. AZ_CLASS_ALLOCATOR(ScriptCanvasEntityIdDataInterface, AZ::SystemAllocator);
  21. ScriptCanvasEntityIdDataInterface(const AZ::EntityId& nodeId, const ScriptCanvas::SlotId& slotId)
  22. : ScriptCanvasDataInterface(nodeId, slotId)
  23. {
  24. }
  25. ~ScriptCanvasEntityIdDataInterface() = default;
  26. // EntityIdDataInterface
  27. AZ::EntityId GetEntityId() const override
  28. {
  29. const ScriptCanvas::Datum* object = GetSlotObject();
  30. if (object)
  31. {
  32. const AZ::EntityId* retVal = object->GetAs<AZ::EntityId>();
  33. if (retVal)
  34. {
  35. return (*retVal);
  36. }
  37. }
  38. return AZ::EntityId();
  39. }
  40. const AZStd::string GetNameOverride() const override
  41. {
  42. const ScriptCanvas::Datum* object = GetSlotObject();
  43. if (object && object->IS_A(ScriptCanvas::Data::Type::EntityID()))
  44. {
  45. if (const AZ::EntityId* entityId = object->GetAs<AZ::EntityId>())
  46. {
  47. if (*entityId == ScriptCanvas::GraphOwnerId)
  48. {
  49. return "Self";
  50. }
  51. else if (*entityId == ScriptCanvas::UniqueId)
  52. {
  53. return "Unique";
  54. }
  55. }
  56. }
  57. return {};
  58. }
  59. void OnShowContextMenu(QWidget* nodePropertyDisplay, const QPoint& pos) override
  60. {
  61. QPoint globalPos = nodePropertyDisplay->mapToGlobal(pos);
  62. enum EntityMenuAction
  63. {
  64. SetToSelf
  65. };
  66. QMenu entityMenu;
  67. QAction* setToSelf = entityMenu.addAction("Set to Self");
  68. setToSelf->setToolTip("Reset the EntityId to the Entity that owns this graph.");
  69. setToSelf->setData(EntityMenuAction::SetToSelf);
  70. QAction* selectedItem = entityMenu.exec(globalPos);
  71. if (selectedItem)
  72. {
  73. if (selectedItem->data().toInt() == EntityMenuAction::SetToSelf)
  74. {
  75. SetEntityId(ScriptCanvas::GraphOwnerId);
  76. }
  77. }
  78. }
  79. void SetEntityId(const AZ::EntityId& entityId) override
  80. {
  81. ScriptCanvas::ModifiableDatumView datumView;
  82. ModifySlotObject(datumView);
  83. if (datumView.IsValid() && datumView.IsType(ScriptCanvas::Data::Type::EntityID()))
  84. {
  85. const AZ::EntityId* storedId = datumView.GetAs<AZ::EntityId>();
  86. if (storedId == nullptr || (*storedId) != entityId)
  87. {
  88. datumView.SetAs(entityId);
  89. PostUndoPoint();
  90. PropertyGridRequestBus::Broadcast(&PropertyGridRequests::RefreshPropertyGrid);
  91. }
  92. }
  93. }
  94. ////
  95. };
  96. }