InspectableInt.cs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.
  11. /// </summary>
  12. public class InspectableInt : InspectableField
  13. {
  14. private GUIIntField guiIntField;
  15. private InspectableState state;
  16. private InspectableFieldStyleInfo style;
  17. /// <summary>
  18. /// Creates a new inspectable integer 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 InspectableInt(Inspector parent, string title, string path, int depth, InspectableFieldLayout layout,
  29. SerializableProperty property, InspectableFieldStyleInfo style)
  30. : base(parent, title, path, SerializableProperty.FieldType.Int, depth, layout, property)
  31. {
  32. this.style = style;
  33. }
  34. /// <inheritdoc/>
  35. protected internal override void Initialize(int layoutIndex)
  36. {
  37. if (property != null)
  38. {
  39. guiIntField = new GUIIntField(new GUIContent(title));
  40. if (style.StepStyle != null && style.StepStyle.Step != 0)
  41. guiIntField.Step = (int)style.StepStyle.Step;
  42. if (style.RangeStyle != null)
  43. guiIntField.SetRange((int)style.RangeStyle.Min, (int)style.RangeStyle.Max);
  44. guiIntField.OnChanged += OnFieldValueChanged;
  45. guiIntField.OnConfirmed += OnFieldValueConfirm;
  46. guiIntField.OnFocusLost += OnFieldValueConfirm;
  47. layout.AddElement(layoutIndex, guiIntField);
  48. }
  49. }
  50. /// <inheritdoc/>
  51. public override InspectableState Refresh(int layoutIndex)
  52. {
  53. if (guiIntField != null && !guiIntField.HasInputFocus)
  54. guiIntField.Value = property.GetValue<int>();
  55. InspectableState oldState = state;
  56. if (state.HasFlag(InspectableState.Modified))
  57. state = InspectableState.NotModified;
  58. return oldState;
  59. }
  60. /// <summary>
  61. /// Triggered when the user inputs a new integer value.
  62. /// </summary>
  63. /// <param name="newValue">New value of the int field.</param>
  64. private void OnFieldValueChanged(int newValue)
  65. {
  66. property.SetValue(newValue);
  67. state |= InspectableState.ModifyInProgress;
  68. }
  69. /// <summary>
  70. /// Triggered when the user confirms input in the integer field.
  71. /// </summary>
  72. private void OnFieldValueConfirm()
  73. {
  74. if(state.HasFlag(InspectableState.ModifyInProgress))
  75. state |= InspectableState.Modified;
  76. }
  77. }
  78. /** @} */
  79. }