AnimationInspector.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System.Collections.Generic;
  4. using BansheeEngine;
  5. namespace BansheeEditor
  6. {
  7. /** @addtogroup Inspectors
  8. * @{
  9. */
  10. /// <summary>
  11. /// Renders an inspector for the <see cref="Animation"/> component.
  12. /// </summary>
  13. [CustomInspector(typeof(Animation))]
  14. internal class AnimationInspector : Inspector
  15. {
  16. private GUIResourceField animationClipField = new GUIResourceField(typeof(AnimationClip), new LocEdString("Clip"));
  17. private GUIEnumField wrapModeField = new GUIEnumField(typeof(AnimWrapMode), new LocEdString("Wrap mode"));
  18. private GUIFloatField speedField = new GUIFloatField(new LocEdString("Speed"));
  19. private GUIToggleField cullingField = new GUIToggleField(new LocEdString("Culling"));
  20. private GUIToggleField overrideBoundsField = new GUIToggleField(new LocEdString("Override bounds"));
  21. private GUIVector3Field centerField = new GUIVector3Field(new LocEdString("Center"));
  22. private GUIVector3Field sizeField = new GUIVector3Field(new LocEdString("Size"));
  23. private InspectableState modifyState;
  24. /// <inheritdoc/>
  25. protected internal override void Initialize()
  26. {
  27. BuildGUI();
  28. }
  29. /// <inheritdoc/>
  30. protected internal override InspectableState Refresh()
  31. {
  32. Animation animation = InspectedObject as Animation;
  33. if (animation == null)
  34. return InspectableState.NotModified;
  35. animationClipField.Value = animation.DefaultClip;
  36. wrapModeField.Value = (ulong)animation.WrapMode;
  37. speedField.Value = animation.Speed;
  38. cullingField.Value = animation.Cull;
  39. overrideBoundsField.Value = animation.UseBounds;
  40. centerField.Value = animation.Bounds.Center;
  41. sizeField.Value = animation.Bounds.Size;
  42. InspectableState oldState = modifyState;
  43. if (modifyState.HasFlag(InspectableState.Modified))
  44. modifyState = InspectableState.NotModified;
  45. return oldState;
  46. }
  47. /// <summary>
  48. /// Recreates all the GUI elements used by this inspector.
  49. /// </summary>
  50. private void BuildGUI()
  51. {
  52. Layout.Clear();
  53. Animation animation = InspectedObject as Animation;
  54. if (animation == null)
  55. return;
  56. animationClipField.OnChanged += x =>
  57. {
  58. AnimationClip clip = Resources.Load<AnimationClip>(x);
  59. animation.DefaultClip = clip;
  60. MarkAsModified();
  61. ConfirmModify();
  62. };
  63. wrapModeField.OnSelectionChanged += x =>
  64. {
  65. animation.WrapMode = (AnimWrapMode)x;
  66. MarkAsModified();
  67. ConfirmModify();
  68. };
  69. speedField.OnChanged += x => { animation.Speed = x; MarkAsModified(); };
  70. speedField.OnConfirmed += ConfirmModify;
  71. speedField.OnFocusLost += ConfirmModify;
  72. cullingField.OnChanged += x => { animation.Cull = x; MarkAsModified(); ConfirmModify(); };
  73. overrideBoundsField.OnChanged += x => { animation.UseBounds = x; MarkAsModified(); ConfirmModify(); };
  74. centerField.OnChanged += x =>
  75. {
  76. AABox bounds = animation.Bounds;
  77. Vector3 min = x - bounds.Size*0.5f;
  78. Vector3 max = x + bounds.Size*0.5f;
  79. animation.Bounds = new AABox(min, max);
  80. MarkAsModified();
  81. };
  82. centerField.OnConfirmed += ConfirmModify;
  83. centerField.OnFocusLost += ConfirmModify;
  84. sizeField.OnChanged += x =>
  85. {
  86. AABox bounds = animation.Bounds;
  87. Vector3 min = bounds.Center - x * 0.5f;
  88. Vector3 max = bounds.Center + x * 0.5f;
  89. animation.Bounds = new AABox(min, max);
  90. MarkAsModified();
  91. };
  92. sizeField.OnConfirmed += ConfirmModify;
  93. sizeField.OnFocusLost += ConfirmModify;
  94. Layout.AddElement(animationClipField);
  95. Layout.AddElement(wrapModeField);
  96. Layout.AddElement(speedField);
  97. Layout.AddElement(cullingField);
  98. Layout.AddElement(overrideBoundsField);
  99. GUILayoutX boundsLayout = Layout.AddLayoutX();
  100. boundsLayout.AddElement(new GUILabel(new LocEdString("Bounds"), GUIOption.FixedWidth(100)));
  101. GUILayoutY boundsContent = boundsLayout.AddLayoutY();
  102. boundsContent.AddElement(centerField);
  103. boundsContent.AddElement(sizeField);
  104. // Morph shapes
  105. Renderable renderable = animation.SceneObject.GetComponent<Renderable>();
  106. MorphShapes morphShapes = renderable?.Mesh?.MorphShapes;
  107. if (morphShapes != null)
  108. {
  109. GUIToggle morphShapesToggle = new GUIToggle(new LocEdString("Morph shapes"), EditorStyles.Foldout);
  110. Layout.AddElement(morphShapesToggle);
  111. GUILayoutY channelsLayout = Layout.AddLayoutY();
  112. morphShapesToggle.OnToggled += x =>
  113. {
  114. channelsLayout.Active = x;
  115. Persistent.SetBool("Channels_Expanded", x);
  116. };
  117. channelsLayout.Active = Persistent.GetBool("Channels_Expanded");
  118. MorphChannel[] channels = morphShapes.Channels;
  119. for (int i = 0; i < channels.Length; i++)
  120. {
  121. GUILayoutY channelLayout = channelsLayout.AddLayoutY();
  122. GUILayoutX channelTitleLayout = channelLayout.AddLayoutX();
  123. channelLayout.AddSpace(5);
  124. GUILayoutY channelContentLayout = channelLayout.AddLayoutY();
  125. string channelName = channels[i].Name;
  126. GUIToggle channelNameField = new GUIToggle(channelName, EditorStyles.Expand, GUIOption.FlexibleWidth());
  127. channelTitleLayout.AddSpace(15); // Indent
  128. channelTitleLayout.AddElement(channelNameField);
  129. channelTitleLayout.AddFlexibleSpace();
  130. channelNameField.OnToggled += x =>
  131. {
  132. channelContentLayout.Active = x;
  133. Persistent.SetBool(channelName + "_Expanded", x);
  134. };
  135. channelContentLayout.Active = Persistent.GetBool(channelName + "_Expanded");
  136. MorphShape[] shapes = channels[i].Shapes;
  137. for (int j = 0; j < shapes.Length; j++)
  138. {
  139. GUILayoutX shapeLayout = channelContentLayout.AddLayoutX();
  140. channelContentLayout.AddSpace(5);
  141. LocString nameString = new LocString("[{0}]. {1}");
  142. nameString.SetParameter(0, j.ToString());
  143. nameString.SetParameter(1, shapes[j].Name);
  144. GUILabel shapeNameField = new GUILabel(shapes[j].Name);
  145. LocString weightString = new LocEdString("Weight: {0}");
  146. weightString.SetParameter(0, shapes[j].Weight.ToString());
  147. GUILabel weightField = new GUILabel(weightString);
  148. shapeLayout.AddSpace(30); // Indent
  149. shapeLayout.AddElement(shapeNameField);
  150. shapeLayout.AddFlexibleSpace();
  151. shapeLayout.AddElement(weightField);
  152. }
  153. }
  154. }
  155. }
  156. /// <summary>
  157. /// Marks the contents of the inspector as modified.
  158. /// </summary>
  159. protected void MarkAsModified()
  160. {
  161. modifyState |= InspectableState.ModifyInProgress;
  162. }
  163. /// <summary>
  164. /// Confirms any queued modifications.
  165. /// </summary>
  166. protected void ConfirmModify()
  167. {
  168. if (modifyState.HasFlag(InspectableState.ModifyInProgress))
  169. modifyState |= InspectableState.Modified;
  170. }
  171. }
  172. /** @} */
  173. }