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

81 lines
3.2 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;
import de.ellpeck.prettypipes.terminal.containers.ItemTerminalGui;
2021-12-02 16:55:04 +01:00
import net.minecraft.ChatFormatting;
2023-07-07 19:54:52 +02:00
import net.minecraft.client.gui.GuiGraphics;
2021-12-02 16:55:04 +01:00
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.narration.NarrationElementOutput;
2023-07-07 19:54:52 +02:00
import net.minecraft.client.gui.screens.Screen;
2022-06-27 13:57:06 +02:00
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
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) {
2022-06-27 13:57:06 +02:00
super(xIn, yIn, 16, 16, Component.literal(""));
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
2023-07-07 19:54:52 +02:00
protected void renderWidget(GuiGraphics graphics, int p_268034_, int p_268009_, float p_268085_) {
2021-12-02 16:55:04 +01:00
var mc = this.screen.getMinecraft();
var renderer = mc.getItemRenderer();
2023-07-07 19:54:52 +02:00
// TODO test this new blit offset replacement?
graphics.pose().translate(0, 0, 100);
2020-05-07 21:10:29 +02:00
if (this.selected)
2023-07-07 19:54:52 +02:00
graphics.fill(this.getX(), this.getY(), this.getX() + 16, this.getY() + 16, -2130706433);
2020-05-07 18:30:40 +02:00
RenderSystem.enableDepthTest();
2023-07-07 19:54:52 +02:00
graphics.renderItem(this.stack, this.getX(), this.getY());
2021-12-02 16:55:04 +01:00
var amount = !this.craftable ? this.stack.getCount() : 0;
var amountStrg = this.stack.getCount() >= 1000 ? amount / 1000 + "k" : String.valueOf(amount);
2023-07-07 19:54:52 +02:00
graphics.renderItemDecorations(mc.font, this.stack, this.getX(), this.getY(), amountStrg);
graphics.pose().translate(0, 0, -100);
2020-05-07 18:30:40 +02:00
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);
2023-07-07 19:54:52 +02:00
graphics.fillGradient(this.getX(), this.getY(), this.getX() + 16, this.getY() + 16, -2130706433, -2130706433);
2020-05-07 18:30:40 +02:00
RenderSystem.colorMask(true, true, true, true);
RenderSystem.enableDepthTest();
}
}
2023-07-07 19:54:52 +02:00
public void renderToolTip(GuiGraphics graphics, int mouseX, int mouseY) {
2023-07-07 20:44:52 +02:00
if (this.visible && this.isHovered()) {
2023-07-07 19:54:52 +02:00
var tooltip = Screen.getTooltipFromItem(this.screen.getMinecraft(), 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);
2023-07-07 19:54:52 +02:00
if (comp instanceof MutableComponent m)
2022-06-27 13:57:06 +02:00
tooltip.set(0, m.append(Component.literal(" (" + this.stack.getCount() + ')').withStyle(ChatFormatting.BOLD)));
2020-09-22 19:14:07 +02:00
}
2023-07-07 19:54:52 +02:00
graphics.renderTooltip(this.screen.getMinecraft().font, tooltip, Optional.empty(), mouseX, mouseY);
2020-05-07 18:30:40 +02:00
}
}
2021-12-02 16:55:04 +01:00
@Override
2023-07-07 19:54:52 +02:00
public void updateWidgetNarration(NarrationElementOutput output) {
this.defaultButtonNarrationText(output);
2021-12-02 16:55:04 +01:00
}
2023-07-07 19:54:52 +02:00
2020-05-07 18:30:40 +02:00
}