InspectableQuaternion.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2018 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.
  11. /// </summary>
  12. public class InspectableQuaternion : InspectableField
  13. {
  14. private GUIVector4Field guiField;
  15. private InspectableState state;
  16. /// <summary>
  17. /// Creates a new inspectable quaternion GUI for the specified property.
  18. /// </summary>
  19. /// <param name="context">Context shared by all inspectable fields created by the same parent.</param>
  20. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  21. /// <param name="path">Full path to this property (includes name of this property and all parent properties).</param>
  22. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  23. /// contain other fields, in which case you should increase this value by one.</param>
  24. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  25. /// <param name="property">Serializable property referencing the field whose contents to display.</param>
  26. public InspectableQuaternion(InspectableContext context, string title, string path, int depth, InspectableFieldLayout layout,
  27. SerializableProperty property)
  28. : base(context, title, path, SerializableProperty.FieldType.Quaternion, depth, layout, property)
  29. {
  30. }
  31. /// <inheritoc/>
  32. protected internal override void Initialize(int layoutIndex)
  33. {
  34. if (property.Type == SerializableProperty.FieldType.Quaternion)
  35. {
  36. guiField = new GUIVector4Field(new GUIContent(title));
  37. guiField.OnValueChanged += OnFieldValueChanged;
  38. guiField.OnConfirm += x => OnFieldValueConfirm();
  39. guiField.OnFocusLost += OnFieldValueConfirm;
  40. guiField.OnFocusGained += StartUndo;
  41. layout.AddElement(layoutIndex, guiField);
  42. }
  43. }
  44. /// <inheritdoc/>
  45. public override InspectableState Refresh(int layoutIndex)
  46. {
  47. if (guiField != null && !guiField.HasInputFocus)
  48. {
  49. Quaternion quaternion = property.GetValue<Quaternion>();
  50. guiField.Value = new Vector4(quaternion.x, quaternion.y, quaternion.z, quaternion.w);
  51. }
  52. InspectableState oldState = state;
  53. if (state.HasFlag(InspectableState.Modified))
  54. state = InspectableState.NotModified;
  55. return oldState;
  56. }
  57. /// <inheritdoc />
  58. public override void SetHasFocus(string subFieldName = null)
  59. {
  60. guiField.Focus = true;
  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(Vector4 newValue)
  67. {
  68. StartUndo();
  69. Quaternion quaternion = new Quaternion(newValue.x, newValue.y, newValue.y, newValue.w);
  70. property.SetValue(quaternion);
  71. state |= InspectableState.ModifyInProgress;
  72. EndUndo();
  73. }
  74. /// <summary>
  75. /// Triggered when the user confirms input in the 3D vector field.
  76. /// </summary>
  77. private void OnFieldValueConfirm()
  78. {
  79. if (state.HasFlag(InspectableState.ModifyInProgress))
  80. state |= InspectableState.Modified;
  81. }
  82. }
  83. /** @} */
  84. }