minecraft-src/net/minecraft/client/gui/components/SpriteIconButton.java
2025-07-04 02:00:41 +03:00

137 lines
4.7 KiB
Java

package net.minecraft.client.gui.components;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;
@Environment(EnvType.CLIENT)
public abstract class SpriteIconButton extends Button {
protected final ResourceLocation sprite;
protected final int spriteWidth;
protected final int spriteHeight;
SpriteIconButton(
int width,
int height,
Component message,
int spriteWidth,
int spriteHeight,
ResourceLocation sprite,
Button.OnPress onPress,
@Nullable Button.CreateNarration createNarration
) {
super(0, 0, width, height, message, onPress, createNarration == null ? DEFAULT_NARRATION : createNarration);
this.spriteWidth = spriteWidth;
this.spriteHeight = spriteHeight;
this.sprite = sprite;
}
public static SpriteIconButton.Builder builder(Component message, Button.OnPress onPress, boolean iconOnly) {
return new SpriteIconButton.Builder(message, onPress, iconOnly);
}
@Environment(EnvType.CLIENT)
public static class Builder {
private final Component message;
private final Button.OnPress onPress;
private final boolean iconOnly;
private int width = 150;
private int height = 20;
@Nullable
private ResourceLocation sprite;
private int spriteWidth;
private int spriteHeight;
@Nullable
Button.CreateNarration narration;
public Builder(Component message, Button.OnPress onPress, boolean iconOnly) {
this.message = message;
this.onPress = onPress;
this.iconOnly = iconOnly;
}
public SpriteIconButton.Builder width(int width) {
this.width = width;
return this;
}
public SpriteIconButton.Builder size(int width, int height) {
this.width = width;
this.height = height;
return this;
}
public SpriteIconButton.Builder sprite(ResourceLocation sprite, int spriteWidth, int spriteHeight) {
this.sprite = sprite;
this.spriteWidth = spriteWidth;
this.spriteHeight = spriteHeight;
return this;
}
public SpriteIconButton.Builder narration(Button.CreateNarration narration) {
this.narration = narration;
return this;
}
public SpriteIconButton build() {
if (this.sprite == null) {
throw new IllegalStateException("Sprite not set");
} else {
return (SpriteIconButton)(this.iconOnly
? new SpriteIconButton.CenteredIcon(this.width, this.height, this.message, this.spriteWidth, this.spriteHeight, this.sprite, this.onPress, this.narration)
: new SpriteIconButton.TextAndIcon(this.width, this.height, this.message, this.spriteWidth, this.spriteHeight, this.sprite, this.onPress, this.narration));
}
}
}
@Environment(EnvType.CLIENT)
public static class CenteredIcon extends SpriteIconButton {
protected CenteredIcon(
int i, int j, Component component, int k, int l, ResourceLocation resourceLocation, Button.OnPress onPress, @Nullable Button.CreateNarration createNarration
) {
super(i, j, component, k, l, resourceLocation, onPress, createNarration);
}
@Override
public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
super.renderWidget(guiGraphics, mouseX, mouseY, partialTick);
int i = this.getX() + this.getWidth() / 2 - this.spriteWidth / 2;
int j = this.getY() + this.getHeight() / 2 - this.spriteHeight / 2;
guiGraphics.blitSprite(RenderType::guiTextured, this.sprite, i, j, this.spriteWidth, this.spriteHeight);
}
@Override
public void renderString(GuiGraphics guiGraphics, Font font, int color) {
}
}
@Environment(EnvType.CLIENT)
public static class TextAndIcon extends SpriteIconButton {
protected TextAndIcon(
int i, int j, Component component, int k, int l, ResourceLocation resourceLocation, Button.OnPress onPress, @Nullable Button.CreateNarration createNarration
) {
super(i, j, component, k, l, resourceLocation, onPress, createNarration);
}
@Override
public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
super.renderWidget(guiGraphics, mouseX, mouseY, partialTick);
int i = this.getX() + this.getWidth() - this.spriteWidth - 2;
int j = this.getY() + this.getHeight() / 2 - this.spriteHeight / 2;
guiGraphics.blitSprite(RenderType::guiTextured, this.sprite, i, j, this.spriteWidth, this.spriteHeight);
}
@Override
public void renderString(GuiGraphics guiGraphics, Font font, int color) {
int i = this.getX() + 2;
int j = this.getX() + this.getWidth() - this.spriteWidth - 4;
int k = this.getX() + this.getWidth() / 2;
renderScrollingString(guiGraphics, font, this.getMessage(), k, i, this.getY(), j, this.getY() + this.getHeight(), color);
}
}
}