Example #1
0
        private void HandleListItemEvents(int itemTotalCount, Rect screenRect)
        {
            var mousePosition = Event.current.mousePosition - new Vector2(0, screenRect.y);

            if (AutoComplete.IsHovered(mousePosition))
            {
                return;
            }

            if (Event.current.type == EventType.MouseDown && Event.current.button == 0)
            {
                var clickedItemIndex = (int)(mousePosition.y / m_ItemRowHeight);
                if (clickedItemIndex >= 0 && clickedItemIndex < itemTotalCount)
                {
                    HandleMouseDown(clickedItemIndex);
                }
            }
            else if (Event.current.type == EventType.MouseUp || IsDragClicked(Event.current))
            {
                var clickedItemIndex = (int)(mousePosition.y / m_ItemRowHeight);
                HandleMouseUp(clickedItemIndex, itemTotalCount);
            }
            else if (Event.current.type == EventType.MouseDrag && m_PrepareDrag)
            {
                var dragIndex = (int)(mousePosition.y / m_ItemRowHeight);
                HandleMouseDrag(dragIndex, itemTotalCount);
            }
        }
        public override void Draw(Rect screenRect, ICollection <int> selection)
        {
            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;

            scrollbarVisible = gridHeight > availableHeight;
            if (scrollbarVisible)
            {
                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    = !AutoComplete.IsHovered(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();
        }