InspectableCurve.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 curve. Curve is displayed as a GUI field that allows
  11. /// the user to manipulate the curve using a curve editor window.
  12. /// </summary>
  13. public class InspectableCurve : InspectableField
  14. {
  15. private GUICurvesField guiField;
  16. private InspectableState state;
  17. /// <summary>
  18. /// Creates a new inspectable curve 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 InspectableCurve(InspectableContext context, string title, string path, int depth,
  28. InspectableFieldLayout layout, SerializableProperty property)
  29. : base(context, title, path, SerializableProperty.FieldType.Curve, depth, layout, property)
  30. { }
  31. /// <inheritoc/>
  32. protected internal override void Initialize(int layoutIndex)
  33. {
  34. if (property != null)
  35. {
  36. guiField = new GUICurvesField(new GUIContent(title));
  37. guiField.OnChanged += OnFieldValueChanged;
  38. layout.AddElement(layoutIndex, guiField);
  39. }
  40. }
  41. /// <inheritdoc/>
  42. public override InspectableState Refresh(int layoutIndex)
  43. {
  44. if (guiField != null)
  45. guiField.SetCurve(property.GetValue<AnimationCurve>());
  46. InspectableState oldState = state;
  47. if (state.HasFlag(InspectableState.Modified))
  48. state = InspectableState.NotModified;
  49. return oldState;
  50. }
  51. /// <summary>
  52. /// Triggered when the user updates the curve.
  53. /// </summary>
  54. /// <param name="newValue">New curve.</param>
  55. private void OnFieldValueChanged(AnimationCurve newValue)
  56. {
  57. StartUndo();
  58. property.SetValue(newValue);
  59. state = InspectableState.Modified;
  60. EndUndo();
  61. }
  62. }
  63. /** @} */
  64. }