SimulatedObjectHelpers.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 <EMotionFX/CommandSystem/Source/CommandManager.h>
  9. #include <EMotionFX/CommandSystem/Source/SimulatedObjectCommands.h>
  10. #include <Editor/SimulatedObjectHelpers.h>
  11. #include <Editor/SimulatedObjectModel.h>
  12. #include <Editor/SkeletonModel.h>
  13. #include <MCore/Source/ReflectionSerializer.h>
  14. namespace EMotionFX
  15. {
  16. bool SimulatedObjectHelpers::AddSimulatedObject(AZ::u32 actorID, AZStd::optional<AZStd::string> name, MCore::CommandGroup* commandGroup)
  17. {
  18. const AZStd::string groupName = AZStd::string::format("Add simulated object");
  19. return CommandSimulatedObjectHelpers::AddSimulatedObject(actorID, name, commandGroup);
  20. }
  21. void SimulatedObjectHelpers::RemoveSimulatedObject(const QModelIndex& modelIndex)
  22. {
  23. const size_t objectIndex = modelIndex.data(SimulatedObjectModel::ROLE_OBJECT_INDEX).value<size_t>();
  24. const Actor* actor = modelIndex.data(SimulatedObjectModel::ROLE_ACTOR_PTR).value<Actor*>();
  25. const AZStd::string groupName = AZStd::string::format("Remove simulated object");
  26. MCore::CommandGroup commandGroup(groupName);
  27. CommandSimulatedObjectHelpers::RemoveSimulatedObject(actor->GetID(), objectIndex, &commandGroup);
  28. AZStd::string result;
  29. if (!CommandSystem::GetCommandManager()->ExecuteCommandGroup(commandGroup, result))
  30. {
  31. AZ_Error("EMotionFX", false, result.c_str());
  32. }
  33. }
  34. bool SimulatedObjectHelpers::AddSimulatedJoints(const QModelIndexList& modelIndices, size_t objectIndex, bool addChildren, MCore::CommandGroup* commandGroup)
  35. {
  36. if (modelIndices.empty())
  37. {
  38. return true;
  39. }
  40. const Actor* actor = modelIndices[0].data(SkeletonModel::ROLE_ACTOR_POINTER).value<Actor*>();
  41. AZStd::vector<size_t> jointIndices;
  42. for (const QModelIndex& selectedIndex : modelIndices)
  43. {
  44. if (SkeletonModel::IndexIsRootNode(selectedIndex))
  45. {
  46. continue;
  47. }
  48. const Node* joint = selectedIndex.data(SkeletonModel::ROLE_POINTER).value<Node*>();
  49. jointIndices.emplace_back(joint->GetNodeIndex());
  50. }
  51. return CommandSimulatedObjectHelpers::AddSimulatedJoints(actor->GetID(), jointIndices, objectIndex, addChildren, commandGroup);
  52. }
  53. void SimulatedObjectHelpers::RemoveSimulatedJoint(const QModelIndex& modelIndex, bool removeChildren)
  54. {
  55. if (!modelIndex.isValid())
  56. {
  57. return;
  58. }
  59. RemoveSimulatedJoints({modelIndex}, removeChildren);
  60. }
  61. void SimulatedObjectHelpers::RemoveSimulatedJoints(const QModelIndexList& modelIndices, bool removeChildren)
  62. {
  63. AZStd::unordered_map<size_t, AZStd::pair<const Actor*, AZStd::vector<size_t>>> objectToSkeletonJointIndices;
  64. for (const QModelIndex& index : modelIndices)
  65. {
  66. const bool isJoint = index.data(SimulatedObjectModel::ROLE_JOINT_BOOL).toBool();
  67. if (!isJoint)
  68. {
  69. // Only take the index belongs to a simulated joint.
  70. continue;
  71. }
  72. const Actor* actor = index.data(SimulatedObjectModel::ROLE_ACTOR_PTR).value<Actor*>();
  73. const size_t objectIndex = static_cast<size_t>(index.data(SimulatedObjectModel::ROLE_OBJECT_INDEX).toInt());
  74. const size_t jointIndex = index.data(SimulatedObjectModel::ROLE_JOINT_PTR).value<SimulatedJoint*>()->GetSkeletonJointIndex();
  75. objectToSkeletonJointIndices[objectIndex].first = actor;
  76. objectToSkeletonJointIndices[objectIndex].second.emplace_back(jointIndex);
  77. }
  78. const AZStd::string groupName = "Remove simulated joints";
  79. MCore::CommandGroup commandGroup(groupName);
  80. for (const auto& objectIndexAndJointIndices : objectToSkeletonJointIndices)
  81. {
  82. const size_t objectIndex = objectIndexAndJointIndices.first;
  83. const Actor* actor = objectIndexAndJointIndices.second.first;
  84. const AZStd::vector<size_t> jointIndices = objectIndexAndJointIndices.second.second;
  85. CommandSimulatedObjectHelpers::RemoveSimulatedJoints(actor->GetID(), jointIndices, objectIndex, removeChildren, &commandGroup);
  86. }
  87. AZStd::string result;
  88. AZ_Error("EMotionFX", CommandSystem::GetCommandManager()->ExecuteCommandGroup(commandGroup, result), result.c_str());
  89. }
  90. } // namespace EMotionFX