Exemple #1
0
 public static void DrawUnityObjectArray <T>(ref T[] curValue,
                                             string label,
                                             SerializedProperty arrayProperty,
                                             ArrayDrawStyle arrayDrawStyle,
                                             string elementLabel = null) where T : Object
 {
     DrawUnityObjectArray <T>(ref curValue,
                              label,
                              arrayProperty,
                              arrayDrawStyle,
                              elementLabel != null ? new Func <int, string>(index => elementLabel) : null);
 }
Exemple #2
0
 public static void DrawArray <T>(ref T[] curValue,
                                  string label,
                                  SerializedProperty arrayProperty,
                                  ArrayDrawStyle arrayDrawStyle,
                                  Func <T, string, SerializedProperty, T> drawerFunction,
                                  string elementLabel = null) where T : struct
 {
     DrawArray <T>(ref curValue,
                   label,
                   arrayProperty,
                   arrayDrawStyle,
                   drawerFunction,
                   elementLabel != null ? new Func <int, string>(index => elementLabel) : null);
 }
Exemple #3
0
        public static void DrawUnityObjectArray <T>(ref T[] curValue,
                                                    string label,
                                                    SerializedProperty arrayProperty,
                                                    ArrayDrawStyle arrayDrawStyle,
                                                    Func <int, string> fieldLabelGetter) where T : Object
        {
            if (curValue == null)
            {
                curValue = new T[0];
                ShouldBeDirty();
            }

            Foldout(arrayProperty, label, arrayDrawStyle.AsHeader);
            if (arrayProperty.isExpanded)
            {
                IncreaseIndent();
                int newSize = curValue.Length;
                DelayedIntField(ref newSize, "Size");
                string failedGetterLabel = ObjectNames.NicifyVariableName(typeof(T).Name);
                failedGetterLabel = $". {failedGetterLabel}";
                int?controlTargetIndex = null;
                int?shiftDirection     = null;
                for (int i = 0; i < curValue.Length; i++)
                {
                    string elementLabel = $"{i.ToString()}{fieldLabelGetter?.Invoke(i) ?? failedGetterLabel}";
                    GUILayout.BeginHorizontal();
                    UnityObjectField <T>(ref curValue[i], elementLabel);
                    if (arrayDrawStyle.AllowElementShift)
                    {
                        EditorGUI.BeginDisabledGroup(i == 0);
                        if (GUILayout.Button(ARRAY_SHIFT_UP, GUILayout.MaxWidth(ARRAY_CONTROL_WIDTH)))
                        {
                            controlTargetIndex = i;
                            shiftDirection     = -1;
                        }

                        EditorGUI.EndDisabledGroup();
                        EditorGUI.BeginDisabledGroup(i == (curValue.Length - 1));
                        if (GUILayout.Button(ARRAY_SHIFT_DOWN, GUILayout.MaxWidth(ARRAY_CONTROL_WIDTH)))
                        {
                            controlTargetIndex = i;
                            shiftDirection     = 1;
                        }

                        EditorGUI.EndDisabledGroup();
                    }

                    if (arrayDrawStyle.AllowElementDelete)
                    {
                        if (GUILayout.Button(ARRAY_DELETE, GUILayout.MaxWidth(ARRAY_CONTROL_WIDTH)))
                        {
                            controlTargetIndex = i;
                        }
                    }

                    EndIndentSpaces();
                }

                if (ControlArray(ref curValue, controlTargetIndex, shiftDirection))
                {
                    ShouldBeDirty();
                    if (!shiftDirection.HasValue)
                    {
                        newSize--;
                    }
                }

                int arraySizeChange = newSize - curValue.Length;
                if (arraySizeChange != 0)
                {
                    ChangeArraySize(ref curValue, arraySizeChange);
                    arrayProperty.arraySize = newSize;
                    ShouldBeDirty();
                }

                DecreaseIndent();
            }

            if (arrayDrawStyle.AsHeader)
            {
                EndFoldoutHeader();
            }
        }
