From 9fa9b616fd4474c7483b63f7a37761d1bdc405a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linnea=20Gr=C3=A4f?= Date: Tue, 16 Jul 2024 23:57:03 +0200 Subject: [PATCH] Add scrolling --- .../exhq/ajarc/computer/ComputerScreen.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/dev/exhq/ajarc/computer/ComputerScreen.java b/src/main/java/dev/exhq/ajarc/computer/ComputerScreen.java index b910b28..1ccb100 100644 --- a/src/main/java/dev/exhq/ajarc/computer/ComputerScreen.java +++ b/src/main/java/dev/exhq/ajarc/computer/ComputerScreen.java @@ -9,6 +9,7 @@ import net.minecraft.world.entity.player.Inventory; import net.neoforged.neoforge.network.PacketDistributor; import org.jetbrains.annotations.NotNull; import org.lwjgl.glfw.GLFW; +import org.lwjgl.system.MathUtil; public class ComputerScreen extends AbstractContainerScreen { public ComputerScreen(ComputerMenu pMenu, Inventory pPlayerInventory, Component pTitle) { @@ -16,6 +17,7 @@ public class ComputerScreen extends AbstractContainerScreen { } String editingLine = ""; + int scroll = 0; public void reinitDimensions() { init(); @@ -43,6 +45,7 @@ public class ComputerScreen extends AbstractContainerScreen { @Override public boolean keyPressed(int pKeyCode, int pScanCode, int pModifiers) { + scroll = 0; InputConstants.Key mouseKey = InputConstants.getKey(pKeyCode, pScanCode); assert this.minecraft != null; if (!this.minecraft.options.keyInventory.isActiveAndMatches(mouseKey) && super.keyPressed(pKeyCode, pScanCode, pModifiers)) @@ -59,12 +62,25 @@ public class ComputerScreen extends AbstractContainerScreen { return false; } + @Override + public boolean mouseScrolled(double pMouseX, double pMouseY, double pScrollX, double pScrollY) { + if (pScrollY < 0) { + scroll--; + return true; + } else if (pScrollY > 0) { + scroll++; + return true; + } + return super.mouseScrolled(pMouseX, pMouseY, pScrollX, pScrollY); + } + @Override protected void renderLabels(@NotNull GuiGraphics graphics, int pMouseX, int pMouseY) { int offsetY = 0; var lines = menu.getLines(); //var renderedlines = lines.subList(lines.size() - menu.getRows(), lines.size()); - for (int i = Math.max(0, lines.size() - menu.getRows() + 1); i < lines.size(); i++) { + scroll = Math.max(Math.min(scroll, lines.size() - 1), 0); + for (int i = Math.max(0, lines.size() - menu.getRows() + 1 - scroll); i < lines.size() - scroll; i++) { graphics.drawString(font, lines.get(i), 0, offsetY, 0xFFFFFFFF); offsetY += 11; }