From 170225262975fb696fd8ca7133705d675e13babe Mon Sep 17 00:00:00 2001 From: bigguy345 Date: Sat, 20 Dec 2025 00:31:30 +0200 Subject: [PATCH] Fixed resizing game window/gui initi'ing newly creating the text editor --- .../noppes/npcs/client/gui/GuiScript.java | 14 +- .../client/gui/script/GuiScriptEffect.java | 14 +- .../client/gui/script/GuiScriptInterface.java | 16 +- .../gui/script/GuiScriptLinkedItem.java | 13 +- .../client/gui/script/GuiScriptRecipe.java | 13 +- .../client/gui/util/GuiScriptTextArea.java | 175 +++++++++--------- 6 files changed, 137 insertions(+), 108 deletions(-) diff --git a/src/main/java/noppes/npcs/client/gui/GuiScript.java b/src/main/java/noppes/npcs/client/gui/GuiScript.java index ea9941b69..8b0d7d24a 100644 --- a/src/main/java/noppes/npcs/client/gui/GuiScript.java +++ b/src/main/java/noppes/npcs/client/gui/GuiScript.java @@ -44,6 +44,8 @@ public class GuiScript extends GuiNPCInterface implements IGuiData, GuiYesNoCall private static int activeConsole = 0; boolean loaded = false; + private GuiScriptTextArea textArea; + public GuiScript(EntityNPCInterface npc) { super(npc); script = npc.script; @@ -96,10 +98,14 @@ public class GuiScript extends GuiNPCInterface implements IGuiData, GuiYesNoCall ScriptContainer container = script.getNPCScript(activeTab); - GuiScriptTextArea ta = new GuiScriptTextArea(this, 2, guiLeft + 74, guiTop + 4, 239, 208, container == null ? "" : container.script); - ta.enableCodeHighlighting(); - ta.setListener(this); - this.addTextField(ta); + if (textArea == null) + textArea = new GuiScriptTextArea(this, 2, guiLeft + 74, guiTop + 4, 239, 208, + container == null ? "" : container.script); + else + textArea.init(guiLeft + 74, guiTop + 4, 239, 208, container == null ? "" : container.script); + textArea.enableCodeHighlighting(); + textArea.setListener(this); + this.addTextField(textArea); addButton(new GuiNpcButton(102, guiLeft + 315, guiTop + 4, 50, 20, "gui.clear")); addButton(new GuiNpcButton(101, guiLeft + 366, guiTop + 4, 50, 20, "gui.paste")); diff --git a/src/main/java/noppes/npcs/client/gui/script/GuiScriptEffect.java b/src/main/java/noppes/npcs/client/gui/script/GuiScriptEffect.java index 33582ad88..f3a4d219d 100644 --- a/src/main/java/noppes/npcs/client/gui/script/GuiScriptEffect.java +++ b/src/main/java/noppes/npcs/client/gui/script/GuiScriptEffect.java @@ -48,6 +48,9 @@ public class GuiScriptEffect extends GuiNPCInterface implements GuiYesNoCallback private final EffectScript scriptHandler; boolean loaded = false; + private GuiScriptTextArea textArea; + + public GuiScriptEffect(GuiNPCManageEffects parent, CustomEffect effect) { this.drawDefaultBackground = true; this.closeOnEsc = true; @@ -109,10 +112,13 @@ public class GuiScriptEffect extends GuiNPCInterface implements GuiYesNoCallback hookLabel.color = 11184810; this.addLabel(hookLabel); ScriptContainer container = this.scriptHandler.container; - GuiScriptTextArea ta = new GuiScriptTextArea(this, 2, this.guiLeft + 1 + yoffset, this.guiTop + yoffset, this.xSize - 108 - yoffset, (int) ((double) this.ySize * 0.96) - yoffset * 2, container == null ? "" : container.script); - ta.enableCodeHighlighting(); - ta.setListener(this); - this.addTextField(ta); + if (textArea == null) + textArea = new GuiScriptTextArea(this, 2, this.guiLeft + 1 + yoffset, this.guiTop + yoffset, this.xSize - 108 - yoffset, (int) ((double) this.ySize * 0.96) - yoffset * 2, container == null ? "" : container.script); + else + textArea.init(this.guiLeft + 1 + yoffset, this.guiTop + yoffset, this.xSize - 108 - yoffset, (int) ((double) this.ySize * 0.96) - yoffset * 2, container == null ? "" : container.script); + textArea.enableCodeHighlighting(); + textArea.setListener(this); + this.addTextField(textArea); int left1 = this.guiLeft + this.xSize - 104; this.addButton(new GuiNpcButton(102, left1, this.guiTop + yoffset, 60, 20, "gui.clear")); this.addButton(new GuiNpcButton(101, left1 + 61, this.guiTop + yoffset, 60, 20, "gui.paste")); diff --git a/src/main/java/noppes/npcs/client/gui/script/GuiScriptInterface.java b/src/main/java/noppes/npcs/client/gui/script/GuiScriptInterface.java index 185eb90fd..4afa22144 100644 --- a/src/main/java/noppes/npcs/client/gui/script/GuiScriptInterface.java +++ b/src/main/java/noppes/npcs/client/gui/script/GuiScriptInterface.java @@ -44,6 +44,7 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb public List hookList = new ArrayList(); protected boolean loaded = false; + private GuiScriptTextArea textArea; public GuiScriptInterface() { this.drawDefaultBackground = true; this.closeOnEsc = true; @@ -108,11 +109,18 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb addLabel(hookLabel); ScriptContainer container = (ScriptContainer) this.handler.getScripts().get(this.activeTab - 1); - GuiScriptTextArea ta = new GuiScriptTextArea(this, 2, guiLeft + 1 + yoffset, guiTop + yoffset, xSize - 108 - yoffset, (int) (ySize * 0.96) - yoffset * 2, container == null ? "" : container.script); - ta.enableCodeHighlighting(); - ta.setListener(this); + if (textArea == null) + textArea = new GuiScriptTextArea(this, 2, guiLeft + 1 + yoffset, guiTop + yoffset, + xSize - 108 - yoffset, (int) (ySize * 0.96) - yoffset * 2, + container == null ? "" : container.script); + else + textArea.init(guiLeft + 1 + yoffset, guiTop + yoffset, xSize - 108 - yoffset, + (int) (ySize * 0.96) - yoffset * 2, container == null ? "" : container.script); - this.addTextField(ta); + textArea.enableCodeHighlighting(); + textArea.setListener(this); + + this.addTextField(textArea); int left1 = this.guiLeft + this.xSize - 104; this.addButton(new GuiNpcButton(102, left1, this.guiTop + yoffset, 60, 20, "gui.clear")); diff --git a/src/main/java/noppes/npcs/client/gui/script/GuiScriptLinkedItem.java b/src/main/java/noppes/npcs/client/gui/script/GuiScriptLinkedItem.java index 664c08d1f..a6803d2c4 100644 --- a/src/main/java/noppes/npcs/client/gui/script/GuiScriptLinkedItem.java +++ b/src/main/java/noppes/npcs/client/gui/script/GuiScriptLinkedItem.java @@ -49,6 +49,8 @@ public class GuiScriptLinkedItem extends GuiNPCInterface implements GuiYesNoCall private final LinkedItemScript scriptHandler; boolean loaded = false; + private GuiScriptTextArea textArea; + public GuiScriptLinkedItem(GuiNPCManageLinked parent, LinkedItem linkedItem) { this.drawDefaultBackground = true; this.closeOnEsc = true; @@ -121,10 +123,13 @@ public class GuiScriptLinkedItem extends GuiNPCInterface implements GuiYesNoCall hookLabel.color = 11184810; this.addLabel(hookLabel); ScriptContainer container = this.scriptHandler.container; - GuiScriptTextArea ta = new GuiScriptTextArea(this, 2, this.guiLeft + 1 + yoffset, this.guiTop + yoffset, this.xSize - 108 - yoffset, (int) ((double) this.ySize * 0.96) - yoffset * 2, container == null ? "" : container.script); - ta.enableCodeHighlighting(); - ta.setListener(this); - this.addTextField(ta); + if (textArea == null) + textArea = new GuiScriptTextArea(this, 2, this.guiLeft + 1 + yoffset, this.guiTop + yoffset, this.xSize - 108 - yoffset, (int) ((double) this.ySize * 0.96) - yoffset * 2, container == null ? "" : container.script); + else + textArea.init(this.guiLeft + 1 + yoffset, this.guiTop + yoffset, this.xSize - 108 - yoffset, (int) ((double) this.ySize * 0.96) - yoffset * 2, container == null ? "" : container.script); + textArea.enableCodeHighlighting(); + textArea.setListener(this); + this.addTextField(textArea); int left1 = this.guiLeft + this.xSize - 104; this.addButton(new GuiNpcButton(102, left1, this.guiTop + yoffset, 60, 20, "gui.clear")); this.addButton(new GuiNpcButton(101, left1 + 61, this.guiTop + yoffset, 60, 20, "gui.paste")); diff --git a/src/main/java/noppes/npcs/client/gui/script/GuiScriptRecipe.java b/src/main/java/noppes/npcs/client/gui/script/GuiScriptRecipe.java index bb93052d7..fd93d8fe3 100644 --- a/src/main/java/noppes/npcs/client/gui/script/GuiScriptRecipe.java +++ b/src/main/java/noppes/npcs/client/gui/script/GuiScriptRecipe.java @@ -50,6 +50,8 @@ public class GuiScriptRecipe extends GuiNPCInterface implements GuiYesNoCallback private final RecipeScript scriptHandler; boolean loaded = false; + private GuiScriptTextArea textArea; + public GuiScriptRecipe(GuiNpcManageRecipes parent, RecipeCarpentry recipe) { this.drawDefaultBackground = true; this.closeOnEsc = true; @@ -129,10 +131,13 @@ public class GuiScriptRecipe extends GuiNPCInterface implements GuiYesNoCallback hookLabel.color = 11184810; this.addLabel(hookLabel); ScriptContainer container = this.scriptHandler.container; - GuiScriptTextArea ta = new GuiScriptTextArea(this, 2, this.guiLeft + 1 + yoffset, this.guiTop + yoffset, this.xSize - 108 - yoffset, (int) ((double) this.ySize * 0.96) - yoffset * 2, container == null ? "" : container.script); - ta.enableCodeHighlighting(); - ta.setListener(this); - this.addTextField(ta); + if (textArea == null) + textArea = new GuiScriptTextArea(this, 2, this.guiLeft + 1 + yoffset, this.guiTop + yoffset, this.xSize - 108 - yoffset, (int) ((double) this.ySize * 0.96) - yoffset * 2, container == null ? "" : container.script); + else + textArea.init(this.guiLeft + 1 + yoffset, this.guiTop + yoffset, this.xSize - 108 - yoffset, (int) ((double) this.ySize * 0.96) - yoffset * 2, container == null ? "" : container.script); + textArea.enableCodeHighlighting(); + textArea.setListener(this); + this.addTextField(textArea); int left1 = this.guiLeft + this.xSize - 104; this.addButton(new GuiNpcButton(102, left1, this.guiTop + yoffset, 60, 20, "gui.clear")); this.addButton(new GuiNpcButton(101, left1 + 61, this.guiTop + yoffset, 60, 20, "gui.paste")); diff --git a/src/main/java/noppes/npcs/client/gui/util/GuiScriptTextArea.java b/src/main/java/noppes/npcs/client/gui/util/GuiScriptTextArea.java index c1e8e44f9..d68735f91 100644 --- a/src/main/java/noppes/npcs/client/gui/util/GuiScriptTextArea.java +++ b/src/main/java/noppes/npcs/client/gui/util/GuiScriptTextArea.java @@ -36,11 +36,8 @@ import static net.minecraft.client.gui.GuiScreen.isCtrlKeyDown; public class GuiScriptTextArea extends GuiNpcTextField { // ==================== DIMENSIONS & POSITION ==================== - public int id; public int x; public int y; - public int width; - public int height; // ==================== STATE FLAGS ==================== public boolean active = false; @@ -85,15 +82,81 @@ public class GuiScriptTextArea extends GuiNpcTextField { public GuiScriptTextArea(GuiScreen guiScreen, int id, int x, int y, int width, int height, String text) { super(id, guiScreen, x, y, width, height, null); - this.id = id; - this.x = x; - this.y = y; - this.width = width; - this.height = height; - this.undoing = true; - this.setText(text); - this.undoing = false; - + + searchBar.setCallback(new SearchReplaceBar.SearchCallback() { + @Override + public String getText() { + return text; + } + + @Override + public void setText(String newText) { + GuiScriptTextArea.this.setText(newText); + } + + @Override + public void scrollToPosition(int position) { + // Find line containing position and scroll to it + if (container == null || container.lines == null) return; + + // Calculate offset to account for search bar height + int searchBarOffset = searchBar.getTotalHeight(); + int effectiveHeight = height - searchBarOffset; + int visibleLines = effectiveHeight / container.lineHeight; + // Calculate how many lines the search bar covers + int linesHiddenBySRB = searchBarOffset > 0 ? (int) Math.ceil((double) searchBarOffset / container.lineHeight) : 0; + + for (int i = 0; i < container.lines.size(); i++) { + LineData ld = container.lines.get(i); + if (position >= ld.start && position < ld.end) { + int visible = Math.max(1, visibleLines); + int maxScroll = Math.max(0, container.linesCount - visible); + int targetLine = i; + + // If search bar is visible and would hide this line, scroll down so it's visible + // The target line should appear below the search bar, not under it + int currentScroll = scroll.getScrolledLine(); + int firstVisibleLine = currentScroll + linesHiddenBySRB; + + if (searchBarOffset > 0 && targetLine < firstVisibleLine) { + // Force scroll so target line appears just below the search bar + scroll.setTargetScroll(Math.max(0, targetLine - linesHiddenBySRB), maxScroll); + } else { + scroll.scrollToLine(targetLine, visible, maxScroll); + } + break; + } + } + } + + @Override + public void setSelection(int start, int end) { + selection.setSelection(start, end); + selection.setCursorPositionDirect(end); + } + + @Override + public int getGutterWidth() { + return LINE_NUMBER_GUTTER_WIDTH; + } + + @Override + public void unfocusMainEditor() { + // Save position but unfocus + active = false; + } + + @Override + public void focusMainEditor() { + active = true; + selection.markActivity(); + } + + @Override + public void onMatchesUpdated() { + // Called when matches change - could be used for UI updates + } + }); // Initialize Go To Line dialog with callback this.goToLineDialog = new GoToLineDialog(new GoToLineDialog.GoToLineCallback() { @Override @@ -154,9 +217,19 @@ public class GuiScriptTextArea extends GuiNpcTextField { }); initializeKeyBindings(); - initGui(); + init(x, y, width, height, text); } + public void init(int x, int y, int width, int height, String text) { + this.x = xPosition = x; + this.y = yPosition = y; + this.width = width; + this.height = height; + this.undoing = true; + this.setText(text); + this.undoing = false; + initGui(); + } public void initGui() { int endX = x + width, endY = y + height; int xOffset = hasVerticalScrollbar() ? -8 : -2; @@ -168,80 +241,6 @@ public class GuiScriptTextArea extends GuiNpcTextField { // Initialize search bar (preserves state across initGui calls) searchBar.initGui(x, y, width); - searchBar.setCallback(new SearchReplaceBar.SearchCallback() { - @Override - public String getText() { - return text; - } - - @Override - public void setText(String newText) { - GuiScriptTextArea.this.setText(newText); - } - - @Override - public void scrollToPosition(int position) { - // Find line containing position and scroll to it - if (container == null || container.lines == null) return; - - // Calculate offset to account for search bar height - int searchBarOffset = searchBar.getTotalHeight(); - int effectiveHeight = height - searchBarOffset; - int visibleLines = effectiveHeight / container.lineHeight; - // Calculate how many lines the search bar covers - int linesHiddenBySRB = searchBarOffset > 0 ? (int) Math.ceil((double) searchBarOffset / container.lineHeight) : 0; - - for (int i = 0; i < container.lines.size(); i++) { - LineData ld = container.lines.get(i); - if (position >= ld.start && position < ld.end) { - int visible = Math.max(1, visibleLines); - int maxScroll = Math.max(0, container.linesCount - visible); - int targetLine = i; - - // If search bar is visible and would hide this line, scroll down so it's visible - // The target line should appear below the search bar, not under it - int currentScroll = scroll.getScrolledLine(); - int firstVisibleLine = currentScroll + linesHiddenBySRB; - - if (searchBarOffset > 0 && targetLine < firstVisibleLine) { - // Force scroll so target line appears just below the search bar - scroll.setTargetScroll(Math.max(0, targetLine - linesHiddenBySRB), maxScroll); - } else { - scroll.scrollToLine(targetLine, visible, maxScroll); - } - break; - } - } - } - - @Override - public void setSelection(int start, int end) { - selection.setSelection(start, end); - selection.setCursorPositionDirect(end); - } - - @Override - public int getGutterWidth() { - return LINE_NUMBER_GUTTER_WIDTH; - } - - @Override - public void unfocusMainEditor() { - // Save position but unfocus - active = false; - } - - @Override - public void focusMainEditor() { - active = true; - selection.markActivity(); - } - - @Override - public void onMatchesUpdated() { - // Called when matches change - could be used for UI updates - } - }); // Initialize Go To Line dialog goToLineDialog.initGui(x, y, width); @@ -1774,7 +1773,7 @@ public class GuiScriptTextArea extends GuiNpcTextField { } public boolean hasVerticalScrollbar() { - return this.container.visibleLines < this.container.linesCount; + return this.container != null && this.container.visibleLines < this.container.linesCount; } public void enableCodeHighlighting() {