Esempio n. 1
0
        protected override void Draw(Rect screenRect, ICollection <int> selection, ref bool focusSelectedItem)
        {
            float itemWidth  = itemSize + itemPadding * 2;
            float itemHeight = itemSize + itemLabelHeight + itemLabelTopPadding + itemPadding * 2;

            var gridWidth       = screenRect.width;
            var itemCount       = items.Count;
            int columnCount     = (int)(gridWidth / itemWidth);
            int lineCount       = Mathf.CeilToInt(itemCount / (float)columnCount);
            var gridHeight      = lineCount * itemHeight - Styles.statusLabel.fixedHeight;
            var availableHeight = screenRect.height;

            if (gridHeight > availableHeight)
            {
                gridWidth  -= Styles.scrollbar.fixedWidth;
                columnCount = (int)(gridWidth / itemWidth);
                lineCount   = Mathf.CeilToInt(itemCount / (float)columnCount);
                gridHeight  = lineCount * itemHeight;
            }

            var spaceBetweenTiles = (gridWidth - (columnCount * itemWidth)) / (columnCount + 1f);

            var viewRect = screenRect; viewRect.width = gridWidth; viewRect.height = gridHeight;

            m_ScrollPosition = GUI.BeginScrollView(screenRect, m_ScrollPosition, viewRect);

            Rect gridRect = new Rect(0, screenRect.y + m_ScrollPosition.y, gridWidth, availableHeight);
            Rect itemRect = new Rect(spaceBetweenTiles, screenRect.y, itemWidth, itemHeight);

            var evt            = Event.current;
            int index          = 0;
            int selectionIndex = selection.Count == 0 ? -1 : selection.Last();
            var eventType      = evt.type;
            var mouseButton    = evt.button;
            var mousePosition  = evt.mousePosition;
            var isHoverGrid    = !SearchField.IsAutoCompleteHovered(evt.mousePosition);

            isHoverGrid &= gridRect.Contains(mousePosition);

            foreach (var item in items)
            {
                if (evt.type == EventType.Repaint && index == selectionIndex && focusSelectedItem)
                {
                    FocusGridItemRect(itemRect, screenRect);
                    focusSelectedItem = false;
                }

                if (itemRect.Overlaps(gridRect))
                {
                    if (evt.isMouse && !isHoverGrid)
                    {
                        // Skip
                    }
                    else if (eventType == EventType.MouseDown && mouseButton == 0)
                    {
                        if (itemRect.Contains(mousePosition))
                        {
                            HandleMouseDown(index);
                        }
                    }
                    else if (evt.type == EventType.MouseUp || IsDragClicked(evt))
                    {
                        if (itemRect.Contains(mousePosition))
                        {
                            HandleMouseUp(index, itemCount);
                            if (index == selectionIndex)
                            {
                                focusSelectedItem = true;
                            }
                        }
                    }
                    else if (eventType == EventType.MouseDrag && m_PrepareDrag)
                    {
                        if (itemRect.Contains(mousePosition))
                        {
                            HandleMouseDrag(index, itemCount);
                        }
                    }
                    else if (eventType == EventType.Repaint)
                    {
                        DrawGridItem(index, item, itemRect, isHoverGrid, selection, evt);
                    }
                    else
                    {
                        item.preview = null;
                    }
                }

                itemRect = new Rect(itemRect.x + itemWidth + spaceBetweenTiles, itemRect.y, itemWidth, itemHeight);
                if (itemRect.xMax > gridWidth)
                {
                    itemRect = new Rect(spaceBetweenTiles, itemRect.y + itemHeight, itemRect.width, itemRect.height);
                }

                ++index;
            }

            GUI.EndScrollView();
        }