HingeJointInspector.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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="HingeJoint"/> component.
  11. /// </summary>
  12. [CustomInspector(typeof(HingeJoint))]
  13. internal class HingeJointInspector : JointInspector
  14. {
  15. private GUIToggleField enableLimitField = new GUIToggleField(new LocEdString("Enable limit"));
  16. private LimitAngularRangeGUI limitGUI;
  17. private GUIToggleField enableDriveField = new GUIToggleField(new LocEdString("Enable drive"));
  18. private GUIFloatField speedField = new GUIFloatField(new LocEdString("Speed"));
  19. private GUIFloatField forceLimitField = new GUIFloatField(new LocEdString("Force limit"));
  20. private GUIFloatField gearRatioField = new GUIFloatField(new LocEdString("Gear ratio"));
  21. private GUIToggleField freeSpinField = new GUIToggleField(new LocEdString("Free spin"));
  22. private GUILayoutX limitLayout;
  23. private GUILayoutX driveLayout;
  24. /// <inheritdoc/>
  25. protected internal override void Initialize()
  26. {
  27. HingeJoint joint = InspectedObject as HingeJoint;
  28. if (joint != null)
  29. BuildGUI(joint);
  30. }
  31. /// <inheritdoc/>
  32. protected internal override InspectableState Refresh()
  33. {
  34. HingeJoint joint = InspectedObject as HingeJoint;
  35. if (joint == null)
  36. return InspectableState.NotModified;
  37. Refresh(joint);
  38. InspectableState oldState = modifyState;
  39. if (modifyState.HasFlag(InspectableState.Modified))
  40. modifyState = InspectableState.NotModified;
  41. return oldState;
  42. }
  43. /// <summary>
  44. /// Creates GUI elements for fields specific to the hinge joint.
  45. /// </summary>
  46. protected void BuildGUI(HingeJoint joint)
  47. {
  48. enableLimitField.OnChanged += x =>
  49. {
  50. joint.SetFlag(HingeJointFlag.Limit, x);
  51. MarkAsModified();
  52. ConfirmModify();
  53. ToggleLimitFields(x);
  54. };
  55. enableDriveField.OnChanged += x =>
  56. {
  57. joint.SetFlag(HingeJointFlag.Drive, x);
  58. MarkAsModified();
  59. ConfirmModify();
  60. ToggleDriveFields(x);
  61. };
  62. speedField.OnChanged += x =>
  63. {
  64. HingeJointDrive driveData = joint.Drive;
  65. driveData.speed = x;
  66. joint.Drive = driveData;
  67. MarkAsModified();
  68. };
  69. speedField.OnFocusLost += ConfirmModify;
  70. speedField.OnConfirmed += ConfirmModify;
  71. forceLimitField.OnChanged += x =>
  72. {
  73. HingeJointDrive driveData = joint.Drive;
  74. driveData.forceLimit = x;
  75. joint.Drive = driveData;
  76. MarkAsModified();
  77. };
  78. forceLimitField.OnFocusLost += ConfirmModify;
  79. forceLimitField.OnConfirmed += ConfirmModify;
  80. gearRatioField.OnChanged += x =>
  81. {
  82. HingeJointDrive driveData = joint.Drive;
  83. driveData.gearRatio = x;
  84. joint.Drive = driveData;
  85. MarkAsModified();
  86. };
  87. gearRatioField.OnFocusLost += ConfirmModify;
  88. gearRatioField.OnConfirmed += ConfirmModify;
  89. freeSpinField.OnChanged += x =>
  90. {
  91. HingeJointDrive driveData = joint.Drive;
  92. driveData.freeSpin = x;
  93. joint.Drive = driveData;
  94. MarkAsModified();
  95. ConfirmModify();
  96. };
  97. Layout.AddElement(enableLimitField);
  98. limitLayout = Layout.AddLayoutX();
  99. {
  100. limitLayout.AddSpace(10);
  101. GUILayoutY limitContentsLayout = limitLayout.AddLayoutY();
  102. limitGUI = new LimitAngularRangeGUI(joint.Limit, limitContentsLayout, Persistent);
  103. limitGUI.OnChanged += (x, y) =>
  104. {
  105. joint.Limit = x;
  106. joint.Limit.SetBase(y);
  107. MarkAsModified();
  108. };
  109. limitGUI.OnConfirmed += ConfirmModify;
  110. }
  111. Layout.AddElement(enableDriveField);
  112. driveLayout = Layout.AddLayoutX();
  113. {
  114. driveLayout.AddSpace(10);
  115. GUILayoutY driveContentsLayout = driveLayout.AddLayoutY();
  116. driveContentsLayout.AddElement(speedField);
  117. driveContentsLayout.AddElement(forceLimitField);
  118. driveContentsLayout.AddElement(gearRatioField);
  119. driveContentsLayout.AddElement(freeSpinField);
  120. }
  121. ToggleLimitFields(joint.HasFlag(HingeJointFlag.Limit));
  122. ToggleDriveFields(joint.HasFlag(HingeJointFlag.Drive));
  123. base.BuildGUI(joint, true);
  124. }
  125. /// <summary>
  126. /// Updates all GUI elements from current values in the joint.
  127. /// </summary>
  128. /// <param name="joint">Joint to update the GUI from.</param>
  129. protected void Refresh(HingeJoint joint)
  130. {
  131. bool enableLimit = joint.HasFlag(HingeJointFlag.Limit);
  132. if (enableLimitField.Value != enableLimit)
  133. {
  134. enableLimitField.Value = enableLimit;
  135. ToggleLimitFields(enableLimit);
  136. }
  137. limitGUI.Limit = joint.Limit;
  138. bool enableDrive = joint.HasFlag(HingeJointFlag.Drive);
  139. if (enableDriveField.Value != enableDrive)
  140. {
  141. enableDriveField.Value = enableDrive;
  142. ToggleDriveFields(enableDrive);
  143. }
  144. speedField.Value = joint.Drive.speed;
  145. forceLimitField.Value = joint.Drive.forceLimit;
  146. gearRatioField.Value = joint.Drive.gearRatio;
  147. freeSpinField.Value = joint.Drive.freeSpin;
  148. base.Refresh(joint);
  149. }
  150. /// <summary>
  151. /// Hides or shows limit property GUI elements.
  152. /// </summary>
  153. /// <param name="enable">True to show, false to hide.</param>
  154. private void ToggleLimitFields(bool enable)
  155. {
  156. limitLayout.Active = enable;
  157. }
  158. /// <summary>
  159. /// Hides or shows drive property GUI elements.
  160. /// </summary>
  161. /// <param name="enable">True to show, false to hide.</param>
  162. private void ToggleDriveFields(bool enable)
  163. {
  164. driveLayout.Active = enable;
  165. }
  166. }
  167. /** @} */
  168. }