JointInspector.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 common inspector elements for all <see cref="Joint"/> components.
  11. /// </summary>
  12. internal abstract class JointInspector : Inspector
  13. {
  14. private GUIGameObjectField targetField;
  15. private GUIVector3Field targetOffsetField;
  16. private GUIGameObjectField anchorField;
  17. private GUIVector3Field anchorOffsetField;
  18. private GUIFloatField breakForceField;
  19. private GUIFloatField breakTorqueField;
  20. private GUIToggleField collisionField;
  21. private bool showOffsets;
  22. protected InspectableState modifyState;
  23. /// <summary>
  24. /// Updates all GUI elements from current values in the joint.
  25. /// </summary>
  26. /// <param name="joint">Joint to update the GUI from.</param>
  27. protected virtual void Refresh(Joint joint)
  28. {
  29. targetField.Value = joint.GetBody(JointBody.Target);
  30. anchorField.Value = joint.GetBody(JointBody.Anchor);
  31. if (showOffsets)
  32. {
  33. targetOffsetField.Value = joint.GetPosition(JointBody.Target);
  34. anchorOffsetField.Value = joint.GetPosition(JointBody.Anchor);
  35. }
  36. breakForceField.Value = joint.BreakForce;
  37. breakTorqueField.Value = joint.BreakTorque;
  38. collisionField.Value = joint.EnableCollision;
  39. }
  40. /// <summary>
  41. /// Creates GUI elements for fields common to all joints.
  42. /// </summary>
  43. protected virtual void BuildGUI(Joint joint, bool showOffsets)
  44. {
  45. this.showOffsets = showOffsets;
  46. targetField = new GUIGameObjectField(typeof(Rigidbody), new LocEdString("Target"));
  47. anchorField = new GUIGameObjectField(typeof(Rigidbody), new LocEdString("Anchor"));
  48. if (showOffsets)
  49. {
  50. targetOffsetField = new GUIVector3Field(new LocEdString("Target offset"));
  51. anchorOffsetField = new GUIVector3Field(new LocEdString("Anchor offset"));
  52. }
  53. breakForceField = new GUIFloatField(new LocEdString("Break force"));
  54. breakTorqueField = new GUIFloatField(new LocEdString("Break torque"));
  55. collisionField = new GUIToggleField(new LocEdString("Enable collision"));
  56. targetField.OnChanged += x => { joint.SetBody(JointBody.Target, (Rigidbody)x); MarkAsModified(); ConfirmModify(); };
  57. anchorField.OnChanged += x => { joint.SetBody(JointBody.Anchor, (Rigidbody)x); MarkAsModified(); ConfirmModify(); };
  58. if(showOffsets)
  59. {
  60. targetOffsetField.OnChanged += x =>
  61. {
  62. joint.SetTransform(JointBody.Target, x, joint.GetRotation(JointBody.Target));
  63. MarkAsModified();
  64. };
  65. targetOffsetField.OnFocusLost += ConfirmModify;
  66. targetOffsetField.OnConfirmed += ConfirmModify;
  67. anchorOffsetField.OnChanged += x =>
  68. {
  69. joint.SetTransform(JointBody.Anchor, x, joint.GetRotation(JointBody.Anchor));
  70. MarkAsModified();
  71. };
  72. anchorOffsetField.OnFocusLost += ConfirmModify;
  73. anchorOffsetField.OnConfirmed += ConfirmModify;
  74. }
  75. breakForceField.OnChanged += x => { joint.BreakForce = x; MarkAsModified(); };
  76. breakForceField.OnFocusLost += ConfirmModify;
  77. breakForceField.OnConfirmed += ConfirmModify;
  78. breakTorqueField.OnChanged += x => { joint.BreakTorque = x; MarkAsModified(); };
  79. breakTorqueField.OnFocusLost += ConfirmModify;
  80. breakTorqueField.OnConfirmed += ConfirmModify;
  81. collisionField.OnChanged += x => { joint.EnableCollision = x; MarkAsModified(); ConfirmModify(); };
  82. Layout.AddElement(targetField);
  83. if(showOffsets)
  84. Layout.AddElement(targetOffsetField);
  85. Layout.AddElement(anchorField);
  86. if(showOffsets)
  87. Layout.AddElement(anchorOffsetField);
  88. Layout.AddElement(breakForceField);
  89. Layout.AddElement(breakTorqueField);
  90. Layout.AddElement(collisionField);
  91. }
  92. /// <summary>
  93. /// Marks the contents of the inspector as modified.
  94. /// </summary>
  95. protected void MarkAsModified()
  96. {
  97. modifyState |= InspectableState.ModifyInProgress;
  98. }
  99. /// <summary>
  100. /// Confirms any queued modifications.
  101. /// </summary>
  102. protected void ConfirmModify()
  103. {
  104. if (modifyState.HasFlag(InspectableState.ModifyInProgress))
  105. modifyState |= InspectableState.Modified;
  106. }
  107. }
  108. /** @} */
  109. }