1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#include "Components/BsCBone.h"
4#include "Scene/BsSceneObject.h"
5#include "Components/BsCAnimation.h"
6#include "Private/RTTI/BsCBoneRTTI.h"
7
8using namespace std::placeholders;
9
10namespace bs
11{
12 CBone::CBone()
13 {
14 setName("Bone");
15
16 mNotifyFlags = TCF_Parent;
17 setFlag(ComponentFlag::AlwaysRun, true);
18 }
19
20 CBone::CBone(const HSceneObject& parent)
21 : Component(parent)
22 {
23 setName("Bone");
24
25 mNotifyFlags = TCF_Parent;
26 }
27
28 void CBone::setBoneName(const String& name)
29 {
30 if (mBoneName == name)
31 return;
32
33 mBoneName = name;
34
35 if (mParent != nullptr)
36 mParent->_notifyBoneChanged(static_object_cast<CBone>(getHandle()));
37 }
38
39 void CBone::onDestroyed()
40 {
41 if (mParent != nullptr)
42 mParent->_removeBone(static_object_cast<CBone>(getHandle()));
43
44 mParent = nullptr;
45 }
46
47 void CBone::onDisabled()
48 {
49 if (mParent != nullptr)
50 mParent->_removeBone(static_object_cast<CBone>(getHandle()));
51
52 mParent = nullptr;
53 }
54
55 void CBone::onEnabled()
56 {
57 updateParentAnimation();
58 }
59
60 void CBone::onTransformChanged(TransformChangedFlags flags)
61 {
62 if (!SO()->getActive())
63 return;
64
65 if ((flags & TCF_Parent) != 0)
66 updateParentAnimation();
67 }
68
69 void CBone::updateParentAnimation()
70 {
71 HSceneObject currentSO = SO();
72 while (currentSO != nullptr)
73 {
74 HAnimation parent = currentSO->getComponent<CAnimation>();
75 if (parent != nullptr)
76 {
77 if (currentSO->getActive())
78 _setParent(parent);
79 else
80 _setParent(HAnimation());
81
82 return;
83 }
84
85 currentSO = currentSO->getParent();
86 }
87
88 _setParent(HAnimation());
89 }
90
91 void CBone::_setParent(const HAnimation& animation, bool isInternal)
92 {
93 if (animation == mParent)
94 return;
95
96 if (!isInternal)
97 {
98 if (mParent != nullptr)
99 mParent->_removeBone(static_object_cast<CBone>(getHandle()));
100
101 if (animation != nullptr)
102 animation->_addBone(static_object_cast<CBone>(getHandle()));
103 }
104
105 mParent = animation;
106 }
107
108 RTTITypeBase* CBone::getRTTIStatic()
109 {
110 return CBoneRTTI::instance();
111 }
112
113 RTTITypeBase* CBone::getRTTI() const
114 {
115 return CBone::getRTTIStatic();
116 }
117}