AnimationInspector.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using BansheeEngine;
  4. namespace BansheeEditor
  5. {
  6. /** @addtogroup Inspectors
  7. * @{
  8. */
  9. /// <summary>
  10. /// Renders an inspector for the <see cref="Animation"/> component.
  11. /// </summary>
  12. [CustomInspector(typeof(Animation))]
  13. internal class AnimationInspector : Inspector
  14. {
  15. private GUIResourceField animationClipField = new GUIResourceField(typeof(AnimationClip), new LocEdString("Clip"));
  16. private GUIEnumField wrapModeField = new GUIEnumField(typeof(AnimWrapMode), new LocEdString("Wrap mode"));
  17. private GUIFloatField speedField = new GUIFloatField(new LocEdString("Speed"));
  18. private GUIToggleField cullingField = new GUIToggleField(new LocEdString("Culling"));
  19. private GUIToggleField overrideBoundsField = new GUIToggleField(new LocEdString("Override bounds"));
  20. private GUIVector3Field centerField = new GUIVector3Field(new LocEdString("Center"));
  21. private GUIVector3Field sizeField = new GUIVector3Field(new LocEdString("Size"));
  22. private InspectableState modifyState;
  23. /// <inheritdoc/>
  24. protected internal override void Initialize()
  25. {
  26. BuildGUI();
  27. }
  28. /// <inheritdoc/>
  29. protected internal override InspectableState Refresh()
  30. {
  31. Animation animation = InspectedObject as Animation;
  32. if (animation == null)
  33. return InspectableState.NotModified;
  34. animationClipField.Value = animation.DefaultClip;
  35. wrapModeField.Value = (ulong)animation.WrapMode;
  36. speedField.Value = animation.Speed;
  37. cullingField.Value = animation.Cull;
  38. overrideBoundsField.Value = animation.UseBounds;
  39. centerField.Value = animation.Bounds.Center;
  40. sizeField.Value = animation.Bounds.Size;
  41. InspectableState oldState = modifyState;
  42. if (modifyState.HasFlag(InspectableState.Modified))
  43. modifyState = InspectableState.NotModified;
  44. return oldState;
  45. }
  46. /// <summary>
  47. /// Recreates all the GUI elements used by this inspector.
  48. /// </summary>
  49. private void BuildGUI()
  50. {
  51. Layout.Clear();
  52. Animation animation = InspectedObject as Animation;
  53. if (animation == null)
  54. return;
  55. animationClipField.OnChanged += x =>
  56. {
  57. AnimationClip clip = Resources.Load<AnimationClip>(x);
  58. animation.DefaultClip = clip;
  59. MarkAsModified();
  60. ConfirmModify();
  61. };
  62. wrapModeField.OnSelectionChanged += x =>
  63. {
  64. animation.WrapMode = (AnimWrapMode)x;
  65. MarkAsModified();
  66. ConfirmModify();
  67. };
  68. speedField.OnChanged += x => { animation.Speed = x; MarkAsModified(); };
  69. speedField.OnConfirmed += ConfirmModify;
  70. speedField.OnFocusLost += ConfirmModify;
  71. cullingField.OnChanged += x => { animation.Cull = x; MarkAsModified(); ConfirmModify(); };
  72. overrideBoundsField.OnChanged += x => { animation.UseBounds = x; MarkAsModified(); ConfirmModify(); };
  73. centerField.OnChanged += x =>
  74. {
  75. AABox bounds = animation.Bounds;
  76. Vector3 min = x - bounds.Size*0.5f;
  77. Vector3 max = x + bounds.Size*0.5f;
  78. animation.Bounds = new AABox(min, max);
  79. MarkAsModified();
  80. };
  81. centerField.OnConfirmed += ConfirmModify;
  82. centerField.OnFocusLost += ConfirmModify;
  83. sizeField.OnChanged += x =>
  84. {
  85. AABox bounds = animation.Bounds;
  86. Vector3 min = bounds.Center - x * 0.5f;
  87. Vector3 max = bounds.Center + x * 0.5f;
  88. animation.Bounds = new AABox(min, max);
  89. MarkAsModified();
  90. };
  91. sizeField.OnConfirmed += ConfirmModify;
  92. sizeField.OnFocusLost += ConfirmModify;
  93. Layout.AddElement(animationClipField);
  94. Layout.AddElement(wrapModeField);
  95. Layout.AddElement(speedField);
  96. Layout.AddElement(cullingField);
  97. Layout.AddElement(overrideBoundsField);
  98. GUILayoutX boundsLayout = Layout.AddLayoutX();
  99. boundsLayout.AddElement(new GUILabel(new LocEdString("Bounds"), GUIOption.FixedWidth(100)));
  100. GUILayoutY boundsContent = boundsLayout.AddLayoutY();
  101. boundsContent.AddElement(centerField);
  102. boundsContent.AddElement(sizeField);
  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. }