InspectableFloatDistribution.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2018 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using BansheeEngine;
  4. namespace BansheeEditor
  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="parent">Parent Inspector this field belongs to.</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(Inspector parent, string title, string path, int depth,
  28. InspectableFieldLayout layout, SerializableProperty property)
  29. : base(parent, 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.OnChanged += OnFieldValueChanged;
  38. guiDistributionField.OnConfirmed += OnFieldValueConfirm;
  39. guiDistributionField.OnFocusLost += OnFieldValueConfirm;
  40. layout.AddElement(layoutIndex, guiDistributionField);
  41. }
  42. }
  43. /// <inheritdoc/>
  44. public override InspectableState Refresh(int layoutIndex)
  45. {
  46. if (guiDistributionField != null && !guiDistributionField.HasInputFocus)
  47. guiDistributionField.Value = property.GetValue<FloatDistribution>();
  48. InspectableState oldState = state;
  49. if (state.HasFlag(InspectableState.Modified))
  50. state = InspectableState.NotModified;
  51. return oldState;
  52. }
  53. /// <summary>
  54. /// Triggered when the user edits the distribution.
  55. /// </summary>
  56. private void OnFieldValueChanged()
  57. {
  58. property.SetValue(guiDistributionField.Value);
  59. state |= InspectableState.ModifyInProgress;
  60. }
  61. /// <summary>
  62. /// Triggered when the user confirms input in the float fields used for displaying the non-curve distribution.
  63. /// </summary>
  64. private void OnFieldValueConfirm()
  65. {
  66. if (state.HasFlag(InspectableState.ModifyInProgress))
  67. state |= InspectableState.Modified;
  68. }
  69. }
  70. /** @} */
  71. }