InspectableFloatDistribution.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 floating point distribution. GUI elements will switch between
  11. /// floating point and curve input depending on the distribution type.
  12. /// </summary>
  13. public class InspectableFloatDistribution : InspectableField
  14. {
  15. private GUIFloatDistributionField guiDistributionField;
  16. private InspectableState state;
  17. /// <summary>
  18. /// Creates a new inspectable float 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 InspectableFloatDistribution(InspectableContext context, string title, string path, int depth,
  28. InspectableFieldLayout layout, SerializableProperty property)
  29. : base(context, title, path, SerializableProperty.FieldType.FloatDistribution, depth, layout, property)
  30. { }
  31. /// <inheritoc/>
  32. protected internal override void Initialize(int layoutIndex)
  33. {
  34. if (property != null)
  35. {
  36. guiDistributionField = new GUIFloatDistributionField(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<FloatDistribution>();
  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. guiDistributionField.SetInputFocus(RangeComponent.Min, VectorComponent.X, true);
  83. if (subFieldName != null && subFieldName.StartsWith("max."))
  84. guiDistributionField.SetInputFocus(RangeComponent.Max, VectorComponent.X, true);
  85. }
  86. /// <summary>
  87. /// Triggered when the user edits the distribution.
  88. /// </summary>
  89. private void OnFieldValueChanged()
  90. {
  91. property.SetValue(guiDistributionField.Value);
  92. state |= InspectableState.ModifyInProgress;
  93. }
  94. /// <summary>
  95. /// Triggered when the user confirms input in the float fields used for displaying the non-curve distribution.
  96. /// </summary>
  97. private void OnFieldValueConfirm()
  98. {
  99. if (state.HasFlag(InspectableState.ModifyInProgress))
  100. state |= InspectableState.Modified;
  101. EndUndo();
  102. }
  103. }
  104. /** @} */
  105. }