aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLulonaut <67191924+Lulonaut@users.noreply.github.com>2022-06-01 08:32:09 +0200
committerGitHub <noreply@github.com>2022-06-01 08:32:09 +0200
commit1c3fe03bf83f1772993de88434accde970863b77 (patch)
tree47d3aa9f24e635786204f0294cc1d7aa193a7557 /src
parentdfdc2d9519556dfec528ab9d1732cdf32aab0639 (diff)
downloadNotEnoughUpdates-1c3fe03bf83f1772993de88434accde970863b77.tar.gz
NotEnoughUpdates-1c3fe03bf83f1772993de88434accde970863b77.tar.bz2
NotEnoughUpdates-1c3fe03bf83f1772993de88434accde970863b77.zip
Override selected text when pasting in Item editor (#150)
* replace selected text * crash when selection starts from the left * cursor pos crash
Diffstat (limited to 'src')
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/itemeditor/GuiElementTextField.java25
1 files changed, 20 insertions, 5 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/itemeditor/GuiElementTextField.java b/src/main/java/io/github/moulberry/notenoughupdates/itemeditor/GuiElementTextField.java
index aa88cb77..f37f2fa1 100644
--- a/src/main/java/io/github/moulberry/notenoughupdates/itemeditor/GuiElementTextField.java
+++ b/src/main/java/io/github/moulberry/notenoughupdates/itemeditor/GuiElementTextField.java
@@ -158,7 +158,12 @@ public class GuiElementTextField extends GuiElement {
String textNC = textNoColour.substring(0, cursorIndex);
int colorCodes = StringUtils.countMatches(textNC, "\u00B6");
- String line = text.substring(cursorIndex + (((options & COLOUR) != 0) ? colorCodes * 2 : 0)).split("\n")[0];
+ String[] lines = text.substring(cursorIndex + (((options & COLOUR) != 0) ? colorCodes * 2 : 0)).split("\n");
+ if (lines.length < 1) {
+ return 0;
+ }
+ String line = lines[0];
+
int padding = Math.min(5, searchBarXSize - strLenNoColor(line)) / 2;
String trimmed = Minecraft.getMinecraft().fontRendererObj.trimStringToWidth(line, xComp - padding);
int linePos = strLenNoColor(trimmed);
@@ -213,16 +218,26 @@ public class GuiElementTextField extends GuiElement {
//allows for pasting formatted text that includes "§"
if (GuiScreen.isKeyComboCtrlV(keyCode)) {
textField.setEnabled(false);
- //The cursor position gets set to the end when using setText
- int oldCursorPosition = textField.getCursorPosition();
+
+ int selectionEnd = textField.getSelectionEnd();
+ int cursorPosition = textField.getCursorPosition();
+
+ if (cursorPosition < selectionEnd) {
+ //swap selectionEnd and cursorPosition
+ selectionEnd = selectionEnd ^ cursorPosition;
+ cursorPosition = selectionEnd ^ cursorPosition;
+ selectionEnd = selectionEnd ^ cursorPosition;
+ }
String clipboardContent = GuiScreen.getClipboardString();
+
StringBuilder stringBuilder = new StringBuilder(getText())
- .insert(textField.getCursorPosition(), clipboardContent);
+ .replace(selectionEnd, cursorPosition, "")
+ .insert(selectionEnd, clipboardContent);
//writeText removes unwanted chars from the String which includes "§"
textField.setText(stringBuilder.toString());
- textField.setCursorPosition(oldCursorPosition + clipboardContent.length());
+ textField.setCursorPosition(selectionEnd + clipboardContent.length());
} else {
textField.setEnabled(true);
}