InspectableVector2Distribution.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 2D vector distribution. GUI elements will switch between
  11. /// vector and curve input depending on the distribution type.
  12. /// </summary>
  13. public class InspectableVector2Distribution : InspectableField
  14. {
  15. private GUIVector2DistributionField guiDistributionField;
  16. private InspectableState state;
  17. /// <summary>
  18. /// Creates a new inspectable 2D 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 InspectableVector2Distribution(InspectableContext context, string title, string path, int depth,
  28. InspectableFieldLayout layout, SerializableProperty property)
  29. : base(context, title, path, SerializableProperty.FieldType.Vector2Distribution, depth, layout, property)
  30. { }
  31. /// <inheritoc/>
  32. protected internal override void Initialize(int layoutIndex)
  33. {
  34. if (property != null)
  35. {
  36. guiDistributionField = new GUIVector2DistributionField(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<Vector2Distribution>();
  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
  89. guiDistributionField.SetInputFocus(RangeComponent.Min, VectorComponent.X, true);
  90. }
  91. if (subFieldName != null && subFieldName.StartsWith("max."))
  92. {
  93. string component = subFieldName.Remove(0, "max.".Length);
  94. if (component == "X")
  95. guiDistributionField.SetInputFocus(RangeComponent.Max, VectorComponent.X, true);
  96. else if (component == "Y")
  97. guiDistributionField.SetInputFocus(RangeComponent.Max, VectorComponent.Y, true);
  98. else
  99. guiDistributionField.SetInputFocus(RangeComponent.Max, VectorComponent.X, true);
  100. }
  101. }
  102. /// <summary>
  103. /// Triggered when the user edits the distribution.
  104. /// </summary>
  105. private void OnFieldValueChanged()
  106. {
  107. property.SetValue(guiDistributionField.Value);
  108. state |= InspectableState.ModifyInProgress;
  109. }
  110. /// <summary>
  111. /// Triggered when the user confirms input in the float fields used for displaying the non-curve distribution.
  112. /// </summary>
  113. private void OnFieldValueConfirm()
  114. {
  115. if (state.HasFlag(InspectableState.ModifyInProgress))
  116. state |= InspectableState.Modified;
  117. EndUndo();
  118. }
  119. }
  120. /** @} */
  121. }