InspectableBool.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 GUIToggleField guiField;
  11. private InspectableState state;
  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, SerializableProperty.FieldType.Bool, depth, layout, property)
  22. {
  23. }
  24. /// <inheritoc/>
  25. protected internal override void Initialize(int layoutIndex)
  26. {
  27. if (property != null)
  28. {
  29. guiField = new GUIToggleField(new GUIContent(title));
  30. guiField.OnChanged += OnFieldValueChanged;
  31. layout.AddElement(layoutIndex, guiField);
  32. }
  33. }
  34. /// <inheritdoc/>
  35. public override InspectableState Refresh(int layoutIndex)
  36. {
  37. if (guiField != null)
  38. guiField.Value = property.GetValue<bool>();
  39. InspectableState oldState = state;
  40. if (state.HasFlag(InspectableState.Modified))
  41. state = InspectableState.NotModified;
  42. return oldState;
  43. }
  44. /// <summary>
  45. /// Triggered when the user toggles the toggle button.
  46. /// </summary>
  47. /// <param name="newValue">New value of the toggle button.</param>
  48. private void OnFieldValueChanged(bool newValue)
  49. {
  50. property.SetValue(newValue);
  51. state = InspectableState.Modified;
  52. }
  53. }
  54. }