using System.Collections.Generic;
using UnityEditor.XR.Interaction.Toolkit.Utilities;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
using UnityEngine.XR.Interaction.Toolkit.Filtering;
namespace UnityEditor.XR.Interaction.Toolkit
{
///
/// Custom editor for an .
///
[CustomEditor(typeof(XRInteractionManager), true), CanEditMultipleObjects]
public class XRInteractionManagerEditor : BaseInteractionEditor
{
const string k_FiltersExpandedKey = "XRI." + nameof(XRInteractionManagerEditor) + ".FiltersExpanded";
const string k_HoverFiltersExpandedKey = "XRI." + nameof(XRInteractionManagerEditor) + ".HoverFiltersExpanded";
const string k_SelectFiltersExpandedKey = "XRI." + nameof(XRInteractionManagerEditor) + ".SelectFiltersExpanded";
/// of the backing .
protected SerializedProperty m_StartingHoverFilters;
/// of the backing .
protected SerializedProperty m_StartingSelectFilters;
bool m_FiltersExpanded;
ReadOnlyReorderableList m_HoverFilters;
ReadOnlyReorderableList m_SelectFilters;
///
/// Contents of GUI elements used by this editor.
///
protected static class Contents
{
/// The global filters foldout.
public static readonly GUIContent globalFilters = EditorGUIUtility.TrTextContent("Global Filters", "Add filters to extend this object without needing to create a derived behavior.");
/// for .
public static readonly GUIContent startingHoverFilters = EditorGUIUtility.TrTextContent("Starting Hover Filters", "The hover filters that this manager will automatically link at startup (optional, may be empty). Used as additional global hover validations for this manager.");
/// for .
public static readonly GUIContent startingSelectFilters = EditorGUIUtility.TrTextContent("Starting Select Filters", "The select filters that this manager will automatically link at startup (optional, may be empty). Used as additional global select validations for this manager.");
/// The list of runtime hover filters.
public static readonly GUIContent hoverFiltersHeader = EditorGUIUtility.TrTextContent("Hover Filters", "The list is populated in Awake, during Play mode.");
/// The list of runtime select filters.
public static readonly GUIContent selectFiltersHeader = EditorGUIUtility.TrTextContent("Select Filters", "The list is populated in Awake, during Play mode.");
}
///
/// This function is called when the object becomes enabled and active.
///
///
protected virtual void OnEnable()
{
m_FiltersExpanded = SessionState.GetBool(k_FiltersExpandedKey, false);
m_StartingHoverFilters = serializedObject.FindProperty("m_StartingHoverFilters");
m_StartingSelectFilters = serializedObject.FindProperty("m_StartingSelectFilters");
var manager = (XRInteractionManager)target;
m_HoverFilters = new ReadOnlyReorderableList(new List(), Contents.hoverFiltersHeader, k_HoverFiltersExpandedKey)
{
isExpanded = SessionState.GetBool(k_HoverFiltersExpandedKey, true),
updateElements = list => manager.hoverFilters.GetAll(list),
onListReordered = (element, newIndex) => manager.hoverFilters.MoveTo(element, newIndex),
};
m_SelectFilters = new ReadOnlyReorderableList(new List(), Contents.selectFiltersHeader, k_SelectFiltersExpandedKey)
{
isExpanded = SessionState.GetBool(k_SelectFiltersExpandedKey, true),
updateElements = list => manager.selectFilters.GetAll(list),
onListReordered = (element, newIndex) => manager.selectFilters.MoveTo(element, newIndex),
};
}
///
/// This method is automatically called by to
/// draw the custom inspector. Override this method to customize the
/// inspector as a whole.
///
protected override void DrawInspector()
{
DrawBeforeProperties();
DrawProperties();
DrawDerivedProperties();
DrawFilters();
}
///
/// This method is automatically called by to
/// draw the section of the custom inspector before .
/// By default, this draws the read-only Script property.
///
protected virtual void DrawBeforeProperties()
{
DrawScript();
}
///
/// This method is automatically called by to
/// draw the property fields. Override this method to customize the
/// properties shown in the Inspector. This is typically the method overridden
/// when a derived behavior adds additional serialized properties
/// that should be displayed in the Inspector.
///
protected virtual void DrawProperties()
{
}
///
/// Draw the Global Filter foldout.
///
protected virtual void DrawFilters()
{
using (var check = new EditorGUI.ChangeCheckScope())
{
m_FiltersExpanded = EditorGUILayout.Foldout(m_FiltersExpanded, Contents.globalFilters, true);
if (check.changed)
SessionState.SetBool(k_FiltersExpandedKey, m_FiltersExpanded);
}
if (!m_FiltersExpanded)
return;
using (new EditorGUI.IndentLevelScope())
{
DrawFiltersNested();
}
}
///
/// Draw the property fields related to the hover and select filters.
///
protected virtual void DrawFiltersNested()
{
using (new EditorGUI.DisabledScope(Application.isPlaying))
{
EditorGUILayout.PropertyField(m_StartingHoverFilters, Contents.startingHoverFilters);
EditorGUILayout.PropertyField(m_StartingSelectFilters, Contents.startingSelectFilters);
}
if (!Application.isPlaying)
return;
if (serializedObject.isEditingMultipleObjects)
{
EditorGUILayout.HelpBox("Filters cannot be multi-edited.", MessageType.None);
return;
}
m_HoverFilters.DoLayoutList();
m_SelectFilters.DoLayoutList();
}
}
}