From 3e23893e7856c563de3634ae5cad4d1132198b89 Mon Sep 17 00:00:00 2001 From: Kam Date: Sun, 23 Feb 2025 15:38:42 -0500 Subject: [PATCH] Fix Effective Scaling --- .../noppes/npcs/client/ClientTickHandler.java | 18 ++- .../client/gui/hud/CompassHudComponent.java | 37 ++++-- .../npcs/client/gui/hud/GuiHudEditor.java | 110 ++++++++++++++---- .../npcs/client/gui/hud/HudComponent.java | 22 +++- .../gui/hud/QuestTrackingComponent.java | 28 ++--- 5 files changed, 162 insertions(+), 53 deletions(-) diff --git a/src/main/java/noppes/npcs/client/ClientTickHandler.java b/src/main/java/noppes/npcs/client/ClientTickHandler.java index e66bf90e6..65e06bf6b 100644 --- a/src/main/java/noppes/npcs/client/ClientTickHandler.java +++ b/src/main/java/noppes/npcs/client/ClientTickHandler.java @@ -141,6 +141,14 @@ public class ClientTickHandler{ private void updateCompassMarks() { Minecraft mc = Minecraft.getMinecraft(); EntityPlayer player = mc.thePlayer; + + // Update compass + HudComponent compass = ClientHudManager.getInstance() + .getHudComponents().get(EnumHudComponent.QuestCompass); + + if(!compass.enabled || mc.thePlayer == null) + return; + ArrayList marks = new ArrayList<>(); // Scan entities in loaded chunks @@ -148,6 +156,9 @@ public class ClientTickHandler{ if(entity instanceof EntityNPCInterface) { EntityNPCInterface npc = (EntityNPCInterface) entity; + if(npc.dimension != player.dimension) + continue; + // Check distance if(player.getDistanceToEntity(npc) > SCAN_RANGE) continue; @@ -169,11 +180,6 @@ public class ClientTickHandler{ } } - // Update compass - HudComponent compass = ClientHudManager.getInstance() - .getHudComponents().get(EnumHudComponent.QuestCompass); - if(compass != null) { - ((CompassHudComponent) compass).updateMarkTargets(marks); - } + ((CompassHudComponent) compass).updateMarkTargets(marks); } } diff --git a/src/main/java/noppes/npcs/client/gui/hud/CompassHudComponent.java b/src/main/java/noppes/npcs/client/gui/hud/CompassHudComponent.java index 67d1e50d3..b2cc71fb2 100644 --- a/src/main/java/noppes/npcs/client/gui/hud/CompassHudComponent.java +++ b/src/main/java/noppes/npcs/client/gui/hud/CompassHudComponent.java @@ -22,8 +22,12 @@ public class CompassHudComponent extends HudComponent { private final int SCALE_DISTANCE_MIN = 10; private final int SCALE_DISTANCE_MAX = 40; private static final int HANDLE_SIZE = 10; + private static final int WIDTH_BAR_SIZE = 6; // Width adjust bar width private final int SCAN_RANGE = 128; + // Flag to indicate if the width bar is being dragged in editing mode. + public boolean resizingWidth = false; + public static class MarkTargetEntry { public int x, z; public int type; @@ -79,16 +83,17 @@ public class CompassHudComponent extends HudComponent { @Override public void renderOnScreen(float partialTicks) { -// if (!hasData || isEditting) return; + // If no mark data or in edit mode, skip normal rendering. + if (!hasData || isEditting) return; ScaledResolution res = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight); - int actualX = (int)((float) posX / 100F * res.getScaledWidth()); - int actualY = (int)((float) posY / 100F * res.getScaledHeight()); - float scaleFactor = scale / 100.0F; + int actualX = (int)(posX / 100F * res.getScaledWidth()); + int actualY = (int)(posY / 100F * res.getScaledHeight()); + float effectiveScale = getEffectiveScale(res); GL11.glPushMatrix(); GL11.glTranslatef(actualX, actualY, 0); - GL11.glScalef(scaleFactor, scaleFactor, scaleFactor); + GL11.glScalef(effectiveScale, effectiveScale, effectiveScale); // Draw background bar drawRect(0, 0, overlayWidth, BAR_HEIGHT, 0x80000000); @@ -194,20 +199,27 @@ public class CompassHudComponent extends HudComponent { public void renderEditing() { isEditting = true; ScaledResolution res = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight); - int actualX = (int)((float) posX / 100F * res.getScaledWidth()); - int actualY = (int)((float) posY / 100F * res.getScaledHeight()); - float scaleFactor = scale / 100.0F; + int actualX = (int)(posX / 100F * res.getScaledWidth()); + int actualY = (int)(posY / 100F * res.getScaledHeight()); + float effectiveScale = getEffectiveScale(res); GL11.glPushMatrix(); GL11.glTranslatef(actualX, actualY, 0); - GL11.glScalef(scaleFactor, scaleFactor, scaleFactor); + GL11.glScalef(effectiveScale, effectiveScale, effectiveScale); + // Draw background bar and border. drawRect(0, 0, overlayWidth, overlayHeight, 0x80000000); drawRectOutline(0, 0, overlayWidth, overlayHeight, 0xFF00FF00); + // Resize handle (bottom-right for scale) drawRect(overlayWidth - HANDLE_SIZE, overlayHeight - HANDLE_SIZE, overlayWidth, overlayHeight, 0xFFCCCCCC); + // Width adjust bar on right side (centered vertically) + int margin = 5; + int barX = overlayWidth + margin; + int barY = (overlayHeight - BAR_HEIGHT) / 2; + drawRect(barX, barY, barX + WIDTH_BAR_SIZE, barY + BAR_HEIGHT, 0xFF888888); - // Demo marks + // Demo marks for preview. renderDemoIcon(overlayWidth * 0.25f, MarkType.EXCLAMATION, 0xFF00FF00); renderDemoIcon(overlayWidth * 0.75f, MarkType.QUESTION, 0xFFFF0000); @@ -227,6 +239,11 @@ public class CompassHudComponent extends HudComponent { } } + /** + * Updates the mark targets directly. + * (This method assumes external code (CNPC+ mark data) supplies the new marks + * without having to loop through all loaded entities.) + */ public void updateMarkTargets(List newMarks) { markTargets.clear(); markTargets.addAll(newMarks); diff --git a/src/main/java/noppes/npcs/client/gui/hud/GuiHudEditor.java b/src/main/java/noppes/npcs/client/gui/hud/GuiHudEditor.java index f7752f0e8..35c2899c0 100644 --- a/src/main/java/noppes/npcs/client/gui/hud/GuiHudEditor.java +++ b/src/main/java/noppes/npcs/client/gui/hud/GuiHudEditor.java @@ -1,9 +1,9 @@ package noppes.npcs.client.gui.hud; -import net.minecraft.client.gui.GuiScreen; -import net.minecraft.client.gui.GuiButton; -import net.minecraft.client.gui.ScaledResolution; import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiButton; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.client.gui.ScaledResolution; import org.lwjgl.input.Mouse; import org.lwjgl.opengl.GL11; import java.util.List; @@ -16,12 +16,18 @@ public class GuiHudEditor extends GuiScreen { private int dragOffsetX, dragOffsetY; private boolean dragging = false; private boolean resizing = false; + // For general scaling resize. private int initialScale, initialMouseX, initialMouseY; + // For Compass width adjustment. + private boolean resizingWidth = false; + private int initialOverlayWidth, initialWidthMouseX; + private final int HANDLE_SIZE = 10; private final int RESET_POSITION_ID = 100; public GuiHudEditor(GuiScreen parent) { this.parent = parent; + // For example, default to QuestTracker if available. selectedComponent = ClientHudManager.getInstance().getHudComponents().get(EnumHudComponent.QuestTracker); } @@ -37,6 +43,7 @@ public class GuiHudEditor extends GuiScreen { * Updates the custom buttons shown on screen. * Removes all buttons except the global Save button (id 0) * then adds the selected component's custom buttons plus a Reset Position button. + * Buttons are now arranged in two columns. */ private void updateCustomButtons() { // Remove any buttons that are not the global Save (id 0) @@ -47,19 +54,41 @@ public class GuiHudEditor extends GuiScreen { } } if (selectedComponent != null) { - int baseX = width / 2 - 60; - int baseY = height - 70; // Start above the Save button. List customButtons = new ArrayList<>(); selectedComponent.addEditorButtons(customButtons); - int offsetY = 0; - for (GuiButton btn : customButtons) { - btn.xPosition = baseX; - btn.yPosition = baseY - offsetY; - offsetY += btn.height + 5; - buttonList.add(btn); + // Arrange buttons in 2 columns. + int spacingX = 10; + int spacingY = 5; + int buttonWidth = 120; // assuming uniform width + int startXLeft = width / 2 - buttonWidth - spacingX/2; + int startXRight = width / 2 + spacingX/2; + int rowHeight = 0; + int numButtons = customButtons.size(); + for (int i = 0; i < numButtons; i += 2) { + // If two buttons in this row. + if (i + 1 < numButtons) { + GuiButton btnLeft = customButtons.get(i); + GuiButton btnRight = customButtons.get(i+1); + btnLeft.xPosition = startXLeft; + btnRight.xPosition = startXRight; + // Determine row y based on button height. + int rowY = height - 70 - rowHeight; + btnLeft.yPosition = rowY; + btnRight.yPosition = rowY; + rowHeight += btnLeft.height + spacingY; + buttonList.add(btnLeft); + buttonList.add(btnRight); + } else { + // One button: center it. + GuiButton btn = customButtons.get(i); + btn.xPosition = width / 2 - buttonWidth / 2; + btn.yPosition = height - 70 - rowHeight; + buttonList.add(btn); + rowHeight += btn.height + spacingY; + } } - // Always add the Reset Position button. - GuiButton resetPos = new GuiButton(RESET_POSITION_ID, baseX, baseY - offsetY, 120, 20, "Reset Position"); + // Always add the Reset Position button below the custom buttons. + GuiButton resetPos = new GuiButton(RESET_POSITION_ID, width / 2 - buttonWidth / 2, height - 70 - rowHeight, buttonWidth, 20, "Reset Position"); buttonList.add(resetPos); } } @@ -77,12 +106,40 @@ public class GuiHudEditor extends GuiScreen { @Override protected void mouseClicked(int mouseX, int mouseY, int mouseButton) { // Iterate over HUD components to check for selection. + // First, check for Compass width adjust hitbox if the mouse is near a CompassHudComponent. + for (HudComponent hud : ClientHudManager.getInstance().getHudComponents().values()) { + if (!(hud instanceof CompassHudComponent)) + continue; + + ScaledResolution res = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight); + float effectiveScale = hud.getEffectiveScale(res); + int actualX = (int)(hud.posX / 100F * res.getScaledWidth()); + int actualY = (int)(hud.posY / 100F * res.getScaledHeight()); + + // Even if the mouse is outside the hud bounds, we want to check for the floating adjust bar. + int margin = 5; + int barWidth = 6; // same as WIDTH_BAR_SIZE + // The adjust bar is drawn to the right of the component. + int barX = actualX + (int)(hud.overlayWidth * effectiveScale) + margin; + int barY = actualY + (int)(((hud.overlayHeight - 20) / 2.0F) * effectiveScale); // 20 = BAR_HEIGHT + int barHeight = (int)(20 * effectiveScale); + if (mouseX >= barX && mouseX <= barX + barWidth && + mouseY >= barY && mouseY <= barY + barHeight) { + resizingWidth = true; + selectedComponent = hud; + initialOverlayWidth = hud.overlayWidth; + initialWidthMouseX = mouseX; + updateCustomButtons(); + return; + } + } + + // Next, proceed with the normal hit detection for dragging/resizing of the HUD components. for (HudComponent hud : ClientHudManager.getInstance().getHudComponents().values()) { ScaledResolution res = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight); - // Use base scaling (scale/100.0F) - float effectiveScale = (hud.scale / 100.0F); - int actualX = (int)((float)hud.posX / 100F * res.getScaledWidth()); - int actualY = (int)((float)hud.posY / 100F * res.getScaledHeight()); + float effectiveScale = hud.getEffectiveScale(res); + int actualX = (int)(hud.posX / 100F * res.getScaledWidth()); + int actualY = (int)(hud.posY / 100F * res.getScaledHeight()); int absWidth = (int)(hud.overlayWidth * effectiveScale); int absHeight = (int)(hud.overlayHeight * effectiveScale); if (mouseX >= actualX && mouseX <= actualX + absWidth && @@ -94,14 +151,16 @@ public class GuiHudEditor extends GuiScreen { initialScale = hud.scale; initialMouseX = mouseX; initialMouseY = mouseY; + updateCustomButtons(); + return; } else { dragging = true; selectedComponent = hud; dragOffsetX = mouseX - actualX; dragOffsetY = mouseY - actualY; + updateCustomButtons(); + return; } - updateCustomButtons(); - break; } } super.mouseClicked(mouseX, mouseY, mouseButton); @@ -111,7 +170,7 @@ public class GuiHudEditor extends GuiScreen { protected void mouseMovedOrUp(int mouseX, int mouseY, int state) { dragging = false; resizing = false; - // Optionally keep selectedComponent so its buttons remain visible. + resizingWidth = false; super.mouseMovedOrUp(mouseX, mouseY, state); } @@ -121,7 +180,7 @@ public class GuiHudEditor extends GuiScreen { ScaledResolution res = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight); int scaledWidth = res.getScaledWidth(); int scaledHeight = res.getScaledHeight(); - float effectiveScale = (selectedComponent.scale / 100.0F); + float effectiveScale = selectedComponent.getEffectiveScale(res); if (dragging) { int newX = Mouse.getX() * width / mc.displayWidth - dragOffsetX; int newY = scaledHeight - (Mouse.getY() * scaledHeight / mc.displayHeight) - dragOffsetY; @@ -141,6 +200,13 @@ public class GuiHudEditor extends GuiScreen { if (selectedComponent.scale < 50) selectedComponent.scale = 50; if (selectedComponent.scale > 300) selectedComponent.scale = 300; } + if (resizingWidth && (selectedComponent instanceof CompassHudComponent)) { + int currentMouseX = Mouse.getX() * width / mc.displayWidth; + int delta = currentMouseX - initialWidthMouseX; + selectedComponent.overlayWidth = initialOverlayWidth + delta; + if (selectedComponent.overlayWidth < 50) selectedComponent.overlayWidth = 50; + if (selectedComponent.overlayWidth > 500) selectedComponent.overlayWidth = 500; + } } super.updateScreen(); } @@ -164,6 +230,8 @@ public class GuiHudEditor extends GuiScreen { } } } + + @Override protected void keyTyped(char typedChar, int keyCode) { if (keyCode == 1) { close(); diff --git a/src/main/java/noppes/npcs/client/gui/hud/HudComponent.java b/src/main/java/noppes/npcs/client/gui/hud/HudComponent.java index 955a8e66d..48bfece66 100644 --- a/src/main/java/noppes/npcs/client/gui/hud/HudComponent.java +++ b/src/main/java/noppes/npcs/client/gui/hud/HudComponent.java @@ -1,7 +1,8 @@ package noppes.npcs.client.gui.hud; -import net.minecraft.nbt.NBTTagCompound; import net.minecraft.client.gui.GuiButton; +import net.minecraft.client.gui.ScaledResolution; +import net.minecraft.nbt.NBTTagCompound; import java.util.List; public abstract class HudComponent { @@ -40,15 +41,30 @@ public abstract class HudComponent { /** * Allows the component to add its own editor buttons. + * The default implementation adds a toggle button for enabling/disabling the component. */ public void addEditorButtons(List buttonList) { - // Default: do nothing. + // Toggle enabled/disabled button (id 999 reserved for toggle) + String label = enabled ? "Enabled" : "Disabled"; + buttonList.add(new GuiButton(999, 0, 0, 120, 20, label)); } /** * Handles editor button actions specific to this HUD component. + * The default implementation toggles the enabled state if button id is 999. */ public void onEditorButtonPressed(GuiButton button) { - // Default: do nothing. + if(button.id == 999){ + enabled = !enabled; + button.displayString = enabled ? "Enabled" : "Disabled"; + } + } + + /** + * Computes the effective scale factor for rendering and hitbox calculations. + * Uses the current resolution (with 1920 as the base width). + */ + public float getEffectiveScale(ScaledResolution res) { + return (scale / 100.0F) * (res.getScaledWidth() / 1920.0F); } } diff --git a/src/main/java/noppes/npcs/client/gui/hud/QuestTrackingComponent.java b/src/main/java/noppes/npcs/client/gui/hud/QuestTrackingComponent.java index 5236c0c19..20ffe5595 100644 --- a/src/main/java/noppes/npcs/client/gui/hud/QuestTrackingComponent.java +++ b/src/main/java/noppes/npcs/client/gui/hud/QuestTrackingComponent.java @@ -52,7 +52,6 @@ public class QuestTrackingComponent extends HudComponent { NBTTagList objectiveList = compound.getTagList("ObjectiveList", 8); for (int i = 0; i < objectiveList.tagCount(); i++) { String objective = objectiveList.getStringTagAt(i); - // Process objective if needed. objectives.add(objective); } String turnIn = ""; @@ -107,14 +106,13 @@ public class QuestTrackingComponent extends HudComponent { return; ScaledResolution res = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight); - int actualX = (int)((float) posX / 100F * res.getScaledWidth()); - int actualY = (int)((float) posY / 100F * res.getScaledHeight()); - // Use base scaling: effective scale = scale/100.0F - float s = (scale / 100.0F); + int actualX = (int)(posX / 100F * res.getScaledWidth()); + int actualY = (int)(posY / 100F * res.getScaledHeight()); + float effectiveScale = getEffectiveScale(res); GL11.glPushMatrix(); GL11.glTranslatef(actualX, actualY, 0); - GL11.glScalef(s, s, s); + GL11.glScalef(effectiveScale, effectiveScale, effectiveScale); // Draw background (semi-transparent). drawRect(0, 0, overlayWidth, overlayHeight, 0x40FFFFFF); @@ -142,13 +140,13 @@ public class QuestTrackingComponent extends HudComponent { public void renderEditing() { isEditting = true; ScaledResolution res = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight); - int actualX = (int)((float) posX / 100F * res.getScaledWidth()); - int actualY = (int)((float) posY / 100F * res.getScaledHeight()); - float s = (scale / 100.0F); + int actualX = (int)(posX / 100F * res.getScaledWidth()); + int actualY = (int)(posY / 100F * res.getScaledHeight()); + float effectiveScale = getEffectiveScale(res); GL11.glPushMatrix(); GL11.glTranslatef(actualX, actualY, 0); - GL11.glScalef(s, s, s); + GL11.glScalef(effectiveScale, effectiveScale, effectiveScale); // Draw editing background, border, and resize handle. drawRect(0, 0, overlayWidth, overlayHeight, 0x40FFFFFF); @@ -172,9 +170,11 @@ public class QuestTrackingComponent extends HudComponent { @Override public void addEditorButtons(List buttonList) { - // Add buttons similar to the original editor: ID 1 for alignment, ID 3 for reset. - buttonList.add(new GuiButton(1, 100, Minecraft.getMinecraft().displayHeight - 70, 120, 20, getAlignText())); - buttonList.add(new GuiButton(3, 100, Minecraft.getMinecraft().displayHeight - 95, 120, 20, "Reset to Center")); + // First add the default toggle button. + super.addEditorButtons(buttonList); + // Then add other custom buttons. + buttonList.add(new GuiButton(1, 100, 0, 120, 20, getAlignText())); + buttonList.add(new GuiButton(3, 100, 0, 120, 20, "Reset to Center")); } @Override @@ -185,6 +185,8 @@ public class QuestTrackingComponent extends HudComponent { } else if (button.id == 3) { // Reset to center. posX = 50; posY = 50; + } else { + super.onEditorButtonPressed(button); } }