Esempio n. 1
0
        public static bool DrawStringArrayAlt(ref string[] data, ref string lastItemText)
        {
            bool hasChange = false;

            GUILayout.BeginVertical();

            //EditorGUIUtility.LookLikeControls(0, 50);
            EditorGUIUtility.LookLikeControls(20, 200);

            if (data != null && data.Length > 0)
            {
                ListButtonResult act = ListButtonResult.None;
                int actInd           = -1;


                for (int i = 0; i < data.Length; i++)
                {
                    GUILayout.BeginHorizontal();

                    string editText = EditorGUILayout.TextField(i.ToString(), data[i]);
                    if (data[i] != editText)
                    {
                        data[i]   = editText;
                        hasChange = true;
                    }

                    GUILayout.Space(6f);

                    if (act == EditorExt.Utility.ListButtonResult.None)
                    {
                        act = DrawListButtons();
                        if (act != ListButtonResult.None)
                        {
                            actInd    = i;
                            hasChange = true;
                        }
                    }

                    GUILayout.EndHorizontal();
                }

                if (actInd != -1)
                {
                    switch (act)
                    {
                    case ListButtonResult.Add:
                        string copy = data[actInd];
                        M8.ArrayUtil.InsertAfter(ref data, actInd, copy);
                        break;

                    case ListButtonResult.Remove:
                        M8.ArrayUtil.RemoveAt(ref data, actInd);
                        break;

                    case ListButtonResult.Up:
                        if (actInd > 0)
                        {
                            string t = data[actInd - 1];
                            data[actInd - 1] = data[actInd];
                            data[actInd]     = t;
                        }
                        break;

                    case ListButtonResult.Down:
                        if (actInd < data.Length - 1)
                        {
                            string t = data[actInd + 1];
                            data[actInd + 1] = data[actInd];
                            data[actInd]     = t;
                        }
                        break;
                    }

                    GUIUtility.keyboardControl = 0;
                    GUIUtility.hotControl      = 0;
                }
            }

            //extra field for append
            GUILayout.BeginHorizontal();

            lastItemText = EditorGUILayout.TextField(" ", lastItemText != null ? lastItemText : "");

            GUILayout.Space(6f);

            if (DrawListButtons(true) == ListButtonResult.Add)
            {
                if (data != null)
                {
                    System.Array.Resize(ref data, data.Length + 1);
                    data[data.Length - 1] = lastItemText;
                }
                else
                {
                    data = new string[] { lastItemText }
                };

                lastItemText = "";

                hasChange = true;
            }

            GUILayout.EndHorizontal();
            //

            EditorGUIUtility.LookLikeControls();

            GUILayout.EndVertical();

            return(hasChange);
        }
Esempio n. 2
0
        /// <summary>
        /// Helper function for editing a string array, note that 'size' should not be data.Length, this needs to be separate.
        /// Also, label is used as the focus control when editing the count.
        /// </summary>
        public static void DrawStringArray(string label, ref bool foldout, ref string[] data, ref int size)
        {
            GUILayout.BeginVertical();

            GUILayout.BeginHorizontal();

            if (data == null)
            {
                data = new string[0];
            }

            foldout = EditorGUILayout.Foldout(foldout, label);

            GUILayout.FlexibleSpace();

            //EditorGUIUtility.LookLikeControls(0, 50);

            bool enterPressed = Event.current.type == EventType.keyDown && Event.current.keyCode == KeyCode.Return;

            GUILayout.Label("count", GUILayout.MaxWidth(50));

            string ctrlName = label;

            GUI.SetNextControlName(ctrlName);
            size = EditorGUILayout.IntField(size, GUILayout.MaxWidth(50));

            GUILayout.EndHorizontal();

            if (GUI.GetNameOfFocusedControl() == ctrlName)
            {
                if (enterPressed && size != data.Length)
                {
                    if (size < 0)
                    {
                        size = 0;
                    }

                    System.Array.Resize(ref data, size);

                    foldout = true;
                }
            }
            else
            {
                size = data.Length;
            }

            if (foldout)
            {
                EditorGUIUtility.LookLikeControls(20, 200);

                ListButtonResult act = ListButtonResult.None;
                int actInd           = -1;

                for (int i = 0; i < data.Length; i++)
                {
                    GUILayout.BeginHorizontal();

                    data[i] = EditorGUILayout.TextField(i.ToString(), data[i]);

                    GUILayout.FlexibleSpace();

                    if (act == EditorExt.Utility.ListButtonResult.None)
                    {
                        act = DrawListButtons();
                        if (act != ListButtonResult.None)
                        {
                            actInd = i;
                        }
                    }

                    GUILayout.EndHorizontal();
                }

                if (actInd != -1)
                {
                    switch (act)
                    {
                    case ListButtonResult.Add:
                        string copy = data[actInd];
                        M8.ArrayUtil.InsertAfter(ref data, actInd, copy);
                        break;

                    case ListButtonResult.Remove:
                        M8.ArrayUtil.RemoveAt(ref data, actInd);
                        break;

                    case ListButtonResult.Up:
                        if (actInd > 0)
                        {
                            string t = data[actInd - 1];
                            data[actInd - 1] = data[actInd];
                            data[actInd]     = t;
                        }
                        break;

                    case ListButtonResult.Down:
                        if (actInd < data.Length - 1)
                        {
                            string t = data[actInd + 1];
                            data[actInd + 1] = data[actInd];
                            data[actInd]     = t;
                        }
                        break;
                    }

                    GUIUtility.keyboardControl = 0;
                    GUIUtility.hotControl      = 0;
                }

                EditorGUIUtility.LookLikeControls();
            }

            GUILayout.EndVertical();
        }