Ejemplo n.º 1
0
        public override void Draw(Batcher batcher, float parentAlpha)
        {
            // update our hoved item if the mouse is over the list
            if (_isMouseOverList)
            {
                var mousePos = ScreenToLocalCoordinates(stage.GetMousePosition());
                _hoveredItemIndex = GetItemIndexUnderMousePosition(mousePos);
            }

            Validate();

            var font             = _style.Font;
            var selectedDrawable = _style.Selection;

            var color = GetColor();

            color = new Color(color, (int)(color.A * parentAlpha));

            float x = GetX(), y = GetY(), width = GetWidth(), height = GetHeight();
            var   itemY = 0f;

            var background = _style.Background;

            if (background != null)
            {
                background.Draw(batcher, x, y, width, height, color);
                var leftWidth = background.LeftWidth;
                x     += leftWidth;
                itemY += background.TopHeight;
                width -= leftWidth + background.RightWidth;
            }

            var unselectedFontColor =
                new Color(_style.FontColorUnselected, (int)(_style.FontColorUnselected.A * parentAlpha));
            var selectedFontColor =
                new Color(_style.FontColorSelected, (int)(_style.FontColorSelected.A * parentAlpha));
            var   hoveredFontColor = new Color(_style.FontColorHovered, (int)(_style.FontColorHovered.A * parentAlpha));
            Color fontColor;

            for (var i = 0; i < _items.Count; i++)
            {
                if (!_cullingArea.HasValue ||
                    (itemY - _itemHeight <= _cullingArea.Value.Y + _cullingArea.Value.Height &&
                     itemY >= _cullingArea.Value.Y))
                {
                    var item     = _items[i];
                    var selected = _selection.Contains(item);
                    if (selected)
                    {
                        selectedDrawable.Draw(batcher, x, y + itemY, width, _itemHeight, color);
                        fontColor = selectedFontColor;
                    }
                    else if (i == _hoveredItemIndex && _style.HoverSelection != null)
                    {
                        _style.HoverSelection.Draw(batcher, x, y + itemY, width, _itemHeight, color);
                        fontColor = hoveredFontColor;
                    }
                    else
                    {
                        fontColor = unselectedFontColor;
                    }

                    var textPos = new Vector2(x + _textOffsetX, y + itemY + _textOffsetY);
                    batcher.DrawString(font, item.ToString(), textPos, fontColor);
                }
                else if (itemY < _cullingArea.Value.Y)
                {
                    break;
                }

                itemY += _itemHeight;
            }
        }