Skip to content

Commit c82f6ca

Browse files
committed
fix textfield cursor offset on click
1 parent 7527553 commit c82f6ca

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/main/java/com/cleanroommc/modularui/widgets/textfield/TextFieldRenderer.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,23 @@ public Point getCursorPos(List<String> lines, int x, int y) {
9595
return new Point();
9696
}
9797
List<Line> measuredLines = measureLines(lines);
98-
y -= getStartY(measuredLines.size()) + this.y;
98+
y -= getStartY(measuredLines.size());
9999
int index = (int) (y / (getFontHeight()));
100100
if (index < 0) return new Point();
101101
if (index >= measuredLines.size())
102102
return new Point(measuredLines.get(measuredLines.size() - 1).getText().length(), measuredLines.size() - 1);
103103
Line line = measuredLines.get(index);
104-
x -= getStartX(line.getWidth()) + this.x;
104+
x -= getStartX(line.getWidth());
105105
if (line.getWidth() <= 0) return new Point(0, index);
106106
if (line.getWidth() < x) return new Point(line.getText().length(), index);
107107
float currentX = 0;
108108
for (int i = 0; i < line.getText().length(); i++) {
109109
char c = line.getText().charAt(i);
110-
currentX += getFontRenderer().getCharWidth(c) * this.scale;
110+
float charWidth = getFontRenderer().getCharWidth(c) * this.scale;
111+
currentX += charWidth;
111112
if (currentX >= x) {
113+
// dist with current letter < dist without current letter -> next letter pos
114+
if (Math.abs(currentX - x) < Math.abs(currentX - charWidth - x)) i++;
112115
return new Point(i, index);
113116
}
114117
}

0 commit comments

Comments
 (0)