BoneInspector.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using bs;
  6. namespace bs.Editor
  7. {
  8. /** @addtogroup Inspectors
  9. * @{
  10. */
  11. /// <summary>
  12. /// Renders an inspector for the <see cref="Bone"/> component.
  13. /// </summary>
  14. [CustomInspector(typeof(Bone))]
  15. internal class BoneInspector : Inspector
  16. {
  17. private GUIListBoxField boneField;
  18. private InspectableState modifyState;
  19. private string selectedBoneName;
  20. /// <inheritdoc/>
  21. protected internal override void Initialize()
  22. {
  23. Layout.Clear();
  24. Bone bone = InspectedObject as Bone;
  25. if (bone == null)
  26. return;
  27. string[] boneNames = GetBoneNames(bone);
  28. if(boneNames == null)
  29. boneNames = new string[0];
  30. boneField = new GUIListBoxField(boneNames, false, new LocEdString("Bone"));
  31. Layout.AddElement(boneField);
  32. boneField.OnSelectionChanged += x =>
  33. {
  34. selectedBoneName = boneNames[x];
  35. StartUndo("bone");
  36. bone.Name = selectedBoneName;
  37. EndUndo();
  38. MarkAsModified();
  39. ConfirmModify();
  40. };
  41. }
  42. /// <inheritdoc/>
  43. protected internal override InspectableState Refresh(bool force = false)
  44. {
  45. Bone bone = InspectedObject as Bone;
  46. if (bone == null)
  47. return InspectableState.NotModified;
  48. if (selectedBoneName != bone.Name)
  49. {
  50. string[] boneNames = GetBoneNames(bone);
  51. if (boneNames != null)
  52. {
  53. for (int i = 0; i < boneNames.Length; i++)
  54. {
  55. if (bone.Name == boneNames[i])
  56. {
  57. selectedBoneName = bone.Name;
  58. boneField.Index = i;
  59. break;
  60. }
  61. }
  62. }
  63. }
  64. InspectableState oldState = modifyState;
  65. if (modifyState.HasFlag(InspectableState.Modified))
  66. modifyState = InspectableState.NotModified;
  67. return oldState;
  68. }
  69. /// <summary>
  70. /// Finds all available bones for the animation the provided bone is a part of.
  71. /// </summary>
  72. /// <param name="bone">Bone for which to return the parent skeleton's bones.</param>
  73. /// <returns>List of bones if parent skeleton is found, or null.</returns>
  74. private string[] GetBoneNames(Bone bone)
  75. {
  76. Animation animParent = null;
  77. SceneObject currentSO = bone.SceneObject;
  78. while (currentSO != null)
  79. {
  80. animParent = currentSO.GetComponent<Animation>();
  81. if (animParent != null)
  82. break;
  83. currentSO = currentSO.Parent;
  84. }
  85. if(animParent == null)
  86. return null;
  87. Renderable renderable = animParent.SceneObject.GetComponent<Renderable>();
  88. if (renderable == null)
  89. return null;
  90. RRef<Mesh> mesh = renderable.Mesh;
  91. if (mesh == null)
  92. return null;
  93. Skeleton skeleton = mesh.Value.Skeleton;
  94. if (skeleton == null)
  95. return null;
  96. string[] boneNames = new string[skeleton.NumBones];
  97. for (int i = 0; i < boneNames.Length; i++)
  98. boneNames[i] = skeleton.GetBoneInfo(i).name;
  99. return boneNames;
  100. }
  101. /// <summary>
  102. /// Marks the contents of the inspector as modified.
  103. /// </summary>
  104. protected void MarkAsModified()
  105. {
  106. modifyState |= InspectableState.ModifyInProgress;
  107. }
  108. /// <summary>
  109. /// Confirms any queued modifications.
  110. /// </summary>
  111. protected void ConfirmModify()
  112. {
  113. if (modifyState.HasFlag(InspectableState.ModifyInProgress))
  114. modifyState |= InspectableState.Modified;
  115. }
  116. }
  117. /** @} */
  118. }