EntityRefTests.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 <AzTest/AzTest.h>
  10. namespace ScriptCanvasTests
  11. {
  12. class EntityRefTestEvents : public AZ::EBusTraits
  13. {
  14. public:
  15. static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::ById;
  16. using BusIdType = AZ::EntityId;
  17. virtual void TestEvent(AZ::EntityId, bool) = 0;
  18. };
  19. using EntityRefTestEventBus = AZ::EBus<EntityRefTestEvents>;
  20. class TestComponent : public AZ::Component
  21. , EntityRefTestEventBus::Handler
  22. {
  23. public:
  24. AZ_COMPONENT(TestComponent, "{527680AE-BF46-4BC8-A923-A39B458A3B53}", AZ::Component);
  25. void Init() override {}
  26. void Activate() override
  27. {
  28. EntityRefTestEventBus::Handler::BusConnect(GetEntityId());
  29. }
  30. void Deactivate() override
  31. {
  32. EntityRefTestEventBus::Handler::BusDisconnect();
  33. }
  34. void TestEvent(AZ::EntityId referencedEntity, bool shouldBeSelf) override
  35. {
  36. // validate entity
  37. if (shouldBeSelf)
  38. {
  39. EXPECT_TRUE(GetEntity()->GetId() == referencedEntity);
  40. }
  41. AZ_TracePrintf("Script Canvas", "TestEvent handled by: %s\n", GetEntity()->GetName().c_str());
  42. }
  43. static void Reflect(AZ::ReflectContext* reflection)
  44. {
  45. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflection);
  46. if (serializeContext)
  47. {
  48. serializeContext->Class<TestComponent, AZ::Component>()
  49. ->Version(0)
  50. ;
  51. }
  52. AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(reflection);
  53. if (behaviorContext)
  54. {
  55. behaviorContext->EBus<EntityRefTestEventBus>("EntityRefTestEventBus")
  56. ->Event("TestEvent", &EntityRefTestEvents::TestEvent)
  57. ;
  58. }
  59. }
  60. };
  61. }