}
}
}
private void drawSelection(Canvas canvas) {
Font font = canvas.getCurrentStyle().getFont();
String currentText = getText();
int start = Math.min(cursorPosition, selectionStart);
int end = Math.max(cursorPosition, selectionStart);
Color textColor = canvas.getCurrentStyle().getTextColor();
int canvasWidth = (multiline) ? canvas.size().x : Integer.MAX_VALUE;
// TODO: Support different text alignments
List<String> rawLinesAfterCursor = TextLineBuilder.getLines(font, currentText, Integer.MAX_VALUE);
int currentChar = 0;
int lineOffset = 0;
for (int lineIndex = 0; lineIndex < rawLinesAfterCursor.size() && currentChar <= end; ++lineIndex) {
String line = rawLinesAfterCursor.get(lineIndex);
List<String> innerLines = TextLineBuilder.getLines(font, line, canvasWidth);
for (int innerLineIndex = 0; innerLineIndex < innerLines.size() && currentChar <= end; ++innerLineIndex) {
String innerLine = innerLines.get(innerLineIndex);
String selectionString;
int offsetX = 0;
if (currentChar + innerLine.length() < start) {
selectionString = "";
} else if (currentChar < start) {
offsetX = font.getWidth(innerLine.substring(0, start - currentChar));
selectionString = innerLine.substring(start - currentChar, Math.min(end - currentChar, innerLine.length()));
} else if (currentChar + innerLine.length() >= end) {
selectionString = innerLine.substring(0, end - currentChar);
} else {
selectionString = innerLine;
}
if (!selectionString.isEmpty()) {
int selectionWidth = font.getWidth(selectionString);
Vector2i selectionTopLeft = new Vector2i(offsetX, (lineOffset) * font.getLineHeight());
Rect2i region = Rect2i.createFromMinAndSize(selectionTopLeft.x, selectionTopLeft.y, selectionWidth, font.getLineHeight());
canvas.drawTexture(cursorTexture, region, textColor);
canvas.drawTextRaw(FontColor.stripColor(selectionString), font, textColor.inverse(), region);
}
currentChar += innerLine.length();