Skip to content

Commit 38b738c

Browse files
committed
Fix other getCursorPos cases
1 parent 4fc3fd9 commit 38b738c

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public Point getCursorPos(List<String> lines, int x, int y) {
4646
}
4747

4848
@Override
49-
protected boolean isIgnoredChar(char c) {
49+
protected boolean isIgnoredChar(int c) {
5050
return c == groupingSeparator;
5151
}
5252

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,15 @@ public Point getCursorPos(List<String> lines, int x, int y) {
103103
y -= getStartY(measuredLines.size());
104104
int index = (int) (y / (getFontHeight()));
105105
if (index < 0) return new Point();
106-
if (index >= measuredLines.size())
107-
return new Point(measuredLines.get(measuredLines.size() - 1).getText().length(), measuredLines.size() - 1);
106+
if (index >= measuredLines.size()) {
107+
return new Point(getRealLength(measuredLines.get(measuredLines.size() - 1).getText()), measuredLines.size() - 1);
108+
}
108109
Line line = measuredLines.get(index);
109110
x -= getStartX(line.getWidth());
110111
if (line.getWidth() <= 0) return new Point(0, index);
111-
if (line.getWidth() < x) return new Point(line.getText().length(), index);
112+
if (line.getWidth() < x) {
113+
return new Point(getRealLength(line.getText()), index);
114+
}
112115
float currentX = 0;
113116
int ignoredChars = 0;
114117
for (int i = 0; i < line.getText().length(); i++) {
@@ -129,10 +132,14 @@ public Point getCursorPos(List<String> lines, int x, int y) {
129132
* Whether the given character should be ignored for cursor positioning purposes
130133
*/
131134
@ApiStatus.Experimental
132-
protected boolean isIgnoredChar(char c) {
135+
protected boolean isIgnoredChar(int c) {
133136
return false;
134137
}
135138

139+
private int getRealLength(String text) {
140+
return text.length() - (int) text.chars().filter(this::isIgnoredChar).count();
141+
}
142+
136143
public Point2D.Float getPosOf(List<Line> measuredLines, Point cursorPos) {
137144
if (measuredLines.isEmpty()) {
138145
return new Point2D.Float(getStartX(0), getStartYOfLines(1));

0 commit comments

Comments
 (0)