/* * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or * its licensors. * * For complete copyright and license terms please see the LICENSE at the root of this * distribution (the "License"). All use of this software is governed by the License, * or, if provided, by the license below or the license accompanying this file. Do not * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ #include #include #include #include #include #include namespace EMotionFX { bool SimulatedObjectHelpers::AddSimulatedObject(AZ::u32 actorID, AZStd::optional name, MCore::CommandGroup* commandGroup) { const AZStd::string groupName = AZStd::string::format("Add simulated object"); return CommandSimulatedObjectHelpers::AddSimulatedObject(actorID, name, commandGroup); } void SimulatedObjectHelpers::RemoveSimulatedObject(const QModelIndex& modelIndex) { const size_t objectIndex = modelIndex.data(SimulatedObjectModel::ROLE_OBJECT_INDEX).value(); const Actor* actor = modelIndex.data(SimulatedObjectModel::ROLE_ACTOR_PTR).value(); const AZStd::string groupName = AZStd::string::format("Remove simulated object"); MCore::CommandGroup commandGroup(groupName); CommandSimulatedObjectHelpers::RemoveSimulatedObject(actor->GetID(), objectIndex, &commandGroup); AZStd::string result; if (!CommandSystem::GetCommandManager()->ExecuteCommandGroup(commandGroup, result)) { AZ_Error("EMotionFX", false, result.c_str()); } } bool SimulatedObjectHelpers::AddSimulatedJoints(const QModelIndexList& modelIndices, size_t objectIndex, bool addChildren, MCore::CommandGroup* commandGroup) { if (modelIndices.empty()) { return true; } const Actor* actor = modelIndices[0].data(SkeletonModel::ROLE_ACTOR_POINTER).value(); AZStd::vector jointIndices; for (const QModelIndex& selectedIndex : modelIndices) { const Node* joint = selectedIndex.data(SkeletonModel::ROLE_POINTER).value(); jointIndices.emplace_back(joint->GetNodeIndex()); } return CommandSimulatedObjectHelpers::AddSimulatedJoints(actor->GetID(), jointIndices, objectIndex, addChildren, commandGroup); } void SimulatedObjectHelpers::RemoveSimulatedJoint(const QModelIndex& modelIndex, bool removeChildren) { if (!modelIndex.isValid()) { return; } RemoveSimulatedJoints({modelIndex}, removeChildren); } void SimulatedObjectHelpers::RemoveSimulatedJoints(const QModelIndexList& modelIndices, bool removeChildren) { AZStd::unordered_map>> objectToSkeletonJointIndices; for (const QModelIndex& index : modelIndices) { const bool isJoint = index.data(SimulatedObjectModel::ROLE_JOINT_BOOL).toBool(); if (!isJoint) { // Only take the index belongs to a simulated joint. continue; } const Actor* actor = index.data(SimulatedObjectModel::ROLE_ACTOR_PTR).value(); const size_t objectIndex = static_cast(index.data(SimulatedObjectModel::ROLE_OBJECT_INDEX).toInt()); const AZ::u32 jointIndex = index.data(SimulatedObjectModel::ROLE_JOINT_PTR).value()->GetSkeletonJointIndex(); objectToSkeletonJointIndices[objectIndex].first = actor; objectToSkeletonJointIndices[objectIndex].second.emplace_back(jointIndex); } const AZStd::string groupName = "Remove simulated joints"; MCore::CommandGroup commandGroup(groupName); for (const auto& objectIndexAndJointIndices : objectToSkeletonJointIndices) { const size_t objectIndex = objectIndexAndJointIndices.first; const Actor* actor = objectIndexAndJointIndices.second.first; const AZStd::vector jointIndices = objectIndexAndJointIndices.second.second; CommandSimulatedObjectHelpers::RemoveSimulatedJoints(actor->GetID(), jointIndices, objectIndex, removeChildren, &commandGroup); } AZStd::string result; AZ_Error("EMotionFX", CommandSystem::GetCommandManager()->ExecuteCommandGroup(commandGroup, result), result.c_str()); } } // namespace EMotionFX