CapsuleColliderInspector.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /// <summary>
  7. /// Renders an inspector for the <see cref="CapsuleCollider"/> component.
  8. /// </summary>
  9. [CustomInspector(typeof(CapsuleCollider))]
  10. public class CapsuleColliderInspector : ColliderInspector
  11. {
  12. private GUIVector3Field centerField = new GUIVector3Field(new LocEdString("Center"));
  13. private GUIVector3Field orientationField = new GUIVector3Field(new LocEdString("Orientation"));
  14. private GUIFloatField radiusField = new GUIFloatField(new LocEdString("Radius"));
  15. private GUIFloatField halfHeightField = new GUIFloatField(new LocEdString("Half height"));
  16. private Vector3 orientation;
  17. /// <inheritdoc/>
  18. protected internal override void Initialize()
  19. {
  20. CapsuleCollider collider = InspectedObject as CapsuleCollider;
  21. if (collider != null)
  22. BuildGUI(collider);
  23. }
  24. /// <inheritdoc/>
  25. protected internal override InspectableState Refresh()
  26. {
  27. CapsuleCollider collider = InspectedObject as CapsuleCollider;
  28. if (collider == null)
  29. return InspectableState.NotModified;
  30. Refresh(collider);
  31. InspectableState oldState = modifyState;
  32. if (modifyState.HasFlag(InspectableState.Modified))
  33. modifyState = InspectableState.NotModified;
  34. return oldState;
  35. }
  36. /// <summary>
  37. /// Creates GUI elements for fields specific to the capsule collider.
  38. /// </summary>
  39. protected void BuildGUI(CapsuleCollider collider)
  40. {
  41. centerField.OnChanged += x => { collider.Center = x; MarkAsModified(); };
  42. centerField.OnFocusLost += ConfirmModify;
  43. centerField.OnConfirmed += ConfirmModify;
  44. orientationField.OnChanged += x =>
  45. {
  46. orientation = x;
  47. Quaternion rotation = Quaternion.FromEuler(x);
  48. collider.Normal = rotation.Rotate(Vector3.YAxis);
  49. MarkAsModified();
  50. };
  51. orientationField.OnFocusLost += ConfirmModify;
  52. orientationField.OnConfirmed += ConfirmModify;
  53. radiusField.OnChanged += x => { collider.Radius = x; MarkAsModified(); };
  54. radiusField.OnFocusLost += ConfirmModify;
  55. radiusField.OnConfirmed += ConfirmModify;
  56. halfHeightField.OnChanged += x => { collider.HalfHeight = x; MarkAsModified(); };
  57. halfHeightField.OnFocusLost += ConfirmModify;
  58. halfHeightField.OnConfirmed += ConfirmModify;
  59. Layout.AddElement(centerField);
  60. Layout.AddElement(orientationField);
  61. Layout.AddElement(radiusField);
  62. Layout.AddElement(halfHeightField);
  63. orientation = Quaternion.FromToRotation(Vector3.YAxis, collider.Normal).ToEuler();
  64. base.BuildGUI(collider);
  65. }
  66. /// <summary>
  67. /// Updates all GUI elements from current values in the collider.
  68. /// </summary>
  69. /// <param name="collider">Collider to update the GUI from.</param>
  70. protected void Refresh(CapsuleCollider collider)
  71. {
  72. if (!centerField.HasInputFocus)
  73. centerField.Value = collider.Center;
  74. if (!orientationField.HasInputFocus)
  75. orientationField.Value = orientation;
  76. if (!radiusField.HasInputFocus)
  77. radiusField.Value = collider.Radius;
  78. if (!halfHeightField.HasInputFocus)
  79. halfHeightField.Value = collider.HalfHeight;
  80. base.Refresh(collider);
  81. }
  82. }
  83. }