InspectableBool.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System;
  2. using BansheeEngine;
  3. namespace BansheeEditor
  4. {
  5. /// <summary>
  6. /// Displays GUI for a serializable property containing a boolean. Boolean is displayed as a toggle button.
  7. /// </summary>
  8. public class InspectableBool : InspectableField
  9. {
  10. private bool propertyValue;
  11. private GUIToggleField guiField;
  12. /// <summary>
  13. /// Creates a new inspectable boolean GUI for the specified property.
  14. /// </summary>
  15. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  16. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  17. /// contain other fields, in which case you should increase this value by one.</param>
  18. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  19. /// <param name="property">Serializable property referencing the array whose contents to display.</param>
  20. public InspectableBool(string title, int depth, InspectableFieldLayout layout, SerializableProperty property)
  21. : base(title, depth, layout, property)
  22. {
  23. }
  24. /// <inheritoc/>
  25. protected internal override void BuildGUI(int layoutIndex)
  26. {
  27. if (property.Type == SerializableProperty.FieldType.Bool)
  28. {
  29. guiField = new GUIToggleField(new GUIContent(title));
  30. guiField.OnChanged += OnFieldValueChanged;
  31. layout.AddElement(layoutIndex, guiField);
  32. }
  33. }
  34. /// <inheritdoc/>
  35. public override bool IsModified()
  36. {
  37. bool newPropertyValue = property.GetValue<bool>();
  38. if (propertyValue != newPropertyValue)
  39. return true;
  40. return base.IsModified();
  41. }
  42. /// <inheritdoc/>
  43. protected internal override void Update(int layoutIndex)
  44. {
  45. propertyValue = property.GetValue<bool>();
  46. if (guiField != null)
  47. guiField.Value = propertyValue;
  48. }
  49. /// <summary>
  50. /// Triggered when the user toggles the toggle button.
  51. /// </summary>
  52. /// <param name="newValue">New value of the toggle button.</param>
  53. private void OnFieldValueChanged(bool newValue)
  54. {
  55. property.SetValue(newValue);
  56. }
  57. }
  58. }