JointInspector.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.GetRigidbody(JointBody.Target);
  30. anchorField.Value = joint.GetRigidbody(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.SetRigidbody(JointBody.Target, (Rigidbody)x); MarkAsModified(); ConfirmModify(); };
  57. anchorField.OnChanged += x => { joint.SetRigidbody(JointBody.Anchor, (Rigidbody)x); MarkAsModified(); ConfirmModify(); };
  58. if(showOffsets)
  59. {
  60. targetOffsetField.OnChanged += x => { joint.SetPosition(JointBody.Target, x); MarkAsModified(); };
  61. targetOffsetField.OnFocusLost += ConfirmModify;
  62. targetOffsetField.OnConfirmed += ConfirmModify;
  63. anchorOffsetField.OnChanged += x => { joint.SetPosition(JointBody.Anchor, x); MarkAsModified(); };
  64. anchorOffsetField.OnFocusLost += ConfirmModify;
  65. anchorOffsetField.OnConfirmed += ConfirmModify;
  66. }
  67. breakForceField.OnChanged += x => { joint.BreakForce = x; MarkAsModified(); };
  68. breakForceField.OnFocusLost += ConfirmModify;
  69. breakForceField.OnConfirmed += ConfirmModify;
  70. breakTorqueField.OnChanged += x => { joint.BreakTorque = x; MarkAsModified(); };
  71. breakTorqueField.OnFocusLost += ConfirmModify;
  72. breakTorqueField.OnConfirmed += ConfirmModify;
  73. collisionField.OnChanged += x => { joint.EnableCollision = x; MarkAsModified(); ConfirmModify(); };
  74. Layout.AddElement(targetField);
  75. if(showOffsets)
  76. Layout.AddElement(targetOffsetField);
  77. Layout.AddElement(anchorField);
  78. if(showOffsets)
  79. Layout.AddElement(anchorOffsetField);
  80. Layout.AddElement(breakForceField);
  81. Layout.AddElement(breakTorqueField);
  82. Layout.AddElement(collisionField);
  83. }
  84. /// <summary>
  85. /// Marks the contents of the inspector as modified.
  86. /// </summary>
  87. protected void MarkAsModified()
  88. {
  89. modifyState |= InspectableState.ModifyInProgress;
  90. }
  91. /// <summary>
  92. /// Confirms any queued modifications.
  93. /// </summary>
  94. protected void ConfirmModify()
  95. {
  96. if (modifyState.HasFlag(InspectableState.ModifyInProgress))
  97. modifyState |= InspectableState.Modified;
  98. }
  99. }
  100. /** @} */
  101. }