InspectableFloat.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 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 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="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 array whose contents to display.</param>
  27. /// <param name="style">Contains information about the field style</param>
  28. public InspectableFloat(Inspector parent, string title, string path, int depth, InspectableFieldLayout layout,
  29. SerializableProperty property, InspectableFieldStyleInfo style)
  30. : base(parent, 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.StepStyle != null && style.StepStyle.Step != 0)
  41. guiFloatField.Step = style.StepStyle.Step;
  42. guiFloatField.OnChanged += OnFieldValueChanged;
  43. guiFloatField.OnConfirmed += OnFieldValueConfirm;
  44. guiFloatField.OnFocusLost += OnFieldValueConfirm;
  45. layout.AddElement(layoutIndex, guiFloatField);
  46. }
  47. }
  48. /// <inheritdoc/>
  49. public override InspectableState Refresh(int layoutIndex)
  50. {
  51. if (guiFloatField != null && !guiFloatField.HasInputFocus)
  52. guiFloatField.Value = property.GetValue<float>();
  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 inputs a new floating point value.
  60. /// </summary>
  61. /// <param name="newValue">New value of the float field.</param>
  62. private void OnFieldValueChanged(float newValue)
  63. {
  64. property.SetValue(newValue);
  65. state |= InspectableState.ModifyInProgress;
  66. }
  67. /// <summary>
  68. /// Triggered when the user confirms input in the float field.
  69. /// </summary>
  70. private void OnFieldValueConfirm()
  71. {
  72. if (state.HasFlag(InspectableState.ModifyInProgress))
  73. state |= InspectableState.Modified;
  74. }
  75. }
  76. /** @} */
  77. }