BoneInspector.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 BansheeEngine;
  6. namespace BansheeEditor
  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. BuildGUI();
  24. }
  25. /// <inheritdoc/>
  26. protected internal override InspectableState Refresh()
  27. {
  28. Bone bone = InspectedObject as Bone;
  29. if (bone == null)
  30. return InspectableState.NotModified;
  31. if (selectedBoneName != bone.Name)
  32. {
  33. string[] boneNames = GetBoneNames(bone);
  34. if (boneNames != null)
  35. {
  36. for (int i = 0; i < boneNames.Length; i++)
  37. {
  38. if (bone.Name == boneNames[i])
  39. {
  40. selectedBoneName = bone.Name;
  41. boneField.Index = i;
  42. break;
  43. }
  44. }
  45. }
  46. }
  47. InspectableState oldState = modifyState;
  48. if (modifyState.HasFlag(InspectableState.Modified))
  49. modifyState = InspectableState.NotModified;
  50. return oldState;
  51. }
  52. /// <summary>
  53. /// Recreates all the GUI elements used by this inspector.
  54. /// </summary>
  55. private void BuildGUI()
  56. {
  57. Layout.Clear();
  58. Bone bone = InspectedObject as Bone;
  59. if (bone == null)
  60. return;
  61. string[] boneNames = GetBoneNames(bone);
  62. if(boneNames == null)
  63. boneNames = new string[0];
  64. boneField = new GUIListBoxField(boneNames, false, new LocEdString("Bone"));
  65. Layout.AddElement(boneField);
  66. boneField.OnSelectionChanged += x =>
  67. {
  68. selectedBoneName = boneNames[x];
  69. bone.Name = selectedBoneName;
  70. MarkAsModified();
  71. ConfirmModify();
  72. };
  73. }
  74. /// <summary>
  75. /// Finds all available bones for the animation the provided bone is a part of.
  76. /// </summary>
  77. /// <param name="bone">Bone for which to return the parent skeleton's bones.</param>
  78. /// <returns>List of bones if parent skeleton is found, or null.</returns>
  79. private string[] GetBoneNames(Bone bone)
  80. {
  81. Animation animParent = null;
  82. SceneObject currentSO = bone.SceneObject;
  83. while (currentSO != null)
  84. {
  85. animParent = currentSO.GetComponent<Animation>();
  86. if (animParent != null)
  87. break;
  88. currentSO = currentSO.Parent;
  89. }
  90. if(animParent == null)
  91. return null;
  92. Renderable renderable = animParent.SceneObject.GetComponent<Renderable>();
  93. if (renderable == null)
  94. return null;
  95. Mesh mesh = renderable.Mesh;
  96. if (mesh == null)
  97. return null;
  98. Skeleton skeleton = mesh.Skeleton;
  99. string[] boneNames = new string[skeleton.NumBones];
  100. for (int i = 0; i < boneNames.Length; i++)
  101. boneNames[i] = skeleton.GetBoneInfo(i).name;
  102. return boneNames;
  103. }
  104. /// <summary>
  105. /// Marks the contents of the inspector as modified.
  106. /// </summary>
  107. protected void MarkAsModified()
  108. {
  109. modifyState |= InspectableState.ModifyInProgress;
  110. }
  111. /// <summary>
  112. /// Confirms any queued modifications.
  113. /// </summary>
  114. protected void ConfirmModify()
  115. {
  116. if (modifyState.HasFlag(InspectableState.ModifyInProgress))
  117. modifyState |= InspectableState.Modified;
  118. }
  119. }
  120. /** @} */
  121. }