InspectableFloat.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using BansheeEngine;
  2. namespace BansheeEditor
  3. {
  4. /// <summary>
  5. /// Displays GUI for a serializable property containing a floating point value.
  6. /// </summary>
  7. public class InspectableFloat : InspectableField
  8. {
  9. private GUIFloatField guiFloatField;
  10. private InspectableState state;
  11. /// <summary>
  12. /// Creates a new inspectable float GUI for the specified property.
  13. /// </summary>
  14. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  15. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  16. /// contain other fields, in which case you should increase this value by one.</param>
  17. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  18. /// <param name="property">Serializable property referencing the array whose contents to display.</param>
  19. public InspectableFloat(string title, int depth, InspectableFieldLayout layout, SerializableProperty property)
  20. : base(title, SerializableProperty.FieldType.Float, depth, layout, property)
  21. {
  22. }
  23. /// <inheritoc/>
  24. protected internal override void Initialize(int layoutIndex)
  25. {
  26. if (property != null)
  27. {
  28. guiFloatField = new GUIFloatField(new GUIContent(title));
  29. guiFloatField.OnChanged += OnFieldValueChanged;
  30. guiFloatField.OnConfirmed += OnFieldValueConfirm;
  31. guiFloatField.OnFocusLost += OnFieldValueConfirm;
  32. layout.AddElement(layoutIndex, guiFloatField);
  33. }
  34. }
  35. /// <inheritdoc/>
  36. public override InspectableState Refresh(int layoutIndex)
  37. {
  38. if (guiFloatField != null && !guiFloatField.HasInputFocus)
  39. guiFloatField.Value = property.GetValue<int>();
  40. InspectableState oldState = state;
  41. if (state.HasFlag(InspectableState.Modified))
  42. state = InspectableState.NotModified;
  43. return oldState;
  44. }
  45. /// <summary>
  46. /// Triggered when the user inputs a new floating point value.
  47. /// </summary>
  48. /// <param name="newValue">New value of the float field.</param>
  49. private void OnFieldValueChanged(float newValue)
  50. {
  51. property.SetValue(newValue);
  52. state |= InspectableState.ModifyInProgress;
  53. }
  54. /// <summary>
  55. /// Triggered when the user confirms input in the float field.
  56. /// </summary>
  57. private void OnFieldValueConfirm()
  58. {
  59. if (state.HasFlag(InspectableState.ModifyInProgress))
  60. state |= InspectableState.Modified;
  61. }
  62. }
  63. }