InspectableInt.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 int propertyValue;
  10. private GUIIntField guiIntField;
  11. /// <summary>
  12. /// Creates a new inspectable integer GUI for the specified property.
  13. /// </summary>
  14. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  15. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  16. /// contain other fields, in which case you should increase this value by one.</param>
  17. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  18. /// <param name="property">Serializable property referencing the array whose contents to display.</param>
  19. public InspectableInt(string title, int depth, InspectableFieldLayout layout, SerializableProperty property)
  20. : base(title, depth, layout, property)
  21. {
  22. }
  23. /// <inheritdoc/>
  24. protected internal override void BuildGUI(int layoutIndex)
  25. {
  26. if (property.Type == SerializableProperty.FieldType.Int)
  27. {
  28. guiIntField = new GUIIntField(new GUIContent(title));
  29. guiIntField.OnChanged += OnFieldValueChanged;
  30. layout.AddElement(layoutIndex, guiIntField);
  31. }
  32. }
  33. /// <inheritdoc/>
  34. public override bool IsModified()
  35. {
  36. int newPropertyValue = property.GetValue<int>();
  37. if (propertyValue != newPropertyValue)
  38. return true;
  39. return base.IsModified();
  40. }
  41. /// <inheritdoc/>
  42. protected internal override void Update(int layoutIndex)
  43. {
  44. propertyValue = property.GetValue<int>();
  45. if (guiIntField != null)
  46. {
  47. if (guiIntField.HasInputFocus())
  48. return;
  49. guiIntField.Value = propertyValue;
  50. }
  51. }
  52. /// <summary>
  53. /// Triggered when the user inputs a new integer value.
  54. /// </summary>
  55. /// <param name="newValue">New value of the int field.</param>
  56. private void OnFieldValueChanged(int newValue)
  57. {
  58. property.SetValue(newValue);
  59. }
  60. }
  61. }