InspectableVector3Distribution.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 3D vector distribution. GUI elements will switch between
  11. /// vector and curve input depending on the distribution type.
  12. /// </summary>
  13. public class InspectableVector3Distribution : InspectableField
  14. {
  15. private GUIVector3DistributionField guiDistributionField;
  16. private InspectableState state;
  17. /// <summary>
  18. /// Creates a new inspectable 3D vector distribution 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 InspectableVector3Distribution(InspectableContext context, string title, string path, int depth,
  28. InspectableFieldLayout layout, SerializableProperty property)
  29. : base(context, title, path, SerializableProperty.FieldType.Vector3Distribution, depth, layout, property)
  30. { }
  31. /// <inheritoc/>
  32. protected internal override void Initialize(int layoutIndex)
  33. {
  34. if (property != null)
  35. {
  36. guiDistributionField = new GUIVector3DistributionField(new GUIContent(title));
  37. guiDistributionField.OnCurveChanged += () =>
  38. {
  39. StartUndo();
  40. property.SetValue(guiDistributionField.Value);
  41. state |= InspectableState.ModifyInProgress;
  42. EndUndo();
  43. };
  44. guiDistributionField.OnConstantModified += (x, y) => OnFieldValueChanged();
  45. guiDistributionField.OnConstantConfirmed += (rangeComp, vectorComp) =>
  46. {
  47. OnFieldValueConfirm();
  48. if (rangeComp == RangeComponent.Min)
  49. StartUndo("min." + vectorComp.ToString());
  50. else
  51. StartUndo("max." + vectorComp.ToString());
  52. };
  53. guiDistributionField.OnConstantFocusChanged += (focus, rangeComp, vectorComp) =>
  54. {
  55. if (focus)
  56. {
  57. if (rangeComp == RangeComponent.Min)
  58. StartUndo("min." + vectorComp.ToString());
  59. else
  60. StartUndo("max." + vectorComp.ToString());
  61. }
  62. else
  63. OnFieldValueConfirm();
  64. };
  65. layout.AddElement(layoutIndex, guiDistributionField);
  66. }
  67. }
  68. /// <inheritdoc/>
  69. public override InspectableState Refresh(int layoutIndex)
  70. {
  71. if (guiDistributionField != null && !guiDistributionField.HasInputFocus)
  72. guiDistributionField.Value = property.GetValue<Vector3Distribution>();
  73. InspectableState oldState = state;
  74. if (state.HasFlag(InspectableState.Modified))
  75. state = InspectableState.NotModified;
  76. return oldState;
  77. }
  78. /// <inheritdoc />
  79. public override void SetHasFocus(string subFieldName = null)
  80. {
  81. if (subFieldName != null && subFieldName.StartsWith("min."))
  82. {
  83. string component = subFieldName.Remove(0, "min.".Length);
  84. if (component == "X")
  85. guiDistributionField.SetInputFocus(RangeComponent.Min, VectorComponent.X, true);
  86. else if (component == "Y")
  87. guiDistributionField.SetInputFocus(RangeComponent.Min, VectorComponent.Y, true);
  88. else if (component == "Z")
  89. guiDistributionField.SetInputFocus(RangeComponent.Min, VectorComponent.Z, true);
  90. else
  91. guiDistributionField.SetInputFocus(RangeComponent.Min, VectorComponent.X, true);
  92. }
  93. if (subFieldName != null && subFieldName.StartsWith("max."))
  94. {
  95. string component = subFieldName.Remove(0, "max.".Length);
  96. if (component == "X")
  97. guiDistributionField.SetInputFocus(RangeComponent.Max, VectorComponent.X, true);
  98. else if (component == "Y")
  99. guiDistributionField.SetInputFocus(RangeComponent.Max, VectorComponent.Y, true);
  100. else if (component == "Z")
  101. guiDistributionField.SetInputFocus(RangeComponent.Max, VectorComponent.Z, true);
  102. else
  103. guiDistributionField.SetInputFocus(RangeComponent.Max, VectorComponent.X, true);
  104. }
  105. }
  106. /// <summary>
  107. /// Triggered when the user edits the distribution.
  108. /// </summary>
  109. private void OnFieldValueChanged()
  110. {
  111. property.SetValue(guiDistributionField.Value);
  112. state |= InspectableState.ModifyInProgress;
  113. }
  114. /// <summary>
  115. /// Triggered when the user confirms input in the float fields used for displaying the non-curve distribution.
  116. /// </summary>
  117. private void OnFieldValueConfirm()
  118. {
  119. if (state.HasFlag(InspectableState.ModifyInProgress))
  120. state |= InspectableState.Modified;
  121. EndUndo();
  122. }
  123. }
  124. /** @} */
  125. }