PrettyPipes/src/main/java/de/ellpeck/prettypipes/misc/ItemTerminalWidget.java

82 lines
3.1 KiB
Java
Raw Normal View History

2020-05-07 18:30:40 +02:00
package de.ellpeck.prettypipes.misc;
import com.mojang.blaze3d.systems.RenderSystem;
2021-12-02 16:55:04 +01:00
import com.mojang.blaze3d.vertex.PoseStack;
2020-05-07 18:30:40 +02:00
import de.ellpeck.prettypipes.terminal.containers.ItemTerminalGui;
2021-12-02 16:55:04 +01:00
import net.minecraft.ChatFormatting;
2022-06-30 16:00:33 +02:00
import net.minecraft.client.gui.GuiComponent;
2021-12-02 16:55:04 +01:00
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.network.chat.TextComponent;
2021-12-02 12:31:04 +01:00
import net.minecraft.world.item.ItemStack;
2020-05-07 18:30:40 +02:00
2021-12-02 16:55:04 +01:00
import java.util.Optional;
2020-05-07 18:30:40 +02:00
2021-12-02 16:55:04 +01:00
public class ItemTerminalWidget extends AbstractWidget {
2020-05-07 18:30:40 +02:00
private final ItemTerminalGui screen;
public final int gridX;
public final int gridY;
2020-05-07 21:10:29 +02:00
public boolean selected;
public ItemStack stack = ItemStack.EMPTY;
public boolean craftable;
2020-05-07 18:30:40 +02:00
2020-05-07 21:10:29 +02:00
public ItemTerminalWidget(int xIn, int yIn, int gridX, int gridY, ItemTerminalGui screen) {
2021-12-02 16:55:04 +01:00
super(xIn, yIn, 16, 16, new TextComponent(""));
2020-05-07 18:30:40 +02:00
this.gridX = gridX;
this.gridY = gridY;
this.screen = screen;
2020-05-07 23:06:35 +02:00
this.visible = false;
2020-05-07 18:30:40 +02:00
}
@Override
2020-05-07 21:10:29 +02:00
public void onClick(double x, double y) {
this.screen.streamWidgets().forEach(w -> w.selected = false);
this.selected = true;
2020-05-07 18:30:40 +02:00
}
@Override
2021-12-02 16:55:04 +01:00
public void renderButton(PoseStack matrix, int mouseX, int mouseY, float partialTicks) {
var mc = this.screen.getMinecraft();
var renderer = mc.getItemRenderer();
2020-05-07 18:30:40 +02:00
this.setBlitOffset(100);
2021-12-02 16:55:04 +01:00
renderer.blitOffset = 100;
2020-05-07 21:10:29 +02:00
if (this.selected)
2022-06-30 16:00:33 +02:00
GuiComponent.fill(matrix, this.x, this.y, this.x + 16, this.y + 16, -2130706433);
2020-05-07 18:30:40 +02:00
RenderSystem.enableDepthTest();
2021-12-02 16:55:04 +01:00
renderer.renderGuiItem(this.stack, this.x, this.y);
var amount = !this.craftable ? this.stack.getCount() : 0;
var amountStrg = this.stack.getCount() >= 1000 ? amount / 1000 + "k" : String.valueOf(amount);
2021-12-02 23:02:58 +01:00
renderer.renderGuiItemDecorations(mc.font, this.stack, this.x, this.y, amountStrg);
2021-12-02 16:55:04 +01:00
renderer.blitOffset = 0;
2020-05-07 18:30:40 +02:00
this.setBlitOffset(0);
2021-12-02 16:55:04 +01:00
if (this.isHoveredOrFocused()) {
2020-05-07 18:30:40 +02:00
RenderSystem.disableDepthTest();
RenderSystem.colorMask(true, true, true, false);
2020-09-22 19:14:07 +02:00
this.fillGradient(matrix, this.x, this.y, this.x + 16, this.y + 16, -2130706433, -2130706433);
2020-05-07 18:30:40 +02:00
RenderSystem.colorMask(true, true, true, true);
RenderSystem.enableDepthTest();
}
}
@Override
2021-12-02 16:55:04 +01:00
public void renderToolTip(PoseStack matrix, int mouseX, int mouseY) {
if (this.visible && this.isHoveredOrFocused()) {
var tooltip = this.screen.getTooltipFromItem(this.stack);
2020-09-22 19:14:07 +02:00
if (this.stack.getCount() >= 1000) {
2021-12-02 16:55:04 +01:00
var comp = tooltip.get(0);
if (comp instanceof TextComponent text) {
tooltip.set(0, text.append(new TextComponent(" (" + this.stack.getCount() + ')').withStyle(ChatFormatting.BOLD)));
2020-09-22 19:14:07 +02:00
}
}
2021-12-02 16:55:04 +01:00
this.screen.renderTooltip(matrix, tooltip, Optional.empty(), mouseX, mouseY);
2020-05-07 18:30:40 +02:00
}
}
2021-12-02 16:55:04 +01:00
@Override
public void updateNarration(NarrationElementOutput output) {
this.defaultButtonNarrationText(output);
2021-12-02 16:55:04 +01:00
}
2020-05-07 18:30:40 +02:00
}