AnimGraphMotionConditionTests.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 <Tests/SystemComponentFixture.h>
  9. #include <EMotionFX/Source/AnimGraphTransitionCondition.h>
  10. #include <EMotionFX/Source/AnimGraphMotionCondition.h>
  11. #include <EMotionFX/Source/TwoStringEventData.h>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. #include <AzCore/Serialization/Utils.h>
  14. namespace EMotionFX
  15. {
  16. // This class has the Version 1 of the AnimGraphMotionCondition fields
  17. // reflected. Instances of this class can be serialized, then the
  18. // production EMotionFX::AnimGraphMotionCondition can be used to
  19. // deserialize. The converter should be able to convert from version 1 to
  20. // 2.
  21. class AnimGraphMotionConditionV1
  22. : public AnimGraphTransitionCondition
  23. {
  24. public:
  25. // It is important that this contain the same UUID as the production
  26. // class, so that the deserialization code will use the converter
  27. AZ_RTTI(AnimGraphMotionConditionV1, "{0E2EDE4E-BDEE-4383-AB18-208CE7F7A784}", AnimGraphTransitionCondition)
  28. AZ_CLASS_ALLOCATOR(AnimGraphMotionConditionV1, AnimGraphAllocator)
  29. enum TestFunction : AZ::u8
  30. {
  31. FUNCTION_EVENT = 0,
  32. FUNCTION_HASENDED = 1,
  33. FUNCTION_HASREACHEDMAXNUMLOOPS = 2,
  34. FUNCTION_PLAYTIME = 3,
  35. FUNCTION_PLAYTIMELEFT = 4,
  36. FUNCTION_ISMOTIONASSIGNED = 5,
  37. FUNCTION_ISMOTIONNOTASSIGNED = 6,
  38. FUNCTION_NONE = 7
  39. };
  40. static void Reflect(AZ::ReflectContext* context)
  41. {
  42. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  43. if (!serializeContext)
  44. {
  45. return;
  46. }
  47. serializeContext->Class<AnimGraphMotionConditionV1, AnimGraphTransitionCondition>()
  48. ->Version(1)
  49. ->Field("motionNodeId", &AnimGraphMotionConditionV1::m_motionNodeId)
  50. ->Field("testFunction", &AnimGraphMotionConditionV1::m_testFunction)
  51. ->Field("numLoops", &AnimGraphMotionConditionV1::m_numLoops)
  52. ->Field("playTime", &AnimGraphMotionConditionV1::m_playTime)
  53. ->Field("eventType", &AnimGraphMotionConditionV1::m_eventType)
  54. ->Field("eventParameter", &AnimGraphMotionConditionV1::m_eventParameter)
  55. ;
  56. }
  57. const char* GetPaletteName() const override { return "Motion Condition"; }
  58. bool TestCondition(AnimGraphInstance* ) const override { return false; }
  59. AZStd::string m_eventType;
  60. AZStd::string m_eventParameter;
  61. AZ::u64 m_motionNodeId;
  62. AZ::u32 m_numLoops;
  63. float m_playTime;
  64. TestFunction m_testFunction;
  65. };
  66. TEST_F(SystemComponentFixture, TestAnimGraphMotionConditionV1Conversion)
  67. {
  68. // Make a new serialization context for the V1 MotionCondition
  69. AZ::SerializeContext v1context;
  70. AnimGraphObject::Reflect(&v1context);
  71. AnimGraphTransitionCondition::Reflect(&v1context);
  72. AnimGraphMotionConditionV1::Reflect(&v1context);
  73. AnimGraphMotionConditionV1 v1condition;
  74. v1condition.m_motionNodeId = 42;
  75. v1condition.m_numLoops = 5;
  76. v1condition.m_playTime = 2.43f;
  77. v1condition.m_testFunction = AnimGraphMotionConditionV1::FUNCTION_EVENT;
  78. v1condition.m_eventType = "My event type";
  79. v1condition.m_eventParameter = "My parameters";
  80. // Serialize using the v1 definition
  81. AZStd::vector<char> charBuffer;
  82. AZ::IO::ByteContainerStream<AZStd::vector<char> > charStream(&charBuffer);
  83. AZ::Utils::SaveObjectToStream(charStream, AZ::ObjectStream::ST_XML, &v1condition, &v1context);
  84. // Load using the v2 definition
  85. AnimGraphMotionCondition* v2condition = AZ::Utils::LoadObjectFromBuffer<AnimGraphMotionCondition>(&charBuffer[0], charBuffer.size(), GetSerializeContext());
  86. // Ensure the correct EventData was created. The eventType and
  87. // eventParameter fields get packed into the EventData
  88. const AZStd::shared_ptr<const TwoStringEventData> expectedEventData = GetEventManager().FindOrCreateEventData<TwoStringEventData>("My event type", "My parameters");
  89. const AZStd::shared_ptr<const EventData>& gotEventData = v2condition->GetEventDatas()[0];
  90. ASSERT_TRUE(gotEventData);
  91. EXPECT_EQ(*gotEventData.get(), *expectedEventData.get());
  92. delete v2condition;
  93. }
  94. } // end namespace EMotionFX