Beispiel #1
0
        /// <summary>
        /// Draw a table from the columns' settings, the path for the corresponding properties and a selector function
        /// that takes a SerializedProperty and returns the TableCell to put in the corresponding cell.
        /// </summary>
        /// <returns>The updated table state.</returns>
        /// <param name="rect">The table's containing rectangle.</param>
        /// <param name="tableState">The Table state.</param>
        /// <param name="collectionProperty">The serialized property of the collection.</param>
        /// <param name="columns">The Selector Columns.</param>
        /// <param name="options">The table options.</param>
        public static GUITableState DrawTable(
            Rect rect,
            GUITableState tableState,
            SerializedProperty collectionProperty,
            List <SelectorColumn> columns,
            params GUITableOption[] options)
        {
            GUITableEntry            tableEntry = new GUITableEntry(options);
            List <List <TableCell> > rows       = new List <List <TableCell> >();

            for (int i = 0; i < collectionProperty.arraySize; i++)
            {
                SerializedProperty sp = collectionProperty.FindPropertyRelative(SerializationHelpers.GetElementAtIndexRelativePath(i));
                if (tableEntry.filter != null && !tableEntry.filter(sp))
                {
                    continue;
                }
                List <TableCell> row = new List <TableCell>();
                foreach (SelectorColumn col in columns)
                {
                    row.Add(col.GetCell(sp));
                }
                rows.Add(row);
            }

            return(DrawTable(rect, tableState, columns.Select((col) => (TableColumn)col).ToList(), rows, collectionProperty, options));
        }
Beispiel #2
0
        /// <summary>
        /// Draw a table completely manually.
        /// Each cell has to be created and given as parameter in cells.
        /// </summary>
        /// <returns>The updated table state.</returns>
        /// <param name="tableState">The Table state.</param>
        /// <param name="columns">The Columns of the table.</param>
        /// <param name="cells">The Cells as a list of rows.</param>
        /// <param name="collectionProperty">The SerializeProperty of the collection. This is useful for reorderable tables.</param>
        /// <param name="options">The table options.</param>
        public static GUITableState DrawTable(
            GUITableState tableState,
            List <TableColumn> columns,
            List <List <TableCell> > cells,
            SerializedProperty collectionProperty,
            params GUITableOption[] options)
        {
            GUITableEntry tableEntry = new GUITableEntry(options);

            if (tableState == null)
            {
                tableState = new GUITableState();
                tableState.CheckState(columns, tableEntry, float.MaxValue);
            }

            float requiredHeight = GUITable.TableHeight(tableEntry, cells.Count) + 10;

            if (tableEntry.allowScrollView && tableState.totalWidth + 19 > Screen.width / EditorGUIUtility.pixelsPerPoint)
            {
                requiredHeight += EditorGUIUtility.singleLineHeight;
            }

            Rect position = GUILayoutUtility.GetRect(
                tableEntry.allowScrollView ? Screen.width / EditorGUIUtility.pixelsPerPoint - 40 : tableState.totalWidth,
                requiredHeight);

            if (Event.current.type == EventType.Layout)
            {
                return(tableState);
            }
            else
            {
                return(GUITable.DrawTable(position, tableState, columns, cells, collectionProperty, options));
            }
        }
        public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            if (!IsFirstElementQuickCheck(property.propertyPath))
            {
                if (!IsCollectionQuickCheck(property.propertyPath))
                {
                    return(EditorGUIUtility.singleLineHeight);
                }
                return(0);
            }

            string collectionPath, index;

            if (!IsCollectionFullCheck(property.propertyPath, out index, out collectionPath))
            {
                return(EditorGUIUtility.singleLineHeight);
            }

            if (index != "0")
            {
                return(0);
            }

            GUITableEntry      tableEntry         = new GUITableEntry(tableOptions);
            SerializedProperty collectionProperty = property.serializedObject.FindProperty(collectionPath);

            return(collectionProperty.arraySize * (GUITable.RowHeight(tableEntry) - 2) + EditorGUIUtility.singleLineHeight + GUITable.HeadersHeight(tableEntry) + GUITable.ExtraHeight(tableEntry));
        }
