InspectableInt.cs 3.2 KB

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