//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
//**************** Copyright (c) 2016 Marko Pintera (marko.pintera@gmail.com). All rights reserved. **********************//
using System;
namespace BansheeEngine
{
/** @addtogroup Animation
* @{
*/
///
/// Component that maps animation for specific bone also be applied to the this component
/// is attached to. The component will attach to the first found parent component.
///
public class Bone : Component
{
[SerializeField]
private string name;
private Animation parent;
///
/// Name of the bone to attach to.
///
public string Name
{
get { return name; }
set
{
if (name != value)
{
name = value;
if (parent != null)
parent.NotifyBoneChanged(this);
}
}
}
private void OnInitialize()
{
NotifyFlags = TransformChangedFlags.Parent;
}
private void OnEnable()
{
UpdateParentAnimation();
}
private void OnDisable()
{
if (parent != null)
parent.RemoveBone(this);
parent = null;
}
private void OnDestroy()
{
if (parent != null)
parent.RemoveBone(this);
parent = null;
}
private void OnTransformChanged(TransformChangedFlags flags)
{
if (!SceneObject.Active)
return;
if ((flags & TransformChangedFlags.Parent) != 0)
UpdateParentAnimation();
}
///
/// Attempts to find the parent component and registers itself with it.
///
private void UpdateParentAnimation()
{
SceneObject currentSO = SceneObject;
while (currentSO != null)
{
Animation parent = currentSO.GetComponent();
if (parent != null)
{
if (currentSO.Active)
SetParent(parent);
else
SetParent(null);
return;
}
currentSO = currentSO.Parent;
}
SetParent(null);
}
///
/// Changes the parent animation of this component.
///
/// New animation parent, can be null.
/// If true the bone will just be changed internally, but parent animation will not be
/// notified.
internal void SetParent(Animation animation, bool isInternal = false)
{
if (animation == parent)
return;
if (!isInternal)
{
if (parent != null)
parent.RemoveBone(this);
if (animation != null)
animation.AddBone(this);
}
parent = animation;
}
}
/** @} */
}