InspectableVector2Distribution.cs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.OnChanged += OnFieldValueChanged;
  38. guiDistributionField.OnConfirmed += () =>
  39. {
  40. OnFieldValueConfirm();
  41. StartUndo();
  42. };
  43. guiDistributionField.OnFocusLost += OnFieldValueConfirm;
  44. guiDistributionField.OnFocusGained += StartUndo;
  45. layout.AddElement(layoutIndex, guiDistributionField);
  46. }
  47. }
  48. /// <inheritdoc/>
  49. public override InspectableState Refresh(int layoutIndex)
  50. {
  51. if (guiDistributionField != null && !guiDistributionField.HasInputFocus)
  52. guiDistributionField.Value = property.GetValue<Vector2Distribution>();
  53. InspectableState oldState = state;
  54. if (state.HasFlag(InspectableState.Modified))
  55. state = InspectableState.NotModified;
  56. return oldState;
  57. }
  58. /// <summary>
  59. /// Triggered when the user edits the distribution.
  60. /// </summary>
  61. private void OnFieldValueChanged()
  62. {
  63. property.SetValue(guiDistributionField.Value);
  64. state |= InspectableState.ModifyInProgress;
  65. }
  66. /// <summary>
  67. /// Triggered when the user confirms input in the float fields used for displaying the non-curve distribution.
  68. /// </summary>
  69. private void OnFieldValueConfirm()
  70. {
  71. if (state.HasFlag(InspectableState.ModifyInProgress))
  72. state |= InspectableState.Modified;
  73. EndUndo();
  74. }
  75. }
  76. /** @} */
  77. }