Bone.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. namespace BansheeEngine
  5. {
  6. /** @addtogroup Animation
  7. * @{
  8. */
  9. /// <summary>
  10. /// Component that maps animation for specific bone also be applied to the <see cref="SceneObject"/> this component
  11. /// is attached to. The component will attach to the first found parent <see cref="Animation"/> component.
  12. /// </summary>
  13. public class Bone : ManagedComponent
  14. {
  15. [SerializeField]
  16. private string name;
  17. private Animation parent;
  18. /// <summary>
  19. /// Name of the bone to attach to.
  20. /// </summary>
  21. public string Name
  22. {
  23. get { return name; }
  24. set
  25. {
  26. if (name != value)
  27. {
  28. name = value;
  29. if (parent != null)
  30. parent.NotifyBoneChanged(this);
  31. }
  32. }
  33. }
  34. private void OnInitialize()
  35. {
  36. NotifyFlags = TransformChangedFlags.Parent;
  37. }
  38. private void OnEnable()
  39. {
  40. UpdateParentAnimation();
  41. }
  42. private void OnDisable()
  43. {
  44. if (parent != null)
  45. parent.RemoveBone(this);
  46. parent = null;
  47. }
  48. private void OnDestroy()
  49. {
  50. if (parent != null)
  51. parent.RemoveBone(this);
  52. parent = null;
  53. }
  54. private void OnTransformChanged(TransformChangedFlags flags)
  55. {
  56. if (!SceneObject.Active)
  57. return;
  58. if ((flags & TransformChangedFlags.Parent) != 0)
  59. UpdateParentAnimation();
  60. }
  61. /// <summary>
  62. /// Attempts to find the parent <see cref="Animation"/> component and registers itself with it.
  63. /// </summary>
  64. private void UpdateParentAnimation()
  65. {
  66. SceneObject currentSO = SceneObject;
  67. while (currentSO != null)
  68. {
  69. Animation parent = currentSO.GetComponent<Animation>();
  70. if (parent != null)
  71. {
  72. if (currentSO.Active)
  73. SetParent(parent);
  74. else
  75. SetParent(null);
  76. return;
  77. }
  78. currentSO = currentSO.Parent;
  79. }
  80. SetParent(null);
  81. }
  82. /// <summary>
  83. /// Changes the parent animation of this component.
  84. /// </summary>
  85. /// <param name="animation">New animation parent, can be null.</param>
  86. /// <param name="isInternal">If true the bone will just be changed internally, but parent animation will not be
  87. /// notified.</param>
  88. internal void SetParent(Animation animation, bool isInternal = false)
  89. {
  90. if (animation == parent)
  91. return;
  92. if (!isInternal)
  93. {
  94. if (parent != null)
  95. parent.RemoveBone(this);
  96. if (animation != null)
  97. animation.AddBone(this);
  98. }
  99. parent = animation;
  100. }
  101. }
  102. /** @} */
  103. }