Exemple #4
0
        public static void DrawArray <T>(ref T[] curValue,
                                         string label,
                                         SerializedProperty arrayProperty,
                                         ArrayDrawStyle arrayDrawStyle,
                                         Func <T, string, SerializedProperty, T> drawerFunction,
                                         Func <int, string> fieldLabelGetter) where T : struct
        {
            if (curValue == null)
            {
                curValue = new T[0];
                ShouldBeDirty();
            }

            Foldout(arrayProperty, label, arrayDrawStyle.AsHeader);
            if (arrayProperty.isExpanded)
            {
                IncreaseIndent();
                int newSize = curValue.Length;
                EditorGUILayout.BeginHorizontal();
                DelayedIntField(ref newSize, "Size");
                string failedGetterLabel = ObjectNames.NicifyVariableName(typeof(T).Name);
                failedGetterLabel = $". {failedGetterLabel}";

                //Expand / Collapse All
                if (newSize > 0)
                {
                    if (GUILayout.Button("Expand All"))
                    {
                        SetExpandState(true);
                    }

                    if (GUILayout.Button("Collapse All"))
                    {
                        SetExpandState(false);
                    }
                }

                EditorGUILayout.EndHorizontal();
                int?controlTargetIndex = null;
                int?shiftDirection     = null;
                for (int i = 0; i < curValue.Length; i++)
                {
                    bool               hasDrawnElement = false;
                    string             elementLabel    = $"{i.ToString()}{fieldLabelGetter?.Invoke(i) ?? failedGetterLabel}";
                    SerializedProperty elementProperty = arrayProperty.GetArrayElementAtIndex(i);
                    GUILayout.BeginHorizontal();
                    switch (arrayDrawStyle.ArrayElementWrap)
                    {
                    case ArrayElementWrap.None:
                        FinishedDrawingElement += ElementWasDrawn;
                        curValue[i]             = drawerFunction?.Invoke(curValue[i], elementLabel, elementProperty) ?? default;
                        if (!hasDrawnElement)
                        {
                            GUILayout.EndHorizontal();
                            FinishedDrawingElement -= ElementWasDrawn;
                        }

                        continue;

                    case ArrayElementWrap.Foldout:
                        Foldout(elementProperty, elementLabel, false);

                        break;

                    case ArrayElementWrap.Header:
                        Header(elementLabel);

                        break;
                    }

                    GUILayout.EndHorizontal();
                    if ((arrayDrawStyle.ArrayElementWrap != ArrayElementWrap.Foldout) || elementProperty.isExpanded)
                    {
                        IncreaseIndent();
                        curValue[i] = drawerFunction?.Invoke(curValue[i], elementLabel, elementProperty) ?? default;
                        DecreaseIndent();
                    }

                    void ElementWasDrawn()
                    {
                        hasDrawnElement         = true;
                        FinishedDrawingElement -= ElementWasDrawn;
                        AttachArrayControl();
                        GUILayout.EndHorizontal();
                    }

                    void AttachArrayControl()
                    {
                        if (arrayDrawStyle.AllowElementShift)
                        {
                            EditorGUI.BeginDisabledGroup(i == 0);
                            if (GUILayout.Button(ARRAY_SHIFT_UP, GUILayout.MaxWidth(ARRAY_CONTROL_WIDTH)))
                            {
                                controlTargetIndex = i;
                                shiftDirection     = -1;
                            }

                            EditorGUI.EndDisabledGroup();
                            EditorGUI.BeginDisabledGroup(i == (newSize - 1));
                            if (GUILayout.Button(ARRAY_SHIFT_DOWN, GUILayout.MaxWidth(ARRAY_CONTROL_WIDTH)))
                            {
                                controlTargetIndex = i;
                                shiftDirection     = 1;
                            }

                            EditorGUI.EndDisabledGroup();
                        }

                        if (arrayDrawStyle.AllowElementDelete)
                        {
                            if (GUILayout.Button(ARRAY_DELETE, GUILayout.MaxWidth(ARRAY_CONTROL_WIDTH)))
                            {
                                controlTargetIndex = i;
                            }
                        }
                    }
                }

                if (ControlArray(ref curValue, controlTargetIndex, shiftDirection))
                {
                    ShouldBeDirty();
                    if (!shiftDirection.HasValue)
                    {
                        newSize--;
                    }
                }

                int arraySizeChange = newSize - curValue.Length;
                if (arraySizeChange != 0)
                {
                    ChangeArraySize(ref curValue, arraySizeChange);
                    arrayProperty.arraySize = newSize;
                    ShouldBeDirty();
                }

                DecreaseIndent();
            }

            if (arrayDrawStyle.AsHeader)
            {
                EndFoldoutHeader();
            }

            void SetExpandState(bool newState)
            {
                for (int i = 0; i < arrayProperty.arraySize; i++)
                {
                    arrayProperty.GetArrayElementAtIndex(i).isExpanded = newState;
                }
            }
        }