public static ReorderableList SetOnReordered(this ReorderableList _list,
                                                     ReorderableList.ReorderCallbackDelegate _onReordered)
        {
            _list.draggable         = true;
            _list.onReorderCallback = _onReordered;

            return(_list);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Draws the states within a ReorderableList.
        /// </summary>
        public static ReorderableList DrawStates(ReorderableList reorderableList, SerializedObject serializedObject, SerializedProperty states, string selectedIndexKey,
                                                 ReorderableList.ElementCallbackDelegate drawCallback, ReorderableList.AddCallbackDelegate addCallback,
                                                 ReorderableList.ReorderCallbackDelegate reorderCallback, ReorderableList.RemoveCallbackDelegate removeCallback)
        {
            // Initialize the reorder list on first run.
            if (reorderableList == null)
            {
                reorderableList = new ReorderableList(serializedObject, states, !Application.isPlaying, true, !Application.isPlaying, !Application.isPlaying && states.arraySize > 1);
                reorderableList.drawHeaderCallback = rect =>
                {
                    // Setup the field sizings.
                    rect.x += 14;
                    rect.x -= EditorGUI.indentLevel * Utility.InspectorUtility.IndentWidth;
                    var fieldWidth     = rect.width / 5;
                    var blockedByWidth = Mathf.Max(c_MinBlockedByWidth, Mathf.Min(c_MaxBlockedByWidth, fieldWidth)) + EditorGUI.indentLevel * Utility.InspectorUtility.IndentWidth;
                    fieldWidth = rect.width / 7;
                    var persistWidth  = Mathf.Max(c_MinPersistWidth, Mathf.Min(c_MaxPersistWidth, fieldWidth));
                    var activateWidth = Mathf.Max(c_MinActivateWidth, Mathf.Min(c_MaxActivateWidth, fieldWidth));
                    fieldWidth = (rect.width - blockedByWidth - persistWidth - activateWidth) / 2 - (c_WidthBuffer * 3);
                    var presetWidth = Mathf.Min(c_MaxPresetWidth, fieldWidth) + EditorGUI.indentLevel * Utility.InspectorUtility.IndentWidth * 2;
                    var nameWidth   = Mathf.Max(0, rect.width - presetWidth - blockedByWidth - persistWidth - activateWidth - (c_WidthBuffer * 6)) + EditorGUI.indentLevel * Utility.InspectorUtility.IndentWidth * 3;
                    var startRectX  = rect.x;

                    EditorGUI.LabelField(new Rect(startRectX, rect.y + 1, nameWidth, EditorGUIUtility.singleLineHeight), "Name");
                    startRectX += nameWidth + c_WidthBuffer - EditorGUI.indentLevel * Utility.InspectorUtility.IndentWidth;
                    EditorGUI.LabelField(new Rect(startRectX, rect.y + 1, presetWidth, EditorGUIUtility.singleLineHeight), "Preset");
                    startRectX += presetWidth - c_WidthBuffer * 3 - EditorGUI.indentLevel * Utility.InspectorUtility.IndentWidth;
                    EditorGUI.LabelField(new Rect(startRectX, rect.y + 1, blockedByWidth + EditorGUI.indentLevel * Utility.InspectorUtility.IndentWidth, EditorGUIUtility.singleLineHeight), "Blocked By");
                    startRectX += blockedByWidth + c_WidthBuffer * 5 - EditorGUI.indentLevel * Utility.InspectorUtility.IndentWidth;
                    EditorGUI.LabelField(new Rect(startRectX, rect.y + 1, persistWidth + EditorGUI.indentLevel * Utility.InspectorUtility.IndentWidth, EditorGUIUtility.singleLineHeight), "Persist");
                    startRectX += persistWidth;
                    EditorGUI.LabelField(new Rect(startRectX, rect.y + 1, activateWidth + EditorGUI.indentLevel * Utility.InspectorUtility.IndentWidth, EditorGUIUtility.singleLineHeight), "Activate");
                };
                reorderableList.drawElementCallback = drawCallback;
                reorderableList.onAddCallback       = addCallback;
                reorderableList.onReorderCallback   = reorderCallback;
                reorderableList.onRemoveCallback    = removeCallback;
                reorderableList.onSelectCallback    = list =>
                {
                    EditorPrefs.SetInt(selectedIndexKey, list.index);
                };
                if (EditorPrefs.GetInt(selectedIndexKey, -1) != -1)
                {
                    reorderableList.index = EditorPrefs.GetInt(selectedIndexKey, -1);
                }
            }

            // Indent the list so it lines up with the rest of the content.
            var listRect = GUILayoutUtility.GetRect(0, reorderableList.GetHeight());

            listRect.x    += Utility.InspectorUtility.IndentWidth * (EditorGUI.indentLevel + 1);
            listRect.xMax -= Utility.InspectorUtility.IndentWidth * (EditorGUI.indentLevel + 1);
            reorderableList.DoList(listRect);
            return(reorderableList);
        }
        /// <summary>
        /// Draws the ReorderableList.
        /// </summary>
        public static void DrawReorderableList(ref ReorderableList reorderableList, InspectorBase inspector, Array drawnObject, string serializedData,
                                               ReorderableList.HeaderCallbackDelegate drawHeaderCallback, ReorderableList.ElementCallbackDelegate drawElementCallback,
                                               ReorderableList.ReorderCallbackDelegate reorderCallback, ReorderableList.AddCallbackDelegate addCallback,
                                               ReorderableList.RemoveCallbackDelegate removeCallback, ReorderableList.SelectCallbackDelegate selectCallback,
                                               Action <int> drawSelectedElementCallback, string key, bool requireOne, bool indentList)
        {
            // Initialize the reorder list on first run.
            if (reorderableList == null)
            {
                var data = inspector.PropertyFromName(inspector.serializedObject, serializedData);
                reorderableList = new ReorderableList(inspector.serializedObject, data, (reorderCallback != null), true, !Application.isPlaying,
                                                      !Application.isPlaying && (!requireOne || (drawnObject != null && drawnObject.Length > 1)));
                reorderableList.drawHeaderCallback = (Rect rect) =>
                {
                    EditorGUI.LabelField(rect, "Name");
                };
                if (drawHeaderCallback != null)
                {
                    reorderableList.drawHeaderCallback = drawHeaderCallback;
                }
                reorderableList.drawElementCallback = drawElementCallback;
                if (reorderCallback != null)
                {
                    reorderableList.onReorderCallback = reorderCallback;
                }
                reorderableList.onAddCallback    = addCallback;
                reorderableList.onRemoveCallback = removeCallback;
                reorderableList.onSelectCallback = selectCallback;
                if (EditorPrefs.GetInt(key, -1) != -1)
                {
                    reorderableList.index = EditorPrefs.GetInt(key, -1);
                }
            }

            var listRect = GUILayoutUtility.GetRect(0, reorderableList.GetHeight());

            // Indent the list so it lines up with the rest of the content.
            if (indentList)
            {
                listRect.x    += InspectorUtility.IndentWidth;
                listRect.xMax -= InspectorUtility.IndentWidth;
            }
            reorderableList.DoList(listRect);
            if (reorderableList != null && reorderableList.index != -1)
            {
                if (drawnObject != null && reorderableList.index < drawnObject.Length)
                {
                    EditorGUI.indentLevel++;
                    drawSelectedElementCallback(reorderableList.index);
                    EditorGUI.indentLevel--;
                }
            }
        }
Exemplo n.º 4
0
        public static CachedReorderableList GetListDrawer(SerializedProperty property, ReorderableList.HeaderCallbackDelegate drawHeaderCallback, ReorderableList.ElementHeightCallbackDelegate getElementHeightCallback, ReorderableList.ElementCallbackDelegate drawElementCallback,
                                                          ReorderableList.FooterCallbackDelegate drawFooterCallback         = null,
                                                          ReorderableList.AddCallbackDelegate onAddCallback                 = null, ReorderableList.RemoveCallbackDelegate onRemoveCallback   = null, ReorderableList.SelectCallbackDelegate onSelectCallback = null,
                                                          ReorderableList.ChangedCallbackDelegate onChangedCallback         = null, ReorderableList.ReorderCallbackDelegate onReorderCallback = null, ReorderableList.CanRemoveCallbackDelegate onCanRemoveCallback = null,
                                                          ReorderableList.AddDropdownCallbackDelegate onAddDropdownCallback = null)
        {
            if (property == null)
            {
                throw new System.ArgumentNullException("property");
            }
            if (!property.isArray)
            {
                throw new System.ArgumentException("SerializedProperty must be a property for an Array or List", "property");
            }

            int hash = GetPropertyHash(property);
            CachedReorderableList lst;

            if (_lstCache.TryGetValue(hash, out lst))
            {
                lst.ReInit(property.serializedObject, property);
            }
            else
            {
                lst             = new CachedReorderableList(property.serializedObject, property);
                _lstCache[hash] = lst;
            }
            lst.drawHeaderCallback    = drawHeaderCallback;
            lst.elementHeightCallback = getElementHeightCallback;
            lst.drawElementCallback   = drawElementCallback;
            lst.drawFooterCallback    = drawFooterCallback;
            lst.onAddCallback         = onAddCallback;
            lst.onRemoveCallback      = onRemoveCallback;
            lst.onSelectCallback      = onSelectCallback;
            lst.onChangedCallback     = onChangedCallback;
            lst.onReorderCallback     = onReorderCallback;
            lst.onCanRemoveCallback   = onCanRemoveCallback;
            lst.onAddDropdownCallback = onAddDropdownCallback;

            return(lst);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Draws the AnimatorAudioStateSet.
        /// </summary>
        public static void DrawAnimatorAudioStateSet(UnityEngine.Object target, AnimatorAudioStateSet animatorAudioStateSet, string animatorAudioStateSetFieldName, bool randomDefaultSelector,
                                                     ref ReorderableList reorderableList, ReorderableList.ElementCallbackDelegate drawCallback, ReorderableList.SelectCallbackDelegate selectCallback,
                                                     ReorderableList.AddCallbackDelegate addCallback, ReorderableList.RemoveCallbackDelegate removeCallback, string preferencesKey,
                                                     ref ReorderableList reorderableAudioList, ReorderableList.ElementCallbackDelegate drawAudioElementCallback,
                                                     ReorderableList.AddCallbackDelegate addAudioCallback, ReorderableList.RemoveCallbackDelegate removeAudioCallback,
                                                     ref ReorderableList reorderableStateList, ReorderableList.ElementCallbackDelegate stateDrawElementCallback,
                                                     ReorderableList.AddCallbackDelegate stateAddCallback, ReorderableList.ReorderCallbackDelegate stateReorderCallback,
                                                     ReorderableList.RemoveCallbackDelegate stateRemoveCallback, string statePreferencesKey)
        {
            PopulateAnimatorAudioStateSelectorTypes();
            if (s_SelectorTypeNameCache != null)
            {
                var selected    = 0;
                var forceUpdate = true;
                if (animatorAudioStateSet.AnimatorAudioStateSelectorData != null && !string.IsNullOrEmpty(animatorAudioStateSet.AnimatorAudioStateSelectorData.ObjectType))
                {
                    for (int i = 0; i < s_SelectorTypeCache.Count; ++i)
                    {
                        if (s_SelectorTypeCache[i].FullName == animatorAudioStateSet.AnimatorAudioStateSelectorData.ObjectType)
                        {
                            selected    = i;
                            forceUpdate = false;
                            break;
                        }
                    }
                }
                var newSelected = EditorGUILayout.Popup("Selector", selected, s_SelectorTypeNameCache.ToArray());
                if (newSelected != selected || forceUpdate)
                {
                    // Use the Sequence selector as the default (or recoil in the case of a melee weapon).
                    if (forceUpdate)
                    {
                        for (int i = 0; i < s_SelectorTypeCache.Count; ++i)
                        {
                            if ((randomDefaultSelector && s_SelectorTypeCache[i].FullName == "Opsive.UltimateCharacterController.Items.AnimatorAudioStates.Sequence") ||
                                (!randomDefaultSelector && s_SelectorTypeCache[i].FullName == "Opsive.UltimateCharacterController.Items.AnimatorAudioStates.ConstantRecoil"))
                            {
                                newSelected = i;
                                break;
                            }
                        }
                    }
                    var animatorAudioOutputSelector = Activator.CreateInstance(s_SelectorTypeCache[newSelected]) as AnimatorAudioStateSelector;
                    animatorAudioStateSet.AnimatorAudioStateSelectorData = Serialization.Serialize(animatorAudioOutputSelector);
                    InspectorUtility.SetDirty(target);
                }
            }

            if (animatorAudioStateSet.AnimatorAudioStateSelector != null)
            {
                EditorGUI.indentLevel++;
                InspectorUtility.DrawObject(animatorAudioStateSet.AnimatorAudioStateSelector, false, true, target, false, () => {
                    animatorAudioStateSet.AnimatorAudioStateSelectorData = Serialization.Serialize(animatorAudioStateSet.AnimatorAudioStateSelector);
                    InspectorUtility.SetDirty(target);
                });
                EditorGUI.indentLevel--;
            }

            if (animatorAudioStateSet.States == null || animatorAudioStateSet.States.Length == 0)
            {
                animatorAudioStateSet.States = new AnimatorAudioStateSet.AnimatorAudioState[] { new AnimatorAudioStateSet.AnimatorAudioState() };
            }

            var serializedObject   = new SerializedObject(target);
            var serializedProperty = serializedObject.FindProperty(animatorAudioStateSetFieldName).FindPropertyRelative("m_States");

            if (reorderableList == null)
            {
                reorderableList = new ReorderableList(animatorAudioStateSet.States, typeof(AnimatorAudioStateSet.AnimatorAudioState), false, true, true, animatorAudioStateSet.States.Length > 1);
                reorderableList.drawHeaderCallback  = OnAnimatorAudioStateListHeaderDraw;
                reorderableList.drawElementCallback = drawCallback;
                reorderableList.onSelectCallback    = selectCallback;
                reorderableList.onAddCallback       = addCallback;
                reorderableList.onRemoveCallback    = removeCallback;
                reorderableList.serializedProperty  = serializedProperty;
                if (EditorPrefs.GetInt(preferencesKey, -1) != -1)
                {
                    reorderableList.index = EditorPrefs.GetInt(preferencesKey, -1);
                }
            }

            // ReorderableLists do not like indentation.
            var indentLevel = EditorGUI.indentLevel;

            while (EditorGUI.indentLevel > 0)
            {
                EditorGUI.indentLevel--;
            }

            var listRect = GUILayoutUtility.GetRect(0, reorderableList.GetHeight());

            // Indent the list so it lines up with the rest of the content.
            listRect.x    += InspectorUtility.IndentWidth * indentLevel;
            listRect.xMax -= InspectorUtility.IndentWidth * indentLevel;
            EditorGUI.BeginChangeCheck();
            var prevPref = EditorPrefs.GetInt(preferencesKey, 0);

            reorderableList.DoList(listRect);
            while (EditorGUI.indentLevel < indentLevel)
            {
                EditorGUI.indentLevel++;
            }
            if (EditorGUI.EndChangeCheck() || prevPref != EditorPrefs.GetInt(preferencesKey, 0))
            {
                reorderableList      = null;
                reorderableAudioList = null;
                reorderableStateList = null;
                return;
            }

            if (EditorPrefs.GetInt(preferencesKey, 0) >= animatorAudioStateSet.States.Length)
            {
                EditorPrefs.SetInt(preferencesKey, 0);
            }

            serializedProperty = serializedProperty.GetArrayElementAtIndex(EditorPrefs.GetInt(preferencesKey, 0));
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(serializedProperty.FindPropertyRelative("m_AllowDuringMovement"));
            EditorGUILayout.PropertyField(serializedProperty.FindPropertyRelative("m_RequireGrounded"));
            EditorGUILayout.PropertyField(serializedProperty.FindPropertyRelative("m_StateName"));
            EditorGUILayout.PropertyField(serializedProperty.FindPropertyRelative("m_ItemSubstateIndex"));
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }

            var animatorAudioState = animatorAudioStateSet.States[EditorPrefs.GetInt(preferencesKey, 0)];

            AudioClipSetInspector.DrawAudioClipSet(animatorAudioState.AudioClipSet, serializedProperty.FindPropertyRelative("m_AudioClipSet"), ref reorderableAudioList, drawAudioElementCallback, addAudioCallback, removeAudioCallback);
            if (InspectorUtility.Foldout(animatorAudioState, new GUIContent("States"), false))
            {
                EditorGUI.indentLevel--;
                // The MovementType class derives from system.object at the base level and reorderable lists can only operate on Unity objects. To get around this restriction
                // create a dummy array within a Unity object that corresponds to the number of elements within the ability's state list. When the reorderable list is drawn
                // the ability object will be used so it's like the dummy object never existed.
                var gameObject       = new GameObject();
                var stateIndexHelper = gameObject.AddComponent <StateInspectorHelper>();
                stateIndexHelper.StateIndexData = new int[animatorAudioState.States.Length];
                for (int i = 0; i < stateIndexHelper.StateIndexData.Length; ++i)
                {
                    stateIndexHelper.StateIndexData[i] = i;
                }
                var stateIndexSerializedObject = new SerializedObject(stateIndexHelper);
                reorderableStateList = StateInspector.DrawStates(reorderableStateList, new SerializedObject(target), stateIndexSerializedObject.FindProperty("m_StateIndexData"),
                                                                 statePreferencesKey, stateDrawElementCallback, stateAddCallback,
                                                                 stateReorderCallback, stateRemoveCallback);
                GameObject.DestroyImmediate(gameObject);
                EditorGUI.indentLevel++;
            }
            GUILayout.Space(5);
        }
        /// <summary>
        /// Creates a cached ReorderableList that can be used on a IList. The serializedProperty passed is used for look-up and is not used in the ReorderableList itself.
        /// </summary>
        /// <param name="memberList"></param>
        /// <param name="tokenProperty"></param>
        /// <param name="drawHeaderCallback"></param>
        /// <param name="drawElementCallback"></param>
        /// <param name="onAddCallback"></param>
        /// <param name="onRemoveCallback"></param>
        /// <param name="onSelectCallback"></param>
        /// <param name="onChangedCallback"></param>
        /// <param name="onReorderCallback"></param>
        /// <param name="onCanRemoveCallback"></param>
        /// <param name="onAddDropdownCallback"></param>
        /// <returns></returns>
        public static CachedReorderableList GetListDrawer(System.Collections.IList memberList, SerializedProperty tokenProperty, ReorderableList.HeaderCallbackDelegate drawHeaderCallback, ReorderableList.ElementCallbackDelegate drawElementCallback,
                                                          ReorderableList.AddCallbackDelegate onAddCallback                 = null, ReorderableList.RemoveCallbackDelegate onRemoveCallback   = null, ReorderableList.SelectCallbackDelegate onSelectCallback = null,
                                                          ReorderableList.ChangedCallbackDelegate onChangedCallback         = null, ReorderableList.ReorderCallbackDelegate onReorderCallback = null, ReorderableList.CanRemoveCallbackDelegate onCanRemoveCallback = null,
                                                          ReorderableList.AddDropdownCallbackDelegate onAddDropdownCallback = null)
        {
            if (memberList == null)
            {
                throw new System.ArgumentNullException("memberList");
            }
            if (tokenProperty == null)
            {
                throw new System.ArgumentNullException("property");
            }

            int hash = PropertyHandlerCache.GetIndexRespectingPropertyHash(tokenProperty);
            CachedReorderableList lst;

            if (_lstCache.TryGetValue(hash, out lst))
            {
                lst.ReInit(memberList);
            }
            else
            {
                lst             = new CachedReorderableList(memberList);
                _lstCache[hash] = lst;
            }

            lst.drawHeaderCallback    = drawHeaderCallback;
            lst.drawElementCallback   = drawElementCallback;
            lst.onAddCallback         = onAddCallback;
            lst.onRemoveCallback      = onRemoveCallback;
            lst.onSelectCallback      = onSelectCallback;
            lst.onChangedCallback     = onChangedCallback;
            lst.onReorderCallback     = onReorderCallback;
            lst.onCanRemoveCallback   = onCanRemoveCallback;
            lst.onAddDropdownCallback = onAddDropdownCallback;

            return(lst);
        }