예제 #1
0
        static object ListField <T>(object v, Type type, Func <T, int, string, object> customElementGUI)
        {
            var list     = v as IList;
            var hasElem  = (list != null) && list.Count > 0;
            var elemType = TypeUtility.GetListInterface(type).GetGenericArguments().First();

            var addIdx    = -1;
            var deleteIdx = -1;

            using (new GUILayout.VerticalScope("box"))
            {
                if (v == null)
                {
                    WarningLabelNoStyle("List is null.");
                }
                else if (!hasElem)
                {
                    WarningLabelNoStyle("List is empty.");
                }
                else
                {
                    for (var i = 0; i < list.Count; ++i)
                    {
                        var label = TypeUtility.IsMultiLine(elemType) ? $"Element {i}" : null;

                        using (new IndentScope(20f))
                        {
                            list[i] = (customElementGUI != null)
                                ? customElementGUI((T)list, i, label)
                                : Field(list[i], elemType, label);
                        }

                        var result = PopupOnLastRect(ListPopupButtonNames, 1);

                        switch (result)
                        {
                        case 0:
                            addIdx = i + 1;
                            break;

                        case 1:
                            deleteIdx = i;
                            break;
                        }
                    }
                }

                if (addIdx >= 0)
                {
                    list = AddElement(list, elemType, list[addIdx - 1], addIdx);
                }
                if (deleteIdx >= 0)
                {
                    list = DeleteElement(list, elemType, deleteIdx);
                }

                // +/- button
                using (new GUILayout.HorizontalScope())
                {
                    GUILayout.FlexibleSpace();

                    var width = GUILayout.Width(20f);
                    if (GUILayout.Button("+", width))
                    {
                        if (list == null)
                        {
                            list = (IList)Activator.CreateInstance(type, 0);
                        }

                        var baseElem = hasElem ? list[list.Count - 1] : null;

                        list = AddElement(list, elemType, baseElem, list.Count);
                    }

                    using (new EnabledScope(hasElem))
                    {
                        if (GUILayout.Button("-", width))
                        {
                            list = DeleteElement(list, elemType, list.Count - 1);
                        }
                    }
                }
            }

            return(list);
        }