D6JointInspector.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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.AcceptsKeyFocus = false;
  70. linearLimitFoldout.OnToggled += x =>
  71. {
  72. linearLimitLayout.Active = x;
  73. Persistent.SetBool("linearLimit_Expanded", x);
  74. };
  75. twistLimitFoldout.AcceptsKeyFocus = false;
  76. twistLimitFoldout.OnToggled += x =>
  77. {
  78. twistLimitLayout.Active = x;
  79. Persistent.SetBool("twistLimit_Expanded", x);
  80. };
  81. swingLimitFoldout.AcceptsKeyFocus = false;
  82. swingLimitFoldout.OnToggled += x =>
  83. {
  84. swingLimitLayout.Active = x;
  85. Persistent.SetBool("swingLimit_Expanded", x);
  86. };
  87. driveFoldout.AcceptsKeyFocus = false;
  88. driveFoldout.OnToggled += x =>
  89. {
  90. driveLayout.Active = x;
  91. Persistent.SetBool("drive_Expanded", x);
  92. };
  93. drivePositionField.OnChanged += x => { joint.SetDriveTransform(x, joint.DriveRotation); MarkAsModified(); };
  94. drivePositionField.OnFocusLost += ConfirmModify;
  95. drivePositionField.OnConfirmed += ConfirmModify;
  96. driveRotationField.OnChanged += x => { joint.SetDriveTransform(joint.DrivePosition, Quaternion.FromEuler(x)); MarkAsModified(); };
  97. driveRotationField.OnFocusLost += ConfirmModify;
  98. driveRotationField.OnConfirmed += ConfirmModify;
  99. driveLinVelocityField.OnChanged += x => { joint.SetDriveVelocity(x, joint.DriveAngularVelocity); MarkAsModified(); };
  100. driveLinVelocityField.OnFocusLost += ConfirmModify;
  101. driveLinVelocityField.OnConfirmed += ConfirmModify;
  102. driveAngVelocityField.OnChanged += x => { joint.SetDriveVelocity(joint.DriveLinearVelocity, x); MarkAsModified(); };
  103. driveAngVelocityField.OnFocusLost += ConfirmModify;
  104. driveAngVelocityField.OnConfirmed += ConfirmModify;
  105. for (int i = 0; i < (int) D6JointAxis.Count; i++)
  106. Layout.AddElement(motionFields[i]);
  107. Layout.AddElement(linearLimitFoldout);
  108. linearLimitLayout = Layout.AddLayoutX();
  109. {
  110. linearLimitLayout.AddSpace(10);
  111. GUILayoutY linearLimitContentsLayout = linearLimitLayout.AddLayoutY();
  112. limitLinearGUI = new LimitLinearGUI(joint.LimitLinear, linearLimitContentsLayout, Persistent);
  113. limitLinearGUI.OnChanged += (x, y) =>
  114. {
  115. joint.LimitLinear = x;
  116. joint.LimitLinear.SetBase(y);
  117. MarkAsModified();
  118. };
  119. limitLinearGUI.OnConfirmed += ConfirmModify;
  120. }
  121. Layout.AddElement(twistLimitFoldout);
  122. twistLimitLayout = Layout.AddLayoutX();
  123. {
  124. twistLimitLayout.AddSpace(10);
  125. GUILayoutY twistLimitContentsLayout = twistLimitLayout.AddLayoutY();
  126. limitTwistGUI = new LimitAngularRangeGUI(joint.LimitTwist, twistLimitContentsLayout, Persistent);
  127. limitTwistGUI.OnChanged += (x, y) =>
  128. {
  129. joint.LimitTwist = x;
  130. joint.LimitTwist.SetBase(y);
  131. MarkAsModified();
  132. };
  133. limitTwistGUI.OnConfirmed += ConfirmModify;
  134. }
  135. Layout.AddElement(swingLimitFoldout);
  136. swingLimitLayout = Layout.AddLayoutX();
  137. {
  138. swingLimitLayout.AddSpace(10);
  139. GUILayoutY swingLimitContentsLayout = swingLimitLayout.AddLayoutY();
  140. limitSwingGUI = new LimitConeRangeGUI(joint.LimitSwing, swingLimitContentsLayout, Persistent);
  141. limitSwingGUI.OnChanged += (x, y) =>
  142. {
  143. joint.LimitSwing = x;
  144. joint.LimitSwing.SetBase(y);
  145. MarkAsModified();
  146. };
  147. limitSwingGUI.OnConfirmed += ConfirmModify;
  148. }
  149. Layout.AddElement(driveFoldout);
  150. driveLayout = Layout.AddLayoutX();
  151. {
  152. driveLayout.AddSpace(10);
  153. GUILayoutY driveContentsLayout = driveLayout.AddLayoutY();
  154. for (int i = 0; i < (int) D6JointDriveType.Count; i++)
  155. {
  156. D6JointDriveType type = (D6JointDriveType)i;
  157. drivesGUI[i] = new D6JointDriveGUI(joint.GetDrive(type), driveContentsLayout);
  158. drivesGUI[i].OnChanged += x => { joint.SetDrive(type, x); MarkAsModified(); };
  159. drivesGUI[i].OnConfirmed += ConfirmModify;
  160. }
  161. driveContentsLayout.AddElement(drivePositionField);
  162. driveContentsLayout.AddElement(driveRotationField);
  163. driveContentsLayout.AddElement(driveLinVelocityField);
  164. driveContentsLayout.AddElement(driveAngVelocityField);
  165. }
  166. linearLimitLayout.Active = Persistent.GetBool("linearLimit_Expanded");
  167. twistLimitLayout.Active = Persistent.GetBool("twistLimit_Expanded");
  168. swingLimitLayout.Active = Persistent.GetBool("swingLimit_Expanded");
  169. driveLayout.Active = Persistent.GetBool("drive_Expanded");
  170. base.BuildGUI(joint, true);
  171. }
  172. /// <summary>
  173. /// Updates all GUI elements from current values in the joint.
  174. /// </summary>
  175. /// <param name="joint">Joint to update the GUI from.</param>
  176. protected void Refresh(D6Joint joint)
  177. {
  178. for (int i = 0; i < (int) D6JointAxis.Count; i++)
  179. motionFields[i].Value = (ulong)joint.GetMotion((D6JointAxis) i);
  180. limitLinearGUI.Limit = joint.LimitLinear;
  181. limitTwistGUI.Limit = joint.LimitTwist;
  182. limitSwingGUI.Limit = joint.LimitSwing;
  183. for (int i = 0; i < (int) D6JointDriveType.Count; i++)
  184. drivesGUI[i].Drive = joint.GetDrive((D6JointDriveType) i);
  185. drivePositionField.Value = joint.DrivePosition;
  186. driveRotationField.Value = joint.DriveRotation.ToEuler();
  187. driveLinVelocityField.Value = joint.DriveLinearVelocity;
  188. driveAngVelocityField.Value = joint.DriveAngularVelocity;
  189. base.Refresh(joint);
  190. }
  191. }
  192. /// <summary>
  193. /// Draws GUI elements for inspecting an <see cref="D6JointDrive"/> object.
  194. /// </summary>
  195. internal class D6JointDriveGUI
  196. {
  197. private D6JointDrive driveData;
  198. private GUIFloatField stiffnessField = new GUIFloatField(new LocEdString("Stiffness"));
  199. private GUIFloatField dampingField = new GUIFloatField(new LocEdString("Damping"));
  200. private GUIFloatField forceLimitField = new GUIFloatField(new LocEdString("Force limit"));
  201. private GUIToggleField accelerationField = new GUIToggleField(new LocEdString("Acceleration"));
  202. public Action<D6JointDrive> OnChanged;
  203. public Action OnConfirmed;
  204. /// <summary>
  205. /// Current drive properties.
  206. /// </summary>
  207. public D6JointDrive Drive
  208. {
  209. set
  210. {
  211. driveData = value;
  212. stiffnessField.Value = driveData.stiffness;
  213. dampingField.Value = driveData.damping;
  214. forceLimitField.Value = driveData.forceLimit;
  215. accelerationField.Value = driveData.acceleration;
  216. }
  217. }
  218. /// <summary>
  219. /// Constructs a new set of GUI elements for inspecting the drive object.
  220. /// </summary>
  221. /// <param name="drive">Initial values to assign to the GUI elements.</param>
  222. /// <param name="layout">Layout to append the GUI elements to.</param>
  223. public D6JointDriveGUI(D6JointDrive drive, GUILayout layout)
  224. {
  225. driveData = drive;
  226. stiffnessField.OnChanged += x => { driveData.stiffness = x; MarkAsModified(); };
  227. stiffnessField.OnFocusLost += ConfirmModify;
  228. stiffnessField.OnConfirmed += ConfirmModify;
  229. dampingField.OnChanged += x => { driveData.damping = x; MarkAsModified(); };
  230. dampingField.OnFocusLost += ConfirmModify;
  231. dampingField.OnConfirmed += ConfirmModify;
  232. forceLimitField.OnChanged += x => { driveData.forceLimit = x; MarkAsModified(); };
  233. forceLimitField.OnFocusLost += ConfirmModify;
  234. forceLimitField.OnConfirmed += ConfirmModify;
  235. accelerationField.OnChanged += x => { driveData.acceleration = x; MarkAsModified(); ConfirmModify(); };
  236. layout.AddElement(stiffnessField);
  237. layout.AddElement(dampingField);
  238. layout.AddElement(forceLimitField);
  239. layout.AddElement(accelerationField);
  240. }
  241. /// <summary>
  242. /// Marks the contents of the inspector as modified.
  243. /// </summary>
  244. private void MarkAsModified()
  245. {
  246. if (OnChanged != null)
  247. OnChanged(driveData);
  248. }
  249. /// <summary>
  250. /// Confirms any queued modifications.
  251. /// </summary>
  252. private void ConfirmModify()
  253. {
  254. if (OnConfirmed != null)
  255. OnConfirmed();
  256. }
  257. }
  258. /** @} */
  259. }