InspectableInt.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using BansheeEngine;
  7. namespace BansheeEditor
  8. {
  9. public class InspectableInt : InspectableObjectBase
  10. {
  11. private int oldPropertyValue;
  12. private GUIIntField guiIntField;
  13. private bool isInitialized = false;
  14. public InspectableInt(string title, SerializableProperty property)
  15. :base(title, property)
  16. {
  17. }
  18. protected void Initialize(GUILayout layout)
  19. {
  20. if(property.Type == SerializableProperty.FieldType.Int)
  21. {
  22. guiIntField = new GUIIntField(new GUIContent(title));
  23. guiIntField.OnChanged += OnFieldValueChanged;
  24. layout.AddElement(guiIntField);
  25. }
  26. isInitialized = true;
  27. }
  28. protected override bool IsModified()
  29. {
  30. if (!isInitialized)
  31. return true;
  32. int newPropertyValue = property.GetValue<int>();
  33. if (oldPropertyValue != newPropertyValue)
  34. {
  35. oldPropertyValue = newPropertyValue;
  36. return true;
  37. }
  38. return base.IsModified();
  39. }
  40. protected override void Update(GUILayout layout)
  41. {
  42. base.Update(layout);
  43. if (!isInitialized)
  44. Initialize(layout);
  45. // TODO - Skip update if it currently has input focus so user can modify the value in peace
  46. guiIntField.Value = property.GetValue<int>();
  47. }
  48. private void OnFieldValueChanged(int newValue)
  49. {
  50. property.SetValue<int>(newValue);
  51. }
  52. public override void Destroy()
  53. {
  54. if (guiIntField != null)
  55. guiIntField.Destroy();
  56. base.Destroy();
  57. }
  58. }
  59. }