BehaviorsAnimationGroup.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 <AzCore/Serialization/SerializeContext.h>
  9. #include <AzCore/std/algorithm.h>
  10. #include <AzCore/std/smart_ptr/make_shared.h>
  11. #include <SceneAPI/SceneCore/Containers/Scene.h>
  12. #include <SceneAPI/SceneCore/Containers/SceneGraph.h>
  13. #include <SceneAPI/SceneCore/Containers/Views/SceneGraphDownwardsIterator.h>
  14. #include <SceneAPI/SceneCore/Containers/Views/PairIterator.h>
  15. #include <SceneAPI/SceneCore/Containers/Utilities/Filters.h>
  16. #include <SceneAPI/SceneCore/Containers/Utilities/SceneGraphUtilities.h>
  17. #include <SceneAPI/SceneCore/DataTypes/GraphData/IAnimationData.h>
  18. #include <SceneAPI/SceneCore/DataTypes/DataTypeUtilities.h>
  19. #include <SceneAPI/SceneData/Groups/AnimationGroup.h>
  20. #include <SceneAPI/SceneData/Behaviors/AnimationGroup.h>
  21. #include <SceneAPI/SceneData/GraphData/RootBoneData.h>
  22. namespace AZ
  23. {
  24. namespace SceneAPI
  25. {
  26. namespace Behaviors
  27. {
  28. const int AnimationGroup::s_animationsPreferredTabOrder = 2;
  29. void AnimationGroup::Activate()
  30. {
  31. }
  32. void AnimationGroup::Deactivate()
  33. {
  34. }
  35. void AnimationGroup::Reflect(ReflectContext* context)
  36. {
  37. SerializeContext* serializeContext = azrtti_cast<SerializeContext*>(context);
  38. if (serializeContext)
  39. {
  40. serializeContext->Class<AnimationGroup, BehaviorComponent>()->Version(1);
  41. }
  42. }
  43. void AnimationGroup::GetCategoryAssignments(CategoryRegistrationList& categories, const Containers::Scene& scene)
  44. {
  45. if (SceneHasAnimationGroup(scene) || Utilities::DoesSceneGraphContainDataLike<DataTypes::IAnimationData>(scene, false))
  46. {
  47. categories.emplace_back("Animations", SceneData::AnimationGroup::TYPEINFO_Uuid(), s_animationsPreferredTabOrder);
  48. }
  49. }
  50. void AnimationGroup::InitializeObject(const Containers::Scene& scene, DataTypes::IManifestObject& target)
  51. {
  52. if (!target.RTTI_IsTypeOf(SceneData::AnimationGroup::TYPEINFO_Uuid()))
  53. {
  54. return;
  55. }
  56. SceneData::AnimationGroup* group = azrtti_cast<SceneData::AnimationGroup*>(&target);
  57. group->SetName(DataTypes::Utilities::CreateUniqueName<DataTypes::IAnimationGroup>(scene.GetName(), scene.GetManifest()));
  58. const Containers::SceneGraph &graph = scene.GetGraph();
  59. auto nameStorage = graph.GetNameStorage();
  60. auto contentStorage = graph.GetContentStorage();
  61. auto nameContentView = Containers::Views::MakePairView(nameStorage, contentStorage);
  62. AZStd::string shallowestRootBoneName;
  63. auto graphDownwardsView = Containers::Views::MakeSceneGraphDownwardsView<Containers::Views::BreadthFirst>(graph, graph.GetRoot(), nameContentView.begin(), true);
  64. for (auto it = graphDownwardsView.begin(); it != graphDownwardsView.end(); ++it)
  65. {
  66. if (!it->second)
  67. {
  68. continue;
  69. }
  70. if (it->second->RTTI_IsTypeOf(AZ::SceneData::GraphData::RootBoneData::TYPEINFO_Uuid()))
  71. {
  72. shallowestRootBoneName = it->first.GetPath();
  73. break;
  74. }
  75. }
  76. group->SetSelectedRootBone(shallowestRootBoneName);
  77. Containers::SceneGraph::ContentStorageConstData graphContent = graph.GetContentStorage();
  78. auto animationData = AZStd::find_if(graphContent.begin(), graphContent.end(), Containers::DerivedTypeFilter<DataTypes::IAnimationData>());
  79. if (animationData == graphContent.end())
  80. {
  81. return;
  82. }
  83. const DataTypes::IAnimationData* animation = azrtti_cast<const DataTypes::IAnimationData*>(animationData->get());
  84. uint32_t frameCount = aznumeric_caster(animation->GetKeyFrameCount());
  85. group->SetStartFrame(0);
  86. group->SetEndFrame(frameCount > 0 ? frameCount - 1 : 0);
  87. }
  88. Events::ProcessingResult AnimationGroup::UpdateManifest(Containers::Scene& scene, ManifestAction action,
  89. RequestingApplication /*requester*/)
  90. {
  91. if (action == ManifestAction::ConstructDefault)
  92. {
  93. return BuildDefault(scene);
  94. }
  95. else if (action == ManifestAction::Update)
  96. {
  97. return UpdateAnimationGroups(scene);
  98. }
  99. else
  100. {
  101. return Events::ProcessingResult::Ignored;
  102. }
  103. }
  104. Events::ProcessingResult AnimationGroup::BuildDefault(Containers::Scene& scene) const
  105. {
  106. if (SceneHasAnimationGroup(scene) || !Utilities::DoesSceneGraphContainDataLike<DataTypes::IAnimationData>(scene, true))
  107. {
  108. return Events::ProcessingResult::Ignored;
  109. }
  110. // There are animations but no animation group, so add a default animation group to the manifest.
  111. AZStd::shared_ptr<SceneData::AnimationGroup> group = AZStd::make_shared<SceneData::AnimationGroup>();
  112. // This is a group that's generated automatically so may not be saved to disk but would need to be recreated
  113. // in the same way again. To guarantee the same uuid, generate a stable one instead.
  114. group->OverrideId(DataTypes::Utilities::CreateStableUuid(scene, SceneData::AnimationGroup::TYPEINFO_Uuid()));
  115. EBUS_EVENT(Events::ManifestMetaInfoBus, InitializeObject, scene, *group);
  116. scene.GetManifest().AddEntry(AZStd::move(group));
  117. return Events::ProcessingResult::Success;
  118. }
  119. Events::ProcessingResult AnimationGroup::UpdateAnimationGroups(Containers::Scene& scene) const
  120. {
  121. bool updated = false;
  122. Containers::SceneManifest& manifest = scene.GetManifest();
  123. auto valueStorage = manifest.GetValueStorage();
  124. auto view = Containers::MakeDerivedFilterView<SceneData::AnimationGroup>(valueStorage);
  125. for (SceneData::AnimationGroup& group : view)
  126. {
  127. if (group.GetName().empty())
  128. {
  129. group.SetName(DataTypes::Utilities::CreateUniqueName<DataTypes::IAnimationGroup>(scene.GetName(), scene.GetManifest()));
  130. updated = true;
  131. }
  132. if (group.GetId().IsNull())
  133. {
  134. // When the uuid is null it's likely because the manifest has been updated from an older version. Include the
  135. // name of the group as there could be multiple groups.
  136. group.OverrideId(DataTypes::Utilities::CreateStableUuid(scene, SceneData::AnimationGroup::TYPEINFO_Uuid(), group.GetName()));
  137. updated = true;
  138. }
  139. }
  140. return updated ? Events::ProcessingResult::Success : Events::ProcessingResult::Ignored;
  141. }
  142. bool AnimationGroup::SceneHasAnimationGroup(const Containers::Scene& scene) const
  143. {
  144. const Containers::SceneManifest& manifest = scene.GetManifest();
  145. Containers::SceneManifest::ValueStorageConstData manifestData = manifest.GetValueStorage();
  146. auto animationGroup = AZStd::find_if(manifestData.begin(), manifestData.end(), Containers::DerivedTypeFilter<DataTypes::IAnimationGroup>());
  147. return animationGroup != manifestData.end();
  148. }
  149. } // namespace Behaviors
  150. } // namespace SceneAPI
  151. } // namespace AZ