D6JointInspector.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using BansheeEngine;
  5. namespace BansheeEditor
  6. {
  7. /** @addtogroup Inspectors
  8. * @{
  9. */
  10. /// <summary>
  11. /// Renders an inspector for the <see cref="D6Joint"/> component.
  12. /// </summary>
  13. [CustomInspector(typeof(D6Joint))]
  14. internal class D6JointInspector : JointInspector
  15. {
  16. private GUIEnumField[] motionFields = new GUIEnumField[(int) D6JointAxis.Count];
  17. private GUIToggle linearLimitFoldout = new GUIToggle(new LocEdString("Linear limit"), EditorStyles.Foldout);
  18. private LimitLinearGUI limitLinearGUI;
  19. private GUIToggle twistLimitFoldout = new GUIToggle(new LocEdString("Twist limit"), EditorStyles.Foldout);
  20. private LimitAngularRangeGUI limitTwistGUI;
  21. private GUIToggle swingLimitFoldout = new GUIToggle(new LocEdString("Swing limit"), EditorStyles.Foldout);
  22. private LimitConeRangeGUI limitSwingGUI;
  23. private GUIToggle driveFoldout = new GUIToggle(new LocEdString("Drive"), EditorStyles.Foldout);
  24. private D6JointDriveGUI[] drivesGUI = new D6JointDriveGUI[(int)D6JointDriveType.Count];
  25. private GUIVector3Field drivePositionField = new GUIVector3Field(new LocEdString("Drive position"));
  26. private GUIVector3Field driveRotationField = new GUIVector3Field(new LocEdString("Drive rotation"));
  27. private GUIVector3Field driveLinVelocityField = new GUIVector3Field(new LocEdString("Drive linear velocity"));
  28. private GUIVector3Field driveAngVelocityField = new GUIVector3Field(new LocEdString("Drive angular velocity"));
  29. private GUILayoutX linearLimitLayout;
  30. private GUILayoutX twistLimitLayout;
  31. private GUILayoutX swingLimitLayout;
  32. private GUILayoutX driveLayout;
  33. /// <inheritdoc/>
  34. protected internal override void Initialize()
  35. {
  36. D6Joint joint = InspectedObject as D6Joint;
  37. if (joint != null)
  38. BuildGUI(joint);
  39. }
  40. /// <inheritdoc/>
  41. protected internal override InspectableState Refresh()
  42. {
  43. D6Joint joint = InspectedObject as D6Joint;
  44. if (joint == null)
  45. return InspectableState.NotModified;
  46. Refresh(joint);
  47. InspectableState oldState = modifyState;
  48. if (modifyState.HasFlag(InspectableState.Modified))
  49. modifyState = InspectableState.NotModified;
  50. return oldState;
  51. }
  52. /// <summary>
  53. /// Creates GUI elements for fields specific to the spherical joint.
  54. /// </summary>
  55. protected void BuildGUI(D6Joint joint)
  56. {
  57. for (int i = 0; i < (int) D6JointAxis.Count; i++)
  58. {
  59. D6JointAxis axis = (D6JointAxis) i;
  60. string entryName = Enum.GetName(typeof (D6JointAxis), axis);
  61. motionFields[i] = new GUIEnumField(typeof (D6JointMotion), new LocEdString(entryName));
  62. motionFields[i].OnSelectionChanged += x =>
  63. {
  64. joint.SetMotion(axis, (D6JointMotion)x);
  65. MarkAsModified();
  66. ConfirmModify();
  67. };
  68. }
  69. linearLimitFoldout.OnToggled += x =>
  70. {
  71. linearLimitLayout.Active = x;
  72. Persistent.SetBool("linearLimit_Expanded", x);
  73. };
  74. twistLimitFoldout.OnToggled += x =>
  75. {
  76. twistLimitLayout.Active = x;
  77. Persistent.SetBool("twistLimit_Expanded", x);
  78. };
  79. swingLimitFoldout.OnToggled += x =>
  80. {
  81. swingLimitLayout.Active = x;
  82. Persistent.SetBool("swingLimit_Expanded", x);
  83. };
  84. driveFoldout.OnToggled += x =>
  85. {
  86. driveLayout.Active = x;
  87. Persistent.SetBool("drive_Expanded", x);
  88. };
  89. drivePositionField.OnChanged += x => { joint.DrivePosition = x; MarkAsModified(); };
  90. drivePositionField.OnFocusLost += ConfirmModify;
  91. drivePositionField.OnConfirmed += ConfirmModify;
  92. driveRotationField.OnChanged += x => { joint.DriveRotation = Quaternion.FromEuler(x); MarkAsModified(); };
  93. driveRotationField.OnFocusLost += ConfirmModify;
  94. driveRotationField.OnConfirmed += ConfirmModify;
  95. driveLinVelocityField.OnChanged += x => { joint.DriveLinearVelocity = x; MarkAsModified(); };
  96. driveLinVelocityField.OnFocusLost += ConfirmModify;
  97. driveLinVelocityField.OnConfirmed += ConfirmModify;
  98. driveAngVelocityField.OnChanged += x => { joint.DriveAngularVelocity = x; MarkAsModified(); };
  99. driveAngVelocityField.OnFocusLost += ConfirmModify;
  100. driveAngVelocityField.OnConfirmed += ConfirmModify;
  101. for (int i = 0; i < (int) D6JointAxis.Count; i++)
  102. Layout.AddElement(motionFields[i]);
  103. Layout.AddElement(linearLimitFoldout);
  104. linearLimitLayout = Layout.AddLayoutX();
  105. {
  106. linearLimitLayout.AddSpace(10);
  107. GUILayoutY linearLimitContentsLayout = linearLimitLayout.AddLayoutY();
  108. limitLinearGUI = new LimitLinearGUI(joint.LimitLinear, linearLimitContentsLayout, Persistent);
  109. limitLinearGUI.OnChanged += (x, y) =>
  110. {
  111. joint.LimitLinear = new LimitLinear(x, y);
  112. MarkAsModified();
  113. };
  114. limitLinearGUI.OnConfirmed += ConfirmModify;
  115. }
  116. Layout.AddElement(twistLimitFoldout);
  117. twistLimitLayout = Layout.AddLayoutX();
  118. {
  119. twistLimitLayout.AddSpace(10);
  120. GUILayoutY twistLimitContentsLayout = twistLimitLayout.AddLayoutY();
  121. limitTwistGUI = new LimitAngularRangeGUI(joint.LimitTwist, twistLimitContentsLayout, Persistent);
  122. limitTwistGUI.OnChanged += (x, y) =>
  123. {
  124. joint.LimitTwist = new LimitAngularRange(x, y);
  125. MarkAsModified();
  126. };
  127. limitTwistGUI.OnConfirmed += ConfirmModify;
  128. }
  129. Layout.AddElement(swingLimitFoldout);
  130. swingLimitLayout = Layout.AddLayoutX();
  131. {
  132. swingLimitLayout.AddSpace(10);
  133. GUILayoutY swingLimitContentsLayout = swingLimitLayout.AddLayoutY();
  134. limitSwingGUI = new LimitConeRangeGUI(joint.LimitSwing, swingLimitContentsLayout, Persistent);
  135. limitSwingGUI.OnChanged += (x, y) =>
  136. {
  137. joint.LimitSwing = new LimitConeRange(x, y);
  138. MarkAsModified();
  139. };
  140. limitSwingGUI.OnConfirmed += ConfirmModify;
  141. }
  142. Layout.AddElement(driveFoldout);
  143. driveLayout = Layout.AddLayoutX();
  144. {
  145. driveLayout.AddSpace(10);
  146. GUILayoutY driveContentsLayout = driveLayout.AddLayoutY();
  147. for (int i = 0; i < (int) D6JointDriveType.Count; i++)
  148. {
  149. D6JointDriveType type = (D6JointDriveType)i;
  150. drivesGUI[i] = new D6JointDriveGUI(joint.GetDrive(type), driveContentsLayout);
  151. drivesGUI[i].OnChanged += x => { joint.SetDrive(type, new D6JointDrive(x)); MarkAsModified(); };
  152. drivesGUI[i].OnConfirmed += ConfirmModify;
  153. }
  154. driveContentsLayout.AddElement(drivePositionField);
  155. driveContentsLayout.AddElement(driveRotationField);
  156. driveContentsLayout.AddElement(driveLinVelocityField);
  157. driveContentsLayout.AddElement(driveAngVelocityField);
  158. }
  159. linearLimitLayout.Active = Persistent.GetBool("linearLimit_Expanded");
  160. twistLimitLayout.Active = Persistent.GetBool("twistLimit_Expanded");
  161. swingLimitLayout.Active = Persistent.GetBool("swingLimit_Expanded");
  162. driveLayout.Active = Persistent.GetBool("drive_Expanded");
  163. base.BuildGUI(joint, true);
  164. }
  165. /// <summary>
  166. /// Updates all GUI elements from current values in the joint.
  167. /// </summary>
  168. /// <param name="joint">Joint to update the GUI from.</param>
  169. protected void Refresh(D6Joint joint)
  170. {
  171. for (int i = 0; i < (int) D6JointAxis.Count; i++)
  172. motionFields[i].Value = (ulong)joint.GetMotion((D6JointAxis) i);
  173. limitLinearGUI.Limit = joint.LimitLinear;
  174. limitTwistGUI.Limit = joint.LimitTwist;
  175. limitSwingGUI.Limit = joint.LimitSwing;
  176. for (int i = 0; i < (int) D6JointDriveType.Count; i++)
  177. drivesGUI[i].Drive = joint.GetDrive((D6JointDriveType) i);
  178. drivePositionField.Value = joint.DrivePosition;
  179. driveRotationField.Value = joint.DriveRotation.ToEuler();
  180. driveLinVelocityField.Value = joint.DriveLinearVelocity;
  181. driveAngVelocityField.Value = joint.DriveAngularVelocity;
  182. base.Refresh(joint);
  183. }
  184. }
  185. /// <summary>
  186. /// Draws GUI elements for inspecting an <see cref="D6JointDrive"/> object.
  187. /// </summary>
  188. internal class D6JointDriveGUI
  189. {
  190. private D6JointDriveData driveData;
  191. private GUIFloatField stiffnessField = new GUIFloatField(new LocEdString("Stiffness"));
  192. private GUIFloatField dampingField = new GUIFloatField(new LocEdString("Damping"));
  193. private GUIFloatField forceLimitField = new GUIFloatField(new LocEdString("Force limit"));
  194. private GUIToggleField accelerationField = new GUIToggleField(new LocEdString("Acceleration"));
  195. public Action<D6JointDriveData> OnChanged;
  196. public Action OnConfirmed;
  197. /// <summary>
  198. /// Current drive properties.
  199. /// </summary>
  200. public D6JointDrive Drive
  201. {
  202. set
  203. {
  204. driveData = value.Data;
  205. stiffnessField.Value = driveData.stiffness;
  206. dampingField.Value = driveData.damping;
  207. forceLimitField.Value = driveData.forceLimit;
  208. accelerationField.Value = driveData.acceleration;
  209. }
  210. }
  211. /// <summary>
  212. /// Constructs a new set of GUI elements for inspecting the drive object.
  213. /// </summary>
  214. /// <param name="drive">Initial values to assign to the GUI elements.</param>
  215. /// <param name="layout">Layout to append the GUI elements to.</param>
  216. public D6JointDriveGUI(D6JointDrive drive, GUILayout layout)
  217. {
  218. driveData = drive.Data;
  219. stiffnessField.OnChanged += x => { driveData.stiffness = x; MarkAsModified(); };
  220. stiffnessField.OnFocusLost += ConfirmModify;
  221. stiffnessField.OnConfirmed += ConfirmModify;
  222. dampingField.OnChanged += x => { driveData.damping = x; MarkAsModified(); };
  223. dampingField.OnFocusLost += ConfirmModify;
  224. dampingField.OnConfirmed += ConfirmModify;
  225. forceLimitField.OnChanged += x => { driveData.forceLimit = x; MarkAsModified(); };
  226. forceLimitField.OnFocusLost += ConfirmModify;
  227. forceLimitField.OnConfirmed += ConfirmModify;
  228. accelerationField.OnChanged += x => { driveData.acceleration = x; MarkAsModified(); ConfirmModify(); };
  229. layout.AddElement(stiffnessField);
  230. layout.AddElement(dampingField);
  231. layout.AddElement(forceLimitField);
  232. layout.AddElement(accelerationField);
  233. }
  234. /// <summary>
  235. /// Marks the contents of the inspector as modified.
  236. /// </summary>
  237. private void MarkAsModified()
  238. {
  239. if (OnChanged != null)
  240. OnChanged(driveData);
  241. }
  242. /// <summary>
  243. /// Confirms any queued modifications.
  244. /// </summary>
  245. private void ConfirmModify()
  246. {
  247. if (OnConfirmed != null)
  248. OnConfirmed();
  249. }
  250. }
  251. /** @} */
  252. }