BehaviorsAnimationGroup.cpp 8.0 KB

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