InspectableInt.cs 3.1 KB

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