Beispiel #1
0
        public virtual void Draw(GraphicsHandler graphicsHandler)
        {
            // textbox itself (includes border)
            box.Draw(graphicsHandler);

            // setting scissor rectangle to the size of the whitespace in textbox
            // anything attempting to be drawn outside of scissor rectangle will be cut off
            // necessary to cut off text and such, since otherwise there's no way to only draw a partial letter of a spritefont
            graphicsHandler.SetScissorRectangle(new Rectangle(
                                                    (int)box.X + box.BorderThickness,
                                                    (int)box.Y + box.BorderThickness,
                                                    box.Width - box.BorderThickness,
                                                    box.Height - box.BorderThickness
                                                    ));

            // highlighting
            int highlightX     = StartLocationX + (HighlightStartIndex * fontCharLength) - ScrollOffset + (HighlightStartIndex * (int)font.Spacing);
            int highlightWidth = ((HighlightEndIndex - HighlightStartIndex) * fontCharLength) + ((HighlightEndIndex - HighlightStartIndex) * (int)font.Spacing);

            graphicsHandler.DrawFilledRectangle(new Rectangle(highlightX, StartLocationY, highlightWidth, EndLocationY - StartLocationY), HighlightColor);

            // text
            graphicsHandler.DrawString(font, Text, new Vector2(StartLocationX - ScrollOffset, box.Y), color: TextColor);

            // cursor
            if (showCursor && !IsTextHighlighted)
            {
                graphicsHandler.DrawLine(new Vector2(StartLocationX + CursorOffset - ScrollOffset, StartLocationY), new Vector2(StartLocationX + CursorOffset - ScrollOffset, EndLocationY), CursorColor);
            }

            graphicsHandler.RemoveScissorRectangle();

            // if text is highlighted, the text needs to be redrawn to be the highlight color
            if (IsTextHighlighted)
            {
                int highlightTextX     = StartLocationX + (HighlightStartIndex * fontCharLength) - ScrollOffset + (HighlightStartIndex * (int)font.Spacing);
                int highlightTextWidth = ((HighlightEndIndex - HighlightStartIndex) * fontCharLength) + ((HighlightEndIndex - HighlightStartIndex) * (int)font.Spacing);
                if (highlightTextX + highlightTextWidth > box.X + box.Width - box.BorderThickness)
                {
                    highlightTextWidth = (int)box.X + box.Width - highlightTextX;
                }
                if (highlightTextX < (int)box.X + box.BorderThickness)
                {
                    int difference = (int)box.X + box.BorderThickness - highlightTextX;
                    highlightTextX      = (int)box.X + box.BorderThickness;
                    highlightTextWidth -= difference;
                }

                // redraw the spritefont text only where the text is highlighted
                graphicsHandler.SetScissorRectangle(new Rectangle(highlightTextX, StartLocationY, highlightTextWidth, EndLocationY - StartLocationY));

                // draw text in highlighted color
                graphicsHandler.DrawString(font, Text, new Vector2(StartLocationX - ScrollOffset, box.Y), color: HighlightTextColor);

                graphicsHandler.RemoveScissorRectangle();
            }
        }