Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -432,4 +432,5 @@ interface ElementVisitor<T extends SizedElement> {

}

public ColorPalette getColorPalette() { return colorPalette; }
}
5 changes: 5 additions & 0 deletions src/configlib/java/fr/alexdoru/configlib/lib/gui/GuiUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public final class GuiUtil {

private GuiUtil() {}

public static final int MOUSE_LEFT = 0;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete this field

@shvaich shvaich Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This field should be used throughtout the codebase instead of the magic value when comparing with mouseButton.

This is not just for this pull request but for further onces aswell.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok but I dislike it being here, just make it a local variable in the RendererEditGuiScreen

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the ConfigGuiScreen I can add an MouseClick enum or something to remove the magic numbers

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand this and I will make it a local variable for now.
but think of it like this, many different classes check mouseButton:
ConfigGuiScreen, RendererEditGuiScreen, ColorEditScreen, ConfigGuiButtons, etc...

and it would be convinient and easy to read if this value was shared.
also for developers who create their own elements.
and there is no need to create an enum for this, this is simple enough and very understandable to me and I assume other devs.
later on you can also add MOUSE_RIGHT = 1 if you want


public static void drawRect(Box box, int color) {
Gui.drawRect(box.LEFT, box.TOP, box.RIGHT, box.BOTTOM, color);
}
Expand Down Expand Up @@ -55,4 +57,7 @@ public static int brightenColor(int color, float amount) {
return (a << 24) | (r << 16) | (g << 8) | b;
}

public static void drawFullTextureWithCustomSize(int left, int top, int drawWidth, int drawHeight) {
Comment thread
Alexdoru marked this conversation as resolved.
Gui.drawModalRectWithCustomSizedTexture(left, top, 0, 0, drawWidth, drawHeight, drawWidth, drawHeight);
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,54 @@
package fr.alexdoru.configlib.lib.gui;

import fr.alexdoru.configlib.api.ColorPalette;
import fr.alexdoru.configlib.api.IRenderer;
import fr.alexdoru.configlib.api.RendererPosition;
import fr.alexdoru.configlib.lib.RendererManager;
import fr.alexdoru.configlib.lib.gui.elements.ClickGuiButton;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;

import java.io.IOException;
import java.util.Collections;

public class RendererEditGuiScreen extends GuiScreen {

private static final int BUTTON_SIZE = 10;

private final RendererManager rendererManager;
private final IRenderer renderer;
private final RendererPosition rendererPosition;
private final GuiScreen parent;
private final ColorPalette colorPalette;
private final Button[] buttons;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't make an array but create mutliple fields

private final double originalRelativeX, originalRelativeY;
private boolean dragging;
private int prevX, prevY;

public RendererEditGuiScreen(RendererManager rendererManager, IRenderer renderer, GuiScreen parent) {
public RendererEditGuiScreen(RendererManager rendererManager, IRenderer renderer, GuiScreen parent, ColorPalette colorPalette) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't need to add ColorPalette as a parameter just change the type of the parent parameter to ConfigGuiScreen

this.rendererManager = rendererManager;
this.renderer = renderer;
this.rendererPosition = renderer.getPosition();
this.parent = parent;
this.colorPalette = colorPalette;
this.originalRelativeX = rendererPosition.getRelativeX();
this.originalRelativeY = rendererPosition.getRelativeY();
this.buttons = new Button[]{
new Button(new ResourceLocation("configlib", "reload.png"), "Reset to Default Position"),
new Button(new ResourceLocation("configlib", "undo.png"), "Undo Changes")
};
}

@Override
public void initGui() {
this.rendererPosition.updateAbsolutePosition();
this.adjustBounds();
final ScaledResolution res = new ScaledResolution(mc);
this.rendererPosition.updateAbsolutePosition(res);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mark deprecated the method this.rendererPosition.updateAbsolutePosition(); that is now not used anymore

this.adjustBounds(res);
}

@Override
Expand All @@ -39,27 +59,74 @@ public void drawScreen(int mouseX, int mouseY, float partialTicks) {
this.rendererManager.renderEditScreenBackground(this.renderer);
this.rendererPosition.setEnabled(prevEnabled);
super.drawDefaultBackground();
this.renderer.renderDummy();
if (this.dragging) {
this.rendererPosition.setAbsolutePositionForRender(
this.rendererPosition.getAbsoluteRenderX() + mouseX - this.prevX,
this.rendererPosition.getAbsoluteRenderY() + mouseY - this.prevY
);
}
this.renderer.renderDummy();
if (!this.dragging) {
final int buttonX = this.width - BUTTON_SIZE - 2;
int buttonY = 2;
String hoveringText = null;
GlStateManager.enableAlpha();
GlStateManager.enableBlend();
GlStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0);
for (final Button button : buttons) {
button.xPosition = buttonX;
button.yPosition = buttonY;
button.drawButton(colorPalette, mc, mouseX, mouseY);
if (button.isMouseOver() && (hoveringText == null || hoveringText.isEmpty())) {
hoveringText = button.getHoveringText();
}
buttonY += BUTTON_SIZE + 2;
}
GlStateManager.disableBlend();
GlStateManager.color(1f, 1f, 1f, 1f);
if (hoveringText != null && !hoveringText.isEmpty()) {
drawHoveringText(Collections.singletonList(hoveringText), mouseX, mouseY + fontRendererObj.FONT_HEIGHT + 6);
}
}
this.prevX = mouseX;
this.prevY = mouseY;
}

@Override
protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException {
if (mouseButton == GuiUtil.MOUSE_LEFT) {
if (!this.dragging) {
int clickedButtonIdx = -1;
for (int i = 0; i < buttons.length; i++) {
if (buttons[i].mousePressed(mc, mouseX, mouseY)) {
clickedButtonIdx = i;
buttons[i].playPressSound(mc.getSoundHandler());
break;
}
}
if (clickedButtonIdx != -1) {
success: {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you shouldn't use labels and break and also this is bugged because if clickedButtonIdx is not 0 or not 1 it will loop infinitely

@shvaich shvaich Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are mistaken, this is not how either works.
the loop I have is itterating over the buttons array, it only breaks early if I find a clicked button because i can only click on one button.

now the label after it is just for simplicity, because if clickedButtonIdx is either 0 or 1 I am updating the relative position and then need to update the absolute position, so what I am doing is updating the relative position, (see if (clickedButtonIdx == 0) and else if (clickedButtonIdx == 1))
and then updating the absolute position and returning from the function to not trigger isDragging.
(see code after else break success)

if clickedButtonIdx isn't 0 or 1 it will simply break from the success block and move on to isDragging logic. (see else break success)

There is nothing in this code that can cause an infinite loop and the only reason its written this way is because if I do decide to add more buttons I can write the logic easily at the end of the success block

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if clickbutton == 2 it will loop indefinitely, also I have never seen labels and breaks outside of loop for a good reason you should not use them outside of loops plase change your code

image

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand your reason for not wanting to use labels so I wont. but you are mistaken in your image.
what happens when you break from a labeled-block is that you go to the end of the block.
what would actually happen in my code if clickButton == 2 is that the code will continue from }.
example:

int clickButton = 2;
success: {
    if (clickButton == 0) {}
    else if (clickButton == 1) {}
    else break success;
    log("0 or 1!");
    return
}
log("not 0 or 1!");

if clickButton is 2, it will log: not 0 or 1!.
if clickButton is 0 or 1 it will log: 0 or 1!

if (clickedButtonIdx == 0) rendererPosition.resetToDefault();
else if (clickedButtonIdx == 1) rendererPosition.setRelativePosition(originalRelativeX, originalRelativeY);
else break success;
final ScaledResolution res = new ScaledResolution(mc);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

save the scaled resolution as a field that is initializd in initGui

this.rendererPosition.updateAbsolutePosition(res);
this.adjustBounds(res);
return;
}
}
}
this.dragging = true;
}
super.mouseClicked(mouseX, mouseY, mouseButton);
this.dragging = true;
}

@Override
protected void mouseReleased(int mouseX, int mouseY, int state) {
super.mouseReleased(mouseX, mouseY, state);
this.dragging = false;
if (state == GuiUtil.MOUSE_LEFT) {
this.dragging = false;
}
}

@Override
Expand All @@ -82,8 +149,7 @@ public boolean doesGuiPauseGame() {
/**
* Makes sure the HUD can't get out of the screen
*/
private void adjustBounds() {
final ScaledResolution res = new ScaledResolution(mc);
private void adjustBounds(ScaledResolution res) {
final int screenWidth = res.getScaledWidth();
final int screenHeight = res.getScaledHeight();
final int absoluteX = Math.max(0, Math.min(rendererPosition.getAbsoluteRenderX(), screenWidth));
Expand All @@ -96,4 +162,26 @@ private void renderCrosshair() {
drawTexturedModalRect(this.width / 2 - 7, this.height / 2 - 7, 0, 0, 16, 16);
}

private static final class Button extends ClickGuiButton {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create a standalone class named TexturedButton that draws the icon

private final ResourceLocation icon;
private final String hoveringText;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the hovering text feature to the parrent class click gui button directly

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do this.
I also have an idea to add possible hovering text to ConfigUIElements like in my codebase.
I will show you what I mean in a different pull request but keep it in mind


public Button(ResourceLocation icon, String hoveringText) {
super(-1, 0, 0, BUTTON_SIZE, BUTTON_SIZE, "");
this.icon = icon;
this.hoveringText = hoveringText;
}

@Override
public void drawButton(ColorPalette colorPalette, Minecraft mc, int mouseX, int mouseY) {
super.drawButton(colorPalette, mc, mouseX, mouseY);
GlStateManager.color(1f, 1f, 1f, 1f);
mc.getTextureManager().bindTexture(icon);
final int iconSize = 6;
final int iconOffset = (BUTTON_SIZE - iconSize) / 2;
GuiUtil.drawFullTextureWithCustomSize(xPosition + iconOffset, yPosition + iconOffset, iconSize, iconSize);
}

public String getHoveringText() { return hoveringText; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,19 @@
import fr.alexdoru.configlib.lib.RendererManager;
import fr.alexdoru.configlib.lib.gui.ConfigGuiScreen;
import fr.alexdoru.configlib.lib.gui.RendererEditGuiScreen;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class RendererGuiButton extends ConfigGuiButton {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree with the changes in this class, please roll back your changes and keep the two buttons like it was before

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand but I very much disagree with this.

  1. I am keeping the Move HUD Button but as shown in the image it is no longer an "Image Button" and its text is Position.
    If you like its text can be Move HUD.

  2. There is no reason for the Reset Button to be placed here.
    2.1. I am already creating such a button for use inside the Hud Edit Screen.
    2.2 It is bad placement. Players cannot see where the hud is once they press this button here, they have to press on the Move HUD aswell to see its new position.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok can you change the text to something else than "Position", "Move HUD" would be more explicit, also you might need to add a color to the palette for the text color


private static final ResourceLocation MOVE_ICON = new ResourceLocation("configlib", "move_icon_64x64.png");
private static final ResourceLocation RESET_ICON = new ResourceLocation("configlib", "reset_icon_64x64.png");

private final ConfigGuiScreen parentScreen;
private final RendererManager rendererManager;
private final RendererPosition rendererPosition;
private boolean toggled;
private final ClickGuiButton buttonEnabled;
private final ClickGuiButton buttonMoveHud;
private final ClickGuiButton buttonResetPos;

public RendererGuiButton(
ConfigGuiScreen configGuiScreen,
Expand All @@ -40,14 +32,14 @@ public RendererGuiButton(
this.rendererManager = rendererManager;
this.rendererPosition = ((RendererPosition) field.get(null));
this.toggled = this.rendererPosition.isEnabled();
this.buttonEnabled = new ClickGuiButton(0, 0, 0, mc.fontRendererObj.getStringWidth(" Disabled "), 20, getButtonText());
this.buttonMoveHud = new ClickGuiButton(0, 0, 0, 20, 20, "");
this.buttonResetPos = new ClickGuiButton(0, 0, 0, 20, 20, "");
final int btnWidth = mc.fontRendererObj.getStringWidth(" Disabled ");
this.buttonEnabled = new ClickGuiButton(0, 0, 0, btnWidth, 20, getButtonText());
this.buttonMoveHud = new ClickGuiButton(0, 0, 0, btnWidth, 20, "Position");
}

@Override
public void setBoxWidth(int boxWidth) {
super.setBoxWidth(boxWidth - mc.fontRendererObj.getStringWidth("Reset Position") - 10);
super.setBoxWidth(boxWidth);
this.boxWidth = boxWidth;
}

Expand All @@ -60,21 +52,6 @@ public void draw(ColorPalette colorPalette, int drawX, int drawY, int mouseX, in
buttonMoveHud.xPosition = buttonEnabled.xPosition;
buttonMoveHud.yPosition = buttonEnabled.yPosition + buttonEnabled.height + 1;
buttonMoveHud.drawButton(colorPalette, mc, mouseX, mouseY);
buttonResetPos.xPosition = buttonEnabled.xPosition + buttonEnabled.width - buttonResetPos.width - 1;
buttonResetPos.yPosition = buttonMoveHud.yPosition;
buttonResetPos.drawButton(colorPalette, mc, mouseX, mouseY);
drawIcon(MOVE_ICON, buttonMoveHud.xPosition, buttonMoveHud.yPosition);
drawIcon(RESET_ICON, buttonResetPos.xPosition, buttonResetPos.yPosition);
if (buttonMoveHud.isMouseOver()) {
final int textX = buttonEnabled.xPosition - 4 - mc.fontRendererObj.getStringWidth("Move HUD");
final int textY = buttonMoveHud.yPosition + mc.fontRendererObj.FONT_HEIGHT / 2 + 1;
mc.fontRendererObj.drawStringWithShadow("Move HUD", textX, textY, colorPalette.HUD_BUTTON_HINT_TEXT);
}
if (buttonResetPos.isMouseOver()) {
final int textX = buttonEnabled.xPosition - 4 - mc.fontRendererObj.getStringWidth("Reset Position");
final int textY = buttonResetPos.yPosition + mc.fontRendererObj.FONT_HEIGHT / 2 + 1;
mc.fontRendererObj.drawStringWithShadow("Reset Position", textX, textY, colorPalette.HUD_BUTTON_HINT_TEXT);
}
}

@Override
Expand All @@ -89,15 +66,11 @@ public boolean mouseClicked(int mouseX, int mouseY, int mouseButton) {
buttonEnabled.playPressSound(mc.getSoundHandler());
final IRenderer renderer = this.rendererManager.getRendererFromPosition(rendererPosition);
if (renderer != null) {
mc.displayGuiScreen(new RendererEditGuiScreen(this.rendererManager, renderer, parentScreen));
mc.displayGuiScreen(new RendererEditGuiScreen(this.rendererManager, renderer, parentScreen, parentScreen.getColorPalette()));
} else {
throw new RuntimeException("No registered renderer associated to " + field.getName());
}
return true;
} else if (buttonResetPos.mousePressed(mc, mouseX, mouseY)) {
rendererPosition.resetToDefault();
buttonEnabled.playPressSound(mc.getSoundHandler());
return true;
}
}
return false;
Expand All @@ -108,20 +81,6 @@ public int getHeight() {
return Math.max(super.getHeight(), 8 + buttonEnabled.height + 1 + buttonMoveHud.height + 8 - 1);
}

private void drawIcon(ResourceLocation icon, int drawX, int drawY) {
drawX += 3;
drawY += 3;
GlStateManager.pushMatrix();
GlStateManager.enableAlpha();
GlStateManager.enableBlend();
GlStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0);
GlStateManager.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
parentScreen.mc.getTextureManager().bindTexture(icon);
GlStateManager.color(1, 1, 1);
Gui.drawModalRectWithCustomSizedTexture(drawX, drawY, 0f, 0f, 14, 14, 14f, 14f);
GlStateManager.popMatrix();
}

private void flipBooleanConfig() {
rendererPosition.setEnabled(!rendererPosition.isEnabled());
toggled = rendererPosition.isEnabled();
Expand Down
Binary file not shown.
Binary file added src/configlib/resources/assets/configlib/reload.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file added src/configlib/resources/assets/configlib/undo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.