/// <summary>
        /// 筛选列表
        /// </summary>
        /// <param name="source">The source.</param>
        /// <returns>List&lt;T&gt;.</returns>
        private List <T> Filter(List <T> source)
        {
            if (GUILayout.Button("F", EditorStyles.toolbarButton, GUILayout.Width(20)))
            {
                PopCustomWindow customWindow = new PopCustomWindow();
                customWindow.DrawGUI = () =>
                {
                    GUILayout.BeginHorizontal("flow overlay box");
                    GUITool.Button("筛选", Color.clear);
                    GUILayout.EndHorizontal();

                    GUILayout.BeginVertical();
                    foreach (var searchFilter in filters)
                    {
                        var show = searchFilter.Show();
                        if (show)
                        {
                            this.SetDirty();
                        }
                    }
                    GUILayout.EndVertical();
                };
                customWindow.PopWindow();
            }

            if (research)
            {
                var list = new List <T>();

                //检索数据,必须满足所有的筛选器
                foreach (var data in source)
                {
                    bool enable = this.filters.All(filter => !filter.IsActive() || filter.RunFilter(data));
                    if (enable)
                    {
                        list.Add(data);
                    }
                }
                return(list);
            }
            return(source);
        }
示例#2
0
        private void DrawTable()
        {
            scrollPosition = GUILayout.BeginScrollView(scrollPosition);
            GUILayout.BeginVertical();

            List <string> unsupportField = new List <string>();

            for (int i = 0; i < this.objects.Count; i++)
            {
                var o = this.objects[i];
                if (search.IsNOTNullOrEmpty())
                {
                    bool finded = false;
                    foreach (var field in fieldList)
                    {
                        string fieldName = GetFieldName(field);
                        if (search.IsNOTNullOrEmpty() && fieldName == currSearchFieldName)
                        {
                            object value = field.GetValue(o);
                            if (value.ToString().Contains(search))
                            {
                                finded = true;
                                break;
                            }
                        }
                    }
                    if (!finded)
                    {
                        continue;
                    }
                }

                if (i % 2 == 0)
                {
                    GUILayout.BeginVertical(GUITool.GetAreaGUIStyle(new Color(0f, 0f, 0f, 0.35f)));
                }
                else
                {
                    GUILayout.BeginVertical(GUITool.GetAreaGUIStyle(new Color(1f, 1f, 1f, 0.1f)));
                }

                GUILayout.BeginHorizontal();
                foreach (var field in fieldList)
                {
                    if (field.GetCustomAttributes(typeof(HideInInspector), true).FirstOrDefault() != null)
                    {
                        //跳过不显示的内容
                        continue;
                    }
                    string fieldName = GetFieldName(field);

                    if (!fieldEnable[field])
                    {
                        //跳过不显示的内容
                        continue;
                    }

                    GUILayout.BeginHorizontal(GUILayout.Width(fieldWantWidth[fieldName]));
                    field.SetValue(
                        o,
                        FieldInspectorTool.GenericField(field.Name, field.GetValue(o), field.FieldType, field, o, false));
                    GUILayout.EndHorizontal();

                    Rect lastRect = GUILayoutUtility.GetLastRect();

                    if (lastRect.size.x > 2 && lastRect.size.y > 2)
                    {
                        if (this.fieldRealWidth[fieldName].position.x < lastRect.position.x)
                        {
                            this.fieldRealWidth[fieldName] = lastRect;
                        }
                    }
                    if (lastRect.size.y == 0)
                    {
                        //可能没被支持的字段
                        unsupportField.Add(fieldName);
                    }
                }

                GUILayout.EndHorizontal();
                GUILayout.Space(5);
                GUILayout.EndVertical();
            }

            GUILayout.EndVertical();
            GUILayout.EndScrollView();

            foreach (var fieldName in unsupportField)
            {
                fieldEnable[nameToFieldDic[fieldName]] = false;
            }
        }
        /// <summary>
        /// 排序列表
        /// </summary>
        /// <param name="list">The list.</param>
        /// <returns>List&lt;T&gt;.</returns>
        private List <T> Sort(List <T> list)
        {
            FieldInfo sortFieldInfo = null;

            if (this.sortField.IsNOTNullOrEmpty())
            {
                sortFieldInfo = this.dataType.GetField(this.sortField);
            }

            bool click = false;

            if (sortFieldInfo != null)
            {
                click = GUILayout.Button("Sort:" + this.dataType.GetField(this.sortField).GetBestName(), EditorStyles.toolbarButton);
            }
            else
            {
                click = GUILayout.Button("S", EditorStyles.toolbarButton, GUILayout.Width(20));
            }
            if (click)
            {
                var customWindow = PopCustomWindow.ShowPopWindow();
                customWindow.DrawGUI = () =>
                {
                    GUILayout.BeginVertical();
                    GUILayout.BeginHorizontal("flow overlay box");
                    GUITool.Button("排序", Color.clear);
                    GUILayout.EndHorizontal();
                    var fieldInfos = this.dataType.GetFields(BindingFlags.Instance | BindingFlags.Public);
                    if (GUILayout.Button("默认"))
                    {
                        this.sortField = string.Empty;
                        this.SaveState();
                        customWindow.CloseWindow();
                        this.SetDirty();
                    }
                    foreach (var fieldInfo in fieldInfos)
                    {
                        if (fieldInfo.FieldType == typeof(int) || fieldInfo.FieldType == typeof(string) ||
                            fieldInfo.FieldType == typeof(uint) || fieldInfo.FieldType == typeof(float))
                        {
                            if (GUILayout.Button(fieldInfo.Name))
                            {
                                this.sortField = fieldInfo.Name;
                                this.SaveState();
                                customWindow.CloseWindow();
                                this.SetDirty();
                            }
                        }
                    }
                    GUILayout.EndVertical();
                };
            }
            if (research)
            {
                if (this.sortField.IsNOTNullOrEmpty())
                {
                    list.Sort(
                        (l, r) =>
                    {
                        var fieldInfo = this.dataType.GetField(this.sortField, BindingFlags.Instance | BindingFlags.Public);
                        var lvalue    = fieldInfo.GetValue(l);
                        var rvalue    = fieldInfo.GetValue(r);
                        if (fieldInfo.FieldType == typeof(string))
                        {
                            return(StringComparer.CurrentCulture.Compare(lvalue, rvalue));
                        }
                        if (fieldInfo.FieldType == typeof(uint))
                        {
                            if ((uint)(lvalue) > (uint)(rvalue))
                            {
                                return(1);
                            }
                            if ((uint)(lvalue) < (uint)(rvalue))
                            {
                                return(-1);
                            }
                            return(0);
                        }
                        if (fieldInfo.FieldType == typeof(int))
                        {
                            if ((int)(lvalue) > (int)(rvalue))
                            {
                                return(1);
                            }
                            if ((int)(lvalue) < (int)(rvalue))
                            {
                                return(-1);
                            }
                            return(0);
                        }
                        if (fieldInfo.FieldType == typeof(float))
                        {
                            if ((float)(lvalue) > (float)(rvalue))
                            {
                                return(1);
                            }
                            if ((float)(lvalue) < (float)(rvalue))
                            {
                                return(-1);
                            }
                            return(0);
                        }
                        return(0);
                    });
                }
            }

            return(list);
        }