InspectableLayerMask.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using bs;
  4. namespace bs.Editor
  5. {
  6. /** @addtogroup Inspector
  7. * @{
  8. */
  9. /// <summary>
  10. /// Displays GUI for a serializable property containing an integer value displayed as a layer mask.
  11. /// </summary>
  12. public class InspectableLayerMask : InspectableField
  13. {
  14. private GUIListBoxField guiLayerMaskField;
  15. private InspectableState state;
  16. private ulong layersValue;
  17. /// <summary>
  18. /// Creates a new inspectable layer mask 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 InspectableLayerMask(InspectableContext context, string title, string path, int depth, InspectableFieldLayout layout,
  28. SerializableProperty property)
  29. : base(context, title, path, SerializableProperty.FieldType.Int, depth, layout, property)
  30. {
  31. }
  32. /// <inheritdoc/>
  33. protected internal override void Initialize(int layoutIndex)
  34. {
  35. if (property != null)
  36. {
  37. guiLayerMaskField = new GUIListBoxField(Layers.Names, true, new GUIContent(title));
  38. guiLayerMaskField.OnSelectionChanged += x =>
  39. {
  40. ulong layers = 0;
  41. bool[] states = guiLayerMaskField.States;
  42. for (int i = 0; i < states.Length; i++)
  43. layers |= states[i] ? Layers.Values[i] : 0;
  44. layersValue = layers;
  45. StartUndo();
  46. property.SetValue(layers);
  47. state |= InspectableState.ModifyInProgress | InspectableState.Modified;
  48. EndUndo();
  49. };
  50. layout.AddElement(layoutIndex, guiLayerMaskField);
  51. }
  52. }
  53. /// <inheritdoc/>
  54. public override InspectableState Refresh(int layoutIndex)
  55. {
  56. if (guiLayerMaskField != null)
  57. {
  58. ulong currentValue = property.GetValue<ulong>();
  59. if (layersValue != currentValue)
  60. {
  61. bool[] states = new bool[64];
  62. for (int i = 0; i < states.Length; i++)
  63. states[i] = (currentValue & Layers.Values[i]) == Layers.Values[i];
  64. guiLayerMaskField.States = states;
  65. layersValue = currentValue;
  66. }
  67. }
  68. InspectableState oldState = state;
  69. if (state.HasFlag(InspectableState.Modified))
  70. state = InspectableState.NotModified;
  71. return oldState;
  72. }
  73. }
  74. /** @} */
  75. }