LimitInspectors.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. /// Draws GUI elements for inspecting an <see cref="Spring"/> object.
  12. /// </summary>
  13. internal class SpringGUI
  14. {
  15. private Spring spring;
  16. private GUIFloatField stiffnessField = new GUIFloatField(new LocEdString("Stiffness"));
  17. private GUIFloatField dampingField = new GUIFloatField(new LocEdString("Damping"));
  18. public Action<Spring> OnChanged;
  19. public Action OnConfirmed;
  20. /// <summary>
  21. /// Current value of the spring object.
  22. /// </summary>
  23. public Spring Spring
  24. {
  25. get { return spring; }
  26. set
  27. {
  28. spring = value;
  29. stiffnessField.Value = value.stiffness;
  30. dampingField.Value = value.damping;
  31. }
  32. }
  33. /// <summary>
  34. /// Constructs a new set of GUI elements for inspecting the spring object.
  35. /// </summary>
  36. /// <param name="spring">Initial values to assign to the GUI elements.</param>
  37. /// <param name="layout">Layout to append the GUI elements to.</param>
  38. public SpringGUI(Spring spring, GUILayout layout)
  39. {
  40. this.spring = spring;
  41. stiffnessField.OnChanged += x => { this.spring.stiffness = x; MarkAsModified(); };
  42. stiffnessField.OnFocusLost += ConfirmModify;
  43. stiffnessField.OnConfirmed += ConfirmModify;
  44. dampingField.OnChanged += x => { this.spring.damping = x; MarkAsModified(); };
  45. dampingField.OnFocusLost += ConfirmModify;
  46. dampingField.OnConfirmed += ConfirmModify;
  47. layout.AddElement(stiffnessField);
  48. layout.AddElement(dampingField);
  49. }
  50. /// <summary>
  51. /// Marks the contents of the inspector as modified.
  52. /// </summary>
  53. private void MarkAsModified()
  54. {
  55. if (OnChanged != null)
  56. OnChanged(spring);
  57. }
  58. /// <summary>
  59. /// Confirms any queued modifications.
  60. /// </summary>
  61. private void ConfirmModify()
  62. {
  63. if (OnConfirmed != null)
  64. OnConfirmed();
  65. }
  66. }
  67. /// <summary>
  68. /// Draws GUI elements for inspecting an <see cref="LimitCommon"/> object.
  69. /// </summary>
  70. internal class LimitCommonGUI
  71. {
  72. private LimitCommon limitData;
  73. private SerializableProperties properties;
  74. private string prefix;
  75. private GUIToggle hardFoldout = new GUIToggle(new LocEdString("Hard"), EditorStyles.Foldout);
  76. private GUIFloatField contactDistanceField = new GUIFloatField(new LocEdString("Contact distance"));
  77. private GUIToggle softFoldout = new GUIToggle(new LocEdString("Soft"), EditorStyles.Foldout);
  78. private GUISliderField restitutionField = new GUISliderField(0, 1, new LocEdString("Restitution"));
  79. private GUIToggle springFoldout = new GUIToggle(new LocEdString("Spring"), EditorStyles.Foldout);
  80. private SpringGUI springGUI;
  81. private GUILayoutX hardLimitLayout;
  82. private GUILayoutX softLimitLayout;
  83. private GUILayoutX springLayout;
  84. public Action<LimitCommon> OnChanged;
  85. public Action OnConfirmed;
  86. /// <summary>
  87. /// Current limit properties.
  88. /// </summary>
  89. public LimitCommon LimitData
  90. {
  91. get { return limitData; }
  92. set
  93. {
  94. limitData = value;
  95. contactDistanceField.Value = value.contactDist;
  96. restitutionField.Value = value.restitution;
  97. springGUI.Spring = value.spring;
  98. }
  99. }
  100. /// <summary>
  101. /// Constructs a new set of GUI elements for inspecting the limit object.
  102. /// </summary>
  103. /// <param name="prefix">Prefix that identifies the exact type of the limit type.</param>
  104. /// <param name="limitData">Initial values to assign to the GUI elements.</param>
  105. /// <param name="layout">Layout to append the GUI elements to.</param>
  106. /// <param name="properties">A set of properties that are persisted by the parent inspector. Used for saving state.
  107. /// </param>
  108. public LimitCommonGUI(string prefix, LimitCommon limitData, GUILayout layout, SerializableProperties properties)
  109. {
  110. this.limitData = limitData;
  111. this.properties = properties;
  112. this.prefix = prefix;
  113. hardFoldout.AcceptsKeyFocus = false;
  114. hardFoldout.OnToggled += x =>
  115. {
  116. properties.SetBool(prefix + "_hardLimit_Expanded", x);
  117. ToggleLimitFields();
  118. };
  119. contactDistanceField.OnChanged += x => { this.limitData.contactDist = x; MarkAsModified(); };
  120. contactDistanceField.OnFocusLost += ConfirmModify;
  121. contactDistanceField.OnConfirmed += ConfirmModify;
  122. softFoldout.AcceptsKeyFocus = false;
  123. softFoldout.OnToggled += x =>
  124. {
  125. properties.SetBool(prefix + "_softLimit_Expanded", x);
  126. ToggleLimitFields();
  127. };
  128. restitutionField.OnChanged += x => { this.limitData.restitution = x; MarkAsModified(); };
  129. restitutionField.OnFocusLost += ConfirmModify;
  130. springFoldout.AcceptsKeyFocus = false;
  131. springFoldout.OnToggled += x =>
  132. {
  133. properties.SetBool(prefix + "_spring_Expanded", x);
  134. ToggleLimitFields();
  135. };
  136. hardLimitLayout = layout.AddLayoutX();
  137. {
  138. hardLimitLayout.AddSpace(10);
  139. GUILayoutY hardLimitContentsLayout = hardLimitLayout.AddLayoutY();
  140. hardLimitContentsLayout.AddElement(contactDistanceField);
  141. }
  142. softLimitLayout = layout.AddLayoutX();
  143. layout.AddElement(softFoldout);
  144. {
  145. softLimitLayout.AddSpace(10);
  146. GUILayoutY softLimitContentsLayout = softLimitLayout.AddLayoutY();
  147. softLimitContentsLayout.AddElement(restitutionField);
  148. softLimitContentsLayout.AddElement(springFoldout);
  149. springLayout = softLimitContentsLayout.AddLayoutX();
  150. {
  151. springLayout.AddSpace(10);
  152. GUILayoutY springContentsLayout = springLayout.AddLayoutY();
  153. springGUI = new SpringGUI(limitData.spring, springContentsLayout);
  154. springGUI.OnChanged += x => { this.limitData.spring = x; MarkAsModified(); };
  155. springGUI.OnConfirmed += ConfirmModify;
  156. }
  157. }
  158. }
  159. /// <summary>
  160. /// Marks the contents of the inspector as modified.
  161. /// </summary>
  162. private void MarkAsModified()
  163. {
  164. if (OnChanged != null)
  165. OnChanged(limitData);
  166. }
  167. /// <summary>
  168. /// Confirms any queued modifications.
  169. /// </summary>
  170. private void ConfirmModify()
  171. {
  172. if (OnConfirmed != null)
  173. OnConfirmed();
  174. }
  175. /// <summary>
  176. /// Hides or shows limit property GUI elements depending on set properties.
  177. /// </summary>
  178. private void ToggleLimitFields()
  179. {
  180. hardLimitLayout.Active = properties.GetBool(prefix + "_hardLimit_Expanded");
  181. softLimitLayout.Active = properties.GetBool(prefix + "_softLimit_Expanded");
  182. springLayout.Active = properties.GetBool(prefix + "_spring_Expanded");
  183. }
  184. }
  185. /// <summary>
  186. /// Draws GUI elements for inspecting an <see cref="LimitLinear"/> object.
  187. /// </summary>
  188. internal class LimitLinearGUI
  189. {
  190. private LimitLinear limitData;
  191. private GUIFloatField limitExtentField = new GUIFloatField(new LocEdString("Extent"));
  192. private LimitCommonGUI limitCommonGUI;
  193. public Action<LimitLinear, LimitCommon> OnChanged;
  194. public Action OnConfirmed;
  195. /// <summary>
  196. /// Current limit properties.
  197. /// </summary>
  198. public LimitLinear Limit
  199. {
  200. set
  201. {
  202. limitData = value;
  203. limitExtentField.Value = limitData.extent;
  204. limitCommonGUI.LimitData = value.GetBase();
  205. }
  206. }
  207. /// <summary>
  208. /// Constructs a new set of GUI elements for inspecting the limit object.
  209. /// </summary>
  210. /// <param name="limit">Initial values to assign to the GUI elements.</param>
  211. /// <param name="layout">Layout to append the GUI elements to.</param>
  212. /// <param name="properties">A set of properties that are persisted by the parent inspector. Used for saving state.
  213. /// </param>
  214. public LimitLinearGUI(LimitLinear limit, GUILayout layout, SerializableProperties properties)
  215. {
  216. limitData = limit;
  217. limitExtentField.OnChanged += x => { limitData.extent = x; MarkAsModified(); };
  218. limitExtentField.OnFocusLost += ConfirmModify;
  219. layout.AddElement(limitExtentField);
  220. limitCommonGUI = new LimitCommonGUI("linear", limit.GetBase(), layout, properties);
  221. limitCommonGUI.OnChanged += x => MarkAsModified();
  222. limitCommonGUI.OnConfirmed += ConfirmModify;
  223. }
  224. /// <summary>
  225. /// Marks the contents of the inspector as modified.
  226. /// </summary>
  227. private void MarkAsModified()
  228. {
  229. if (OnChanged != null)
  230. OnChanged(limitData, limitCommonGUI.LimitData);
  231. }
  232. /// <summary>
  233. /// Confirms any queued modifications.
  234. /// </summary>
  235. private void ConfirmModify()
  236. {
  237. if (OnConfirmed != null)
  238. OnConfirmed();
  239. }
  240. }
  241. /// <summary>
  242. /// Draws GUI elements for inspecting an <see cref="LimitLinearRange"/> object.
  243. /// </summary>
  244. internal class LimitLinearRangeGUI
  245. {
  246. private LimitLinearRange limitData;
  247. private GUIFloatField limitLowerField = new GUIFloatField(new LocEdString("Lower"));
  248. private GUIFloatField limitUpperField = new GUIFloatField(new LocEdString("Upper"));
  249. private LimitCommonGUI limitCommonGUI;
  250. public Action<LimitLinearRange, LimitCommon> OnChanged;
  251. public Action OnConfirmed;
  252. /// <summary>
  253. /// Current limit properties.
  254. /// </summary>
  255. public LimitLinearRange Limit
  256. {
  257. set
  258. {
  259. limitData = value;
  260. limitLowerField.Value = limitData.lower;
  261. limitUpperField.Value = limitData.upper;
  262. limitCommonGUI.LimitData = value.GetBase();
  263. }
  264. }
  265. /// <summary>
  266. /// Constructs a new set of GUI elements for inspecting the limit object.
  267. /// </summary>
  268. /// <param name="limit">Initial values to assign to the GUI elements.</param>
  269. /// <param name="layout">Layout to append the GUI elements to.</param>
  270. /// <param name="properties">A set of properties that are persisted by the parent inspector. Used for saving state.
  271. /// </param>
  272. public LimitLinearRangeGUI(LimitLinearRange limit, GUILayout layout, SerializableProperties properties)
  273. {
  274. this.limitData = limit;
  275. limitLowerField.OnChanged += x => { limitData.lower = x; MarkAsModified(); };
  276. limitLowerField.OnFocusLost += ConfirmModify;
  277. limitUpperField.OnChanged += x => { limitData.upper = x; MarkAsModified(); };
  278. limitUpperField.OnFocusLost += ConfirmModify;
  279. layout.AddElement(limitLowerField);
  280. layout.AddElement(limitUpperField);
  281. limitCommonGUI = new LimitCommonGUI("linearRange", limit.GetBase(), layout, properties);
  282. limitCommonGUI.OnChanged += x => MarkAsModified();
  283. limitCommonGUI.OnConfirmed += ConfirmModify;
  284. }
  285. /// <summary>
  286. /// Marks the contents of the inspector as modified.
  287. /// </summary>
  288. private void MarkAsModified()
  289. {
  290. if (OnChanged != null)
  291. OnChanged(limitData, limitCommonGUI.LimitData);
  292. }
  293. /// <summary>
  294. /// Confirms any queued modifications.
  295. /// </summary>
  296. private void ConfirmModify()
  297. {
  298. if (OnConfirmed != null)
  299. OnConfirmed();
  300. }
  301. }
  302. /// <summary>
  303. /// Draws GUI elements for inspecting an <see cref="LimitAngularRange"/> object.
  304. /// </summary>
  305. internal class LimitAngularRangeGUI
  306. {
  307. private LimitAngularRange limitData;
  308. private GUISliderField limitLowerField = new GUISliderField(0, 359, new LocEdString("Lower"));
  309. private GUISliderField limitUpperField = new GUISliderField(0, 359, new LocEdString("Upper"));
  310. private LimitCommonGUI limitCommonGUI;
  311. public Action<LimitAngularRange, LimitCommon> OnChanged;
  312. public Action OnConfirmed;
  313. /// <summary>
  314. /// Current limit properties.
  315. /// </summary>
  316. public LimitAngularRange Limit
  317. {
  318. set
  319. {
  320. limitData = value;
  321. limitLowerField.Value = limitData.lower.Degrees;
  322. limitUpperField.Value = limitData.upper.Degrees;
  323. limitCommonGUI.LimitData = value.GetBase();
  324. }
  325. }
  326. /// <summary>
  327. /// Constructs a new set of GUI elements for inspecting the limit object.
  328. /// </summary>
  329. /// <param name="limit">Initial values to assign to the GUI elements.</param>
  330. /// <param name="layout">Layout to append the GUI elements to.</param>
  331. /// <param name="properties">A set of properties that are persisted by the parent inspector. Used for saving state.
  332. /// </param>
  333. public LimitAngularRangeGUI(LimitAngularRange limit, GUILayout layout, SerializableProperties properties)
  334. {
  335. this.limitData = limit;
  336. limitLowerField.OnChanged += x => { limitData.lower = new Degree(x); MarkAsModified(); };
  337. limitLowerField.OnFocusLost += ConfirmModify;
  338. limitUpperField.OnChanged += x => { limitData.upper = new Degree(x); MarkAsModified(); };
  339. limitUpperField.OnFocusLost += ConfirmModify;
  340. layout.AddElement(limitLowerField);
  341. layout.AddElement(limitUpperField);
  342. limitCommonGUI = new LimitCommonGUI("angularRange", limit.GetBase(), layout, properties);
  343. limitCommonGUI.OnChanged += x => MarkAsModified();
  344. limitCommonGUI.OnConfirmed += ConfirmModify;
  345. }
  346. /// <summary>
  347. /// Marks the contents of the inspector as modified.
  348. /// </summary>
  349. private void MarkAsModified()
  350. {
  351. if (OnChanged != null)
  352. OnChanged(limitData, limitCommonGUI.LimitData);
  353. }
  354. /// <summary>
  355. /// Confirms any queued modifications.
  356. /// </summary>
  357. private void ConfirmModify()
  358. {
  359. if (OnConfirmed != null)
  360. OnConfirmed();
  361. }
  362. }
  363. /// <summary>
  364. /// Draws GUI elements for inspecting an <see cref="LimitConeRange"/> object.
  365. /// </summary>
  366. internal class LimitConeRangeGUI
  367. {
  368. private LimitConeRange limitData;
  369. private GUISliderField yLimitAngleField = new GUISliderField(0, 180, new LocEdString("Y limit"));
  370. private GUISliderField zLimitAngleField = new GUISliderField(0, 180, new LocEdString("Z limit"));
  371. private LimitCommonGUI limitCommonGUI;
  372. public Action<LimitConeRange, LimitCommon> OnChanged;
  373. public Action OnConfirmed;
  374. /// <summary>
  375. /// Current limit properties.
  376. /// </summary>
  377. public LimitConeRange Limit
  378. {
  379. set
  380. {
  381. limitData = value;
  382. yLimitAngleField.Value = limitData.yLimitAngle.Degrees;
  383. zLimitAngleField.Value = limitData.zLimitAngle.Degrees;
  384. limitCommonGUI.LimitData = value.GetBase();
  385. }
  386. }
  387. /// <summary>
  388. /// Constructs a new set of GUI elements for inspecting the limit object.
  389. /// </summary>
  390. /// <param name="limit">Initial values to assign to the GUI elements.</param>
  391. /// <param name="layout">Layout to append the GUI elements to.</param>
  392. /// <param name="properties">A set of properties that are persisted by the parent inspector. Used for saving state.
  393. /// </param>
  394. public LimitConeRangeGUI(LimitConeRange limit, GUILayout layout, SerializableProperties properties)
  395. {
  396. this.limitData = limit;
  397. yLimitAngleField.OnChanged += x => { limitData.yLimitAngle = new Degree(x); MarkAsModified(); };
  398. yLimitAngleField.OnFocusLost += ConfirmModify;
  399. zLimitAngleField.OnChanged += x => { limitData.zLimitAngle = new Degree(x); MarkAsModified(); };
  400. zLimitAngleField.OnFocusLost += ConfirmModify;
  401. layout.AddElement(yLimitAngleField);
  402. layout.AddElement(zLimitAngleField);
  403. limitCommonGUI = new LimitCommonGUI("coneRange", limit.GetBase(), layout, properties);
  404. limitCommonGUI.OnChanged += x => MarkAsModified();
  405. limitCommonGUI.OnConfirmed += ConfirmModify;
  406. }
  407. /// <summary>
  408. /// Marks the contents of the inspector as modified.
  409. /// </summary>
  410. private void MarkAsModified()
  411. {
  412. if (OnChanged != null)
  413. OnChanged(limitData, limitCommonGUI.LimitData);
  414. }
  415. /// <summary>
  416. /// Confirms any queued modifications.
  417. /// </summary>
  418. private void ConfirmModify()
  419. {
  420. if (OnConfirmed != null)
  421. OnConfirmed();
  422. }
  423. }
  424. /** @} */
  425. }