InspectableEuler.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2019 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using BansheeEngine;
  4. namespace BansheeEditor
  5. {
  6. /** @addtogroup Inspector
  7. * @{
  8. */
  9. /// <summary>
  10. /// Displays GUI for a serializable property containing a quaternion, displayed as euler angles.
  11. /// </summary>
  12. public class InspectableEuler : InspectableField
  13. {
  14. private GUIVector3Field guiField;
  15. private InspectableState state;
  16. private Quaternion quatValue = Quaternion.Identity;
  17. /// <summary>
  18. /// Creates a new inspectable euler angle GUI for the specified property.
  19. /// </summary>
  20. /// <param name="parent">Parent Inspector this field belongs to.</param>
  21. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  22. /// <param name="path">Full path to this property (includes name of this property and all parent properties).</param>
  23. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  24. /// contain other fields, in which case you should increase this value by one.</param>
  25. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  26. /// <param name="property">Serializable property referencing the field whose contents to display.</param>
  27. public InspectableEuler(Inspector parent, string title, string path, int depth, InspectableFieldLayout layout,
  28. SerializableProperty property)
  29. : base(parent, title, path, SerializableProperty.FieldType.Quaternion, depth, layout, property)
  30. {
  31. }
  32. /// <inheritoc/>
  33. protected internal override void Initialize(int layoutIndex)
  34. {
  35. if (property.Type == SerializableProperty.FieldType.Quaternion)
  36. {
  37. guiField = new GUIVector3Field(new GUIContent(title));
  38. guiField.Value = quatValue.ToEuler();
  39. guiField.OnChanged += OnFieldValueChanged;
  40. guiField.OnConfirmed += OnFieldValueConfirm;
  41. guiField.OnFocusLost += OnFieldValueConfirm;
  42. layout.AddElement(layoutIndex, guiField);
  43. }
  44. }
  45. /// <inheritdoc/>
  46. public override InspectableState Refresh(int layoutIndex)
  47. {
  48. if (guiField != null && !guiField.HasInputFocus)
  49. {
  50. Quaternion quaternion = property.GetValue<Quaternion>();
  51. if (quaternion != quatValue)
  52. {
  53. guiField.Value = quaternion.ToEuler();
  54. quatValue = quaternion;
  55. }
  56. }
  57. InspectableState oldState = state;
  58. if (state.HasFlag(InspectableState.Modified))
  59. state = InspectableState.NotModified;
  60. return oldState;
  61. }
  62. /// <summary>
  63. /// Triggered when the user changes the field value.
  64. /// </summary>
  65. /// <param name="newValue">New value of the 3D vector field.</param>
  66. private void OnFieldValueChanged(Vector3 newValue)
  67. {
  68. quatValue = Quaternion.FromEuler(newValue);
  69. property.SetValue(quatValue);
  70. state |= InspectableState.ModifyInProgress;
  71. }
  72. /// <summary>
  73. /// Triggered when the user confirms input in the 3D vector field.
  74. /// </summary>
  75. private void OnFieldValueConfirm()
  76. {
  77. if (state.HasFlag(InspectableState.ModifyInProgress))
  78. state |= InspectableState.Modified;
  79. }
  80. }
  81. /** @} */
  82. }