Skip to content

Commit f3c8183

Browse files
committed
Fix text filtering (#122)
1 parent edc2bd1 commit f3c8183

1 file changed

Lines changed: 38 additions & 45 deletions

File tree

src/main/java/com/ldtteam/blockui/controls/TextField.java

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@
33
import com.ldtteam.blockui.BOGuiGraphics;
44
import com.ldtteam.blockui.Pane;
55
import com.ldtteam.blockui.PaneParams;
6+
import com.ldtteam.blockui.mod.Log;
67
import com.ldtteam.blockui.util.cursor.Cursor;
78
import com.ldtteam.blockui.views.View;
8-
import com.mojang.blaze3d.vertex.*;
9-
import org.joml.Matrix4f;
109
import com.mojang.blaze3d.platform.GlStateManager.LogicOp;
1110
import com.mojang.blaze3d.systems.RenderSystem;
11+
import com.mojang.blaze3d.vertex.BufferBuilder;
12+
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
13+
import com.mojang.blaze3d.vertex.Tesselator;
14+
import com.mojang.blaze3d.vertex.VertexFormat;
1215
import net.minecraft.client.gui.screens.Screen;
1316
import net.minecraft.client.renderer.GameRenderer;
1417
import net.minecraft.util.Mth;
1518
import org.jetbrains.annotations.Nullable;
19+
import org.joml.Matrix4f;
1620
import org.lwjgl.glfw.GLFW;
1721

1822
/**
@@ -22,22 +26,22 @@ public class TextField extends Pane
2226
{
2327
protected InputHandler handler;
2428

25-
private static final int RECT_COLOR = -3_092_272;
26-
private static final int DEFAULT_MAX_TEXT_LENGTH = 32;
29+
private static final int RECT_COLOR = -3_092_272;
30+
private static final int DEFAULT_MAX_TEXT_LENGTH = 32;
2731
// Attributes
28-
protected int maxTextLength = DEFAULT_MAX_TEXT_LENGTH;
29-
protected int textColor = 0xE0E0E0;
30-
protected int textColorDisabled = 0x707070;
31-
protected boolean shadow = true;
32+
protected int maxTextLength = DEFAULT_MAX_TEXT_LENGTH;
33+
protected int textColor = 0xE0E0E0;
34+
protected int textColorDisabled = 0x707070;
35+
protected boolean shadow = true;
3236
@Nullable
33-
protected String tabNextPaneID = null;
37+
protected String tabNextPaneID = null;
3438
// Runtime
35-
protected String text = "";
36-
protected Filter filter;
37-
protected int cursorPosition = 0;
38-
protected int scrollOffset = 0;
39-
protected int selectionEnd = 0;
40-
protected int cursorBlinkCounter = 0;
39+
protected String text = "";
40+
protected Filter filter;
41+
protected int cursorPosition = 0;
42+
protected int scrollOffset = 0;
43+
protected int selectionEnd = 0;
44+
protected int cursorBlinkCounter = 0;
4145

4246
/**
4347
* Simple public constructor to instantiate.
@@ -198,7 +202,7 @@ else if (selectionEnd <= scrollOffset)
198202
}
199203
}
200204

201-
public String getSelectedText()
205+
public String getSelectedText()
202206
{
203207
final int start = Math.min(cursorPosition, selectionEnd);
204208
final int end = Math.max(cursorPosition, selectionEnd);
@@ -500,11 +504,20 @@ public void onUpdate()
500504
/**
501505
* Write text into the field.
502506
*
503-
* @param str the string to write.
507+
* @param input the string to write.
504508
*/
505-
public void writeText(final String str)
509+
public void writeText(String input)
506510
{
507-
final String filteredStr = filter.filter(str);
511+
final char[] chars = input.toCharArray();
512+
final StringBuilder strBuilder = new StringBuilder();
513+
for (final char aChar : chars)
514+
{
515+
if (filter.isAllowedCharacter(aChar))
516+
{
517+
strBuilder.append(aChar);
518+
}
519+
}
520+
input = strBuilder.toString();
508521

509522
final int insertAt = Math.min(cursorPosition, selectionEnd);
510523
final int insertEnd = Math.max(cursorPosition, selectionEnd);
@@ -515,30 +528,10 @@ public void writeText(final String str)
515528
return;
516529
}
517530

518-
final StringBuilder resultBuffer = new StringBuilder();
519-
if (text.length() > 0 && insertAt > 0)
520-
{
521-
resultBuffer.append(text.substring(0, insertAt));
522-
}
523-
524-
final int insertedLength;
525-
if (availableChars < filteredStr.length())
526-
{
527-
resultBuffer.append(filteredStr.substring(0, availableChars));
528-
insertedLength = availableChars;
529-
}
530-
else
531-
{
532-
resultBuffer.append(filteredStr);
533-
insertedLength = filteredStr.length();
534-
}
535-
536-
if (text.length() > 0 && insertEnd < text.length())
537-
{
538-
resultBuffer.append(text.substring(insertEnd));
539-
}
540-
541-
text = resultBuffer.toString();
531+
String filteredText = filter.filter(text.substring(0, insertAt) + input + text.substring(insertEnd));
532+
filteredText = (filteredText.substring(0, Math.min(filteredText.length(), maxTextLength)));
533+
final int insertedLength = filteredText.length() - text.length();
534+
text = filteredText;
542535
moveCursorBy((insertAt - selectionEnd) + insertedLength);
543536

544537
triggerHandler();
@@ -610,7 +603,7 @@ public void deleteFromCursor(final int count)
610603
final boolean backwards = count < 0;
611604
final int start = backwards ? (this.cursorPosition + count) : this.cursorPosition;
612605
final int end = backwards ? this.cursorPosition : (this.cursorPosition + count);
613-
String result = "";
606+
String result = "";
614607

615608
if (start > 0)
616609
{
@@ -622,7 +615,7 @@ public void deleteFromCursor(final int count)
622615
result = result + text.substring(end);
623616
}
624617

625-
text = result;
618+
text = filter.filter(result);
626619

627620
if (backwards)
628621
{

0 commit comments

Comments
 (0)