Beispiel #1
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;
            float requiredWidth  = tableEntry.allowScrollView ? GUIHelpers.GetViewWidth() : tableState.totalWidth;

            if (tableEntry.allowScrollView && tableState.totalWidth > GUIHelpers.GetViewWidth() + 21)
            {
                requiredHeight += EditorGUIUtility.singleLineHeight;
            }

            Rect position = GUILayoutUtility.GetRect(requiredWidth, requiredHeight);

            if (Event.current.type == EventType.Layout)
            {
                return(tableState);
            }
            else
            {
                return(GUITable.DrawTable(position, tableState, columns, cells, collectionProperty, options));
            }
        }
        public void ResizeColumn(List <TableColumn> columns, int indexColumn, float currentX, Rect?position = null)
        {
            int  controlId = EditorGUIUtility.GetControlID(FocusType.Passive);
            Rect resizeRect;

            if (position == null)
            {
                resizeRect = new Rect(currentX + absoluteColumnSizes[indexColumn] + 2, 0, 10, EditorGUIUtility.singleLineHeight);
            }
            else
            {
                resizeRect = new Rect(currentX + absoluteColumnSizes[indexColumn] + 2, position.Value.y, 10, EditorGUIUtility.singleLineHeight);
            }
            EditorGUIUtility.AddCursorRect(resizeRect, MouseCursor.ResizeHorizontal, controlId);
            switch (Event.current.type)
            {
            case EventType.MouseDown:
            {
                if (resizeRect.Contains(Event.current.mousePosition))
                {
                    GUIUtility.hotControl = controlId;
                    Event.current.Use();
                    isBeingResized = true;
                }
                break;
            }

            case EventType.MouseDrag:
            {
                if (GUIUtility.hotControl == controlId)
                {
                    float absoluteColumnNewSize = Event.current.mousePosition.x - currentX - 5;
                    columnSizes[indexColumn] = columns[indexColumn].entry.relative ?
                                               absoluteColumnNewSize / GUIHelpers.GetViewWidth() :
                                               absoluteColumnNewSize;
                    Event.current.Use();
                    isBeingResized = true;
                }
                break;
            }

            case EventType.MouseUp:
            {
                if (GUIUtility.hotControl == controlId)
                {
                    GUIUtility.hotControl = 0;
                    Event.current.Use();
                    isBeingResized = false;
                }
                break;
            }
            }
        }
        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;
            }
        }