BoneInspector.cs 4.4 KB

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