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 @@ -2,7 +2,6 @@

import fr.alexdoru.configlib.api.ColorPalette;
import fr.alexdoru.configlib.api.ConfigProperty;
import net.minecraft.util.EnumChatFormatting;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
Expand All @@ -15,22 +14,27 @@ public class BooleanGuiButton extends ConfigGuiButton {
public BooleanGuiButton(Field field, Method event, ConfigProperty annotation) throws IllegalAccessException {
super(field, event, annotation);
this.toggled = (boolean) this.field.get(null);
this.button = new ClickGuiButton(0, 0, 0, mc.fontRendererObj.getStringWidth(" Disabled "), 20, getButtonText());
this.button = getMainButton(getBooleanText(toggled));
}

@Override
protected int getRightSideContentWidth() {
return button.width + BUTTON_RIGHT_MARGIN;
}

@Override
public void draw(ColorPalette colorPalette, int drawX, int drawY, int mouseX, int mouseY) {
super.draw(colorPalette, drawX, drawY, mouseX, mouseY);
button.xPosition = drawX + boxWidth - button.width - 20;
button.yPosition = drawY + (hasComment ? 8 + mc.fontRendererObj.FONT_HEIGHT / 2 : (getHeight() - button.height) / 2);
button.xPosition = contentLeft;
button.yPosition = drawY + (hasComment ? PADDING + mc.fontRendererObj.FONT_HEIGHT / 2 : (getHeight() - button.height) / 2);
button.drawButton(colorPalette, mc, mouseX, mouseY);
}

@Override
public boolean mouseClicked(int mouseX, int mouseY, int mouseButton) throws IllegalAccessException {
if (mouseButton == 0 && button.mousePressed(mc, mouseX, mouseY)) {
flipBooleanConfig();
button.displayString = getButtonText();
button.displayString = getBooleanText(toggled);
button.playPressSound(mc.getSoundHandler());
return true;
}
Expand All @@ -42,9 +46,4 @@ private void flipBooleanConfig() throws IllegalAccessException {
toggled = (boolean) this.field.get(null);
invokeConfigEvent();
}

private String getButtonText() {
return toggled ? EnumChatFormatting.GREEN + "Enabled" : EnumChatFormatting.RED + "Disabled";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ public ColorEnumGuiButton(Field field, Method event, ConfigProperty annotation)
@Override
public void draw(ColorPalette colorPalette, int drawX, int drawY, int mouseX, int mouseY) {
super.draw(colorPalette, drawX, drawY, mouseX, mouseY);
final int left = this.button.xPosition - 20 - 1;
final int colorBoxSize = button.height;
final int left = this.button.xPosition - colorBoxSize - 1;
final int top = this.button.yPosition;
GuiUtil.drawBoxWithOutline(left, top, left + 20, top + 20, 255 << 24 | color, colorPalette.COLOR_BUTTON_INDICATOR_BORDER);
GuiUtil.drawBoxWithOutline(left, top, left + colorBoxSize, top + colorBoxSize, 255 << 24 | color, colorPalette.COLOR_BUTTON_INDICATOR_BORDER);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,23 @@ public ColorGuiButton(ConfigGuiScreen configGuiScreen, Field field, ConfigProper
this.parentScreen = configGuiScreen;
this.color = (int) this.field.get(null);
this.defaultColor = defaultColor;
this.button = new ClickGuiButton(0, 0, 0, mc.fontRendererObj.getStringWidth(" Disabled "), 20, "Change");
this.button = getMainButton("Change");
}

@Override
protected int getRightSideContentWidth() {
return button.width + BUTTON_RIGHT_MARGIN + button.height /* color box width */ + 1;
}

@Override
public void draw(ColorPalette colorPalette, int drawX, int drawY, int mouseX, int mouseY) {
super.draw(colorPalette, drawX, drawY, mouseX, mouseY);
button.xPosition = drawX + boxWidth - button.width - 20;
button.yPosition = drawY + (hasComment ? 8 + mc.fontRendererObj.FONT_HEIGHT / 2 : (getHeight() - button.height) / 2);
final int top = drawY + (hasComment ? PADDING + mc.fontRendererObj.FONT_HEIGHT / 2 : (getHeight() - button.height) / 2);
final int colorBoxSize = button.height;
GuiUtil.drawBoxWithOutline(contentLeft, top, contentLeft + colorBoxSize, top + colorBoxSize, 255 << 24 | color, colorPalette.COLOR_BUTTON_INDICATOR_BORDER);
button.xPosition = contentLeft + colorBoxSize + 1;
button.yPosition = top;
button.drawButton(colorPalette, mc, mouseX, mouseY);
final int left = button.xPosition - 20 - 1;
final int top = button.yPosition;
GuiUtil.drawBoxWithOutline(left, top, left + 20, top + 20, 255 << 24 | color, colorPalette.COLOR_BUTTON_INDICATOR_BORDER);
}

@Override
Expand All @@ -43,5 +48,4 @@ public boolean mouseClicked(int mouseX, int mouseY, int mouseButton) throws Ille
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import fr.alexdoru.configlib.lib.gui.GuiUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.client.audio.PositionedSoundRecord;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.ResourceLocation;

import java.lang.reflect.Field;
Expand All @@ -15,6 +16,9 @@

public abstract class ConfigGuiButton implements ConfigUIElement {

protected static final int PADDING = 8;
protected static final int BUTTON_RIGHT_MARGIN = 20;

protected final Minecraft mc = Minecraft.getMinecraft();
protected final Field field;
private final Method event;
Expand All @@ -23,6 +27,7 @@ public abstract class ConfigGuiButton implements ConfigUIElement {
protected final boolean hasComment;
protected int boxWidth;
protected int posX, posY;
protected int contentLeft;

protected ConfigGuiButton(Field field, Method event, ConfigProperty annotation) {
this.field = field;
Expand All @@ -31,11 +36,16 @@ protected ConfigGuiButton(Field field, Method event, ConfigProperty annotation)
this.hasComment = !this.annotation.comment().isEmpty();
}

/**
* @return The distance between the left-most position of the content (button) and the right side of the rect
*/
protected abstract int getRightSideContentWidth();

@Override
public void setBoxWidth(int boxWidth) {
this.boxWidth = boxWidth;
if (hasComment) {
final int wrapWidth = boxWidth - mc.fontRendererObj.getStringWidth(" Disabled ") - 20 - 20;
final int wrapWidth = boxWidth - getLeftPadding() - getRightSideContentWidth() - 12; // 20
this.commentToRender.clear();
this.commentToRender.addAll(resizeCommentLines(annotation.comment(), wrapWidth, mc));
}
Expand All @@ -45,12 +55,15 @@ public void setBoxWidth(int boxWidth) {
public void draw(ColorPalette colorPalette, int drawX, int drawY, int mouseX, int mouseY) {
this.posX = drawX;
this.posY = drawY;
GuiUtil.drawBoxWithOutline(drawX, drawY, drawX + boxWidth, drawY + getHeight(), colorPalette.SETTING_BACKGROUND, colorPalette.SETTING_BACKGROUND_BORDER);
mc.fontRendererObj.drawStringWithShadow(annotation.name(), drawX + 8, drawY + 8, colorPalette.SETTING_NAME_TEXT);
final int right = drawX + boxWidth;
this.contentLeft = right - getRightSideContentWidth();
GuiUtil.drawBoxWithOutline(drawX, drawY, right, drawY + getHeight(), colorPalette.SETTING_BACKGROUND, colorPalette.SETTING_BACKGROUND_BORDER);
final int textX = drawX + getLeftPadding();
mc.fontRendererObj.drawStringWithShadow(annotation.name(), textX, drawY + PADDING, colorPalette.SETTING_NAME_TEXT);
if (hasComment) {
int commentY = drawY + 8 + mc.fontRendererObj.FONT_HEIGHT + 8;
int commentY = drawY + PADDING + mc.fontRendererObj.FONT_HEIGHT + 6; // '6' here represents the vertical space between name and comment
for (final String line : commentToRender) {
mc.fontRendererObj.drawStringWithShadow(line, drawX + 8, commentY, colorPalette.SETTING_COMMENT_TEXT);
mc.fontRendererObj.drawStringWithShadow(line, textX, commentY, colorPalette.SETTING_COMMENT_TEXT);
commentY += mc.fontRendererObj.FONT_HEIGHT;
}
}
Expand All @@ -59,9 +72,9 @@ public void draw(ColorPalette colorPalette, int drawX, int drawY, int mouseX, in
@Override
public int getHeight() {
if (hasComment) {
return 8 + mc.fontRendererObj.FONT_HEIGHT + 8 + mc.fontRendererObj.FONT_HEIGHT * commentToRender.size() + 8 - 1;
return PADDING + mc.fontRendererObj.FONT_HEIGHT + PADDING + mc.fontRendererObj.FONT_HEIGHT * commentToRender.size() + 6 - 1; // '6' here represents the vertical space between name and comment
}
return 8 + mc.fontRendererObj.FONT_HEIGHT + 8 - 1;
return PADDING + mc.fontRendererObj.FONT_HEIGHT + PADDING - 1;
}

@Override
Expand Down Expand Up @@ -95,4 +108,17 @@ protected void playPressSound() {
this.mc.getSoundHandler().playSound(PositionedSoundRecord.create(new ResourceLocation("gui.button.press"), 1.0F));
}

protected int getLeftPadding() { return PADDING; }

protected final ClickGuiButton getMainButton(String text) {
return new ClickGuiButton(-1, 0, 0, getMainButtonWidth(), 20, text);
}

protected final int getMainButtonWidth() {
return mc.fontRendererObj.getStringWidth("Disabled") + 9;
}

protected static String getBooleanText(boolean value) {
return value ? EnumChatFormatting.GREEN + "Enabled" : EnumChatFormatting.RED + "Disabled";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,22 @@ public EnumGuiButton(Field field, Method event, ConfigProperty annotation) throw
}
int width = 0;
for (final Enum<?> e : values) {
width = Math.max(width, mc.fontRendererObj.getStringWidth(" " + e.name()));
width = Math.max(width, mc.fontRendererObj.getStringWidth(e.name()) + 9);
}
this.button = new ClickGuiButton(0, 0, 0, width, 20, "");
this.setValue((Enum<?>) this.field.get(null));
}

@Override
protected int getRightSideContentWidth() {
return button.width + BUTTON_RIGHT_MARGIN;
}

@Override
public void draw(ColorPalette colorPalette, int drawX, int drawY, int mouseX, int mouseY) {
super.draw(colorPalette, drawX, drawY, mouseX, mouseY);
button.xPosition = drawX + boxWidth - button.width - 20;
button.yPosition = drawY + (hasComment ? 8 + mc.fontRendererObj.FONT_HEIGHT / 2 : (getHeight() - button.height) / 2);
button.xPosition = contentLeft;
button.yPosition = drawY + (hasComment ? PADDING + mc.fontRendererObj.FONT_HEIGHT / 2 : (getHeight() - button.height) / 2);
button.drawButton(colorPalette, mc, mouseX, mouseY);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
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;

Expand Down Expand Up @@ -40,22 +39,27 @@ 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.buttonEnabled = getMainButton(getBooleanText(toggled));
this.buttonMoveHud = new ClickGuiButton(0, 0, 0, 20, 20, "");
this.buttonResetPos = new ClickGuiButton(0, 0, 0, 20, 20, "");
}

@Override
protected int getRightSideContentWidth() {
return buttonEnabled.width + BUTTON_RIGHT_MARGIN;
}

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

@Override
public void draw(ColorPalette colorPalette, int drawX, int drawY, int mouseX, int mouseY) {
super.draw(colorPalette, drawX, drawY, mouseX, mouseY);
buttonEnabled.xPosition = drawX + boxWidth - buttonEnabled.width - 20;
buttonEnabled.yPosition = drawY + 8;
buttonEnabled.xPosition = contentLeft;
buttonEnabled.yPosition = drawY + PADDING;
buttonEnabled.drawButton(colorPalette, mc, mouseX, mouseY);
buttonMoveHud.xPosition = buttonEnabled.xPosition;
buttonMoveHud.yPosition = buttonEnabled.yPosition + buttonEnabled.height + 1;
Expand All @@ -82,7 +86,7 @@ public boolean mouseClicked(int mouseX, int mouseY, int mouseButton) {
if (mouseButton == 0) {
if (buttonEnabled.mousePressed(mc, mouseX, mouseY)) {
flipBooleanConfig();
buttonEnabled.displayString = getButtonText();
buttonEnabled.displayString = getBooleanText(toggled);
buttonEnabled.playPressSound(mc.getSoundHandler());
return true;
} else if (buttonMoveHud.mousePressed(mc, mouseX, mouseY)) {
Expand All @@ -105,7 +109,7 @@ public boolean mouseClicked(int mouseX, int mouseY, int mouseButton) {

@Override
public int getHeight() {
return Math.max(super.getHeight(), 8 + buttonEnabled.height + 1 + buttonMoveHud.height + 8 - 1);
return Math.max(super.getHeight(), PADDING + buttonEnabled.height + 1 + buttonMoveHud.height + PADDING - 1);
}

private void drawIcon(ResourceLocation icon, int drawX, int drawY) {
Expand All @@ -116,7 +120,7 @@ private void drawIcon(ResourceLocation icon, int drawX, int drawY) {
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);
mc.getTextureManager().bindTexture(icon);
GlStateManager.color(1, 1, 1);
Gui.drawModalRectWithCustomSizedTexture(drawX, drawY, 0f, 0f, 14, 14, 14f, 14f);
GlStateManager.popMatrix();
Expand All @@ -127,9 +131,4 @@ private void flipBooleanConfig() {
toggled = rendererPosition.isEnabled();
invokeConfigEvent();
}

private String getButtonText() {
return toggled ? EnumChatFormatting.GREEN + "Enabled" : EnumChatFormatting.RED + "Disabled";
}

}
Loading