Added option to allow button background to not appear

This commit is contained in:
coolGi
2023-10-03 14:38:52 +10:30
parent 90c53d8116
commit 2463d8c024
2 changed files with 66 additions and 42 deletions
@@ -33,10 +33,14 @@ import net.minecraft.client.gui.components.WidgetSprites;
#endif
/**
* Creates a button with a texture on it
* Creates a button with a texture on it (and a background) that works with all mc versions
*
* @author coolGi
* @version 2023-10-03
*/
public class TexturedButtonWidget extends ImageButton
{
public final boolean renderBackground;
#if POST_MC_1_20_2
public static WidgetSprites locationToSprite(ResourceLocation resourceLocation, int u, int v)
{
@@ -64,58 +68,73 @@ public class TexturedButtonWidget extends ImageButton
#if POST_MC_1_17_1
public TexturedButtonWidget(int x, int y, int width, int height, int u, int v, ResourceLocation texture, OnPress pressAction)
public TexturedButtonWidget(int x, int y, int width, int height, int u, int v, ResourceLocation texture, OnPress pressAction) {
this(x, y, width, height, u, v, texture, pressAction, true);
}
public TexturedButtonWidget(int x, int y, int width, int height, int u, int v, ResourceLocation texture, OnPress pressAction, boolean renderBackground)
{
#if PRE_MC_1_20_2
super(x, y, width, height, u, v, texture, pressAction);
#else
super(x, y, width, height, locationToSprite(texture, u, v), pressAction);
#endif
this.renderBackground = renderBackground;
}
#endif
public TexturedButtonWidget(int x, int y, int width, int height, int u, int v, int hoveredVOffset, ResourceLocation texture, int textureWidth, int textureHeight, OnPress pressAction)
public TexturedButtonWidget(int x, int y, int width, int height, int u, int v, int hoveredVOffset, ResourceLocation texture, int textureWidth, int textureHeight, OnPress pressAction) {
this(x, y, width, height, u, v, hoveredVOffset, texture, textureWidth, textureHeight, pressAction, true);
}
public TexturedButtonWidget(int x, int y, int width, int height, int u, int v, int hoveredVOffset, ResourceLocation texture, int textureWidth, int textureHeight, OnPress pressAction, boolean renderBackground)
{
#if PRE_MC_1_20_2
super(x, y, width, height, u, v, hoveredVOffset, texture, textureWidth, textureHeight, pressAction);
#else
super(x, y, width, height, locationToSprite(texture, u, v, textureWidth, textureHeight, hoveredVOffset), pressAction);
#endif
this.renderBackground = renderBackground;
}
public TexturedButtonWidget(int x, int y, int width, int height, int u, int v, int hoveredVOffset, ResourceLocation texture, int textureWidth, int textureHeight, OnPress pressAction, Component text)
public TexturedButtonWidget(int x, int y, int width, int height, int u, int v, int hoveredVOffset, ResourceLocation texture, int textureWidth, int textureHeight, OnPress pressAction, Component text) {
this(x, y, width, height, u, v, hoveredVOffset, texture, textureWidth, textureHeight, pressAction, text, true);
}
public TexturedButtonWidget(int x, int y, int width, int height, int u, int v, int hoveredVOffset, ResourceLocation texture, int textureWidth, int textureHeight, OnPress pressAction, Component text, boolean renderBackground)
{
#if PRE_MC_1_20_2
super(x, y, width, height, u, v, hoveredVOffset, texture, textureWidth, textureHeight, pressAction, text);
#else
super(x, y, locationToSprite(texture, u, v, textureWidth, textureHeight, hoveredVOffset), pressAction, text);
#endif
this.renderBackground = renderBackground;
}
#if PRE_MC_1_19_4
@Override
public void renderButton(PoseStack matrices, int mouseX, int mouseY, float delta) {
#if PRE_MC_1_17_1
Minecraft.getInstance().getTextureManager().bind(WIDGETS_LOCATION);
RenderSystem.color4f(1.0F, 1.0F, 1.0F, this.alpha);
#else
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderTexture(0, WIDGETS_LOCATION);
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, this.alpha);
#endif
if (this.renderBackground) // Renders the background of the button
{
#if PRE_MC_1_17_1
Minecraft.getInstance().getTextureManager().bind(WIDGETS_LOCATION);
RenderSystem.color4f(1.0F, 1.0F, 1.0F, this.alpha);
#else
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderTexture(0, WIDGETS_LOCATION);
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, this.alpha);
#endif
int i = this.getYImage(this.isHovered);
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderSystem.enableDepthTest();
#if PRE_MC_1_19_4
this.blit(matrices, this.x, this.y, 0, 46 + i * 20, this.width / 2, this.height);
this.blit(matrices, this.x + this.width / 2, this.y, 200 - this.width / 2, 46 + i * 20, this.width / 2, this.height);
#else
this.blit(matrices, this.getX(), this.getY(), 0, 46 + i * 20, this.getWidth() / 2, this.getHeight());
this.blit(matrices, this.getX() + this.getWidth() / 2, this.getY(), 200 - this.width / 2, 46 + i * 20, this.getWidth() / 2, this.getHeight());
#endif
int i = this.getYImage(this.isHovered);
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderSystem.enableDepthTest();
#if PRE_MC_1_19_4
this.blit(matrices, this.x, this.y, 0, 46 + i * 20, this.width / 2, this.height);
this.blit(matrices, this.x + this.width / 2, this.y, 200 - this.width / 2, 46 + i * 20, this.width / 2, this.height);
#else
this.blit(matrices, this.getX(), this.getY(), 0, 46 + i * 20, this.getWidth() / 2, this.getHeight());
this.blit(matrices, this.getX() + this.getWidth() / 2, this.getY(), 200 - this.width / 2, 46 + i * 20, this.getWidth() / 2, this.getHeight());
#endif
}
super.renderButton(matrices, mouseX, mouseY, delta);
}
@@ -131,25 +150,28 @@ public class TexturedButtonWidget extends ImageButton
public void renderWidget(GuiGraphics matrices, int mouseX, int mouseY, float delta)
{
#endif
int i = 1;
if (!this.active) i = 0;
else if (this.isHovered) i = 2;
if (this.renderBackground) // Renders the background of the button
{
int i = 1;
if (!this.active) i = 0;
else if (this.isHovered) i = 2;
#if PRE_MC_1_20_1
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderSystem.enableDepthTest();
this.blit(matrices, this.getX(), this.getY(), 0, 46 + i * 20, this.getWidth() / 2, this.getHeight());
this.blit(matrices, this.getX() + this.getWidth() / 2, this.getY(), 200 - this.width / 2, 46 + i * 20, this.getWidth() / 2, this.getHeight());
#elif MC_1_20_1
matrices.blit(WIDGETS_LOCATION, this.getX(), this.getY(), 0, 46 + i * 20, this.getWidth() / 2, this.getHeight());
matrices.blit(WIDGETS_LOCATION, this.getX() + this.getWidth() / 2, this.getY(), 200 - this.width / 2, 46 + i * 20, this.getWidth() / 2, this.getHeight());
#else
// FIXME[1.20.2]: Later changed `.enabled()` to a proper thing taking the hovered and active into account
matrices.blit(sprites.enabled(), this.getX(), this.getY(), 0, 46 + i * 20, this.getWidth() / 2, this.getHeight());
matrices.blit(sprites.enabled(), this.getX() + this.getWidth() / 2, this.getY(), 200 - this.width / 2, 46 + i * 20, this.getWidth() / 2, this.getHeight());
#endif
#if PRE_MC_1_20_1
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderSystem.enableDepthTest();
this.blit(matrices, this.getX(), this.getY(), 0, 46 + i * 20, this.getWidth() / 2, this.getHeight());
this.blit(matrices, this.getX() + this.getWidth() / 2, this.getY(), 200 - this.width / 2, 46 + i * 20, this.getWidth() / 2, this.getHeight());
#elif MC_1_20_1
matrices.blit(WIDGETS_LOCATION, this.getX(), this.getY(), 0, 46 + i * 20, this.getWidth() / 2, this.getHeight());
matrices.blit(WIDGETS_LOCATION, this.getX() + this.getWidth() / 2, this.getY(), 200 - this.width / 2, 46 + i * 20, this.getWidth() / 2, this.getHeight());
#else
// FIXME[1.20.2]: Later changed `.enabled()` to a proper thing taking the hovered and active into account
matrices.blit(sprites.enabled(), this.getX(), this.getY(), 0, 46 + i * 20, this.getWidth() / 2, this.getHeight());
matrices.blit(sprites.enabled(), this.getX() + this.getWidth() / 2, this.getY(), 200 - this.width / 2, 46 + i * 20, this.getWidth() / 2, this.getHeight());
#endif
}
super.renderWidget(matrices, mouseX, mouseY, delta);
}
@@ -90,7 +90,9 @@ public class UpdateModScreen extends DhScreen
// For now it goes to the client option by default
(buttonWidget) -> System.out.println("Nice, you found an easter egg :)"), // TODO: Add a proper easter egg to pressing the logo (maybe with confetti)
// Add a title to the button
Translatable(ModInfo.ID + ".updater.title")
Translatable(ModInfo.ID + ".updater.title"),
// Dont render the background of the button
false
));
}
catch (Exception e)