Beispiel #4
0
 public void CheckState(List <TableColumn> columns, GUITableEntry tableEntry, float availableWidth)
 {
     if (columnSizes == null || columnSizes.Count < columns.Count)
     {
         columnSizes = columns.Select((column) => column.GetDefaultWidth()).ToList();
     }
     if (columnVisible == null || columnVisible.Count < columns.Count)
     {
         columnVisible = columns.Select((column) => column.entry.visibleByDefault).ToList();
     }
     if (totalWidth < availableWidth && !isBeingResized)
     {
         List <int> expandableColumns = new List <int> ();
         for (int i = 0; i < columns.Count; i++)
         {
             if (columns[i].entry.expandWidth && columnSizes[i] < columns[i].entry.maxWidth)
             {
                 expandableColumns.Add(i);
             }
         }
         float addWidth = (availableWidth - totalWidth) / expandableColumns.Count;
         foreach (int i in expandableColumns)
         {
             columnSizes[i] += addWidth;
         }
     }
     for (int i = 0; i < columns.Count; i++)
     {
         columnSizes[i] = Mathf.Clamp(columnSizes[i], columns[i].entry.minWidth, columns[i].entry.maxWidth);
     }
     if (reorderableList != null)
     {
         reorderableList.draggable = sortByColumnIndex < 0;
     }
 }
        public void CheckState(List <TableColumn> columns, GUITableEntry tableEntry, float availableWidth)
        {
            if (tableEntry.defaultColumnWidth > 0f)
            {
                columns.ForEach(c => { if (c.entry.defaultWidth == 0f)
                                       {
                                           c.entry.defaultWidth = tableEntry.defaultColumnWidth;
                                       }
                                });
            }

            if (columnSizes == null || columnSizes.Count != columns.Count)
            {
                columnSizes = columns.Select((column) => column.GetDefaultWidth()).ToList();
            }
            if (columnVisible == null || columnVisible.Count != columns.Count)
            {
                columnVisible = columns.Select((column) => column.entry.visibleByDefault).ToList();
            }
            if (totalWidth < availableWidth && !isBeingResized)
            {
                List <int> expandableColumns = new List <int> ();
                for (int i = 0; i < columns.Count; i++)
                {
                    if (columns[i].entry.expandWidth && columnSizes[i] < columns[i].entry.maxWidth)
                    {
                        expandableColumns.Add(i);
                    }
                }
                float addWidth = (availableWidth - totalWidth) / expandableColumns.Count;
                foreach (int i in expandableColumns)
                {
                    columnSizes[i] += addWidth;
                }
            }

            absoluteColumnSizes = columnSizes.Select((columnSize, i) =>
                                                     Mathf.Clamp(
                                                         columns[i].entry.relative ? columnSize * GUIHelpers.GetViewWidth() : columnSize,
                                                         columns[i].entry.minWidth,
                                                         columns[i].entry.maxWidth))
                                  .ToList();

            if (reorderableList != null)
            {
                reorderableList.draggable = sortByColumnIndex < 0;
            }
        }
Beispiel #6
0
 public static float TableHeight(GUITableEntry tableEntry, int nbRows)
 {
     return((tableEntry.rowHeight + (tableEntry.reorderable ? 5 : 0)) * Mathf.Max(1, nbRows)
            + EditorGUIUtility.singleLineHeight * (tableEntry.reorderable ? 2 : 1));
 }
