SkeletonModel.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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/Source/Skeleton.h>
  9. #include <Source/Editor/SkeletonModel.h>
  10. #include <EMotionFX/Source/SimulatedObjectSetup.h>
  11. namespace EMotionFX
  12. {
  13. int SkeletonModel::s_defaultIconSize = 16;
  14. int SkeletonModel::s_columnCount = 7;
  15. const char* SkeletonModel::s_jointIconPath = ":/EMotionFX/Joint.svg";
  16. const char* SkeletonModel::s_clothColliderIconPath = ":/EMotionFX/Cloth.svg";
  17. const char* SkeletonModel::s_hitDetectionColliderIconPath = ":/EMotionFX/HitDetection.svg";
  18. const char* SkeletonModel::s_ragdollColliderIconPath = ":/EMotionFX/RagdollCollider.svg";
  19. const char* SkeletonModel::s_ragdollJointLimitIconPath = ":/EMotionFX/RagdollJointLimit.svg";
  20. const char* SkeletonModel::s_simulatedJointIconPath = ":/EMotionFX/SimulatedObjectColored.svg";
  21. const char* SkeletonModel::s_simulatedColliderIconPath = ":/EMotionFX/SimulatedObjectCollider.svg";
  22. const char* SkeletonModel::s_characterIconPath = ":/EMotionFX/Character.svg";
  23. SkeletonModel::SkeletonModel()
  24. : m_selectionModel(this)
  25. , m_skeleton(nullptr)
  26. , m_actor(nullptr)
  27. , m_actorInstance(nullptr)
  28. , m_characterRootNode(Node::Create("Character", nullptr))
  29. , m_jointIcon(s_jointIconPath)
  30. , m_clothColliderIcon(s_clothColliderIconPath)
  31. , m_hitDetectionColliderIcon(s_hitDetectionColliderIconPath)
  32. , m_ragdollColliderIcon(s_ragdollColliderIconPath)
  33. , m_ragdollJointLimitIcon(s_ragdollJointLimitIconPath)
  34. , m_simulatedJointIcon(s_simulatedJointIconPath)
  35. , m_simulatedColliderIcon(s_simulatedColliderIconPath)
  36. , m_characterIcon(s_characterIconPath)
  37. {
  38. m_selectionModel.setModel(this);
  39. ActorEditorNotificationBus::Handler::BusConnect();
  40. ActorInstance* selectedActorInstance = nullptr;
  41. ActorEditorRequestBus::BroadcastResult(selectedActorInstance, &ActorEditorRequests::GetSelectedActorInstance);
  42. if (selectedActorInstance)
  43. {
  44. SetActorInstance(selectedActorInstance);
  45. }
  46. else
  47. {
  48. Actor* selectedActor = nullptr;
  49. ActorEditorRequestBus::BroadcastResult(selectedActor, &ActorEditorRequests::GetSelectedActor);
  50. SetActor(selectedActor);
  51. }
  52. // UI 2.0 styling paints icons white on row selection. It cannot discern between layers though, so everything that is non-transparent is filled with white.
  53. // As a temp solution, we specify the selected icon.
  54. m_jointIcon.addFile(s_jointIconPath, QSize(), QIcon::Selected);
  55. m_clothColliderIcon.addFile(s_clothColliderIconPath, QSize(), QIcon::Selected);
  56. m_hitDetectionColliderIcon.addFile(s_hitDetectionColliderIconPath, QSize(), QIcon::Selected);
  57. m_ragdollColliderIcon.addFile(s_ragdollColliderIconPath, QSize(), QIcon::Selected);
  58. m_ragdollJointLimitIcon.addFile(s_ragdollJointLimitIconPath, QSize(), QIcon::Selected);
  59. m_simulatedJointIcon.addFile(s_simulatedJointIconPath, QSize(), QIcon::Selected);
  60. m_simulatedColliderIcon.addFile(s_simulatedColliderIconPath, QSize(), QIcon::Selected);
  61. }
  62. SkeletonModel::~SkeletonModel()
  63. {
  64. // Calling Reset will trigger the modelReset signal, thus notify other widgets to clear cached information about this model.
  65. Reset();
  66. m_characterRootNode->Destroy();
  67. ActorEditorNotificationBus::Handler::BusDisconnect();
  68. }
  69. void SkeletonModel::SetActor(Actor* actor)
  70. {
  71. beginResetModel();
  72. m_actorInstance = nullptr;
  73. m_actor = actor;
  74. m_skeleton = nullptr;
  75. if (m_actor)
  76. {
  77. m_skeleton = actor->GetSkeleton();
  78. }
  79. UpdateNodeInfos(m_actor);
  80. endResetModel();
  81. }
  82. void SkeletonModel::SetActorInstance(ActorInstance* actorInstance)
  83. {
  84. beginResetModel();
  85. m_actorInstance = actorInstance;
  86. m_actor = nullptr;
  87. m_skeleton = nullptr;
  88. if (m_actorInstance)
  89. {
  90. m_actor = actorInstance->GetActor();
  91. m_skeleton = m_actor->GetSkeleton();
  92. }
  93. UpdateNodeInfos(m_actor);
  94. endResetModel();
  95. }
  96. QModelIndex SkeletonModel::index(int row, int column, const QModelIndex& parent) const
  97. {
  98. if (!m_skeleton)
  99. {
  100. AZ_Assert(false, "Cannot get model index. Skeleton invalid.");
  101. return QModelIndex();
  102. }
  103. if (parent.isValid())
  104. {
  105. const Node* parentNode = static_cast<const Node*>(parent.internalPointer());
  106. if (parentNode == m_characterRootNode) {
  107. if (row >= static_cast<int>(m_skeleton->GetNumRootNodes()))
  108. {
  109. AZ_Assert(false, "Cannot get model index. Row out of range.");
  110. return QModelIndex();
  111. }
  112. const size_t rootNodeIndex = m_skeleton->GetRootNodeIndex(row);
  113. Node* rootNode = m_skeleton->GetNode(rootNodeIndex);
  114. return createIndex(row, column, rootNode);
  115. }
  116. if (row >= static_cast<int>(parentNode->GetNumChildNodes()))
  117. {
  118. AZ_Assert(false, "Cannot get model index. Row out of range.");
  119. return QModelIndex();
  120. }
  121. const size_t childNodeIndex = parentNode->GetChildIndex(row);
  122. Node* childNode = m_skeleton->GetNode(childNodeIndex);
  123. return createIndex(row, column, childNode);
  124. }
  125. else
  126. {
  127. // The root node is now only the character root node, and all skeleton root nodes
  128. // are its children. We never expect more than one node at the root level.
  129. if (row >= 1)
  130. {
  131. AZ_Assert(false, "Cannot get model index. Row out of range.");
  132. return QModelIndex();
  133. }
  134. return createIndex(row, column, m_characterRootNode);
  135. }
  136. }
  137. QModelIndex SkeletonModel::parent(const QModelIndex& child) const
  138. {
  139. if (!m_skeleton)
  140. {
  141. AZ_Assert(false, "Cannot get parent model index. Skeleton invalid.");
  142. return QModelIndex();
  143. }
  144. AZ_Assert(child.isValid(), "Expected valid child model index.");
  145. Node* childNode = static_cast<Node*>(child.internalPointer());
  146. // The virtual character root node has no parent
  147. if (childNode == m_characterRootNode)
  148. {
  149. return QModelIndex();
  150. }
  151. // Actual roots from the skeleton tree (nodes that have no parent node on the skeleton tree)
  152. // are children of the virtual character root
  153. Node* parentNode = childNode->GetParentNode();
  154. if (!parentNode)
  155. {
  156. return createIndex(0, 0, m_characterRootNode);
  157. }
  158. // Children of skeleton root nodes have their parent node (GetParentNode()) as parent,
  159. // and the row value is computed through GetNumRootNodes()/GetRootNodeIndex()
  160. Node* grandParentNode = parentNode->GetParentNode();
  161. if (!grandParentNode)
  162. {
  163. const int numRootNodes = aznumeric_caster(m_skeleton->GetNumRootNodes());
  164. for (int i = 0; i < numRootNodes; ++i)
  165. {
  166. const Node* rootNode = m_skeleton->GetNode(m_skeleton->GetRootNodeIndex(i));
  167. if (rootNode == parentNode)
  168. {
  169. return createIndex(i, 0, parentNode);
  170. }
  171. }
  172. AZ_Assert(false, "Cannot get parent model index. Skeleton invalid.");
  173. }
  174. // Other skeleton nodes nodes have their parent node (GetParentNode()) as parent,
  175. // and the row value needs to be computed through GetNumChildNodes()/GetChildIndex() w.r.t. the parent
  176. const int numChildNodes = aznumeric_caster(grandParentNode->GetNumChildNodes());
  177. for (int i = 0; i < numChildNodes; ++i)
  178. {
  179. const Node* grandParentChildNode = m_skeleton->GetNode(grandParentNode->GetChildIndex(i));
  180. if (grandParentChildNode == parentNode)
  181. {
  182. return createIndex(i, 0, parentNode);
  183. }
  184. }
  185. return QModelIndex();
  186. }
  187. int SkeletonModel::rowCount(const QModelIndex& parent) const
  188. {
  189. if (!m_skeleton)
  190. {
  191. return 0;
  192. }
  193. if (parent.isValid())
  194. {
  195. const Node* parentNode = static_cast<const Node*>(parent.internalPointer());
  196. if (parentNode == m_characterRootNode)
  197. {
  198. return static_cast<int>(m_skeleton->GetNumRootNodes());
  199. }
  200. return static_cast<int>(parentNode->GetNumChildNodes());
  201. }
  202. else
  203. {
  204. // We only have m_characterRootNode as root node, and all skeleton root nodes as their children, by construction.
  205. return 1;
  206. }
  207. }
  208. int SkeletonModel::columnCount([[maybe_unused]] const QModelIndex& parent) const
  209. {
  210. return s_columnCount;
  211. }
  212. QVariant SkeletonModel::headerData(int section, Qt::Orientation orientation, int role) const
  213. {
  214. if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
  215. {
  216. switch (section)
  217. {
  218. case COLUMN_NAME:
  219. return "Name";
  220. default:
  221. return "";
  222. }
  223. }
  224. return QVariant();
  225. }
  226. QVariant SkeletonModel::data(const QModelIndex& index, int role) const
  227. {
  228. if (!m_skeleton || !index.isValid())
  229. {
  230. AZ_Assert(false, "Cannot get model data. Skeleton or model index invalid.");
  231. return QVariant();
  232. }
  233. Node* node = static_cast<Node*>(index.internalPointer());
  234. AZ_Assert(node, "Expected valid node pointer.");
  235. const bool isRootNode = node == m_characterRootNode;
  236. const size_t nodeIndex = node->GetNodeIndex();
  237. const NodeInfo& nodeInfo = GetNodeInfo(node);
  238. // Handle roles for the special case with a root node
  239. if (isRootNode)
  240. {
  241. switch (role)
  242. {
  243. case Qt::DecorationRole:
  244. {
  245. switch (index.column())
  246. {
  247. case COLUMN_NAME:
  248. return m_characterIcon;
  249. case COLUMN_RAGDOLL_LIMIT:
  250. case COLUMN_RAGDOLL_COLLIDERS:
  251. case COLUMN_HITDETECTION_COLLIDERS:
  252. case COLUMN_CLOTH_COLLIDERS:
  253. case COLUMN_SIMULATED_JOINTS:
  254. case COLUMN_SIMULATED_COLLIDERS:
  255. return {};
  256. default:
  257. break;
  258. }
  259. break;
  260. }
  261. case ROLE_RAGDOLL:
  262. case ROLE_HITDETECTION:
  263. case ROLE_CLOTH:
  264. case ROLE_SIMULATED_JOINT:
  265. case ROLE_SIMULATED_OBJECT_COLLIDER:
  266. return {};
  267. default:
  268. break;
  269. }
  270. }
  271. // Handle roles for all other nodes
  272. switch (role)
  273. {
  274. case Qt::ToolTipRole:
  275. {
  276. switch (index.column())
  277. {
  278. case COLUMN_RAGDOLL_LIMIT:
  279. {
  280. return QString("Ragdoll Limit");
  281. }
  282. case COLUMN_CLOTH_COLLIDERS:
  283. {
  284. return QString("Cloth Colliders");
  285. }
  286. case COLUMN_HITDETECTION_COLLIDERS:
  287. {
  288. return QString("Hit Detection Colliders");
  289. }
  290. case COLUMN_RAGDOLL_COLLIDERS:
  291. {
  292. return QString("Ragdoll Colliders");
  293. }
  294. case COLUMN_SIMULATED_JOINTS:
  295. {
  296. return QString("Simulated Joints");
  297. }
  298. case COLUMN_SIMULATED_COLLIDERS:
  299. {
  300. return QString("Simulated Colliders");
  301. }
  302. default:
  303. break;
  304. }
  305. break;
  306. }
  307. case Qt::DisplayRole:
  308. {
  309. switch (index.column())
  310. {
  311. case COLUMN_NAME:
  312. return node->GetName();
  313. default:
  314. break;
  315. }
  316. break;
  317. }
  318. case Qt::CheckStateRole:
  319. {
  320. if (index.column() == 0 && nodeInfo.m_checkable)
  321. {
  322. return nodeInfo.m_checkState;
  323. }
  324. break;
  325. }
  326. case Qt::DecorationRole:
  327. {
  328. switch (index.column())
  329. {
  330. case COLUMN_NAME:
  331. {
  332. return m_jointIcon;
  333. }
  334. case COLUMN_RAGDOLL_LIMIT:
  335. {
  336. const AZStd::shared_ptr<PhysicsSetup>& physicsSetup = m_actor->GetPhysicsSetup();
  337. if (physicsSetup)
  338. {
  339. const Physics::RagdollConfiguration& ragdollConfig = physicsSetup->GetRagdollConfig();
  340. const Physics::RagdollNodeConfiguration* ragdollNodeConfig = ragdollConfig.FindNodeConfigByName(node->GetName());
  341. if (ragdollNodeConfig)
  342. {
  343. return m_ragdollJointLimitIcon;
  344. }
  345. }
  346. break;
  347. }
  348. case COLUMN_RAGDOLL_COLLIDERS:
  349. {
  350. const AZStd::shared_ptr<PhysicsSetup>& physicsSetup = m_actor->GetPhysicsSetup();
  351. if (physicsSetup)
  352. {
  353. const Physics::RagdollConfiguration& ragdollConfig = physicsSetup->GetRagdollConfig();
  354. Physics::CharacterColliderNodeConfiguration* ragdollColliderNodeConfig = ragdollConfig.m_colliders.FindNodeConfigByName(node->GetName());
  355. if (ragdollColliderNodeConfig && !ragdollColliderNodeConfig->m_shapes.empty())
  356. {
  357. return m_ragdollColliderIcon;
  358. }
  359. }
  360. break;
  361. }
  362. case COLUMN_HITDETECTION_COLLIDERS:
  363. {
  364. const AZStd::shared_ptr<PhysicsSetup>& physicsSetup = m_actor->GetPhysicsSetup();
  365. if (physicsSetup)
  366. {
  367. const Physics::CharacterColliderConfiguration& hitDetectionConfig = physicsSetup->GetHitDetectionConfig();
  368. Physics::CharacterColliderNodeConfiguration* hitDetectionNodeConfig = hitDetectionConfig.FindNodeConfigByName(node->GetName());
  369. if (hitDetectionNodeConfig && !hitDetectionNodeConfig->m_shapes.empty())
  370. {
  371. return m_hitDetectionColliderIcon;
  372. }
  373. }
  374. break;
  375. }
  376. case COLUMN_CLOTH_COLLIDERS:
  377. {
  378. const AZStd::shared_ptr<PhysicsSetup>& physicsSetup = m_actor->GetPhysicsSetup();
  379. if (physicsSetup)
  380. {
  381. const Physics::CharacterColliderConfiguration& clothConfig = physicsSetup->GetClothConfig();
  382. Physics::CharacterColliderNodeConfiguration* clothNodeConfig = clothConfig.FindNodeConfigByName(node->GetName());
  383. if (clothNodeConfig && !clothNodeConfig->m_shapes.empty())
  384. {
  385. return m_clothColliderIcon;
  386. }
  387. }
  388. break;
  389. }
  390. case COLUMN_SIMULATED_JOINTS:
  391. {
  392. const AZStd::shared_ptr<SimulatedObjectSetup>& simulatedObjectSetup = m_actor->GetSimulatedObjectSetup();
  393. if (simulatedObjectSetup)
  394. {
  395. const AZStd::vector<SimulatedObject*> objects = simulatedObjectSetup->GetSimulatedObjects();
  396. for (const SimulatedObject* object : objects)
  397. {
  398. if (object->FindSimulatedJointBySkeletonJointIndex(node->GetNodeIndex()))
  399. {
  400. return m_simulatedJointIcon;
  401. }
  402. }
  403. }
  404. break;
  405. }
  406. case COLUMN_SIMULATED_COLLIDERS:
  407. {
  408. const AZStd::shared_ptr<PhysicsSetup>& physicsSetup = m_actor->GetPhysicsSetup();
  409. if (physicsSetup)
  410. {
  411. const Physics::CharacterColliderConfiguration& simulatedColliderConfig = physicsSetup->GetSimulatedObjectColliderConfig();
  412. Physics::CharacterColliderNodeConfiguration* simulatedColliderNodeConfig = simulatedColliderConfig.FindNodeConfigByName(node->GetName());
  413. if (simulatedColliderNodeConfig && !simulatedColliderNodeConfig->m_shapes.empty())
  414. {
  415. return m_simulatedColliderIcon;
  416. }
  417. }
  418. break;
  419. }
  420. }
  421. break;
  422. }
  423. case ROLE_NODE_INDEX:
  424. return qulonglong(nodeIndex);
  425. case ROLE_IS_CHARACTER_ROOT_NODE:
  426. return isRootNode;
  427. case ROLE_POINTER:
  428. return QVariant::fromValue(node);
  429. case ROLE_ACTOR_POINTER:
  430. return QVariant::fromValue(m_actor);
  431. case ROLE_ACTOR_INSTANCE_POINTER:
  432. return QVariant::fromValue(m_actorInstance);
  433. case ROLE_BONE:
  434. return nodeInfo.m_isBone;
  435. case ROLE_HASMESH:
  436. return nodeInfo.m_hasMesh;
  437. case ROLE_RAGDOLL:
  438. {
  439. const AZStd::shared_ptr<PhysicsSetup>& physicsSetup = m_actor->GetPhysicsSetup();
  440. if (physicsSetup)
  441. {
  442. const Physics::RagdollConfiguration& ragdollConfig = physicsSetup->GetRagdollConfig();
  443. const Physics::RagdollNodeConfiguration* ragdollNodeConfig = ragdollConfig.FindNodeConfigByName(node->GetName());
  444. return ragdollNodeConfig != nullptr;
  445. }
  446. }
  447. case ROLE_HITDETECTION:
  448. {
  449. const AZStd::shared_ptr<PhysicsSetup>& physicsSetup = m_actor->GetPhysicsSetup();
  450. if (physicsSetup)
  451. {
  452. const Physics::CharacterColliderConfiguration& hitDetectionConfig = physicsSetup->GetHitDetectionConfig();
  453. Physics::CharacterColliderNodeConfiguration* hitDetectionNodeConfig = hitDetectionConfig.FindNodeConfigByName(node->GetName());
  454. return (hitDetectionNodeConfig && !hitDetectionNodeConfig->m_shapes.empty());
  455. }
  456. }
  457. case ROLE_CLOTH:
  458. {
  459. const AZStd::shared_ptr<PhysicsSetup>& physicsSetup = m_actor->GetPhysicsSetup();
  460. if (physicsSetup)
  461. {
  462. const Physics::CharacterColliderConfiguration& clothConfig = physicsSetup->GetClothConfig();
  463. Physics::CharacterColliderNodeConfiguration* clothNodeConfig = clothConfig.FindNodeConfigByName(node->GetName());
  464. return (clothNodeConfig && !clothNodeConfig->m_shapes.empty());
  465. }
  466. }
  467. case ROLE_SIMULATED_JOINT:
  468. {
  469. const AZStd::shared_ptr<SimulatedObjectSetup>& simulatedObjectSetup = m_actor->GetSimulatedObjectSetup();
  470. if (simulatedObjectSetup)
  471. {
  472. const AZStd::vector<SimulatedObject*>& objects = simulatedObjectSetup->GetSimulatedObjects();
  473. const auto found = AZStd::find_if(objects.begin(), objects.end(), [node](const SimulatedObject* object)
  474. {
  475. return object->FindSimulatedJointBySkeletonJointIndex(node->GetNodeIndex());
  476. });
  477. return found != objects.end();
  478. }
  479. }
  480. case ROLE_SIMULATED_OBJECT_COLLIDER:
  481. {
  482. const AZStd::shared_ptr<PhysicsSetup>& physicsSetup = m_actor->GetPhysicsSetup();
  483. if (physicsSetup)
  484. {
  485. // TODO: Get the data from simulated collider setup.
  486. const Physics::CharacterColliderConfiguration& simulatedObjectConfig = physicsSetup->GetSimulatedObjectColliderConfig();
  487. Physics::CharacterColliderNodeConfiguration* simulatedObjectNodeConfig = simulatedObjectConfig.FindNodeConfigByName(node->GetName());
  488. return (simulatedObjectNodeConfig && !simulatedObjectNodeConfig->m_shapes.empty());
  489. }
  490. }
  491. default:
  492. break;
  493. }
  494. return QVariant();
  495. }
  496. Qt::ItemFlags SkeletonModel::flags(const QModelIndex& index) const
  497. {
  498. Qt::ItemFlags result = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
  499. if (!m_skeleton || !index.isValid())
  500. {
  501. AZ_Assert(false, "Cannot get model data. Skeleton or model index invalid.");
  502. return Qt::NoItemFlags;
  503. }
  504. Node* node = static_cast<Node*>(index.internalPointer());
  505. AZ_Assert(node, "Expected valid node pointer.");
  506. const NodeInfo& nodeInfo = GetNodeInfo(node);
  507. if (nodeInfo.m_checkable)
  508. {
  509. result |= Qt::ItemIsUserCheckable;
  510. }
  511. return result;
  512. }
  513. bool SkeletonModel::setData(const QModelIndex& index, const QVariant& value, int role)
  514. {
  515. if (!m_skeleton || !index.isValid())
  516. {
  517. AZ_Assert(false, "Cannot get model data. Skeleton or model index invalid.");
  518. return false;
  519. }
  520. const Node* node = static_cast<Node*>(index.internalPointer());
  521. AZ_Assert(node, "Expected valid node pointer.");
  522. NodeInfo& nodeInfo = GetNodeInfo(node);
  523. switch (role)
  524. {
  525. case Qt::CheckStateRole:
  526. {
  527. if (index.column() == 0 && nodeInfo.m_checkable)
  528. {
  529. nodeInfo.m_checkState = (Qt::CheckState)value.toInt();
  530. return true;
  531. }
  532. break;
  533. }
  534. }
  535. return true;
  536. }
  537. QModelIndex SkeletonModel::GetModelIndex(Node* node) const
  538. {
  539. if (!node)
  540. {
  541. return QModelIndex();
  542. }
  543. if (node == m_characterRootNode)
  544. {
  545. return createIndex(0, 0, node);
  546. }
  547. Node* parentNode = node->GetParentNode();
  548. if (parentNode)
  549. {
  550. const int numChildNodes = aznumeric_caster(parentNode->GetNumChildNodes());
  551. for (int i = 0; i < numChildNodes; ++i)
  552. {
  553. const Node* childNode = m_skeleton->GetNode(parentNode->GetChildIndex(i));
  554. if (childNode == node)
  555. {
  556. return createIndex(i, 0, node);
  557. }
  558. }
  559. }
  560. const int numRootNodes = aznumeric_caster(m_skeleton->GetNumRootNodes());
  561. for (int i = 0; i < numRootNodes; ++i)
  562. {
  563. const Node* rootNode = m_skeleton->GetNode(m_skeleton->GetRootNodeIndex(i));
  564. if (rootNode == node)
  565. {
  566. return createIndex(i, 0, node);
  567. }
  568. }
  569. return QModelIndex();
  570. }
  571. QModelIndexList SkeletonModel::GetModelIndicesForFullSkeleton() const
  572. {
  573. QModelIndexList result;
  574. const size_t jointCount = m_skeleton->GetNumNodes();
  575. for (size_t i = 0; i < jointCount; ++i)
  576. {
  577. Node* joint = m_skeleton->GetNode(i);
  578. result.push_back(GetModelIndex(joint));
  579. }
  580. return result;
  581. }
  582. void SkeletonModel::Reset()
  583. {
  584. beginResetModel();
  585. endResetModel();
  586. }
  587. void SkeletonModel::SetCheckable(bool isCheckable)
  588. {
  589. if (rowCount() > 0)
  590. {
  591. for (NodeInfo& nodeInfo : m_nodeInfos)
  592. {
  593. nodeInfo.m_checkable = isCheckable;
  594. }
  595. QVector<int> roles;
  596. roles.push_back(Qt::CheckStateRole);
  597. dataChanged(index(0, 0), index(rowCount() - 1, 0), roles);
  598. }
  599. }
  600. void SkeletonModel::ForEach(const AZStd::function<void(const QModelIndex& modelIndex)>& func)
  601. {
  602. QModelIndex modelIndex;
  603. const size_t jointCount = m_skeleton->GetNumNodes();
  604. for (size_t i = 0; i < jointCount; ++i)
  605. {
  606. Node* joint = m_skeleton->GetNode(i);
  607. modelIndex = GetModelIndex(joint);
  608. if (modelIndex.isValid())
  609. {
  610. func(modelIndex);
  611. }
  612. }
  613. }
  614. void SkeletonModel::ActorSelectionChanged(Actor* actor)
  615. {
  616. SetActor(actor);
  617. }
  618. void SkeletonModel::ActorInstanceSelectionChanged(EMotionFX::ActorInstance* actorInstance)
  619. {
  620. SetActorInstance(actorInstance);
  621. }
  622. SkeletonModel::NodeInfo& SkeletonModel::GetNodeInfo(const Node* node)
  623. {
  624. if (node == m_characterRootNode)
  625. {
  626. return m_nodeInfos[0];
  627. }
  628. return m_nodeInfos[node->GetNodeIndex() + 1];
  629. }
  630. const SkeletonModel::NodeInfo& SkeletonModel::GetNodeInfo(const Node* node) const
  631. {
  632. if (node == m_characterRootNode)
  633. {
  634. return m_nodeInfos[0];
  635. }
  636. return m_nodeInfos[node->GetNodeIndex() + 1];
  637. }
  638. void SkeletonModel::UpdateNodeInfos(Actor* actor)
  639. {
  640. if (!actor)
  641. {
  642. m_nodeInfos.clear();
  643. return;
  644. }
  645. const size_t numLodLevels = actor->GetNumLODLevels();
  646. const Skeleton* skeleton = actor->GetSkeleton();
  647. const size_t numNodes = skeleton->GetNumNodes();
  648. // We need to keep NodeInfo information for the "virtual" character root node that we add in this model.
  649. // That node is not coming from the skeleton so we need to manually make room for an extra NodeInfo struct
  650. // and adjust indices accordingly.
  651. // The info struct for the root node will be in m_nodeInfos[0] and all the other will be stored in
  652. // m_nodeInfos[node.index + 1]
  653. m_nodeInfos.resize(numNodes + 1);
  654. AZStd::vector<AZStd::vector<size_t> > boneListPerLodLevel;
  655. boneListPerLodLevel.resize(numLodLevels);
  656. for (size_t lodLevel = 0; lodLevel < numLodLevels; ++lodLevel)
  657. {
  658. actor->ExtractBoneList(lodLevel, &boneListPerLodLevel[lodLevel]);
  659. }
  660. // Starting from 1 to skip character root node info (we use default values for that node).
  661. for (size_t nodeIndex = 1; nodeIndex < numNodes; ++nodeIndex)
  662. {
  663. NodeInfo& nodeInfo = m_nodeInfos[nodeIndex];
  664. // Is bone?
  665. nodeInfo.m_isBone = AZStd::any_of(begin(boneListPerLodLevel), end(boneListPerLodLevel), [nodeIndex](const AZStd::vector<size_t>& lodLevel)
  666. {
  667. return AZStd::find(begin(lodLevel), end(lodLevel), nodeIndex - 1) != end(lodLevel);
  668. });
  669. // Has mesh?
  670. nodeInfo.m_hasMesh = false;
  671. for (size_t lodLevel = 0; lodLevel < numLodLevels; ++lodLevel)
  672. {
  673. if (actor->GetMesh(lodLevel, nodeIndex - 1))
  674. {
  675. nodeInfo.m_hasMesh = true;
  676. break;
  677. }
  678. }
  679. }
  680. }
  681. bool SkeletonModel::IndexIsRootNode(const QModelIndex& idx)
  682. {
  683. return idx.data(SkeletonModel::ROLE_IS_CHARACTER_ROOT_NODE).template value<bool>();
  684. }
  685. bool SkeletonModel::IndicesContainRootNode(const QModelIndexList& indices)
  686. {
  687. return AZStd::ranges::any_of(indices, [](const auto& index)
  688. {
  689. return index.data(SkeletonModel::ROLE_IS_CHARACTER_ROOT_NODE).template value<bool>();
  690. });
  691. }
  692. } // namespace EMotionFX