InspectableRangedInt.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 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="context">Context shared by all inspectable fields created by the same parent.</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(InspectableContext context, string title, string path, int depth, InspectableFieldLayout layout,
  28. SerializableProperty property, InspectableFieldStyleInfo style)
  29. : base(context, 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. guiIntField.OnFocusGained += StartUndo;
  43. layout.AddElement(layoutIndex, guiIntField);
  44. }
  45. }
  46. /// <inheritdoc/>
  47. public override InspectableState Refresh(int layoutIndex)
  48. {
  49. if (guiIntField != null && !guiIntField.HasInputFocus)
  50. guiIntField.Value = property.GetValue<int>();
  51. InspectableState oldState = state;
  52. if (state.HasFlag(InspectableState.Modified))
  53. state = InspectableState.NotModified;
  54. return oldState;
  55. }
  56. /// <inheritdoc />
  57. public override void SetHasFocus(string subFieldName = null)
  58. {
  59. guiIntField.Focus = true;
  60. }
  61. /// <summary>
  62. /// Triggered when the user inputs a new integer value.
  63. /// </summary>
  64. /// <param name="newValue">New value of the float field.</param>
  65. private void OnFieldValueChanged(float newValue)
  66. {
  67. property.SetValue((int)newValue);
  68. state |= InspectableState.ModifyInProgress;
  69. }
  70. /// <summary>
  71. /// Triggered when the user confirms input in the int field.
  72. /// </summary>
  73. private void OnFieldValueConfirm()
  74. {
  75. if (state.HasFlag(InspectableState.ModifyInProgress))
  76. state |= InspectableState.Modified;
  77. EndUndo();
  78. }
  79. }
  80. /** @} */
  81. }