Beispiel #7
0
        /// <summary>
        /// Draw a table completely manually.
        /// Each cell has to be created and given as parameter in cells.
        /// </summary>
        /// <returns>The updated table state.</returns>
        /// <param name="rect">The table's containing rectangle.</param>
        /// <param name="tableState">The Table state.</param>
        /// <param name="columns">The Columns of the table.</param>
        /// <param name="cells">The Cells as a list of rows.</param>
        /// <param name="collectionProperty">The SerializeProperty of the collection. This is useful for reorderable tables.</param>
        /// <param name="options">The table options.</param>
        public static GUITableState DrawTable(
            Rect rect,
            GUITableState tableState,
            List <TableColumn> columns,
            List <List <TableCell> > cells,
            SerializedProperty collectionProperty,
            params GUITableOption[] options)
        {
            GUITableEntry tableEntry = new GUITableEntry(options);

            if (tableState == null)
            {
                tableState = new GUITableState();
            }

            if (tableEntry.reorderable)
            {
                if (collectionProperty == null)
                {
                    Debug.LogError("The collection's serialized property is needed to draw a reorderable table.");
                    return(tableState);
                }

                staticCells = cells;

                if (tableState.reorderableList == null)
                {
                    ReorderableList list = new ReorderableList(
                        collectionProperty.serializedObject,
                        collectionProperty,
                        true, true, true, true);

                    list.drawElementCallback = (Rect r, int index, bool isActive, bool isFocused) => {
                        DrawLine(tableState, columns, orderedRows[index], r.xMin + (list.draggable ? 0 : 14), r.yMin, tableEntry.rowHeight);
                    };

                    list.elementHeight = tableEntry.rowHeight;

                    list.drawHeaderCallback = (Rect r) => {
                        DrawHeaders(r, tableState, columns, r.xMin + 12, r.yMin);
                    };

                    list.onRemoveCallback = (l) =>
                    {
                        l.serializedProperty.DeleteArrayElementAtIndex(staticCells.IndexOf(orderedRows[l.index]));
                    };

                    tableState.SetReorderableList(list);
                }
            }

            tableState.CheckState(columns, tableEntry, rect.width);

            orderedRows = cells;
            if (tableState.sortByColumnIndex >= 0)
            {
                if (tableState.sortIncreasing)
                {
                    orderedRows = cells.OrderBy(row => row [tableState.sortByColumnIndex]).ToList();
                }
                else
                {
                    orderedRows = cells.OrderByDescending(row => row [tableState.sortByColumnIndex]).ToList();
                }
            }

            if (tableEntry.reorderable)
            {
                tableState.reorderableList.DoList(new Rect(rect.x, rect.y, tableState.totalWidth + 23f, rect.height));
                collectionProperty.serializedObject.ApplyModifiedProperties();
                return(tableState);
            }


            float rowHeight = tableEntry.rowHeight;

            float currentX = rect.x;
            float currentY = rect.y + 5;

            bool displayScrollView = tableState.totalWidth > rect.width && tableEntry.allowScrollView;

            tableState.RightClickMenu(columns, rect);

            DrawHeaders(rect, tableState, columns, currentX - tableState.scrollPos.x, currentY);

            GUI.enabled = true;

            currentY += EditorGUIUtility.singleLineHeight;

            if (tableEntry.allowScrollView)
            {
                tableState.scrollPos = GUI.BeginScrollView(
                    new Rect(currentX, currentY, rect.width, TableHeight(tableEntry, cells.Count)),
                    tableState.scrollPos,
                    new Rect(0f, 0f, tableState.totalWidth, tableEntry.rowHeight * cells.Count));
                currentX = 0f;
                currentY = 0f;
            }

            if (orderedRows.Count == 0)
            {
                currentX = tableEntry.allowScrollView ? 0 : rect.x;
                GUIHelpers.DrawRect(new Rect(currentX, currentY, tableState.totalWidth, rowHeight), TABLE_BG_COLOR);
                GUI.Label(new Rect(currentX + 5, currentY, rect.width, rowHeight), "Collection is empty");
            }
            else
            {
                foreach (List <TableCell> row in orderedRows)
                {
                    currentX = tableEntry.allowScrollView ? 0 : rect.x;
                    DrawLine(tableState, columns, row, currentX, currentY, rowHeight);
                    currentY += rowHeight;
                }
            }

            GUI.enabled = true;

            if (tableEntry.allowScrollView)
            {
                GUI.EndScrollView();
            }

            tableState.Save();

            return(tableState);
        }
Beispiel #8
0
 public static float ExtraHeight(GUITableEntry tableEntry)
 {
     return(tableEntry.reorderable ? EditorGUIUtility.singleLineHeight : 0f);
 }
Beispiel #9
0
 public static float RowHeight(GUITableEntry tableEntry)
 {
     return(tableEntry.rowHeight + (tableEntry.reorderable ? 5 : 0));
 }
Beispiel #10
0
 public static float RowsHeight(GUITableEntry tableEntry, int nbRows)
 {
     return(RowHeight(tableEntry) * Mathf.Max(1, nbRows));
 }
Beispiel #11
0
 public static float HeadersHeight(GUITableEntry tableEntry)
 {
     return(tableEntry.rotateHeaders ? 70f : EditorGUIUtility.singleLineHeight);
 }
Beispiel #12
0
 public static float TableHeight(GUITableEntry tableEntry, int nbRows)
 {
     return(HeadersHeight(tableEntry) + RowsHeight(tableEntry, nbRows) + ExtraHeight(tableEntry));
 }