InspectableRangedInt.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 an integer value with a range.
  11. /// </summary>
  12. public class InspectableRangedInt : InspectableRangedField
  13. {
  14. private GUISliderField guiIntField;
  15. private InspectableState state;
  16. /// <summary>
  17. /// Creates a new inspectable float GUI for the specified property with a range.
  18. /// </summary>
  19. /// <param name="parent">Parent Inspector this field belongs to.</param>
  20. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  21. /// <param name="path">Full path to this property (includes name of this property and all parent properties).</param>
  22. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  23. /// contain other fields, in which case you should increase this value by one.</param>
  24. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  25. /// <param name="property">Serializable property referencing the field whose contents to display.</param>
  26. /// <param name="style">Information about the range of the field.</param>
  27. public InspectableRangedInt(Inspector parent, string title, string path, int depth, InspectableFieldLayout layout,
  28. SerializableProperty property, InspectableFieldStyleInfo style)
  29. : base(parent, title, path, SerializableProperty.FieldType.Int, depth, layout, property, style)
  30. {
  31. }
  32. /// <inheritoc/>
  33. protected internal override void Initialize(int layoutIndex)
  34. {
  35. if (property != null)
  36. {
  37. guiIntField = new GUISliderField(style.RangeStyle.Min, style.RangeStyle.Max, new GUIContent(title));
  38. if (style != null && style.StepStyle != null && style.StepStyle.Step != 0)
  39. guiIntField.Step = style.StepStyle.Step;
  40. guiIntField.OnChanged += OnFieldValueChanged;
  41. guiIntField.OnFocusLost += OnFieldValueConfirm;
  42. layout.AddElement(layoutIndex, guiIntField);
  43. }
  44. }
  45. /// <inheritdoc/>
  46. public override InspectableState Refresh(int layoutIndex)
  47. {
  48. if (guiIntField != null && !guiIntField.HasInputFocus)
  49. guiIntField.Value = property.GetValue<int>();
  50. InspectableState oldState = state;
  51. if (state.HasFlag(InspectableState.Modified))
  52. state = InspectableState.NotModified;
  53. return oldState;
  54. }
  55. /// <summary>
  56. /// Triggered when the user inputs a new integer value.
  57. /// </summary>
  58. /// <param name="newValue">New value of the float field.</param>
  59. private void OnFieldValueChanged(float newValue)
  60. {
  61. property.SetValue((int)newValue);
  62. state |= InspectableState.ModifyInProgress;
  63. }
  64. /// <summary>
  65. /// Triggered when the user confirms input in the int field.
  66. /// </summary>
  67. private void OnFieldValueConfirm()
  68. {
  69. if (state.HasFlag(InspectableState.ModifyInProgress))
  70. state |= InspectableState.Modified;
  71. }
  72. }
  73. /** @} */
  74. }