|
| 1 | +package com.hanprogramer.androids.client.screen.widgets; |
| 2 | + |
| 3 | +import net.minecraft.client.MinecraftClient; |
| 4 | +import net.minecraft.client.gl.RenderPipelines; |
| 5 | +import net.minecraft.client.gui.DrawContext; |
| 6 | +import net.minecraft.client.gui.screen.ButtonTextures; |
| 7 | +import net.minecraft.client.gui.widget.ButtonWidget; |
| 8 | +import net.minecraft.text.Text; |
| 9 | +import net.minecraft.util.Identifier; |
| 10 | +import net.minecraft.util.math.ColorHelper; |
| 11 | + |
| 12 | +import java.util.function.Supplier; |
| 13 | + |
| 14 | +public class ColoredButtonWidget extends ButtonWidget { |
| 15 | + private static final ButtonTextures TEXTURES = new ButtonTextures(Identifier.ofVanilla("widget/button"), Identifier.ofVanilla("widget/button_disabled"), Identifier.ofVanilla("widget/button_highlighted")); |
| 16 | + private final int textColor; |
| 17 | + |
| 18 | + public ColoredButtonWidget(int x, int y, int width, int height, Text message, PressAction onPress, int textColor) { |
| 19 | + super(x, y, width, height, message, onPress, Supplier::get); |
| 20 | + this.textColor = textColor; |
| 21 | + } |
| 22 | + |
| 23 | + @Override |
| 24 | + protected void renderWidget(DrawContext context, int mouseX, int mouseY, float deltaTicks) { |
| 25 | + MinecraftClient minecraftClient = MinecraftClient.getInstance(); |
| 26 | + context.drawGuiTexture(RenderPipelines.GUI_TEXTURED, TEXTURES.get(this.active, this.isSelected()), this.getX(), this.getY(), this.getWidth(), this.getHeight(), ColorHelper.getWhite(this.alpha)); |
| 27 | + int i = ColorHelper.withAlpha(this.alpha, textColor); |
| 28 | + this.drawMessage(context, minecraftClient.textRenderer, i); |
| 29 | + context.drawCenteredTextWithShadow(minecraftClient.textRenderer, getMessage(), |
| 30 | + this.getX() + this.width / 2, |
| 31 | + this.getY() + (this.height - 8) / 2, |
| 32 | + textColor); |
| 33 | + } |
| 34 | +} |
0 commit comments