package dev.isxander.yacl.gui; import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.vertex.PoseStack; import dev.isxander.yacl.api.utils.Dimension; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.Font; import net.minecraft.client.gui.GuiComponent; import net.minecraft.client.gui.components.Renderable; import net.minecraft.client.gui.components.events.GuiEventListener; import net.minecraft.client.gui.narration.NarratableEntry; import net.minecraft.client.gui.narration.NarrationElementOutput; import net.minecraft.client.renderer.GameRenderer; import net.minecraft.client.resources.sounds.SimpleSoundInstance; import net.minecraft.sounds.SoundEvents; import java.awt.*; public abstract class AbstractWidget implements GuiEventListener, Renderable, NarratableEntry { protected final Minecraft client = Minecraft.getInstance(); protected final Font textRenderer = client.font; protected final int inactiveColor = 0xFFA0A0A0; private Dimension dim; public AbstractWidget(Dimension dim) { this.dim = dim; } public void postRender(PoseStack matrices, int mouseX, int mouseY, float delta) { } public boolean canReset() { return false; } @Override public boolean isMouseOver(double mouseX, double mouseY) { if (dim == null) return false; return this.dim.isPointInside((int) mouseX, (int) mouseY); } public void setDimension(Dimension dim) { this.dim = dim; } public Dimension getDimension() { return dim; } @Override public NarrationPriority narrationPriority() { return NarrationPriority.NONE; } public void unfocus() { } public boolean matchesSearch(String query) { return true; } @Override public void updateNarration(NarrationElementOutput builder) { } protected void drawButtonRect(PoseStack matrices, int x1, int y1, int x2, int y2, boolean hovered, boolean enabled) { if (x1 > x2) { int xx1 = x1; x1 = x2; x2 = xx1; } if (y1 > y2) { int yy1 = y1; y1 = y2; y2 = yy1; } int width = x2 - x1; int height = y2 - y1; RenderSystem.setShader(GameRenderer::getPositionTexShader); RenderSystem.setShaderTexture(0, net.minecraft.client.gui.components.AbstractWidget.WIDGETS_LOCATION); RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); int i = !enabled ? 0 : hovered ? 2 : 1; RenderSystem.enableBlend(); RenderSystem.defaultBlendFunc(); RenderSystem.enableDepthTest(); GuiComponent.blit(matrices, x1, y1, 0, 0, 46 + i * 20, width / 2, height, 256, 256); GuiComponent.blit(matrices, x1 + width / 2, y1, 0, 200 - width / 2f, 46 + i * 20, width / 2, height, 256, 256); } protected int multiplyColor(int hex, float amount) { Color color = new Color(hex, true); return new Color(Math.max((int)(color.getRed() * amount), 0), Math.max((int)(color.getGreen() * amount), 0), Math.max((int)(color.getBlue() * amount), 0), color.getAlpha()).getRGB(); } public void playDownSound() { Minecraft.getInstance().getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1.0F)); } }