InspectableVector3.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 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 3D vector.
  11. /// </summary>
  12. public class InspectableVector3 : InspectableField
  13. {
  14. private GUIVector3Field guiField;
  15. private InspectableState state;
  16. /// <summary>
  17. /// Creates a new inspectable 3D vector 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 InspectableVector3(InspectableContext context, string title, string path, int depth, InspectableFieldLayout layout,
  27. SerializableProperty property)
  28. : base(context, title, path, SerializableProperty.FieldType.Vector3, depth, layout, property)
  29. {
  30. }
  31. /// <inheritoc/>
  32. protected internal override void Initialize(int layoutIndex)
  33. {
  34. if (property.Type == SerializableProperty.FieldType.Vector3)
  35. {
  36. guiField = new GUIVector3Field(new GUIContent(title));
  37. guiField.OnComponentChanged += OnFieldValueChanged;
  38. guiField.OnConfirm += x =>
  39. {
  40. OnFieldValueConfirm();
  41. StartUndo(x.ToString());
  42. };
  43. guiField.OnComponentFocusChanged += (focus, comp) =>
  44. {
  45. if(focus)
  46. StartUndo(comp.ToString());
  47. else
  48. OnFieldValueConfirm();
  49. };
  50. layout.AddElement(layoutIndex, guiField);
  51. }
  52. }
  53. /// <inheritdoc/>
  54. public override InspectableState Refresh(int layoutIndex)
  55. {
  56. if (guiField != null && !guiField.HasInputFocus)
  57. guiField.Value = property.GetValue<Vector3>();
  58. InspectableState oldState = state;
  59. if (state.HasFlag(InspectableState.Modified))
  60. state = InspectableState.NotModified;
  61. return oldState;
  62. }
  63. /// <inheritdoc/>
  64. public override void SetHasFocus(string subFieldName = null)
  65. {
  66. if(subFieldName == "X")
  67. guiField.SetInputFocus(VectorComponent.X, true);
  68. else if(subFieldName == "Y")
  69. guiField.SetInputFocus(VectorComponent.Y, true);
  70. else if(subFieldName == "Z")
  71. guiField.SetInputFocus(VectorComponent.Z, true);
  72. else
  73. guiField.SetInputFocus(VectorComponent.X, true);
  74. }
  75. /// <summary>
  76. /// Triggered when the user changes the field value of a single component.
  77. /// </summary>
  78. /// <param name="newValue">New value of a single component in the 3D vector field.</param>
  79. /// <param name="component">Component that was changed.</param>
  80. private void OnFieldValueChanged(float newValue, VectorComponent component)
  81. {
  82. property.SetValue(guiField.Value);
  83. state |= InspectableState.ModifyInProgress;
  84. }
  85. /// <summary>
  86. /// Triggered when the user confirms input in the 3D vector field.
  87. /// </summary>
  88. private void OnFieldValueConfirm()
  89. {
  90. if (state.HasFlag(InspectableState.ModifyInProgress))
  91. state |= InspectableState.Modified;
  92. EndUndo();
  93. }
  94. }
  95. /** @} */
  96. }