InspectableEuler.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2019 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using bs;
  4. namespace bs.Editor
  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="context">Context shared by all inspectable fields created by the same parent.</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(InspectableContext context, string title, string path, int depth, InspectableFieldLayout layout,
  28. SerializableProperty property)
  29. : base(context, 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.OnComponentChanged += OnFieldValueChanged;
  40. guiField.OnConfirm += x =>
  41. {
  42. OnFieldValueConfirm();
  43. StartUndo(x.ToString());
  44. };
  45. guiField.OnComponentFocusChanged += (focus, comp) =>
  46. {
  47. if (focus)
  48. StartUndo(comp.ToString());
  49. else
  50. OnFieldValueConfirm();
  51. };
  52. layout.AddElement(layoutIndex, guiField);
  53. }
  54. }
  55. /// <inheritdoc/>
  56. public override InspectableState Refresh(int layoutIndex)
  57. {
  58. if (guiField != null && !guiField.HasInputFocus)
  59. {
  60. Quaternion quaternion = property.GetValue<Quaternion>();
  61. if (quaternion != quatValue)
  62. {
  63. guiField.Value = quaternion.ToEuler();
  64. quatValue = quaternion;
  65. }
  66. }
  67. InspectableState oldState = state;
  68. if (state.HasFlag(InspectableState.Modified))
  69. state = InspectableState.NotModified;
  70. return oldState;
  71. }
  72. /// <inheritdoc />
  73. public override void SetHasFocus(string subFieldName = null)
  74. {
  75. if (subFieldName == "X")
  76. guiField.SetInputFocus(VectorComponent.X, true);
  77. else if (subFieldName == "Y")
  78. guiField.SetInputFocus(VectorComponent.Y, true);
  79. else if (subFieldName == "Z")
  80. guiField.SetInputFocus(VectorComponent.Z, true);
  81. else
  82. guiField.SetInputFocus(VectorComponent.X, true);
  83. }
  84. /// <summary>
  85. /// Triggered when the user changes the field value of a single component.
  86. /// </summary>
  87. /// <param name="newValue">New value of a single component in the 3D vector field.</param>
  88. /// <param name="component">Component that was changed.</param>
  89. private void OnFieldValueChanged(float newValue, VectorComponent component)
  90. {
  91. property.SetValue(guiField.Value);
  92. state |= InspectableState.ModifyInProgress;
  93. }
  94. /// <summary>
  95. /// Triggered when the user confirms input in the 3D vector field.
  96. /// </summary>
  97. private void OnFieldValueConfirm()
  98. {
  99. if (state.HasFlag(InspectableState.ModifyInProgress))
  100. state |= InspectableState.Modified;
  101. EndUndo();
  102. }
  103. }
  104. /** @} */
  105. }