InspectableFloat.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 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 value.
  11. /// </summary>
  12. public class InspectableFloat : InspectableField
  13. {
  14. private GUIFloatField guiFloatField;
  15. private InspectableState state;
  16. private InspectableFieldStyleInfo style;
  17. /// <summary>
  18. /// Creates a new inspectable float 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. /// <param name="style">Information that can be used for customizing field rendering and behaviour.</param>
  28. public InspectableFloat(InspectableContext context, string title, string path, int depth, InspectableFieldLayout layout,
  29. SerializableProperty property, InspectableFieldStyleInfo style)
  30. : base(context, title, path, SerializableProperty.FieldType.Float, depth, layout, property)
  31. {
  32. this.style = style;
  33. }
  34. /// <inheritoc/>
  35. protected internal override void Initialize(int layoutIndex)
  36. {
  37. if (property != null)
  38. {
  39. guiFloatField = new GUIFloatField(new GUIContent(title));
  40. if (style != null)
  41. {
  42. if (style.StepStyle != null && style.StepStyle.Step != 0)
  43. guiFloatField.Step = style.StepStyle.Step;
  44. if (style.RangeStyle != null)
  45. guiFloatField.SetRange(style.RangeStyle.Min, style.RangeStyle.Max);
  46. }
  47. guiFloatField.OnChanged += OnFieldValueChanged;
  48. guiFloatField.OnConfirmed += () =>
  49. {
  50. OnFieldValueConfirm();
  51. StartUndo();
  52. };
  53. guiFloatField.OnFocusLost += OnFieldValueConfirm;
  54. guiFloatField.OnFocusGained += StartUndo;
  55. layout.AddElement(layoutIndex, guiFloatField);
  56. }
  57. }
  58. /// <inheritdoc/>
  59. public override InspectableState Refresh(int layoutIndex)
  60. {
  61. if (guiFloatField != null && !guiFloatField.HasInputFocus)
  62. guiFloatField.Value = property.GetValue<float>();
  63. InspectableState oldState = state;
  64. if (state.HasFlag(InspectableState.Modified))
  65. state = InspectableState.NotModified;
  66. return oldState;
  67. }
  68. /// <inheritdoc />
  69. public override void SetHasFocus(string subFieldName = null)
  70. {
  71. guiFloatField.Focus = true;
  72. }
  73. /// <summary>
  74. /// Triggered when the user inputs a new floating point value.
  75. /// </summary>
  76. /// <param name="newValue">New value of the float field.</param>
  77. private void OnFieldValueChanged(float newValue)
  78. {
  79. property.SetValue(newValue);
  80. state |= InspectableState.ModifyInProgress;
  81. }
  82. /// <summary>
  83. /// Triggered when the user confirms input in the float field.
  84. /// </summary>
  85. private void OnFieldValueConfirm()
  86. {
  87. if (state.HasFlag(InspectableState.ModifyInProgress))
  88. state |= InspectableState.Modified;
  89. EndUndo();
  90. }
  91. }
  92. /** @} */
  93. }