InspectableBool.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using bs;
  5. namespace bs.Editor
  6. {
  7. /** @addtogroup Inspector
  8. * @{
  9. */
  10. /// <summary>
  11. /// Displays GUI for a serializable property containing a boolean. Boolean is displayed as a toggle button.
  12. /// </summary>
  13. public class InspectableBool : InspectableField
  14. {
  15. private GUIToggleField guiField;
  16. private InspectableState state;
  17. /// <summary>
  18. /// Creates a new inspectable boolean GUI for the specified property.
  19. /// </summary>
  20. /// <param name="context">Context shared by all inspectable fields created by the same parent.</param>
  21. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  22. /// <param name="path">Full path to this property (includes name of this property and all parent properties).</param>
  23. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  24. /// contain other fields, in which case you should increase this value by one.</param>
  25. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  26. /// <param name="property">Serializable property referencing the field whose contents to display.</param>
  27. public InspectableBool(InspectableContext context, string title, string path, int depth, InspectableFieldLayout layout,
  28. SerializableProperty property)
  29. : base(context, title, path, SerializableProperty.FieldType.Bool, depth, layout, property)
  30. {
  31. }
  32. /// <inheritoc/>
  33. protected internal override void Initialize(int layoutIndex)
  34. {
  35. if (property != null)
  36. {
  37. guiField = new GUIToggleField(new GUIContent(title));
  38. guiField.OnChanged += OnFieldValueChanged;
  39. layout.AddElement(layoutIndex, guiField);
  40. }
  41. }
  42. /// <inheritdoc/>
  43. public override InspectableState Refresh(int layoutIndex)
  44. {
  45. if (guiField != null)
  46. guiField.Value = property.GetValue<bool>();
  47. InspectableState oldState = state;
  48. if (state.HasFlag(InspectableState.Modified))
  49. state = InspectableState.NotModified;
  50. return oldState;
  51. }
  52. /// <inheritdoc />
  53. public override void SetHasFocus(string subFieldName = null)
  54. {
  55. guiField.Focus = true;
  56. }
  57. /// <summary>
  58. /// Triggered when the user toggles the toggle button.
  59. /// </summary>
  60. /// <param name="newValue">New value of the toggle button.</param>
  61. private void OnFieldValueChanged(bool newValue)
  62. {
  63. StartUndo();
  64. property.SetValue(newValue);
  65. state = InspectableState.Modified;
  66. EndUndo();
  67. }
  68. }
  69. /** @} */
  70. }